pass_through.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file pass_through.hpp
  3. ///
  4. /// Definition of the pass_through transform, which is the default transform
  5. /// of all of the expression generator metafunctions such as unary_plus<>, plus<>
  6. /// and nary_expr<>.
  7. //
  8. // Copyright 2008 Eric Niebler. Distributed under the Boost
  9. // Software License, Version 1.0. (See accompanying file
  10. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  11. #ifndef BOOST_PROTO_TRANSFORM_PASS_THROUGH_HPP_EAN_12_26_2006
  12. #define BOOST_PROTO_TRANSFORM_PASS_THROUGH_HPP_EAN_12_26_2006
  13. #include <boost/preprocessor/cat.hpp>
  14. #include <boost/preprocessor/repetition/enum.hpp>
  15. #include <boost/preprocessor/iteration/iterate.hpp>
  16. #include <boost/mpl/bool.hpp>
  17. #include <boost/mpl/if.hpp>
  18. #include <boost/type_traits/is_same.hpp>
  19. #include <boost/type_traits/remove_reference.hpp>
  20. #include <boost/proto/proto_fwd.hpp>
  21. #include <boost/proto/args.hpp>
  22. #include <boost/proto/transform/impl.hpp>
  23. #include <boost/proto/detail/ignore_unused.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<
  33. typename Grammar
  34. , typename Domain
  35. , typename Expr
  36. , typename State
  37. , typename Data
  38. , long Arity = arity_of<Expr>::value
  39. >
  40. struct pass_through_impl
  41. {};
  42. #include <boost/proto/transform/detail/pass_through_impl.hpp>
  43. template<typename Grammar, typename Domain, typename Expr, typename State, typename Data>
  44. struct pass_through_impl<Grammar, Domain, Expr, State, Data, 0>
  45. : transform_impl<Expr, State, Data>
  46. {
  47. typedef Expr result_type;
  48. /// \param e An expression
  49. /// \return \c e
  50. /// \throw nothrow
  51. BOOST_FORCEINLINE
  52. BOOST_PROTO_RETURN_TYPE_STRICT_LOOSE(result_type, typename pass_through_impl::expr_param)
  53. operator()(
  54. typename pass_through_impl::expr_param e
  55. , typename pass_through_impl::state_param
  56. , typename pass_through_impl::data_param
  57. ) const
  58. {
  59. return e;
  60. }
  61. };
  62. } // namespace detail
  63. /// \brief A PrimitiveTransform that transforms the child expressions
  64. /// of an expression node according to the corresponding children of
  65. /// a Grammar.
  66. ///
  67. /// Given a Grammar such as <tt>plus\<T0, T1\></tt>, an expression type
  68. /// that matches the grammar such as <tt>plus\<E0, E1\>::type</tt>, a
  69. /// state \c S and a data \c V, the result of applying the
  70. /// <tt>pass_through\<plus\<T0, T1\> \></tt> transform is:
  71. ///
  72. /// \code
  73. /// plus<
  74. /// T0::result<T0(E0, S, V)>::type
  75. /// , T1::result<T1(E1, S, V)>::type
  76. /// >::type
  77. /// \endcode
  78. ///
  79. /// The above demonstrates how child transforms and child expressions
  80. /// are applied pairwise, and how the results are reassembled into a new
  81. /// expression node with the same tag type as the original.
  82. ///
  83. /// The explicit use of <tt>pass_through\<\></tt> is not usually needed,
  84. /// since the expression generator metafunctions such as
  85. /// <tt>plus\<\></tt> have <tt>pass_through\<\></tt> as their default
  86. /// transform. So, for instance, these are equivalent:
  87. ///
  88. /// \code
  89. /// // Within a grammar definition, these are equivalent:
  90. /// when< plus<X, Y>, pass_through< plus<X, Y> > >
  91. /// when< plus<X, Y>, plus<X, Y> >
  92. /// when< plus<X, Y> > // because of when<class X, class Y=X>
  93. /// plus<X, Y> // because plus<> is both a
  94. /// // grammar and a transform
  95. /// \endcode
  96. ///
  97. /// For example, consider the following transform that promotes all
  98. /// \c float terminals in an expression to \c double.
  99. ///
  100. /// \code
  101. /// // This transform finds all float terminals in an expression and promotes
  102. /// // them to doubles.
  103. /// struct Promote
  104. /// : or_<
  105. /// when<terminal<float>, terminal<double>::type(_value) >
  106. /// // terminal<>'s default transform is a no-op:
  107. /// , terminal<_>
  108. /// // nary_expr<> has a pass_through<> transform:
  109. /// , nary_expr<_, vararg<Promote> >
  110. /// >
  111. /// {};
  112. /// \endcode
  113. template<typename Grammar, typename Domain /* = deduce_domain*/>
  114. struct pass_through
  115. : transform<pass_through<Grammar, Domain> >
  116. {
  117. template<typename Expr, typename State, typename Data>
  118. struct impl
  119. : detail::pass_through_impl<Grammar, Domain, Expr, State, Data>
  120. {};
  121. };
  122. /// INTERNAL ONLY
  123. ///
  124. template<typename Grammar, typename Domain>
  125. struct is_callable<pass_through<Grammar, Domain> >
  126. : mpl::true_
  127. {};
  128. }} // namespace boost::proto
  129. #if defined(_MSC_VER)
  130. # pragma warning(pop)
  131. #endif
  132. #endif