Chris@16: /*============================================================================= Chris@16: Copyright (c) 2007 Tobias Schwinger Chris@16: Chris@16: Use modification and distribution are subject to the Boost Software Chris@16: License, Version 1.0. (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: #ifndef BOOST_FUNCTIONAL_LIGHTWEIGHT_FORWARD_ADAPTER_HPP_INCLUDED Chris@16: # ifndef BOOST_PP_IS_ITERATING Chris@16: Chris@16: # include Chris@16: # include Chris@16: Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: # include Chris@16: Chris@16: # include Chris@16: # include Chris@16: Chris@16: # ifndef BOOST_FUNCTIONAL_LIGHTWEIGHT_FORWARD_ADAPTER_MAX_ARITY Chris@16: # define BOOST_FUNCTIONAL_LIGHTWEIGHT_FORWARD_ADAPTER_MAX_ARITY 10 Chris@16: # elif BOOST_FUNCTIONAL_FORDWARD_ADAPTER_MAX_ARITY < 3 Chris@16: # undef BOOST_FUNCTIONAL_LIGHTWEIGHT_FORWARD_ADAPTER_MAX_ARITY Chris@16: # define BOOST_FUNCTIONAL_LIGHTWEIGHT_FORWARD_ADAPTER_MAX_ARITY 3 Chris@16: # endif Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: template< typename Function, int Arity_Or_MinArity = -1, int MaxArity = -1 > Chris@16: class lightweight_forward_adapter; Chris@16: Chris@16: //----- ---- --- -- - - - - Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: template< class MostDerived, typename Function, typename FunctionConst, Chris@16: int Arity, int MinArity > Chris@16: struct lightweight_forward_adapter_impl; Chris@16: Chris@16: struct lightweight_forward_adapter_result Chris@16: { Chris@16: template< typename Sig > struct apply; Chris@16: Chris@16: // Utility metafunction for argument transform Chris@16: template< typename T > struct x { typedef T const& t; }; Chris@16: template< typename T > struct x< boost::reference_wrapper > Chris@16: { typedef T& t; }; Chris@16: template< typename T > struct x : x { }; Chris@16: template< typename T > struct x : x { }; Chris@16: template< typename T > struct x : x { }; Chris@16: Chris@16: // Utility metafunction to choose target function qualification Chris@16: template< typename T > struct c Chris@16: { typedef typename T::target_function_t t; }; Chris@16: template< typename T > struct c Chris@16: { typedef typename T::target_function_t t; }; Chris@16: template< typename T > struct c Chris@16: { typedef typename T::target_function_const_t t; }; Chris@16: template< typename T > struct c Chris@16: { typedef typename T::target_function_const_t t; }; Chris@16: }; Chris@16: } Chris@16: Chris@16: # define BOOST_TMP_MACRO(f,fn,fc) \ Chris@16: boost::detail::lightweight_forward_adapter_impl< \ Chris@16: lightweight_forward_adapter, fn, fc, \ Chris@16: (MaxArity!=-1? MaxArity :Arity_Or_MinArity!=-1? Arity_Or_MinArity \ Chris@16: :BOOST_FUNCTIONAL_LIGHTWEIGHT_FORWARD_ADAPTER_MAX_ARITY), \ Chris@16: (Arity_Or_MinArity!=-1? Arity_Or_MinArity : 0) > Chris@16: Chris@16: template< typename Function, int Arity_Or_MinArity, int MaxArity > Chris@16: class lightweight_forward_adapter Chris@16: : public BOOST_TMP_MACRO(Function,Function,Function const) Chris@16: , private Function Chris@16: { Chris@16: public: Chris@16: lightweight_forward_adapter(Function const& f = Function()) Chris@16: : Function(f) Chris@16: { } Chris@16: Chris@16: typedef Function target_function_t; Chris@16: typedef Function const target_function_const_t; Chris@16: Chris@16: Function & target_function() { return *this; } Chris@16: Function const & target_function() const { return *this; } Chris@16: Chris@16: template< typename Sig > struct result Chris@16: : detail::lightweight_forward_adapter_result::template apply Chris@16: { }; Chris@16: Chris@16: using BOOST_TMP_MACRO(Function,Function, Function const)::operator(); Chris@16: }; Chris@16: template< typename Function, int Arity_Or_MinArity, int MaxArity > Chris@16: class lightweight_forward_adapter< Function const, Arity_Or_MinArity, Chris@16: MaxArity > Chris@16: : public BOOST_TMP_MACRO(Function const, Function const, Function const) Chris@16: , private Function Chris@16: { Chris@16: public: Chris@16: lightweight_forward_adapter(Function const& f = Function()) Chris@16: : Function(f) Chris@16: { } Chris@16: Chris@16: typedef Function const target_function_t; Chris@16: typedef Function const target_function_const_t; Chris@16: Chris@16: Function const & target_function() const { return *this; } Chris@16: Chris@16: template< typename Sig > struct result Chris@16: : detail::lightweight_forward_adapter_result::template apply Chris@16: { }; Chris@16: Chris@16: using BOOST_TMP_MACRO(Function const,Function const, Function const) Chris@16: ::operator(); Chris@16: }; Chris@16: template< typename Function, int Arity_Or_MinArity, int MaxArity > Chris@16: class lightweight_forward_adapter< Function &, Arity_Or_MinArity, MaxArity > Chris@16: : public BOOST_TMP_MACRO(Function&, Function, Function) Chris@16: { Chris@16: Function& ref_function; Chris@16: public: Chris@16: lightweight_forward_adapter(Function& f) Chris@16: : ref_function(f) Chris@16: { } Chris@16: Chris@16: typedef Function target_function_t; Chris@16: typedef Function target_function_const_t; Chris@16: Chris@16: Function & target_function() const { return this->ref_function; } Chris@16: Chris@16: template< typename Sig > struct result Chris@16: : detail::lightweight_forward_adapter_result::template apply Chris@16: { }; Chris@16: Chris@16: using BOOST_TMP_MACRO(Function&, Function, Function)::operator(); Chris@16: }; Chris@16: Chris@16: #undef BOOST_TMP_MACRO Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: template< class Self > Chris@16: struct lightweight_forward_adapter_result::apply< Self() > Chris@16: : boost::result_of< BOOST_DEDUCED_TYPENAME c::t() > Chris@16: { }; Chris@16: Chris@16: template< class MD, class F, class FC > Chris@16: struct lightweight_forward_adapter_impl Chris@16: : lightweight_forward_adapter_result Chris@16: { Chris@16: inline typename boost::result_of< FC() >::type Chris@16: operator()() const Chris@16: { Chris@16: return static_cast(this)->target_function()(); Chris@16: } Chris@16: Chris@16: inline typename boost::result_of< F() >::type Chris@16: operator()() Chris@16: { Chris@16: return static_cast(this)->target_function()(); Chris@16: } Chris@16: }; Chris@16: Chris@16: # define BOOST_PP_FILENAME_1 \ Chris@16: Chris@16: # define BOOST_PP_ITERATION_LIMITS \ Chris@16: (1,BOOST_FUNCTIONAL_LIGHTWEIGHT_FORWARD_ADAPTER_MAX_ARITY) Chris@16: # include BOOST_PP_ITERATE() Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: template Chris@16: struct result_of const ()> Chris@16: : boost::detail::lightweight_forward_adapter_result::template apply< Chris@16: boost::lightweight_forward_adapter const () > Chris@16: { }; Chris@16: template Chris@16: struct result_of()> Chris@16: : boost::detail::lightweight_forward_adapter_result::template apply< Chris@16: boost::lightweight_forward_adapter() > Chris@16: { }; Chris@16: template Chris@16: struct result_of const& ()> Chris@16: : boost::detail::lightweight_forward_adapter_result::template apply< Chris@16: boost::lightweight_forward_adapter const () > Chris@16: { }; Chris@16: template Chris@16: struct result_of& ()> Chris@16: : boost::detail::lightweight_forward_adapter_result::template apply< Chris@16: boost::lightweight_forward_adapter() > Chris@16: { }; Chris@16: } Chris@16: Chris@16: # define BOOST_FUNCTIONAL_LIGHTWEIGHT_FORWARD_ADAPTER_HPP_INCLUDED Chris@16: Chris@16: # else // defined(BOOST_PP_IS_ITERATING) Chris@16: # define N BOOST_PP_ITERATION() Chris@16: Chris@16: template< class Self, BOOST_PP_ENUM_PARAMS(N,typename T) > Chris@16: struct lightweight_forward_adapter_result::apply< Chris@16: Self (BOOST_PP_ENUM_PARAMS(N,T)) > Chris@16: : boost::result_of< Chris@16: BOOST_DEDUCED_TYPENAME c::t (BOOST_PP_ENUM_BINARY_PARAMS(N, Chris@16: typename x::t BOOST_PP_INTERCEPT)) > Chris@16: { }; Chris@16: Chris@16: template< class MD, class F, class FC > Chris@16: struct lightweight_forward_adapter_impl Chris@16: : lightweight_forward_adapter_result Chris@16: { Chris@16: template< BOOST_PP_ENUM_PARAMS(N,typename T) > Chris@16: inline typename boost::result_of< F(BOOST_PP_ENUM_BINARY_PARAMS(N, Chris@16: T,const& BOOST_PP_INTERCEPT)) >::type Chris@16: operator()(BOOST_PP_ENUM_BINARY_PARAMS(N,T,& BOOST_PP_INTERCEPT)); Chris@16: }; Chris@16: Chris@16: template< class MD, class F, class FC, int MinArity > Chris@16: struct lightweight_forward_adapter_impl Chris@16: : lightweight_forward_adapter_impl Chris@16: { Chris@16: using lightweight_forward_adapter_impl::operator(); Chris@16: Chris@16: # define M(z,i,d) \ Chris@16: static_cast::t>(a##i) Chris@16: Chris@16: template< BOOST_PP_ENUM_PARAMS(N,typename T) > Chris@16: inline typename lightweight_forward_adapter_result::template apply< Chris@16: MD const (BOOST_PP_ENUM_BINARY_PARAMS(N, Chris@16: T,const& BOOST_PP_INTERCEPT)) >::type Chris@16: operator()(BOOST_PP_ENUM_BINARY_PARAMS(N,T,const& a)) const Chris@16: { Chris@16: typedef lightweight_forward_adapter_result _; Chris@16: return static_cast(this)->target_function()( Chris@16: BOOST_PP_ENUM(N,M,_)); Chris@16: } Chris@16: template< BOOST_PP_ENUM_PARAMS(N,typename T) > Chris@16: inline typename lightweight_forward_adapter_result::template apply< Chris@16: MD (BOOST_PP_ENUM_BINARY_PARAMS(N, Chris@16: T,const& BOOST_PP_INTERCEPT)) >::type Chris@16: operator()(BOOST_PP_ENUM_BINARY_PARAMS(N,T,const& a)) Chris@16: { Chris@16: typedef lightweight_forward_adapter_result _; Chris@16: return static_cast(this)->target_function()( Chris@16: BOOST_PP_ENUM(N,M,_)); Chris@16: } Chris@16: # undef M Chris@16: }; Chris@16: Chris@16: # undef N Chris@16: # endif // defined(BOOST_PP_IS_ITERATING) Chris@16: Chris@16: #endif // include guard Chris@16: