/////////////////////////////////////////////////////////////////////////////// /// \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 #if defined(_MSC_VER) # pragma warning(push) # pragma warning(disable: 4714) // function 'xxx' marked as __forceinline not inlined #endif #include #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 A specialization that treats function pointer Transforms as /// if they were function type Transforms. /// /// This specialization requires that \c Fun is actually a function type. /// /// This specialization is required for nested transforms such as /// call\. In C++, functions that are used as /// parameters to other functions automatically decay to funtion /// pointer types. In other words, the type T0(T1(_)) is /// indistinguishable from T0(T1(*)(_)). This specialization /// is required to handle these nested function pointer type transforms /// properly. template struct call : call {}; /// INTERNAL ONLY template struct call > : call {}; /// \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; BOOST_FORCEINLINE 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; BOOST_FORCEINLINE 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; BOOST_FORCEINLINE 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; BOOST_FORCEINLINE 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; BOOST_FORCEINLINE 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; BOOST_FORCEINLINE 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; BOOST_FORCEINLINE 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> {}; }; #include /// INTERNAL ONLY /// template struct is_callable > : mpl::true_ {}; }} // namespace boost::proto #if defined(_MSC_VER) # pragma warning(pop) #endif #endif