/////////////////////////////////////////////////////////////////////////////// /// \file when.hpp /// Definition of when 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_WHEN_HPP_EAN_10_29_2007 #define BOOST_PROTO_TRANSFORM_WHEN_HPP_EAN_10_29_2007 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(_MSC_VER) && (_MSC_VER >= 1020) # pragma warning(push) # pragma warning(disable : 4714) // function 'xxx' marked as __forceinline not inlined #endif namespace boost { namespace proto { namespace detail { template struct when_impl : transform > { typedef Grammar first; typedef Fun second; typedef typename Grammar::proto_grammar proto_grammar; // Note: do not evaluate is_callable in this scope. // R may be an incomplete type at this point. template struct impl : transform_impl { // OK to evaluate is_callable here. R should be compete by now. typedef typename mpl::if_c< is_callable::value , proto::call // "R" is a function to call , proto::make // "R" is an object to construct >::type which; typedef typename which::template impl::result_type result_type; /// Evaluate R(A0,A1,...) as a transform either with /// call\<\> or with make\<\> depending on /// whether is_callable\::value is \c true or /// \c false. /// /// \param e The current expression /// \param s The current state /// \param d An arbitrary data /// \pre matches\::value is \c true /// \return which()(e, s, d) BOOST_FORCEINLINE result_type operator ()( typename impl::expr_param e , typename impl::state_param s , typename impl::data_param d ) const { return typename which::template impl()(e, s, d); } }; }; } /// \brief A grammar element and a PrimitiveTransform that associates /// a transform with the grammar. /// /// Use when\<\> to override a grammar's default transform /// with a custom transform. It is for used when composing larger /// transforms by associating smaller transforms with individual /// rules in your grammar, as in the following transform which /// counts the number of terminals in an expression. /// /// \code /// // Count the terminals in an expression tree. /// // Must be invoked with initial state == mpl::int_<0>(). /// struct CountLeaves /// : or_< /// when, mpl::next<_state>()> /// , otherwise > /// > /// {}; /// \endcode /// /// In when\, when \c T is a class type it is a /// PrimitiveTransform and the following equivalencies hold: /// /// boost::result_of\(E,S,V)\>::type is the same as /// boost::result_of\::type. /// /// when\()(e,s,d) is the same as /// T()(e,s,d). template struct when : PrimitiveTransform { typedef Grammar first; typedef PrimitiveTransform second; typedef typename Grammar::proto_grammar proto_grammar; }; /// \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 /// when\. 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 when : when {}; /// \brief Syntactic sugar for when\<_, Fun\>, for use /// in grammars to handle all the cases not yet handled. /// /// Use otherwise\ in your grammars as a synonym for /// when\<_, T\> as in the following transform which /// counts the number of terminals in an expression. /// /// \code /// // Count the terminals in an expression tree. /// // Must be invoked with initial state == mpl::int_<0>(). /// struct CountLeaves /// : or_< /// when, mpl::next<_state>()> /// , otherwise > /// > /// {}; /// \endcode template struct otherwise : when<_, Fun> {}; namespace envns_ { // Define the transforms global BOOST_PROTO_DEFINE_ENV_VAR(transforms_type, transforms); } using envns_::transforms; /// \brief This specialization uses the Data parameter as a collection /// of transforms that can be indexed by the specified rule. /// /// Use when\ in your code when you would like /// to define a grammar once and use it to evaluate expressions with /// many different sets of transforms. The transforms are found by /// using the Data parameter as a map from rules to transforms. /// /// See \c action_map for an example. template struct when : proto::transform > { typedef Grammar first; typedef external_transform second; typedef typename Grammar::proto_grammar proto_grammar; template struct impl : remove_reference< typename mpl::eval_if_c< proto::result_of::has_env_var::value , proto::result_of::env_var , proto::result_of::env_var >::type >::type::template when::template impl {}; }; /// \brief For defining a map of Rule/Transform pairs for use with /// when\ to make transforms external to the grammar /// /// The following code defines a grammar with a couple of external transforms. /// It also defines an action_map that maps from rules to transforms. It then /// passes that transforms map at the Data parameter to the grammar. In this way, /// the behavior of the grammar can be modified post-hoc by passing a different /// action_map. /// /// \code /// struct int_terminal /// : proto::terminal /// {}; /// /// struct char_terminal /// : proto::terminal /// {}; /// /// struct my_grammar /// : proto::or_< /// proto::when< int_terminal, proto::external_transform > /// , proto::when< char_terminal, proto::external_transform > /// , proto::when< /// proto::plus< my_grammar, my_grammar > /// , proto::fold< _, int(), my_grammar > /// > /// > /// {}; /// /// struct my_transforms /// : proto::external_transforms< /// proto::when /// , proto::when /// > /// {}; /// /// proto::literal i(1); /// proto::literal c('a'); /// my_transforms trx; /// /// // Evaluate "i+c" using my_grammar with the specified transforms: /// my_grammar()(i + c, 0, trx); /// \endcode template struct external_transforms { typedef mpl::map map_type; template struct when : proto::when<_, typename mpl::at::type> {}; }; // Other specializations of proto::when are generated by the preprocessor... #include /// INTERNAL ONLY /// template struct is_callable > : mpl::true_ {}; }} // namespace boost::proto #if defined(_MSC_VER) && (_MSC_VER >= 1020) # pragma warning(pop) #endif #endif