1 ///////////////////////////////////////////////////////////////////////////////
3 /// Contains macros to ease the generation of repetitious code constructs
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)
9 #ifndef BOOST_PROTO_REPEAT_HPP_EAN_11_24_2008
10 #define BOOST_PROTO_REPEAT_HPP_EAN_11_24_2008
12 #include <boost/preprocessor/cat.hpp>
13 #include <boost/preprocessor/facilities/intercept.hpp>
14 #include <boost/preprocessor/repetition/enum.hpp>
15 #include <boost/preprocessor/repetition/enum_params.hpp>
16 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
17 #include <boost/preprocessor/iteration/local.hpp>
18 #include <boost/preprocessor/tuple/elem.hpp>
19 #include <boost/proto/proto_fwd.hpp> // for BOOST_PROTO_MAX_ARITY
21 ////////////////////////////////////////////
23 #define BOOST_PROTO_ref_a_aux(Z, N, DATA)\
24 boost::ref(BOOST_PP_CAT(proto_a, N))
26 /// \brief Generates a sequence like <tt>typename A0, typename A1, ...</tt>
28 #define BOOST_PROTO_typename_A(N)\
29 BOOST_PP_ENUM_PARAMS(N, typename proto_A)
31 /// \brief Generates a sequence like <tt>A0 const &, A1 const &, ...</tt>
33 #define BOOST_PROTO_A_const_ref(N)\
34 BOOST_PP_ENUM_BINARY_PARAMS(N, proto_A, const & BOOST_PP_INTERCEPT)
36 /// \brief Generates a sequence like <tt>A0 &, A1 &, ...</tt>
38 #define BOOST_PROTO_A_ref(N)\
39 BOOST_PP_ENUM_BINARY_PARAMS(N, proto_A, & BOOST_PP_INTERCEPT)
41 /// \brief Generates a sequence like <tt>A0, A1, ...</tt>
43 #define BOOST_PROTO_A(N)\
44 BOOST_PP_ENUM_PARAMS(N, proto_A)
46 /// \brief Generates a sequence like <tt>A0 const, A1 const, ...</tt>
48 #define BOOST_PROTO_A_const(N)\
49 BOOST_PP_ENUM_PARAMS(N, const proto_A)
51 /// \brief Generates a sequence like <tt>A0 const &a0, A1 const &a0, ...</tt>
53 #define BOOST_PROTO_A_const_ref_a(N)\
54 BOOST_PP_ENUM_BINARY_PARAMS(N, proto_A, const &proto_a)
56 /// \brief Generates a sequence like <tt>A0 &a0, A1 &a0, ...</tt>
58 #define BOOST_PROTO_A_ref_a(N)\
59 BOOST_PP_ENUM_BINARY_PARAMS(N, proto_A, &proto_a)
61 /// \brief Generates a sequence like <tt>boost::ref(a0), boost::ref(a1), ...</tt>
63 #define BOOST_PROTO_ref_a(N)\
64 BOOST_PP_ENUM(N, BOOST_PROTO_ref_a_aux, ~)
66 /// \brief Generates a sequence like <tt>a0, a1, ...</tt>
68 #define BOOST_PROTO_a(N)\
69 BOOST_PP_ENUM_PARAMS(N, proto_a)
71 ////////////////////////////////////////////
73 #define BOOST_PROTO_invoke(Z, N, DATA)\
74 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))
76 /// \brief Repeatedly invoke the specified macro.
78 /// BOOST_PROTO_REPEAT_FROM_TO_EX() is used generate the kind of repetitive code that is typical
79 /// of EDSLs built with Proto. BOOST_PROTO_REPEAT_FROM_TO_EX(FROM, TO, MACRO, typename_A, A, A_a, a) is equivalent to:
82 /// MACRO(FROM, typename_A, A, A_a, a)
83 /// MACRO(FROM+1, typename_A, A, A_a, a)
85 /// MACRO(TO-1, typename_A, A, A_a, a)
87 #define BOOST_PROTO_REPEAT_FROM_TO_EX(FROM, TO, MACRO, typename_A, A, A_a, a)\
88 BOOST_PP_REPEAT_FROM_TO(FROM, TO, BOOST_PROTO_invoke, (MACRO, typename_A, A, A_a, a))
90 /// \brief Repeatedly invoke the specified macro.
92 /// BOOST_PROTO_REPEAT_FROM_TO() is used generate the kind of repetitive code that is typical
93 /// of EDSLs built with Proto. BOOST_PROTO_REPEAT_FROM_TO(FROM, TO, MACRO) is equivalent to:
96 /// MACRO(FROM, BOOST_PROTO_typename_A, BOOST_PROTO_A_const_ref, BOOST_PROTO_A_const_ref_a, BOOST_PROTO_ref_a)
97 /// MACRO(FROM+1, BOOST_PROTO_typename_A, BOOST_PROTO_A_const_ref, BOOST_PROTO_A_const_ref_a, BOOST_PROTO_ref_a)
99 /// MACRO(TO-1, BOOST_PROTO_typename_A, BOOST_PROTO_A_const_ref, BOOST_PROTO_A_const_ref_a, BOOST_PROTO_ref_a)
106 // Generate BOOST_PROTO_MAX_ARITY-1 overloads of the
107 // following construct() function template.
108 #define M0(N, typename_A, A_const_ref, A_const_ref_a, ref_a) \
109 template<typename T, typename_A(N)> \
110 typename proto::result_of::make_expr< \
111 proto::tag::function \
112 , construct_helper<T> \
115 construct(A_const_ref_a(N)) \
117 return proto::make_expr< \
118 proto::tag::function \
120 construct_helper<T>() \
124 BOOST_PROTO_REPEAT_FROM_TO(1, BOOST_PROTO_MAX_ARITY, M0)
129 /// The above invocation of BOOST_PROTO_REPEAT_FROM_TO() will generate
130 /// the following code:
133 /// template<typename T, typename A0>
134 /// typename proto::result_of::make_expr<
135 /// proto::tag::function
136 /// , construct_helper<T>
139 /// construct(A0 const & a0)
141 /// return proto::make_expr<
142 /// proto::tag::function
144 /// construct_helper<T>()
149 /// template<typename T, typename A0, typename A1>
150 /// typename proto::result_of::make_expr<
151 /// proto::tag::function
152 /// , construct_helper<T>
156 /// construct(A0 const & a0, A1 const & a1)
158 /// return proto::make_expr<
159 /// proto::tag::function
161 /// construct_helper<T>()
167 /// // ... and so on, up to BOOST_PROTO_MAX_ARITY-1 arguments ...
169 #define BOOST_PROTO_REPEAT_FROM_TO(FROM, TO, MACRO)\
170 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)
172 /// \brief Repeatedly invoke the specified macro.
174 /// BOOST_PROTO_REPEAT_EX() is used generate the kind of repetitive code that is typical
175 /// of EDSLs built with Proto. BOOST_PROTO_REPEAT_EX(MACRO, typename_A, A, A_a, a) is equivalent to:
178 /// MACRO(1, typename_A, A, A_a, a)
179 /// MACRO(2, typename_A, A, A_a, a)
181 /// MACRO(BOOST_PROTO_MAX_ARITY, typename_A, A, A_a, a)
183 #define BOOST_PROTO_REPEAT_EX(MACRO, typename_A, A, A_a, a)\
184 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)
186 /// \brief Repeatedly invoke the specified macro.
188 /// BOOST_PROTO_REPEAT() is used generate the kind of repetitive code that is typical
189 /// of EDSLs built with Proto. BOOST_PROTO_REPEAT(MACRO) is equivalent to:
192 /// MACRO(1, BOOST_PROTO_typename_A, BOOST_PROTO_A_const_ref, BOOST_PROTO_A_const_ref_a, BOOST_PROTO_ref_a)
193 /// MACRO(2, BOOST_PROTO_typename_A, BOOST_PROTO_A_const_ref, BOOST_PROTO_A_const_ref_a, BOOST_PROTO_ref_a)
195 /// MACRO(BOOST_PROTO_MAX_ARITY, BOOST_PROTO_typename_A, BOOST_PROTO_A_const_ref, BOOST_PROTO_A_const_ref_a, BOOST_PROTO_ref_a)
197 #define BOOST_PROTO_REPEAT(MACRO)\
198 BOOST_PROTO_REPEAT_FROM_TO(1, BOOST_PP_INC(BOOST_PROTO_MAX_ARITY), MACRO)
200 /// \brief Repeatedly invoke the specified macro.
202 /// BOOST_PROTO_LOCAL_ITERATE() is used generate the kind of repetitive code that is typical
203 /// of EDSLs built with Proto. This macro causes the user-defined macro BOOST_PROTO_LOCAL_MACRO to
204 /// be expanded with values in the range specified by BOOST_PROTO_LOCAL_LIMITS.
209 /// #include BOOST_PROTO_LOCAL_ITERATE()
216 // Generate BOOST_PROTO_MAX_ARITY-1 overloads of the
217 // following construct() function template.
218 #define BOOST_PROTO_LOCAL_MACRO(N, typename_A, A_const_ref, \
219 A_const_ref_a, ref_a) \
220 template<typename T, typename_A(N)> \
221 typename proto::result_of::make_expr< \
222 proto::tag::function \
223 , construct_helper<T> \
226 construct(A_const_ref_a(N)) \
228 return proto::make_expr< \
229 proto::tag::function \
231 construct_helper<T>() \
235 #define BOOST_PROTO_LOCAL_LIMITS (1, BOOST_PP_DEC(BOOST_PROTO_MAX_ARITY))
236 #include BOOST_PROTO_LOCAL_ITERATE()
240 /// The above inclusion of BOOST_PROTO_LOCAL_ITERATE() will generate
241 /// the following code:
244 /// template<typename T, typename A0>
245 /// typename proto::result_of::make_expr<
246 /// proto::tag::function
247 /// , construct_helper<T>
250 /// construct(A0 const & a0)
252 /// return proto::make_expr<
253 /// proto::tag::function
255 /// construct_helper<T>()
260 /// template<typename T, typename A0, typename A1>
261 /// typename proto::result_of::make_expr<
262 /// proto::tag::function
263 /// , construct_helper<T>
267 /// construct(A0 const & a0, A1 const & a1)
269 /// return proto::make_expr<
270 /// proto::tag::function
272 /// construct_helper<T>()
278 /// // ... and so on, up to BOOST_PROTO_MAX_ARITY-1 arguments ...
281 /// If BOOST_PROTO_LOCAL_LIMITS is not defined by the user, it defaults
282 /// to (1, BOOST_PROTO_MAX_ARITY)
284 /// At each iteration, BOOST_PROTO_LOCAL_MACRO is invoked with the current
285 /// iteration number and the following 4 macro parameters:
287 /// \li BOOST_PROTO_LOCAL_typename_A
288 /// \li BOOST_PROTO_LOCAL_A
289 /// \li BOOST_PROTO_LOCAL_A_a
290 /// \li BOOST_PROTO_LOCAL_a
292 /// If these macros are not defined by the user, they default respectively to:
294 /// \li BOOST_PROTO_typename_A
295 /// \li BOOST_PROTO_A_const_ref
296 /// \li BOOST_PROTO_A_const_ref_a
297 /// \li BOOST_PROTO_ref_a
299 /// After including BOOST_PROTO_LOCAL_ITERATE(), the following macros are
300 /// automatically undefined:
302 /// \li BOOST_PROTO_LOCAL_MACRO
303 /// \li BOOST_PROTO_LOCAL_LIMITS
304 /// \li BOOST_PROTO_LOCAL_typename_A
305 /// \li BOOST_PROTO_LOCAL_A
306 /// \li BOOST_PROTO_LOCAL_A_a
307 /// \li BOOST_PROTO_LOCAL_a
308 #define BOOST_PROTO_LOCAL_ITERATE() <boost/proto/detail/local.hpp>