bind.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file bind.hpp
  9. * \author Andrey Semashev
  10. * \date 30.03.2008
  11. *
  12. * This header contains function object adapters.
  13. * This is a lightweight alternative to what Boost.Phoenix and Boost.Bind provides.
  14. */
  15. #ifndef BOOST_LOG_UTILITY_FUNCTIONAL_BIND_HPP_INCLUDED_
  16. #define BOOST_LOG_UTILITY_FUNCTIONAL_BIND_HPP_INCLUDED_
  17. #include <boost/type_traits/remove_cv.hpp>
  18. #include <boost/log/detail/config.hpp>
  19. #include <boost/log/detail/header.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. namespace boost {
  24. BOOST_LOG_OPEN_NAMESPACE
  25. namespace aux {
  26. template< typename T >
  27. struct make_arg_type
  28. {
  29. typedef T const& type;
  30. };
  31. template< typename T >
  32. struct make_arg_type< T& >
  33. {
  34. typedef T& type;
  35. };
  36. } // namespace aux
  37. //! First argument binder
  38. template< typename FunT, typename FirstArgT >
  39. struct binder1st :
  40. private FunT
  41. {
  42. typedef typename FunT::result_type result_type;
  43. binder1st(FunT const& fun, typename aux::make_arg_type< FirstArgT >::type arg) : FunT(fun), m_arg(arg) {}
  44. result_type operator() () const
  45. {
  46. return FunT::operator()(m_arg);
  47. }
  48. template< typename T0 >
  49. result_type operator() (T0 const& arg0) const
  50. {
  51. return FunT::operator()(m_arg, arg0);
  52. }
  53. template< typename T0, typename T1 >
  54. result_type operator() (T0 const& arg0, T1 const& arg1) const
  55. {
  56. return FunT::operator()(m_arg, arg0, arg1);
  57. }
  58. private:
  59. FirstArgT m_arg;
  60. };
  61. //! First argument binder
  62. template< typename FunT, typename FirstArgT >
  63. struct binder1st< FunT&, FirstArgT >
  64. {
  65. typedef typename remove_cv< FunT >::type::result_type result_type;
  66. binder1st(FunT& fun, typename aux::make_arg_type< FirstArgT >::type arg) : m_fun(fun), m_arg(arg) {}
  67. result_type operator() () const
  68. {
  69. return m_fun(m_arg);
  70. }
  71. template< typename T0 >
  72. result_type operator() (T0 const& arg0) const
  73. {
  74. return m_fun(m_arg, arg0);
  75. }
  76. template< typename T0, typename T1 >
  77. result_type operator() (T0 const& arg0, T1 const& arg1) const
  78. {
  79. return m_fun(m_arg, arg0, arg1);
  80. }
  81. private:
  82. FunT& m_fun;
  83. FirstArgT m_arg;
  84. };
  85. template< typename FunT, typename FirstArgT >
  86. BOOST_FORCEINLINE binder1st< FunT, FirstArgT > bind1st(FunT fun, FirstArgT const& arg)
  87. {
  88. return binder1st< FunT, FirstArgT >(fun, arg);
  89. }
  90. template< typename FunT, typename FirstArgT >
  91. BOOST_FORCEINLINE binder1st< FunT, FirstArgT > bind1st(FunT fun, FirstArgT& arg)
  92. {
  93. return binder1st< FunT, FirstArgT >(fun, arg);
  94. }
  95. //! Second argument binder
  96. template< typename FunT, typename SecondArgT >
  97. struct binder2nd :
  98. private FunT
  99. {
  100. typedef typename FunT::result_type result_type;
  101. binder2nd(FunT const& fun, typename aux::make_arg_type< SecondArgT >::type arg) : FunT(fun), m_arg(arg) {}
  102. template< typename T >
  103. result_type operator() (T const& arg) const
  104. {
  105. return FunT::operator()(arg, m_arg);
  106. }
  107. template< typename T0, typename T1 >
  108. result_type operator() (T0 const& arg0, T1 const& arg1) const
  109. {
  110. return FunT::operator()(arg0, m_arg, arg1);
  111. }
  112. private:
  113. SecondArgT m_arg;
  114. };
  115. //! Second argument binder
  116. template< typename FunT, typename SecondArgT >
  117. struct binder2nd< FunT&, SecondArgT >
  118. {
  119. typedef typename remove_cv< FunT >::type::result_type result_type;
  120. binder2nd(FunT& fun, typename aux::make_arg_type< SecondArgT >::type arg) : m_fun(fun), m_arg(arg) {}
  121. template< typename T >
  122. result_type operator() (T const& arg) const
  123. {
  124. return m_fun(arg, m_arg);
  125. }
  126. template< typename T0, typename T1 >
  127. result_type operator() (T0 const& arg0, T1 const& arg1) const
  128. {
  129. return m_fun(arg0, m_arg, arg1);
  130. }
  131. private:
  132. FunT& m_fun;
  133. SecondArgT m_arg;
  134. };
  135. template< typename FunT, typename SecondArgT >
  136. BOOST_FORCEINLINE binder2nd< FunT, SecondArgT > bind2nd(FunT fun, SecondArgT const& arg)
  137. {
  138. return binder2nd< FunT, SecondArgT >(fun, arg);
  139. }
  140. template< typename FunT, typename SecondArgT >
  141. BOOST_FORCEINLINE binder2nd< FunT, SecondArgT > bind2nd(FunT fun, SecondArgT& arg)
  142. {
  143. return binder2nd< FunT, SecondArgT >(fun, arg);
  144. }
  145. //! Third argument binder
  146. template< typename FunT, typename ThirdArgT >
  147. struct binder3rd :
  148. private FunT
  149. {
  150. typedef typename FunT::result_type result_type;
  151. binder3rd(FunT const& fun, typename aux::make_arg_type< ThirdArgT >::type arg) : FunT(fun), m_arg(arg) {}
  152. template< typename T0, typename T1 >
  153. result_type operator() (T0 const& arg0, T1 const& arg1) const
  154. {
  155. return FunT::operator()(arg0, arg1, m_arg);
  156. }
  157. private:
  158. ThirdArgT m_arg;
  159. };
  160. //! Third argument binder
  161. template< typename FunT, typename ThirdArgT >
  162. struct binder3rd< FunT&, ThirdArgT >
  163. {
  164. typedef typename remove_cv< FunT >::type::result_type result_type;
  165. binder3rd(FunT& fun, typename aux::make_arg_type< ThirdArgT >::type arg) : m_fun(fun), m_arg(arg) {}
  166. template< typename T0, typename T1 >
  167. result_type operator() (T0 const& arg0, T1 const& arg1) const
  168. {
  169. return m_fun(arg0, arg1, m_arg);
  170. }
  171. private:
  172. FunT& m_fun;
  173. ThirdArgT m_arg;
  174. };
  175. template< typename FunT, typename ThirdArgT >
  176. BOOST_FORCEINLINE binder3rd< FunT, ThirdArgT > bind3rd(FunT fun, ThirdArgT const& arg)
  177. {
  178. return binder3rd< FunT, ThirdArgT >(fun, arg);
  179. }
  180. template< typename FunT, typename ThirdArgT >
  181. BOOST_FORCEINLINE binder3rd< FunT, ThirdArgT > bind3rd(FunT fun, ThirdArgT& arg)
  182. {
  183. return binder3rd< FunT, ThirdArgT >(fun, arg);
  184. }
  185. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  186. } // namespace boost
  187. #include <boost/log/detail/footer.hpp>
  188. #endif // BOOST_LOG_UTILITY_FUNCTIONAL_BIND_HPP_INCLUDED_