📄 call.hpp
字号:
#ifndef BOOST_PP_IS_ITERATING /////////////////////////////////////////////////////////////////////////////// /// \file call.hpp /// Contains definition of the call<> transform. // // Copyright 2008 Eric Niebler. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_PROTO_TRANSFORM_CALL_HPP_EAN_11_02_2007 #define BOOST_PROTO_TRANSFORM_CALL_HPP_EAN_11_02_2007 #include <boost/proto/detail/prefix.hpp> #include <boost/preprocessor/cat.hpp> #include <boost/preprocessor/facilities/intercept.hpp> #include <boost/preprocessor/iteration/iterate.hpp> #include <boost/preprocessor/repetition/enum.hpp> #include <boost/preprocessor/repetition/repeat.hpp> #include <boost/preprocessor/repetition/enum_params.hpp> #include <boost/preprocessor/repetition/enum_binary_params.hpp> #include <boost/preprocessor/repetition/enum_trailing_params.hpp> #include <boost/ref.hpp> #include <boost/utility/result_of.hpp> #include <boost/proto/proto_fwd.hpp> #include <boost/proto/traits.hpp> #include <boost/proto/transform/impl.hpp> #include <boost/proto/detail/dont_care.hpp> #include <boost/proto/detail/as_lvalue.hpp> #include <boost/proto/detail/poly_function.hpp> #include <boost/proto/detail/suffix.hpp> namespace boost { namespace proto { /// \brief Wrap \c PrimitiveTransform so that <tt>when\<\></tt> knows /// it is callable. Requires that the parameter is actually a /// PrimitiveTransform. /// /// This form of <tt>call\<\></tt> is useful for annotating an /// arbitrary PrimitiveTransform as callable when using it with /// <tt>when\<\></tt>. Consider the following transform, which /// is parameterized with another transform. /// /// \code /// template<typename Grammar> /// struct Foo /// : when< /// unary_plus<Grammar> /// , Grammar(_child) // May or may not work. /// > /// {}; /// \endcode /// /// The problem with the above is that <tt>when\<\></tt> may or /// may not recognize \c Grammar as callable, depending on how /// \c Grammar is implemented. (See <tt>is_callable\<\></tt> for /// a discussion of this issue.) You can guard against /// the issue by wrapping \c Grammar in <tt>call\<\></tt>, such /// as: /// /// \code /// template<typename Grammar> /// struct Foo /// : when< /// unary_plus<Grammar> /// , call<Grammar>(_child) // OK, this works /// > /// {}; /// \endcode /// /// The above could also have been written as: /// /// \code /// template<typename Grammar> /// struct Foo /// : when< /// unary_plus<Grammar> /// , call<Grammar(_child)> // OK, this works, too /// > /// {}; /// \endcode template<typename PrimitiveTransform> struct call : PrimitiveTransform { }; /// \brief Either call the PolymorphicFunctionObject with 0 /// arguments, or invoke the PrimitiveTransform with 3 /// arguments. template<typename Fun> struct call<Fun()> : transform<call<Fun()> > { /// INTERNAL ONLY template<typename Expr, typename State, typename Data, bool B> struct impl2 : transform_impl<Expr, State, Data> { typedef typename boost::result_of<Fun()>::type result_type; result_type operator()( typename impl2::expr_param , typename impl2::state_param , typename impl2::data_param ) const { return Fun()(); } }; /// INTERNAL ONLY template<typename Expr, typename State, typename Data> struct impl2<Expr, State, Data, true> : Fun::template impl<Expr, State, Data> {}; /// Either call the PolymorphicFunctionObject \c Fun with 0 arguments; or /// invoke the PrimitiveTransform \c Fun with 3 arguments: the current /// expression, state, and data. /// /// If \c Fun is a nullary PolymorphicFunctionObject, return <tt>Fun()()</tt>. /// Otherwise, return <tt>Fun()(expr, state, data)</tt>. /// /// \param expr The current expression /// \param state The current state /// \param data An arbitrary data /// If \c Fun is a nullary PolymorphicFunctionObject, \c type is a typedef /// for <tt>boost::result_of\<Fun()\>::::type</tt>. Otherwise, it is /// a typedef for <tt>boost::result_of\<Fun(Expr, State, Data)\>::::type</tt>. template<typename Expr, typename State, typename Data> struct impl : impl2<Expr, State, Data, is_transform<Fun>::value> {}; }; /// \brief Either call the PolymorphicFunctionObject with 1 /// argument, or invoke the PrimitiveTransform with 3 /// arguments. template<typename Fun, typename A0> struct call<Fun(A0)> : transform<call<Fun(A0)> > { template<typename Expr, typename State, typename Data, bool B> struct impl2 : transform_impl<Expr, State, Data> { typedef typename when<_, A0>::template impl<Expr, State, Data>::result_type a0; typedef typename detail::as_mono_function<Fun(a0)>::type mono_fun; typedef typename boost::result_of<mono_fun(a0)>::type result_type; result_type operator ()( typename impl2::expr_param expr , typename impl2::state_param state , typename impl2::data_param data ) const { return mono_fun()( detail::as_lvalue(typename when<_, A0>::template impl<Expr, State, Data>()(expr, state, data)) ); } }; template<typename Expr, typename State, typename Data> struct impl2<Expr, State, Data, true> : transform_impl<Expr, State, Data> { typedef typename when<_, A0>::template impl<Expr, State, Data>::result_type a0; typedef typename Fun::template impl<a0, State, Data>::result_type result_type; result_type operator ()( typename impl2::expr_param expr , typename impl2::state_param state , typename impl2::data_param data ) const { return typename Fun::template impl<a0, State, Data>()( typename when<_, A0>::template impl<Expr, State, Data>()(expr, state, data) , state , data ); } }; /// Let \c x be <tt>when\<_, A0\>()(expr, state, data)</tt> and \c X /// be the type of \c x. /// If \c Fun is a unary PolymorphicFunctionObject that accepts \c x, /// then \c type is a typedef for <tt>boost::result_of\<Fun(X)\>::::type</tt>. /// Otherwise, it is a typedef for <tt>boost::result_of\<Fun(X, State, Data)\>::::type</tt>. /// Either call the PolymorphicFunctionObject with 1 argument: /// the result of applying the \c A0 transform; or /// invoke the PrimitiveTransform with 3 arguments: /// result of applying the \c A0 transform, the state, and the /// data. /// /// Let \c x be <tt>when\<_, A0\>()(expr, state, data)</tt>. /// If \c Fun is a unary PolymorphicFunctionObject that accepts \c x, /// then return <tt>Fun()(x)</tt>. Otherwise, return /// <tt>Fun()(x, state, data)</tt>. /// /// \param expr The current expression /// \param state The current state /// \param data An arbitrary data template<typename Expr, typename State, typename Data> struct impl : impl2<Expr, State, Data, is_transform<Fun>::value> {}; }; /// \brief Either call the PolymorphicFunctionObject with 2 /// arguments, or invoke the PrimitiveTransform with 3 /// arguments. template<typename Fun, typename A0, typename A1> struct call<Fun(A0, A1)> : transform<call<Fun(A0, A1)> > { template<typename Expr, typename State, typename Data, bool B> struct impl2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -