#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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace proto { /// \brief Wrap \c PrimitiveTransform so that when\<\> knows /// it is callable. Requires that the parameter is actually a /// PrimitiveTransform. /// /// This form of call\<\> is useful for annotating an /// arbitrary PrimitiveTransform as callable when using it with /// when\<\>. Consider the following transform, which /// is parameterized with another transform. /// /// \code /// template /// struct Foo /// : when< /// unary_plus /// , Grammar(_child) // May or may not work. /// > /// {}; /// \endcode /// /// The problem with the above is that when\<\> may or /// may not recognize \c Grammar as callable, depending on how /// \c Grammar is implemented. (See is_callable\<\> for /// a discussion of this issue.) You can guard against /// the issue by wrapping \c Grammar in call\<\>, such /// as: /// /// \code /// template /// struct Foo /// : when< /// unary_plus /// , call(_child) // OK, this works /// > /// {}; /// \endcode /// /// The above could also have been written as: /// /// \code /// template /// struct Foo /// : when< /// unary_plus /// , call // OK, this works, too /// > /// {}; /// \endcode template struct call : PrimitiveTransform {}; /// \brief Either call the PolymorphicFunctionObject with 0 /// arguments, or invoke the PrimitiveTransform with 3 /// arguments. template struct call : transform > { /// INTERNAL ONLY template struct impl2 : transform_impl { typedef typename BOOST_PROTO_RESULT_OF::type result_type; result_type operator()( typename impl2::expr_param , typename impl2::state_param , typename impl2::data_param ) const { return Fun()(); } }; /// INTERNAL ONLY template struct impl2 : Fun::template impl {}; /// 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 Fun()(). /// Otherwise, return Fun()(e, s, d). /// /// \param e The current expression /// \param s The current state /// \param d An arbitrary data /// If \c Fun is a nullary PolymorphicFunctionObject, \c type is a typedef /// for boost::result_of\::type. Otherwise, it is /// a typedef for boost::result_of\::type. template struct impl : impl2::value> {}; }; /// \brief Either call the PolymorphicFunctionObject with 1 /// argument, or invoke the PrimitiveTransform with 3 /// arguments. template struct call : transform > { template struct impl2 : transform_impl { typedef typename when<_, A0>::template impl::result_type a0; typedef typename detail::poly_function_traits::result_type result_type; result_type operator ()( typename impl2::expr_param e , typename impl2::state_param s , typename impl2::data_param d ) const { return typename detail::poly_function_traits::function_type()( detail::as_lvalue(typename when<_, A0>::template impl()(e, s, d)) ); } }; template struct impl2 : transform_impl { typedef typename when<_, A0>::template impl::result_type a0; typedef typename Fun::template impl::result_type result_type; result_type operator ()( typename impl2::expr_param e , typename impl2::state_param s , typename impl2::data_param d ) const { return typename Fun::template impl()( typename when<_, A0>::template impl()(e, s, d) , s , d ); } }; /// Let \c x be when\<_, A0\>()(e, s, d) 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 boost::result_of\::type. /// Otherwise, it is a typedef for boost::result_of\::type. /// 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 when\<_, A0\>()(e, s, d). /// If \c Fun is a unary PolymorphicFunctionObject that accepts \c x, /// then return Fun()(x). Otherwise, return /// Fun()(x, s, d). /// /// \param e The current expression /// \param s The current state /// \param d An arbitrary data template struct impl : impl2::value> {}; }; /// \brief Either call the PolymorphicFunctionObject with 2 /// arguments, or invoke the PrimitiveTransform with 3 /// arguments. template struct call : transform > { template struct impl2 : transform_impl { typedef typename when<_, A0>::template impl::result_type a0; typedef typename when<_, A1>::template impl::result_type a1; typedef typename detail::poly_function_traits::result_type result_type; result_type operator ()( typename impl2::expr_param e , typename impl2::state_param s , typename impl2::data_param d ) const { return typename detail::poly_function_traits::function_type()( detail::as_lvalue(typename when<_, A0>::template impl()(e, s, d)) , detail::as_lvalue(typename when<_, A1>::template impl()(e, s, d)) ); } }; template struct impl2 : transform_impl { typedef typename when<_, A0>::template impl::result_type a0; typedef typename when<_, A1>::template impl::result_type a1; typedef typename Fun::template impl::result_type result_type; result_type operator ()( typename impl2::expr_param e , typename impl2::state_param s , typename impl2::data_param d ) const { return typename Fun::template impl()( typename when<_, A0>::template impl()(e, s, d) , typename when<_, A1>::template impl()(e, s, d) , d ); } }; /// Let \c x be when\<_, A0\>()(e, s, d) and \c X /// be the type of \c x. /// Let \c y be when\<_, A1\>()(e, s, d) and \c Y /// be the type of \c y. /// If \c Fun is a binary PolymorphicFunction object that accepts \c x /// and \c y, then \c type is a typedef for /// boost::result_of\::type. Otherwise, it is /// a typedef for boost::result_of\::type. /// Either call the PolymorphicFunctionObject with 2 arguments: /// the result of applying the \c A0 transform, and the /// result of applying the \c A1 transform; or invoke the /// PrimitiveTransform with 3 arguments: the result of applying /// the \c A0 transform, the result of applying the \c A1 /// transform, and the data. /// /// Let \c x be when\<_, A0\>()(e, s, d). /// Let \c y be when\<_, A1\>()(e, s, d). /// If \c Fun is a binary PolymorphicFunction object that accepts \c x /// and \c y, return Fun()(x, y). Otherwise, return /// Fun()(x, y, d). /// /// \param e The current expression /// \param s The current state /// \param d An arbitrary data template struct impl : impl2::value> {}; }; /// \brief Call the PolymorphicFunctionObject or the /// PrimitiveTransform with the current expression, state /// and data, transformed according to \c A0, \c A1, and /// \c A2, respectively. template struct call : transform > { template struct impl2 : transform_impl { typedef typename when<_, A0>::template impl::result_type a0; typedef typename when<_, A1>::template impl::result_type a1; typedef typename when<_, A2>::template impl::result_type a2; typedef typename detail::poly_function_traits::result_type result_type; result_type operator ()( typename impl2::expr_param e , typename impl2::state_param s , typename impl2::data_param d ) const { return typename detail::poly_function_traits::function_type()( detail::as_lvalue(typename when<_, A0>::template impl()(e, s, d)) , detail::as_lvalue(typename when<_, A1>::template impl()(e, s, d)) , detail::as_lvalue(typename when<_, A2>::template impl()(e, s, d)) ); } }; template struct impl2 : transform_impl { typedef typename when<_, A0>::template impl::result_type a0; typedef typename when<_, A1>::template impl::result_type a1; typedef typename when<_, A2>::template impl::result_type a2; typedef typename Fun::template impl::result_type result_type; result_type operator ()( typename impl2::expr_param e , typename impl2::state_param s , typename impl2::data_param d ) const { return typename Fun::template impl()( typename when<_, A0>::template impl()(e, s, d) , typename when<_, A1>::template impl()(e, s, d) , typename when<_, A2>::template impl()(e, s, d) ); } }; /// Let \c x be when\<_, A0\>()(e, s, d). /// Let \c y be when\<_, A1\>()(e, s, d). /// Let \c z be when\<_, A2\>()(e, s, d). /// Return Fun()(x, y, z). /// /// \param e The current expression /// \param s The current state /// \param d An arbitrary data template struct impl : impl2::value> {}; }; #if BOOST_PROTO_MAX_ARITY > 3 #define BOOST_PP_ITERATION_PARAMS_1 (3, (4, BOOST_PROTO_MAX_ARITY, )) #include BOOST_PP_ITERATE() #endif /// INTERNAL ONLY /// template struct is_callable > : mpl::true_ {}; }} // namespace boost::proto #endif #else #define N BOOST_PP_ITERATION() /// \brief Call the PolymorphicFunctionObject \c Fun with the /// current expression, state and data, transformed according /// to \c A0 through \c AN. template struct call : transform > { template struct impl : transform_impl { #define M0(Z, M, DATA) \ typedef \ typename when<_, BOOST_PP_CAT(A, M)> \ ::template impl \ ::result_type \ BOOST_PP_CAT(a, M); \ /**/ BOOST_PP_REPEAT(N, M0, ~) #undef M0 typedef typename detail::poly_function_traits::result_type result_type; /// Let \c ax be when\<_, Ax\>()(e, s, d) /// for each \c x in [0,N]. /// Return Fun()(a0, a1,... aN). /// /// \param e The current expression /// \param s The current state /// \param d An arbitrary data result_type operator ()( typename impl::expr_param e , typename impl::state_param s , typename impl::data_param d ) const { #define M0(Z, M, DATA) \ detail::as_lvalue( \ typename when<_, BOOST_PP_CAT(A, M)> \ ::template impl()(e, s, d)) \ /**/ return typename detail::poly_function_traits::function_type()( BOOST_PP_ENUM(N, M0, ~) ); #undef M0 } }; }; #undef N #endif