/Src/Dependencies/Boost/boost/proto/transform/when.hpp

http://hadesmem.googlecode.com/ · C++ Header · 197 lines · 64 code · 14 blank · 119 comment · 0 complexity · 2c0da20fa74cfe596d94065577e1410c MD5 · raw file

  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file when.hpp
  3. /// Definition of when transform.
  4. //
  5. // Copyright 2008 Eric Niebler. Distributed under the Boost
  6. // Software License, Version 1.0. (See accompanying file
  7. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_PROTO_TRANSFORM_WHEN_HPP_EAN_10_29_2007
  9. #define BOOST_PROTO_TRANSFORM_WHEN_HPP_EAN_10_29_2007
  10. #include <boost/preprocessor/cat.hpp>
  11. #include <boost/preprocessor/repetition/enum_params.hpp>
  12. #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
  13. #include <boost/preprocessor/iteration/iterate.hpp>
  14. #include <boost/mpl/at.hpp>
  15. #include <boost/mpl/if.hpp>
  16. #include <boost/mpl/map.hpp>
  17. #include <boost/proto/proto_fwd.hpp>
  18. #include <boost/proto/traits.hpp>
  19. #include <boost/proto/transform/call.hpp>
  20. #include <boost/proto/transform/make.hpp>
  21. #include <boost/proto/transform/impl.hpp>
  22. namespace boost { namespace proto
  23. {
  24. /// \brief A grammar element and a PrimitiveTransform that associates
  25. /// a transform with the grammar.
  26. ///
  27. /// Use <tt>when\<\></tt> to override a grammar's default transform
  28. /// with a custom transform. It is for used when composing larger
  29. /// transforms by associating smaller transforms with individual
  30. /// rules in your grammar, as in the following transform which
  31. /// counts the number of terminals in an expression.
  32. ///
  33. /// \code
  34. /// // Count the terminals in an expression tree.
  35. /// // Must be invoked with initial state == mpl::int_<0>().
  36. /// struct CountLeaves
  37. /// : or_<
  38. /// when<terminal<_>, mpl::next<_state>()>
  39. /// , otherwise<fold<_, _state, CountLeaves> >
  40. /// >
  41. /// {};
  42. /// \endcode
  43. ///
  44. /// In <tt>when\<G, T\></tt>, when \c T is a class type it is a
  45. /// PrimitiveTransform and the following equivalencies hold:
  46. ///
  47. /// <tt>boost::result_of\<when\<G,T\>(E,S,V)\>::type</tt> is the same as
  48. /// <tt>boost::result_of\<T(E,S,V)\>::type</tt>.
  49. ///
  50. /// <tt>when\<G,T\>()(e,s,d)</tt> is the same as
  51. /// <tt>T()(e,s,d)</tt>.
  52. template<typename Grammar, typename PrimitiveTransform /*= Grammar*/>
  53. struct when
  54. : PrimitiveTransform
  55. {
  56. typedef Grammar first;
  57. typedef PrimitiveTransform second;
  58. typedef typename Grammar::proto_grammar proto_grammar;
  59. };
  60. /// \brief A specialization that treats function pointer Transforms as
  61. /// if they were function type Transforms.
  62. ///
  63. /// This specialization requires that \c Fun is actually a function type.
  64. ///
  65. /// This specialization is required for nested transforms such as
  66. /// <tt>when\<G, T0(T1(_))\></tt>. In C++, functions that are used as
  67. /// parameters to other functions automatically decay to funtion
  68. /// pointer types. In other words, the type <tt>T0(T1(_))</tt> is
  69. /// indistinguishable from <tt>T0(T1(*)(_))</tt>. This specialization
  70. /// is required to handle these nested function pointer type transforms
  71. /// properly.
  72. template<typename Grammar, typename Fun>
  73. struct when<Grammar, Fun *>
  74. : when<Grammar, Fun>
  75. {};
  76. /// \brief Syntactic sugar for <tt>when\<_, Fun\></tt>, for use
  77. /// in grammars to handle all the cases not yet handled.
  78. ///
  79. /// Use <tt>otherwise\<T\></tt> in your grammars as a synonym for
  80. /// <tt>when\<_, T\></tt> as in the following transform which
  81. /// counts the number of terminals in an expression.
  82. ///
  83. /// \code
  84. /// // Count the terminals in an expression tree.
  85. /// // Must be invoked with initial state == mpl::int_<0>().
  86. /// struct CountLeaves
  87. /// : or_<
  88. /// when<terminal<_>, mpl::next<_state>()>
  89. /// , otherwise<fold<_, _state, CountLeaves> >
  90. /// >
  91. /// {};
  92. /// \endcode
  93. template<typename Fun>
  94. struct otherwise
  95. : when<_, Fun>
  96. {};
  97. /// \brief This specialization uses the Data parameter as a collection
  98. /// of transforms that can be indexed by the specified rule.
  99. ///
  100. /// Use <tt>when\<T, external_transform\></tt> in your code when you would like
  101. /// to define a grammar once and use it to evaluate expressions with
  102. /// many different sets of transforms. The transforms are found by
  103. /// using the Data parameter as a map from rules to transforms.
  104. ///
  105. /// See \c action_map for an example.
  106. template<typename Grammar>
  107. struct when<Grammar, external_transform>
  108. : proto::transform<when<Grammar, external_transform> >
  109. {
  110. typedef Grammar first;
  111. typedef external_transform second;
  112. typedef typename Grammar::proto_grammar proto_grammar;
  113. template<typename Expr, typename State, typename Data>
  114. struct impl
  115. : Data::template when<Grammar>::template impl<Expr, State, Data>
  116. {};
  117. template<typename Expr, typename State, typename Data>
  118. struct impl<Expr, State, Data &>
  119. : Data::template when<Grammar>::template impl<Expr, State, Data &>
  120. {};
  121. };
  122. /// \brief For defining a map of Rule/Transform pairs for use with
  123. /// <tt>when\<T, external_transform\></tt> to make transforms external to the grammar
  124. ///
  125. /// The following code defines a grammar with a couple of external transforms.
  126. /// It also defines an action_map that maps from rules to transforms. It then
  127. /// passes that transforms map at the Data parameter to the grammar. In this way,
  128. /// the behavior of the grammar can be modified post-hoc by passing a different
  129. /// action_map.
  130. ///
  131. /// \code
  132. /// struct int_terminal
  133. /// : proto::terminal<int>
  134. /// {};
  135. ///
  136. /// struct char_terminal
  137. /// : proto::terminal<char>
  138. /// {};
  139. ///
  140. /// struct my_grammar
  141. /// : proto::or_<
  142. /// proto::when< int_terminal, proto::external_transform >
  143. /// , proto::when< char_terminal, proto::external_transform >
  144. /// , proto::when<
  145. /// proto::plus< my_grammar, my_grammar >
  146. /// , proto::fold< _, int(), my_grammar >
  147. /// >
  148. /// >
  149. /// {};
  150. ///
  151. /// struct my_transforms
  152. /// : proto::external_transforms<
  153. /// proto::when<int_terminal, print(proto::_value)>
  154. /// , proto::when<char_terminal, print(proto::_value)>
  155. /// >
  156. /// {};
  157. ///
  158. /// proto::literal<int> i(1);
  159. /// proto::literal<char> c('a');
  160. /// my_transforms trx;
  161. ///
  162. /// // Evaluate "i+c" using my_grammar with the specified transforms:
  163. /// my_grammar()(i + c, 0, trx);
  164. /// \endcode
  165. template<BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(BOOST_MPL_LIMIT_MAP_SIZE, typename T, mpl::na)>
  166. struct external_transforms
  167. {
  168. typedef mpl::map<BOOST_PP_ENUM_PARAMS(BOOST_MPL_LIMIT_MAP_SIZE, T)> map_type;
  169. template<typename Rule>
  170. struct when
  171. : proto::when<_, typename mpl::at<map_type, Rule>::type>
  172. {};
  173. };
  174. // Other specializations of proto::when are generated by the preprocessor...
  175. #include <boost/proto/transform/detail/when.hpp>
  176. /// INTERNAL ONLY
  177. ///
  178. template<typename Grammar, typename Transform>
  179. struct is_callable<when<Grammar, Transform> >
  180. : mpl::true_
  181. {};
  182. }} // namespace boost::proto
  183. #endif