repeat.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file repeat.hpp
  3. /// Contains macros to ease the generation of repetitious code constructs
  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_REPEAT_HPP_EAN_11_24_2008
  9. #define BOOST_PROTO_REPEAT_HPP_EAN_11_24_2008
  10. #include <boost/preprocessor/cat.hpp>
  11. #include <boost/preprocessor/facilities/intercept.hpp>
  12. #include <boost/preprocessor/repetition/enum.hpp>
  13. #include <boost/preprocessor/repetition/enum_params.hpp>
  14. #include <boost/preprocessor/repetition/enum_binary_params.hpp>
  15. #include <boost/preprocessor/iteration/local.hpp>
  16. #include <boost/preprocessor/tuple/elem.hpp>
  17. #include <boost/proto/proto_fwd.hpp> // for BOOST_PROTO_MAX_ARITY
  18. ////////////////////////////////////////////
  19. /// INTERNAL ONLY
  20. #define BOOST_PROTO_ref_a_aux(Z, N, DATA)\
  21. boost::ref(BOOST_PP_CAT(proto_a, N))
  22. /// \brief Generates a sequence like <tt>typename A0, typename A1, ...</tt>
  23. ///
  24. #define BOOST_PROTO_typename_A(N)\
  25. BOOST_PP_ENUM_PARAMS(N, typename proto_A)
  26. /// \brief Generates a sequence like <tt>A0 const &, A1 const &, ...</tt>
  27. ///
  28. #define BOOST_PROTO_A_const_ref(N)\
  29. BOOST_PP_ENUM_BINARY_PARAMS(N, proto_A, const & BOOST_PP_INTERCEPT)
  30. /// \brief Generates a sequence like <tt>A0 &, A1 &, ...</tt>
  31. ///
  32. #define BOOST_PROTO_A_ref(N)\
  33. BOOST_PP_ENUM_BINARY_PARAMS(N, proto_A, & BOOST_PP_INTERCEPT)
  34. /// \brief Generates a sequence like <tt>A0, A1, ...</tt>
  35. ///
  36. #define BOOST_PROTO_A(N)\
  37. BOOST_PP_ENUM_PARAMS(N, proto_A)
  38. /// \brief Generates a sequence like <tt>A0 const, A1 const, ...</tt>
  39. ///
  40. #define BOOST_PROTO_A_const(N)\
  41. BOOST_PP_ENUM_PARAMS(N, const proto_A)
  42. /// \brief Generates a sequence like <tt>A0 const &a0, A1 const &a0, ...</tt>
  43. ///
  44. #define BOOST_PROTO_A_const_ref_a(N)\
  45. BOOST_PP_ENUM_BINARY_PARAMS(N, proto_A, const &proto_a)
  46. /// \brief Generates a sequence like <tt>A0 &a0, A1 &a0, ...</tt>
  47. ///
  48. #define BOOST_PROTO_A_ref_a(N)\
  49. BOOST_PP_ENUM_BINARY_PARAMS(N, proto_A, &proto_a)
  50. /// \brief Generates a sequence like <tt>boost::ref(a0), boost::ref(a1), ...</tt>
  51. ///
  52. #define BOOST_PROTO_ref_a(N)\
  53. BOOST_PP_ENUM(N, BOOST_PROTO_ref_a_aux, ~)
  54. /// \brief Generates a sequence like <tt>a0, a1, ...</tt>
  55. ///
  56. #define BOOST_PROTO_a(N)\
  57. BOOST_PP_ENUM_PARAMS(N, proto_a)
  58. ////////////////////////////////////////////
  59. /// INTERNAL ONLY
  60. #define BOOST_PROTO_invoke(Z, N, DATA)\
  61. BOOST_PP_TUPLE_ELEM(5,0,DATA)(N, BOOST_PP_TUPLE_ELEM(5,1,DATA), BOOST_PP_TUPLE_ELEM(5,2,DATA), BOOST_PP_TUPLE_ELEM(5,3,DATA), BOOST_PP_TUPLE_ELEM(5,4,DATA))
  62. /// \brief Repeatedly invoke the specified macro.
  63. ///
  64. /// BOOST_PROTO_REPEAT_FROM_TO_EX() is used generate the kind of repetitive code that is typical
  65. /// of EDSLs built with Proto. BOOST_PROTO_REPEAT_FROM_TO_EX(FROM, TO, MACRO, typename_A, A, A_a, a) is equivalent to:
  66. ///
  67. /// \code
  68. /// MACRO(FROM, typename_A, A, A_a, a)
  69. /// MACRO(FROM+1, typename_A, A, A_a, a)
  70. /// ...
  71. /// MACRO(TO-1, typename_A, A, A_a, a)
  72. /// \endcode
  73. #define BOOST_PROTO_REPEAT_FROM_TO_EX(FROM, TO, MACRO, typename_A, A, A_a, a)\
  74. BOOST_PP_REPEAT_FROM_TO(FROM, TO, BOOST_PROTO_invoke, (MACRO, typename_A, A, A_a, a))
  75. /// \brief Repeatedly invoke the specified macro.
  76. ///
  77. /// BOOST_PROTO_REPEAT_FROM_TO() is used generate the kind of repetitive code that is typical
  78. /// of EDSLs built with Proto. BOOST_PROTO_REPEAT_FROM_TO(FROM, TO, MACRO) is equivalent to:
  79. ///
  80. /// \code
  81. /// MACRO(FROM, BOOST_PROTO_typename_A, BOOST_PROTO_A_const_ref, BOOST_PROTO_A_const_ref_a, BOOST_PROTO_ref_a)
  82. /// MACRO(FROM+1, BOOST_PROTO_typename_A, BOOST_PROTO_A_const_ref, BOOST_PROTO_A_const_ref_a, BOOST_PROTO_ref_a)
  83. /// ...
  84. /// MACRO(TO-1, BOOST_PROTO_typename_A, BOOST_PROTO_A_const_ref, BOOST_PROTO_A_const_ref_a, BOOST_PROTO_ref_a)
  85. /// \endcode
  86. ///
  87. /// Example:
  88. ///
  89. /** \code
  90. // Generate BOOST_PROTO_MAX_ARITY-1 overloads of the
  91. // following construct() function template.
  92. #define M0(N, typename_A, A_const_ref, A_const_ref_a, ref_a) \
  93. template<typename T, typename_A(N)> \
  94. typename proto::result_of::make_expr< \
  95. proto::tag::function \
  96. , construct_helper<T> \
  97. , A_const_ref(N) \
  98. >::type const \
  99. construct(A_const_ref_a(N)) \
  100. { \
  101. return proto::make_expr< \
  102. proto::tag::function \
  103. >( \
  104. construct_helper<T>() \
  105. , ref_a(N) \
  106. ); \
  107. }
  108. BOOST_PROTO_REPEAT_FROM_TO(1, BOOST_PROTO_MAX_ARITY, M0)
  109. #undef M0
  110. \endcode
  111. **/
  112. /// The above invocation of BOOST_PROTO_REPEAT_FROM_TO() will generate
  113. /// the following code:
  114. ///
  115. /// \code
  116. /// template<typename T, typename A0>
  117. /// typename proto::result_of::make_expr<
  118. /// proto::tag::function
  119. /// , construct_helper<T>
  120. /// , A0 const &
  121. /// >::type const
  122. /// construct(A0 const & a0)
  123. /// {
  124. /// return proto::make_expr<
  125. /// proto::tag::function
  126. /// >(
  127. /// construct_helper<T>()
  128. /// , boost::ref(a0)
  129. /// );
  130. /// }
  131. ///
  132. /// template<typename T, typename A0, typename A1>
  133. /// typename proto::result_of::make_expr<
  134. /// proto::tag::function
  135. /// , construct_helper<T>
  136. /// , A0 const &
  137. /// , A1 const &
  138. /// >::type const
  139. /// construct(A0 const & a0, A1 const & a1)
  140. /// {
  141. /// return proto::make_expr<
  142. /// proto::tag::function
  143. /// >(
  144. /// construct_helper<T>()
  145. /// , boost::ref(a0)
  146. /// , boost::ref(a1)
  147. /// );
  148. /// }
  149. ///
  150. /// // ... and so on, up to BOOST_PROTO_MAX_ARITY-1 arguments ...
  151. /// \endcode
  152. #define BOOST_PROTO_REPEAT_FROM_TO(FROM, TO, MACRO)\
  153. BOOST_PROTO_REPEAT_FROM_TO_EX(FROM, TO, MACRO, BOOST_PROTO_typename_A, BOOST_PROTO_A_const_ref, BOOST_PROTO_A_const_ref_a, BOOST_PROTO_ref_a)
  154. /// \brief Repeatedly invoke the specified macro.
  155. ///
  156. /// BOOST_PROTO_REPEAT_EX() is used generate the kind of repetitive code that is typical
  157. /// of EDSLs built with Proto. BOOST_PROTO_REPEAT_EX(MACRO, typename_A, A, A_a, a) is equivalent to:
  158. ///
  159. /// \code
  160. /// MACRO(1, typename_A, A, A_a, a)
  161. /// MACRO(2, typename_A, A, A_a, a)
  162. /// ...
  163. /// MACRO(BOOST_PROTO_MAX_ARITY, typename_A, A, A_a, a)
  164. /// \endcode
  165. #define BOOST_PROTO_REPEAT_EX(MACRO, typename_A, A, A_a, a)\
  166. BOOST_PROTO_REPEAT_FROM_TO_EX(1, BOOST_PP_INC(BOOST_PROTO_MAX_ARITY), MACRO, BOOST_PROTO_typename_A, BOOST_PROTO_A_const_ref, BOOST_PROTO_A_const_ref_a, BOOST_PROTO_ref_a)
  167. /// \brief Repeatedly invoke the specified macro.
  168. ///
  169. /// BOOST_PROTO_REPEAT() is used generate the kind of repetitive code that is typical
  170. /// of EDSLs built with Proto. BOOST_PROTO_REPEAT(MACRO) is equivalent to:
  171. ///
  172. /// \code
  173. /// MACRO(1, BOOST_PROTO_typename_A, BOOST_PROTO_A_const_ref, BOOST_PROTO_A_const_ref_a, BOOST_PROTO_ref_a)
  174. /// MACRO(2, BOOST_PROTO_typename_A, BOOST_PROTO_A_const_ref, BOOST_PROTO_A_const_ref_a, BOOST_PROTO_ref_a)
  175. /// ...
  176. /// MACRO(BOOST_PROTO_MAX_ARITY, BOOST_PROTO_typename_A, BOOST_PROTO_A_const_ref, BOOST_PROTO_A_const_ref_a, BOOST_PROTO_ref_a)
  177. /// \endcode
  178. #define BOOST_PROTO_REPEAT(MACRO)\
  179. BOOST_PROTO_REPEAT_FROM_TO(1, BOOST_PP_INC(BOOST_PROTO_MAX_ARITY), MACRO)
  180. /// \brief Repeatedly invoke the specified macro.
  181. ///
  182. /// BOOST_PROTO_LOCAL_ITERATE() is used generate the kind of repetitive code that is typical
  183. /// of EDSLs built with Proto. This macro causes the user-defined macro BOOST_PROTO_LOCAL_MACRO to
  184. /// be expanded with values in the range specified by BOOST_PROTO_LOCAL_LIMITS.
  185. ///
  186. /// Usage:
  187. ///
  188. /// \code
  189. /// #include BOOST_PROTO_LOCAL_ITERATE()
  190. /// \endcode
  191. ///
  192. /// Example:
  193. ///
  194. /** \code
  195. // Generate BOOST_PROTO_MAX_ARITY-1 overloads of the
  196. // following construct() function template.
  197. #define BOOST_PROTO_LOCAL_MACRO(N, typename_A, A_const_ref, \
  198. A_const_ref_a, ref_a) \
  199. template<typename T, typename_A(N)> \
  200. typename proto::result_of::make_expr< \
  201. proto::tag::function \
  202. , construct_helper<T> \
  203. , A_const_ref(N) \
  204. >::type const \
  205. construct(A_const_ref_a(N)) \
  206. { \
  207. return proto::make_expr< \
  208. proto::tag::function \
  209. >( \
  210. construct_helper<T>() \
  211. , ref_a(N) \
  212. ); \
  213. }
  214. #define BOOST_PROTO_LOCAL_LIMITS (1, BOOST_PP_DEC(BOOST_PROTO_MAX_ARITY))
  215. #include BOOST_PROTO_LOCAL_ITERATE()
  216. \endcode
  217. **/
  218. /// The above inclusion of BOOST_PROTO_LOCAL_ITERATE() will generate
  219. /// the following code:
  220. ///
  221. /// \code
  222. /// template<typename T, typename A0>
  223. /// typename proto::result_of::make_expr<
  224. /// proto::tag::function
  225. /// , construct_helper<T>
  226. /// , A0 const &
  227. /// >::type const
  228. /// construct(A0 const & a0)
  229. /// {
  230. /// return proto::make_expr<
  231. /// proto::tag::function
  232. /// >(
  233. /// construct_helper<T>()
  234. /// , boost::ref(a0)
  235. /// );
  236. /// }
  237. ///
  238. /// template<typename T, typename A0, typename A1>
  239. /// typename proto::result_of::make_expr<
  240. /// proto::tag::function
  241. /// , construct_helper<T>
  242. /// , A0 const &
  243. /// , A1 const &
  244. /// >::type const
  245. /// construct(A0 const & a0, A1 const & a1)
  246. /// {
  247. /// return proto::make_expr<
  248. /// proto::tag::function
  249. /// >(
  250. /// construct_helper<T>()
  251. /// , boost::ref(a0)
  252. /// , boost::ref(a1)
  253. /// );
  254. /// }
  255. ///
  256. /// // ... and so on, up to BOOST_PROTO_MAX_ARITY-1 arguments ...
  257. /// \endcode
  258. ///
  259. /// If BOOST_PROTO_LOCAL_LIMITS is not defined by the user, it defaults
  260. /// to (1, BOOST_PROTO_MAX_ARITY)
  261. ///
  262. /// At each iteration, BOOST_PROTO_LOCAL_MACRO is invoked with the current
  263. /// iteration number and the following 4 macro parameters:
  264. ///
  265. /// \li BOOST_PROTO_LOCAL_typename_A
  266. /// \li BOOST_PROTO_LOCAL_A
  267. /// \li BOOST_PROTO_LOCAL_A_a
  268. /// \li BOOST_PROTO_LOCAL_a
  269. ///
  270. /// If these macros are not defined by the user, they default respectively to:
  271. ///
  272. /// \li BOOST_PROTO_typename_A
  273. /// \li BOOST_PROTO_A_const_ref
  274. /// \li BOOST_PROTO_A_const_ref_a
  275. /// \li BOOST_PROTO_ref_a
  276. ///
  277. /// After including BOOST_PROTO_LOCAL_ITERATE(), the following macros are
  278. /// automatically undefined:
  279. ///
  280. /// \li BOOST_PROTO_LOCAL_MACRO
  281. /// \li BOOST_PROTO_LOCAL_LIMITS
  282. /// \li BOOST_PROTO_LOCAL_typename_A
  283. /// \li BOOST_PROTO_LOCAL_A
  284. /// \li BOOST_PROTO_LOCAL_A_a
  285. /// \li BOOST_PROTO_LOCAL_a
  286. #define BOOST_PROTO_LOCAL_ITERATE() <boost/proto/detail/local.hpp>
  287. #endif