traits.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #if !defined(BOOST_PROTO_DONT_USE_PREPROCESSED_FILES)
  2. #include <boost/proto/detail/preprocessed/traits.hpp>
  3. #elif !defined(BOOST_PP_IS_ITERATING)
  4. #define BOOST_PROTO_CHILD(Z, N, DATA) \
  5. /** INTERNAL ONLY */ \
  6. typedef BOOST_PP_CAT(DATA, N) BOOST_PP_CAT(proto_child, N); \
  7. /**/
  8. #if defined(__WAVE__) && defined(BOOST_PROTO_CREATE_PREPROCESSED_FILES)
  9. #pragma wave option(preserve: 2, line: 0, output: "preprocessed/traits.hpp")
  10. #endif
  11. ///////////////////////////////////////////////////////////////////////////////
  12. /// \file traits.hpp
  13. /// Definitions of proto::function, proto::nary_expr and proto::result_of::child_c
  14. //
  15. // Copyright 2008 Eric Niebler. Distributed under the Boost
  16. // Software License, Version 1.0. (See accompanying file
  17. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  18. #if defined(__WAVE__) && defined(BOOST_PROTO_CREATE_PREPROCESSED_FILES)
  19. #pragma wave option(preserve: 1)
  20. #endif
  21. #define BOOST_PP_ITERATION_PARAMS_1 \
  22. (3, (0, BOOST_PROTO_MAX_ARITY, <boost/proto/detail/traits.hpp>))
  23. #include BOOST_PP_ITERATE()
  24. #if defined(__WAVE__) && defined(BOOST_PROTO_CREATE_PREPROCESSED_FILES)
  25. #pragma wave option(output: null)
  26. #endif
  27. #undef BOOST_PROTO_CHILD
  28. #else // BOOST_PP_IS_ITERATING
  29. #define N BOOST_PP_ITERATION()
  30. #if N > 0
  31. /// \brief A metafunction for generating function-call expression types,
  32. /// a grammar element for matching function-call expressions, and a
  33. /// PrimitiveTransform that dispatches to the <tt>pass_through\<\></tt>
  34. /// transform.
  35. template<BOOST_PP_ENUM_PARAMS(N, typename A)>
  36. struct function
  37. #if N != BOOST_PROTO_MAX_ARITY
  38. <
  39. BOOST_PP_ENUM_PARAMS(N, A)
  40. BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_SUB(BOOST_PROTO_MAX_ARITY, N), void BOOST_PP_INTERCEPT)
  41. >
  42. #endif
  43. : proto::transform<
  44. function<
  45. BOOST_PP_ENUM_PARAMS(N, A)
  46. BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_SUB(BOOST_PROTO_MAX_ARITY, N), void BOOST_PP_INTERCEPT)
  47. >
  48. , int
  49. >
  50. {
  51. typedef proto::expr<proto::tag::function, BOOST_PP_CAT(list, N)<BOOST_PP_ENUM_PARAMS(N, A)>, N> type;
  52. typedef proto::basic_expr<proto::tag::function, BOOST_PP_CAT(list, N)<BOOST_PP_ENUM_PARAMS(N, A)>, N> proto_grammar;
  53. template<typename Expr, typename State, typename Data>
  54. struct impl
  55. : detail::pass_through_impl<function, deduce_domain, Expr, State, Data>
  56. {};
  57. /// INTERNAL ONLY
  58. typedef proto::tag::function proto_tag;
  59. BOOST_PP_REPEAT(N, BOOST_PROTO_CHILD, A)
  60. BOOST_PP_REPEAT_FROM_TO(
  61. N
  62. , BOOST_PROTO_MAX_ARITY
  63. , BOOST_PROTO_CHILD
  64. , detail::if_vararg<BOOST_PP_CAT(A, BOOST_PP_DEC(N))> BOOST_PP_INTERCEPT
  65. )
  66. };
  67. /// \brief A metafunction for generating n-ary expression types with a
  68. /// specified tag type,
  69. /// a grammar element for matching n-ary expressions, and a
  70. /// PrimitiveTransform that dispatches to the <tt>pass_through\<\></tt>
  71. /// transform.
  72. ///
  73. /// Use <tt>nary_expr\<_, vararg\<_\> \></tt> as a grammar element to match any
  74. /// n-ary expression; that is, any non-terminal.
  75. template<typename Tag BOOST_PP_ENUM_TRAILING_PARAMS(N, typename A)>
  76. struct nary_expr
  77. #if N != BOOST_PROTO_MAX_ARITY
  78. <
  79. Tag
  80. BOOST_PP_ENUM_TRAILING_PARAMS(N, A)
  81. BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_SUB(BOOST_PROTO_MAX_ARITY, N), void BOOST_PP_INTERCEPT)
  82. >
  83. #endif
  84. : proto::transform<
  85. nary_expr<
  86. Tag
  87. BOOST_PP_ENUM_TRAILING_PARAMS(N, A)
  88. BOOST_PP_ENUM_TRAILING_PARAMS(BOOST_PP_SUB(BOOST_PROTO_MAX_ARITY, N), void BOOST_PP_INTERCEPT)
  89. >
  90. , int
  91. >
  92. {
  93. typedef proto::expr<Tag, BOOST_PP_CAT(list, N)<BOOST_PP_ENUM_PARAMS(N, A)>, N> type;
  94. typedef proto::basic_expr<Tag, BOOST_PP_CAT(list, N)<BOOST_PP_ENUM_PARAMS(N, A)>, N> proto_grammar;
  95. template<typename Expr, typename State, typename Data>
  96. struct impl
  97. : detail::pass_through_impl<nary_expr, deduce_domain, Expr, State, Data>
  98. {};
  99. /// INTERNAL ONLY
  100. typedef Tag proto_tag;
  101. BOOST_PP_REPEAT(N, BOOST_PROTO_CHILD, A)
  102. BOOST_PP_REPEAT_FROM_TO(
  103. N
  104. , BOOST_PROTO_MAX_ARITY
  105. , BOOST_PROTO_CHILD
  106. , detail::if_vararg<BOOST_PP_CAT(A, BOOST_PP_DEC(N))> BOOST_PP_INTERCEPT
  107. )
  108. };
  109. namespace detail
  110. {
  111. template<
  112. template<BOOST_PP_ENUM_PARAMS(N, typename BOOST_PP_INTERCEPT)> class T
  113. , BOOST_PP_ENUM_PARAMS(N, typename A)
  114. >
  115. struct is_callable_<T<BOOST_PP_ENUM_PARAMS(N, A)> BOOST_PROTO_TEMPLATE_ARITY_PARAM(N)>
  116. : is_same<BOOST_PP_CAT(A, BOOST_PP_DEC(N)), callable>
  117. {};
  118. }
  119. #endif
  120. namespace result_of
  121. {
  122. /// \brief A metafunction that returns the type of the Nth child
  123. /// of a Proto expression.
  124. ///
  125. /// A metafunction that returns the type of the Nth child
  126. /// of a Proto expression. \c N must be less than
  127. /// \c Expr::proto_arity::value.
  128. template<typename Expr>
  129. struct child_c<Expr, N>
  130. {
  131. /// Verify that we are not operating on a terminal
  132. BOOST_STATIC_ASSERT(0 != Expr::proto_arity_c);
  133. /// The raw type of the Nth child as it is stored within
  134. /// \c Expr. This may be a value or a reference
  135. typedef typename Expr::BOOST_PP_CAT(proto_child, N) value_type;
  136. /// The "value" type of the child, suitable for return by value,
  137. /// computed as follows:
  138. /// \li <tt>T const &</tt> becomes <tt>T</tt>
  139. /// \li <tt>T &</tt> becomes <tt>T</tt>
  140. /// \li <tt>T</tt> becomes <tt>T</tt>
  141. typedef typename detail::expr_traits<typename Expr::BOOST_PP_CAT(proto_child, N)>::value_type type;
  142. };
  143. template<typename Expr>
  144. struct child_c<Expr &, N>
  145. {
  146. /// Verify that we are not operating on a terminal
  147. BOOST_STATIC_ASSERT(0 != Expr::proto_arity_c);
  148. /// The raw type of the Nth child as it is stored within
  149. /// \c Expr. This may be a value or a reference
  150. typedef typename Expr::BOOST_PP_CAT(proto_child, N) value_type;
  151. /// The "reference" type of the child, suitable for return by
  152. /// reference, computed as follows:
  153. /// \li <tt>T const &</tt> becomes <tt>T const &</tt>
  154. /// \li <tt>T &</tt> becomes <tt>T &</tt>
  155. /// \li <tt>T</tt> becomes <tt>T &</tt>
  156. typedef typename detail::expr_traits<typename Expr::BOOST_PP_CAT(proto_child, N)>::reference type;
  157. /// INTERNAL ONLY
  158. ///
  159. BOOST_FORCEINLINE
  160. static type call(Expr &e)
  161. {
  162. return e.proto_base().BOOST_PP_CAT(child, N);
  163. }
  164. };
  165. template<typename Expr>
  166. struct child_c<Expr const &, N>
  167. {
  168. /// Verify that we are not operating on a terminal
  169. BOOST_STATIC_ASSERT(0 != Expr::proto_arity_c);
  170. /// The raw type of the Nth child as it is stored within
  171. /// \c Expr. This may be a value or a reference
  172. typedef typename Expr::BOOST_PP_CAT(proto_child, N) value_type;
  173. /// The "const reference" type of the child, suitable for return by
  174. /// const reference, computed as follows:
  175. /// \li <tt>T const &</tt> becomes <tt>T const &</tt>
  176. /// \li <tt>T &</tt> becomes <tt>T &</tt>
  177. /// \li <tt>T</tt> becomes <tt>T const &</tt>
  178. typedef typename detail::expr_traits<typename Expr::BOOST_PP_CAT(proto_child, N)>::const_reference type;
  179. /// INTERNAL ONLY
  180. ///
  181. BOOST_FORCEINLINE
  182. static type call(Expr const &e)
  183. {
  184. return e.proto_base().BOOST_PP_CAT(child, N);
  185. }
  186. };
  187. }
  188. #undef N
  189. #endif