Chris@16: /* Chris@101: * Copyright Andrey Semashev 2007 - 2015. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file bind.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 30.03.2008 Chris@16: * Chris@16: * This header contains function object adapters. Chris@16: * This is a lightweight alternative to what Boost.Phoenix and Boost.Bind provides. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_UTILITY_FUNCTIONAL_BIND_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_UTILITY_FUNCTIONAL_BIND_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: namespace aux { Chris@16: Chris@16: template< typename T > Chris@16: struct make_arg_type Chris@16: { Chris@16: typedef T const& type; Chris@16: }; Chris@16: Chris@16: template< typename T > Chris@16: struct make_arg_type< T& > Chris@16: { Chris@16: typedef T& type; Chris@16: }; Chris@16: Chris@16: } // namespace aux Chris@16: Chris@16: //! First argument binder Chris@16: template< typename FunT, typename FirstArgT > Chris@16: struct binder1st : Chris@16: private FunT Chris@16: { Chris@16: typedef typename FunT::result_type result_type; Chris@16: Chris@16: binder1st(FunT const& fun, typename aux::make_arg_type< FirstArgT >::type arg) : FunT(fun), m_arg(arg) {} Chris@16: Chris@16: result_type operator() () const Chris@16: { Chris@16: return FunT::operator()(m_arg); Chris@16: } Chris@16: Chris@16: template< typename T0 > Chris@16: result_type operator() (T0 const& arg0) const Chris@16: { Chris@16: return FunT::operator()(m_arg, arg0); Chris@16: } Chris@16: Chris@16: template< typename T0, typename T1 > Chris@16: result_type operator() (T0 const& arg0, T1 const& arg1) const Chris@16: { Chris@16: return FunT::operator()(m_arg, arg0, arg1); Chris@16: } Chris@16: Chris@16: private: Chris@16: FirstArgT m_arg; Chris@16: }; Chris@16: Chris@16: //! First argument binder Chris@16: template< typename FunT, typename FirstArgT > Chris@16: struct binder1st< FunT&, FirstArgT > Chris@16: { Chris@16: typedef typename remove_cv< FunT >::type::result_type result_type; Chris@16: Chris@16: binder1st(FunT& fun, typename aux::make_arg_type< FirstArgT >::type arg) : m_fun(fun), m_arg(arg) {} Chris@16: Chris@16: result_type operator() () const Chris@16: { Chris@16: return m_fun(m_arg); Chris@16: } Chris@16: Chris@16: template< typename T0 > Chris@16: result_type operator() (T0 const& arg0) const Chris@16: { Chris@16: return m_fun(m_arg, arg0); Chris@16: } Chris@16: Chris@16: template< typename T0, typename T1 > Chris@16: result_type operator() (T0 const& arg0, T1 const& arg1) const Chris@16: { Chris@16: return m_fun(m_arg, arg0, arg1); Chris@16: } Chris@16: Chris@16: private: Chris@16: FunT& m_fun; Chris@16: FirstArgT m_arg; Chris@16: }; Chris@16: Chris@16: template< typename FunT, typename FirstArgT > Chris@16: BOOST_FORCEINLINE binder1st< FunT, FirstArgT > bind1st(FunT fun, FirstArgT const& arg) Chris@16: { Chris@16: return binder1st< FunT, FirstArgT >(fun, arg); Chris@16: } Chris@16: Chris@16: template< typename FunT, typename FirstArgT > Chris@16: BOOST_FORCEINLINE binder1st< FunT, FirstArgT > bind1st(FunT fun, FirstArgT& arg) Chris@16: { Chris@16: return binder1st< FunT, FirstArgT >(fun, arg); Chris@16: } Chris@16: Chris@16: //! Second argument binder Chris@16: template< typename FunT, typename SecondArgT > Chris@16: struct binder2nd : Chris@16: private FunT Chris@16: { Chris@16: typedef typename FunT::result_type result_type; Chris@16: Chris@16: binder2nd(FunT const& fun, typename aux::make_arg_type< SecondArgT >::type arg) : FunT(fun), m_arg(arg) {} Chris@16: Chris@16: template< typename T > Chris@16: result_type operator() (T const& arg) const Chris@16: { Chris@16: return FunT::operator()(arg, m_arg); Chris@16: } Chris@16: Chris@16: template< typename T0, typename T1 > Chris@16: result_type operator() (T0 const& arg0, T1 const& arg1) const Chris@16: { Chris@16: return FunT::operator()(arg0, m_arg, arg1); Chris@16: } Chris@16: Chris@16: private: Chris@16: SecondArgT m_arg; Chris@16: }; Chris@16: Chris@16: //! Second argument binder Chris@16: template< typename FunT, typename SecondArgT > Chris@16: struct binder2nd< FunT&, SecondArgT > Chris@16: { Chris@16: typedef typename remove_cv< FunT >::type::result_type result_type; Chris@16: Chris@16: binder2nd(FunT& fun, typename aux::make_arg_type< SecondArgT >::type arg) : m_fun(fun), m_arg(arg) {} Chris@16: Chris@16: template< typename T > Chris@16: result_type operator() (T const& arg) const Chris@16: { Chris@16: return m_fun(arg, m_arg); Chris@16: } Chris@16: Chris@16: template< typename T0, typename T1 > Chris@16: result_type operator() (T0 const& arg0, T1 const& arg1) const Chris@16: { Chris@16: return m_fun(arg0, m_arg, arg1); Chris@16: } Chris@16: Chris@16: private: Chris@16: FunT& m_fun; Chris@16: SecondArgT m_arg; Chris@16: }; Chris@16: Chris@16: template< typename FunT, typename SecondArgT > Chris@16: BOOST_FORCEINLINE binder2nd< FunT, SecondArgT > bind2nd(FunT fun, SecondArgT const& arg) Chris@16: { Chris@16: return binder2nd< FunT, SecondArgT >(fun, arg); Chris@16: } Chris@16: Chris@16: template< typename FunT, typename SecondArgT > Chris@16: BOOST_FORCEINLINE binder2nd< FunT, SecondArgT > bind2nd(FunT fun, SecondArgT& arg) Chris@16: { Chris@16: return binder2nd< FunT, SecondArgT >(fun, arg); Chris@16: } Chris@16: Chris@16: //! Third argument binder Chris@16: template< typename FunT, typename ThirdArgT > Chris@16: struct binder3rd : Chris@16: private FunT Chris@16: { Chris@16: typedef typename FunT::result_type result_type; Chris@16: Chris@16: binder3rd(FunT const& fun, typename aux::make_arg_type< ThirdArgT >::type arg) : FunT(fun), m_arg(arg) {} Chris@16: Chris@16: template< typename T0, typename T1 > Chris@16: result_type operator() (T0 const& arg0, T1 const& arg1) const Chris@16: { Chris@16: return FunT::operator()(arg0, arg1, m_arg); Chris@16: } Chris@16: Chris@16: private: Chris@16: ThirdArgT m_arg; Chris@16: }; Chris@16: Chris@16: //! Third argument binder Chris@16: template< typename FunT, typename ThirdArgT > Chris@16: struct binder3rd< FunT&, ThirdArgT > Chris@16: { Chris@16: typedef typename remove_cv< FunT >::type::result_type result_type; Chris@16: Chris@16: binder3rd(FunT& fun, typename aux::make_arg_type< ThirdArgT >::type arg) : m_fun(fun), m_arg(arg) {} Chris@16: Chris@16: template< typename T0, typename T1 > Chris@16: result_type operator() (T0 const& arg0, T1 const& arg1) const Chris@16: { Chris@16: return m_fun(arg0, arg1, m_arg); Chris@16: } Chris@16: Chris@16: private: Chris@16: FunT& m_fun; Chris@16: ThirdArgT m_arg; Chris@16: }; Chris@16: Chris@16: template< typename FunT, typename ThirdArgT > Chris@16: BOOST_FORCEINLINE binder3rd< FunT, ThirdArgT > bind3rd(FunT fun, ThirdArgT const& arg) Chris@16: { Chris@16: return binder3rd< FunT, ThirdArgT >(fun, arg); Chris@16: } Chris@16: Chris@16: template< typename FunT, typename ThirdArgT > Chris@16: BOOST_FORCEINLINE binder3rd< FunT, ThirdArgT > bind3rd(FunT fun, ThirdArgT& arg) Chris@16: { Chris@16: return binder3rd< FunT, ThirdArgT >(fun, arg); Chris@16: } Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_UTILITY_FUNCTIONAL_BIND_HPP_INCLUDED_