when.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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/mpl/eval_if.hpp>
  18. #include <boost/proto/proto_fwd.hpp>
  19. #include <boost/proto/traits.hpp>
  20. #include <boost/proto/transform/call.hpp>
  21. #include <boost/proto/transform/make.hpp>
  22. #include <boost/proto/transform/impl.hpp>
  23. #include <boost/proto/transform/env.hpp>
  24. #if defined(_MSC_VER)
  25. # pragma warning(push)
  26. # pragma warning(disable : 4714) // function 'xxx' marked as __forceinline not inlined
  27. #endif
  28. namespace boost { namespace proto
  29. {
  30. namespace detail
  31. {
  32. template<typename Grammar, typename R, typename Fun>
  33. struct when_impl
  34. : transform<when<Grammar, Fun> >
  35. {
  36. typedef Grammar first;
  37. typedef Fun second;
  38. typedef typename Grammar::proto_grammar proto_grammar;
  39. // Note: do not evaluate is_callable<R> in this scope.
  40. // R may be an incomplete type at this point.
  41. template<typename Expr, typename State, typename Data>
  42. struct impl : transform_impl<Expr, State, Data>
  43. {
  44. // OK to evaluate is_callable<R> here. R should be compete by now.
  45. typedef
  46. typename mpl::if_c<
  47. is_callable<R>::value
  48. , proto::call<Fun> // "R" is a function to call
  49. , proto::make<Fun> // "R" is an object to construct
  50. >::type
  51. which;
  52. typedef typename which::template impl<Expr, State, Data>::result_type result_type;
  53. /// Evaluate <tt>R(A0,A1,...)</tt> as a transform either with
  54. /// <tt>call\<\></tt> or with <tt>make\<\></tt> depending on
  55. /// whether <tt>is_callable\<R\>::value</tt> is \c true or
  56. /// \c false.
  57. ///
  58. /// \param e The current expression
  59. /// \param s The current state
  60. /// \param d An arbitrary data
  61. /// \pre <tt>matches\<Expr, Grammar\>::value</tt> is \c true
  62. /// \return <tt>which()(e, s, d)</tt>
  63. BOOST_FORCEINLINE
  64. result_type operator ()(
  65. typename impl::expr_param e
  66. , typename impl::state_param s
  67. , typename impl::data_param d
  68. ) const
  69. {
  70. return typename which::template impl<Expr, State, Data>()(e, s, d);
  71. }
  72. };
  73. };
  74. }
  75. /// \brief A grammar element and a PrimitiveTransform that associates
  76. /// a transform with the grammar.
  77. ///
  78. /// Use <tt>when\<\></tt> to override a grammar's default transform
  79. /// with a custom transform. It is for used when composing larger
  80. /// transforms by associating smaller transforms with individual
  81. /// rules in your grammar, as in the following transform which
  82. /// counts the number of terminals in an expression.
  83. ///
  84. /// \code
  85. /// // Count the terminals in an expression tree.
  86. /// // Must be invoked with initial state == mpl::int_<0>().
  87. /// struct CountLeaves
  88. /// : or_<
  89. /// when<terminal<_>, mpl::next<_state>()>
  90. /// , otherwise<fold<_, _state, CountLeaves> >
  91. /// >
  92. /// {};
  93. /// \endcode
  94. ///
  95. /// In <tt>when\<G, T\></tt>, when \c T is a class type it is a
  96. /// PrimitiveTransform and the following equivalencies hold:
  97. ///
  98. /// <tt>boost::result_of\<when\<G,T\>(E,S,V)\>::type</tt> is the same as
  99. /// <tt>boost::result_of\<T(E,S,V)\>::type</tt>.
  100. ///
  101. /// <tt>when\<G,T\>()(e,s,d)</tt> is the same as
  102. /// <tt>T()(e,s,d)</tt>.
  103. template<typename Grammar, typename PrimitiveTransform /*= Grammar*/>
  104. struct when
  105. : PrimitiveTransform
  106. {
  107. typedef Grammar first;
  108. typedef PrimitiveTransform second;
  109. typedef typename Grammar::proto_grammar proto_grammar;
  110. };
  111. /// \brief A specialization that treats function pointer Transforms as
  112. /// if they were function type Transforms.
  113. ///
  114. /// This specialization requires that \c Fun is actually a function type.
  115. ///
  116. /// This specialization is required for nested transforms such as
  117. /// <tt>when\<G, T0(T1(_))\></tt>. In C++, functions that are used as
  118. /// parameters to other functions automatically decay to funtion
  119. /// pointer types. In other words, the type <tt>T0(T1(_))</tt> is
  120. /// indistinguishable from <tt>T0(T1(*)(_))</tt>. This specialization
  121. /// is required to handle these nested function pointer type transforms
  122. /// properly.
  123. template<typename Grammar, typename Fun>
  124. struct when<Grammar, Fun *>
  125. : when<Grammar, Fun>
  126. {};
  127. /// \brief Syntactic sugar for <tt>when\<_, Fun\></tt>, for use
  128. /// in grammars to handle all the cases not yet handled.
  129. ///
  130. /// Use <tt>otherwise\<T\></tt> in your grammars as a synonym for
  131. /// <tt>when\<_, T\></tt> as in the following transform which
  132. /// counts the number of terminals in an expression.
  133. ///
  134. /// \code
  135. /// // Count the terminals in an expression tree.
  136. /// // Must be invoked with initial state == mpl::int_<0>().
  137. /// struct CountLeaves
  138. /// : or_<
  139. /// when<terminal<_>, mpl::next<_state>()>
  140. /// , otherwise<fold<_, _state, CountLeaves> >
  141. /// >
  142. /// {};
  143. /// \endcode
  144. template<typename Fun>
  145. struct otherwise
  146. : when<_, Fun>
  147. {};
  148. namespace envns_
  149. {
  150. // Define the transforms global
  151. BOOST_PROTO_DEFINE_ENV_VAR(transforms_type, transforms);
  152. }
  153. using envns_::transforms;
  154. /// \brief This specialization uses the Data parameter as a collection
  155. /// of transforms that can be indexed by the specified rule.
  156. ///
  157. /// Use <tt>when\<T, external_transform\></tt> in your code when you would like
  158. /// to define a grammar once and use it to evaluate expressions with
  159. /// many different sets of transforms. The transforms are found by
  160. /// using the Data parameter as a map from rules to transforms.
  161. ///
  162. /// See \c action_map for an example.
  163. template<typename Grammar>
  164. struct when<Grammar, external_transform>
  165. : proto::transform<when<Grammar, external_transform> >
  166. {
  167. typedef Grammar first;
  168. typedef external_transform second;
  169. typedef typename Grammar::proto_grammar proto_grammar;
  170. template<typename Expr, typename State, typename Data>
  171. struct impl
  172. : remove_reference<
  173. typename mpl::eval_if_c<
  174. proto::result_of::has_env_var<Data, transforms_type>::value
  175. , proto::result_of::env_var<Data, transforms_type>
  176. , proto::result_of::env_var<Data, data_type>
  177. >::type
  178. >::type::template when<Grammar>::template impl<Expr, State, Data>
  179. {};
  180. };
  181. /// \brief For defining a map of Rule/Transform pairs for use with
  182. /// <tt>when\<T, external_transform\></tt> to make transforms external to the grammar
  183. ///
  184. /// The following code defines a grammar with a couple of external transforms.
  185. /// It also defines an action_map that maps from rules to transforms. It then
  186. /// passes that transforms map at the Data parameter to the grammar. In this way,
  187. /// the behavior of the grammar can be modified post-hoc by passing a different
  188. /// action_map.
  189. ///
  190. /// \code
  191. /// struct int_terminal
  192. /// : proto::terminal<int>
  193. /// {};
  194. ///
  195. /// struct char_terminal
  196. /// : proto::terminal<char>
  197. /// {};
  198. ///
  199. /// struct my_grammar
  200. /// : proto::or_<
  201. /// proto::when< int_terminal, proto::external_transform >
  202. /// , proto::when< char_terminal, proto::external_transform >
  203. /// , proto::when<
  204. /// proto::plus< my_grammar, my_grammar >
  205. /// , proto::fold< _, int(), my_grammar >
  206. /// >
  207. /// >
  208. /// {};
  209. ///
  210. /// struct my_transforms
  211. /// : proto::external_transforms<
  212. /// proto::when<int_terminal, print(proto::_value)>
  213. /// , proto::when<char_terminal, print(proto::_value)>
  214. /// >
  215. /// {};
  216. ///
  217. /// proto::literal<int> i(1);
  218. /// proto::literal<char> c('a');
  219. /// my_transforms trx;
  220. ///
  221. /// // Evaluate "i+c" using my_grammar with the specified transforms:
  222. /// my_grammar()(i + c, 0, trx);
  223. /// \endcode
  224. template<BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(BOOST_MPL_LIMIT_MAP_SIZE, typename T, mpl::na)>
  225. struct external_transforms
  226. {
  227. typedef mpl::map<BOOST_PP_ENUM_PARAMS(BOOST_MPL_LIMIT_MAP_SIZE, T)> map_type;
  228. template<typename Rule>
  229. struct when
  230. : proto::when<_, typename mpl::at<map_type, Rule>::type>
  231. {};
  232. };
  233. // Other specializations of proto::when are generated by the preprocessor...
  234. #include <boost/proto/transform/detail/when.hpp>
  235. /// INTERNAL ONLY
  236. ///
  237. template<typename Grammar, typename Transform>
  238. struct is_callable<when<Grammar, Transform> >
  239. : mpl::true_
  240. {};
  241. }} // namespace boost::proto
  242. #if defined(_MSC_VER)
  243. # pragma warning(pop)
  244. #endif
  245. #endif