2 * Copyright Andrey Semashev 2007 - 2014.
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)
9 * \author Andrey Semashev
12 * This header contains function object adapters.
13 * This is a lightweight alternative to what Boost.Phoenix and Boost.Bind provides.
16 #ifndef BOOST_LOG_UTILITY_FUNCTIONAL_BIND_HPP_INCLUDED_
17 #define BOOST_LOG_UTILITY_FUNCTIONAL_BIND_HPP_INCLUDED_
19 #include <boost/type_traits/remove_cv.hpp>
20 #include <boost/log/detail/config.hpp>
21 #include <boost/log/detail/header.hpp>
23 #ifdef BOOST_HAS_PRAGMA_ONCE
29 BOOST_LOG_OPEN_NAMESPACE
33 template< typename T >
36 typedef T const& type;
39 template< typename T >
40 struct make_arg_type< T& >
47 //! First argument binder
48 template< typename FunT, typename FirstArgT >
52 typedef typename FunT::result_type result_type;
54 binder1st(FunT const& fun, typename aux::make_arg_type< FirstArgT >::type arg) : FunT(fun), m_arg(arg) {}
56 result_type operator() () const
58 return FunT::operator()(m_arg);
61 template< typename T0 >
62 result_type operator() (T0 const& arg0) const
64 return FunT::operator()(m_arg, arg0);
67 template< typename T0, typename T1 >
68 result_type operator() (T0 const& arg0, T1 const& arg1) const
70 return FunT::operator()(m_arg, arg0, arg1);
77 //! First argument binder
78 template< typename FunT, typename FirstArgT >
79 struct binder1st< FunT&, FirstArgT >
81 typedef typename remove_cv< FunT >::type::result_type result_type;
83 binder1st(FunT& fun, typename aux::make_arg_type< FirstArgT >::type arg) : m_fun(fun), m_arg(arg) {}
85 result_type operator() () const
90 template< typename T0 >
91 result_type operator() (T0 const& arg0) const
93 return m_fun(m_arg, arg0);
96 template< typename T0, typename T1 >
97 result_type operator() (T0 const& arg0, T1 const& arg1) const
99 return m_fun(m_arg, arg0, arg1);
107 template< typename FunT, typename FirstArgT >
108 BOOST_FORCEINLINE binder1st< FunT, FirstArgT > bind1st(FunT fun, FirstArgT const& arg)
110 return binder1st< FunT, FirstArgT >(fun, arg);
113 template< typename FunT, typename FirstArgT >
114 BOOST_FORCEINLINE binder1st< FunT, FirstArgT > bind1st(FunT fun, FirstArgT& arg)
116 return binder1st< FunT, FirstArgT >(fun, arg);
119 //! Second argument binder
120 template< typename FunT, typename SecondArgT >
124 typedef typename FunT::result_type result_type;
126 binder2nd(FunT const& fun, typename aux::make_arg_type< SecondArgT >::type arg) : FunT(fun), m_arg(arg) {}
128 template< typename T >
129 result_type operator() (T const& arg) const
131 return FunT::operator()(arg, m_arg);
134 template< typename T0, typename T1 >
135 result_type operator() (T0 const& arg0, T1 const& arg1) const
137 return FunT::operator()(arg0, m_arg, arg1);
144 //! Second argument binder
145 template< typename FunT, typename SecondArgT >
146 struct binder2nd< FunT&, SecondArgT >
148 typedef typename remove_cv< FunT >::type::result_type result_type;
150 binder2nd(FunT& fun, typename aux::make_arg_type< SecondArgT >::type arg) : m_fun(fun), m_arg(arg) {}
152 template< typename T >
153 result_type operator() (T const& arg) const
155 return m_fun(arg, m_arg);
158 template< typename T0, typename T1 >
159 result_type operator() (T0 const& arg0, T1 const& arg1) const
161 return m_fun(arg0, m_arg, arg1);
169 template< typename FunT, typename SecondArgT >
170 BOOST_FORCEINLINE binder2nd< FunT, SecondArgT > bind2nd(FunT fun, SecondArgT const& arg)
172 return binder2nd< FunT, SecondArgT >(fun, arg);
175 template< typename FunT, typename SecondArgT >
176 BOOST_FORCEINLINE binder2nd< FunT, SecondArgT > bind2nd(FunT fun, SecondArgT& arg)
178 return binder2nd< FunT, SecondArgT >(fun, arg);
181 //! Third argument binder
182 template< typename FunT, typename ThirdArgT >
186 typedef typename FunT::result_type result_type;
188 binder3rd(FunT const& fun, typename aux::make_arg_type< ThirdArgT >::type arg) : FunT(fun), m_arg(arg) {}
190 template< typename T0, typename T1 >
191 result_type operator() (T0 const& arg0, T1 const& arg1) const
193 return FunT::operator()(arg0, arg1, m_arg);
200 //! Third argument binder
201 template< typename FunT, typename ThirdArgT >
202 struct binder3rd< FunT&, ThirdArgT >
204 typedef typename remove_cv< FunT >::type::result_type result_type;
206 binder3rd(FunT& fun, typename aux::make_arg_type< ThirdArgT >::type arg) : m_fun(fun), m_arg(arg) {}
208 template< typename T0, typename T1 >
209 result_type operator() (T0 const& arg0, T1 const& arg1) const
211 return m_fun(arg0, arg1, m_arg);
219 template< typename FunT, typename ThirdArgT >
220 BOOST_FORCEINLINE binder3rd< FunT, ThirdArgT > bind3rd(FunT fun, ThirdArgT const& arg)
222 return binder3rd< FunT, ThirdArgT >(fun, arg);
225 template< typename FunT, typename ThirdArgT >
226 BOOST_FORCEINLINE binder3rd< FunT, ThirdArgT > bind3rd(FunT fun, ThirdArgT& arg)
228 return binder3rd< FunT, ThirdArgT >(fun, arg);
231 BOOST_LOG_CLOSE_NAMESPACE // namespace log
235 #include <boost/log/detail/footer.hpp>
237 #endif // BOOST_LOG_UTILITY_FUNCTIONAL_BIND_HPP_INCLUDED_