1 ///////////////////////////////////////////////////////////////////////////////
3 /// Contains definition of generate\<\> class template, which end users can
4 /// specialize for generating domain-specific expression wrappers.
6 // Copyright 2008 Eric Niebler. Distributed under the Boost
7 // Software License, Version 1.0. (See accompanying file
8 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_PROTO_GENERATE_HPP_EAN_02_13_2007
11 #define BOOST_PROTO_GENERATE_HPP_EAN_02_13_2007
13 #include <boost/config.hpp>
14 #include <boost/version.hpp>
15 #include <boost/preprocessor/cat.hpp>
16 #include <boost/preprocessor/iteration/iterate.hpp>
17 #include <boost/preprocessor/facilities/intercept.hpp>
18 #include <boost/preprocessor/repetition/enum_params.hpp>
19 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
20 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
21 #include <boost/mpl/bool.hpp>
22 #include <boost/utility/enable_if.hpp>
23 #include <boost/utility/result_of.hpp>
24 #include <boost/proto/proto_fwd.hpp>
25 #include <boost/proto/args.hpp>
28 # pragma warning(push)
29 # pragma warning(disable : 4714) // function 'xxx' marked as __forceinline not inlined
32 namespace boost { namespace proto
37 template<typename Expr>
38 struct by_value_generator_;
40 template<typename Tag, typename Arg>
41 struct by_value_generator_<proto::expr<Tag, term<Arg>, 0> >
46 , term<typename detail::term_traits<Arg>::value_type>
52 static type const call(proto::expr<Tag, term<Arg>, 0> const &e)
54 type that = {e.child0};
59 template<typename Tag, typename Arg>
60 struct by_value_generator_<proto::basic_expr<Tag, term<Arg>, 0> >
65 , term<typename detail::term_traits<Arg>::value_type>
71 static type const call(proto::basic_expr<Tag, term<Arg>, 0> const &e)
73 type that = {e.child0};
78 // Include the other specializations of by_value_generator_
79 #include <boost/proto/detail/generate_by_value.hpp>
82 /// \brief Annotate a generator to indicate that it would
83 /// prefer to be passed instances of \c proto::basic_expr\<\> rather
84 /// than \c proto::expr\<\>. <tt>use_basic_expr\<Generator\></tt> is
85 /// itself a generator.
87 template<typename Generator>
91 BOOST_PROTO_USE_BASIC_EXPR()
94 /// \brief A simple generator that passes an expression
95 /// through unchanged.
97 /// Generators are intended for use as the first template parameter
98 /// to the \c domain\<\> class template and control if and how
99 /// expressions within that domain are to be customized.
100 /// The \c default_generator makes no modifications to the expressions
102 struct default_generator
104 BOOST_PROTO_CALLABLE()
106 template<typename Sig>
109 template<typename This, typename Expr>
110 struct result<This(Expr)>
115 /// \param expr A Proto expression
117 template<typename Expr>
119 BOOST_PROTO_RETURN_TYPE_STRICT_LOOSE(Expr, Expr const &)
120 operator ()(Expr const &e) const
126 /// \brief A simple generator that passes an expression
127 /// through unchanged and specifies a preference for
128 /// \c proto::basic_expr\<\> over \c proto::expr\<\>.
130 /// Generators are intended for use as the first template parameter
131 /// to the \c domain\<\> class template and control if and how
132 /// expressions within that domain are to be customized.
133 /// The \c default_generator makes no modifications to the expressions
135 struct basic_default_generator
136 : proto::use_basic_expr<default_generator>
139 /// \brief A generator that wraps expressions passed
140 /// to it in the specified extension wrapper.
142 /// Generators are intended for use as the first template parameter
143 /// to the \c domain\<\> class template and control if and how
144 /// expressions within that domain are to be customized.
145 /// \c generator\<\> wraps each expression passed to it in
146 /// the \c Extends\<\> wrapper.
147 template<template<typename> class Extends>
150 BOOST_PROTO_CALLABLE()
151 BOOST_PROTO_USE_BASIC_EXPR()
153 template<typename Sig>
156 template<typename This, typename Expr>
157 struct result<This(Expr)>
159 typedef Extends<Expr> type;
162 template<typename This, typename Expr>
163 struct result<This(Expr &)>
165 typedef Extends<Expr> type;
168 template<typename This, typename Expr>
169 struct result<This(Expr const &)>
171 typedef Extends<Expr> type;
174 /// \param expr A Proto expression
175 /// \return Extends<Expr>(expr)
176 template<typename Expr>
178 Extends<Expr> operator ()(Expr const &e) const
180 return Extends<Expr>(e);
184 /// \brief A generator that wraps expressions passed
185 /// to it in the specified extension wrapper and uses
186 /// aggregate initialization for the wrapper.
188 /// Generators are intended for use as the first template parameter
189 /// to the \c domain\<\> class template and control if and how
190 /// expressions within that domain are to be customized.
191 /// \c pod_generator\<\> wraps each expression passed to it in
192 /// the \c Extends\<\> wrapper, and uses aggregate initialzation
193 /// for the wrapped object.
194 template<template<typename> class Extends>
197 BOOST_PROTO_CALLABLE()
198 BOOST_PROTO_USE_BASIC_EXPR()
200 template<typename Sig>
203 template<typename This, typename Expr>
204 struct result<This(Expr)>
206 typedef Extends<Expr> type;
209 template<typename This, typename Expr>
210 struct result<This(Expr &)>
212 typedef Extends<Expr> type;
215 template<typename This, typename Expr>
216 struct result<This(Expr const &)>
218 typedef Extends<Expr> type;
221 /// \param expr The expression to wrap
222 /// \return <tt>Extends\<Expr\> that = {expr}; return that;</tt>
223 template<typename Expr>
225 Extends<Expr> operator ()(Expr const &e) const
227 Extends<Expr> that = {e};
232 // https://connect.microsoft.com/VisualStudio/feedback/details/765449/codegen-stack-corruption-using-runtime-checks-when-aggregate-initializing-struct
233 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1700))
234 template<typename Class, typename Member>
236 Extends<expr<tag::terminal, proto::term<Member Class::*> > > operator ()(expr<tag::terminal, proto::term<Member Class::*> > const &e) const
238 Extends<expr<tag::terminal, proto::term<Member Class::*> > > that;
239 proto::value(that.proto_expr_) = proto::value(e);
243 template<typename Class, typename Member>
245 Extends<basic_expr<tag::terminal, proto::term<Member Class::*> > > operator ()(basic_expr<tag::terminal, proto::term<Member Class::*> > const &e) const
247 Extends<basic_expr<tag::terminal, proto::term<Member Class::*> > > that;
248 proto::value(that.proto_expr_) = proto::value(e);
254 /// \brief A generator that replaces child nodes held by
255 /// reference with ones held by value. Use with
256 /// \c compose_generators to forward that result to another
259 /// Generators are intended for use as the first template parameter
260 /// to the \c domain\<\> class template and control if and how
261 /// expressions within that domain are to be customized.
262 /// \c by_value_generator ensures all child nodes are
263 /// held by value. This generator is typically composed with a
264 /// second generator for further processing, as
265 /// <tt>compose_generators\<by_value_generator, MyGenerator\></tt>.
266 struct by_value_generator
268 BOOST_PROTO_CALLABLE()
270 template<typename Sig>
273 template<typename This, typename Expr>
274 struct result<This(Expr)>
277 typename detail::by_value_generator_<Expr>::type
281 template<typename This, typename Expr>
282 struct result<This(Expr &)>
285 typename detail::by_value_generator_<Expr>::type
289 template<typename This, typename Expr>
290 struct result<This(Expr const &)>
293 typename detail::by_value_generator_<Expr>::type
297 /// \param expr The expression to modify.
298 /// \return <tt>deep_copy(expr)</tt>
299 template<typename Expr>
301 typename result<by_value_generator(Expr)>::type operator ()(Expr const &e) const
303 return detail::by_value_generator_<Expr>::call(e);
307 /// \brief A composite generator that first applies one
308 /// transform to an expression and then forwards the result
309 /// on to another generator for further transformation.
311 /// Generators are intended for use as the first template parameter
312 /// to the \c domain\<\> class template and control if and how
313 /// expressions within that domain are to be customized.
314 /// \c compose_generators\<\> is a composite generator that first
315 /// applies one transform to an expression and then forwards the
316 /// result on to another generator for further transformation.
317 template<typename First, typename Second>
318 struct compose_generators
320 BOOST_PROTO_CALLABLE()
322 template<typename Sig>
325 template<typename This, typename Expr>
326 struct result<This(Expr)>
329 typename Second::template result<
330 Second(typename First::template result<First(Expr)>::type)
335 template<typename This, typename Expr>
336 struct result<This(Expr &)>
339 typename Second::template result<
340 Second(typename First::template result<First(Expr)>::type)
345 template<typename This, typename Expr>
346 struct result<This(Expr const &)>
349 typename Second::template result<
350 Second(typename First::template result<First(Expr)>::type)
355 /// \param expr The expression to modify.
356 /// \return Second()(First()(expr))
357 template<typename Expr>
359 typename result<compose_generators(Expr)>::type operator ()(Expr const &e) const
361 return Second()(First()(e));
365 /// \brief Tests a generator to see whether it would prefer
366 /// to be passed instances of \c proto::basic_expr\<\> rather than
367 /// \c proto::expr\<\>.
369 template<typename Generator, typename Void>
370 struct wants_basic_expr
374 template<typename Generator>
375 struct wants_basic_expr<Generator, typename Generator::proto_use_basic_expr_>
381 struct is_callable<default_generator>
386 template<template<typename> class Extends>
387 struct is_callable<generator<Extends> >
392 template<template<typename> class Extends>
393 struct is_callable<pod_generator<Extends> >
399 struct is_callable<by_value_generator>
404 template<typename First, typename Second>
405 struct is_callable<compose_generators<First, Second> >
411 // Specializations of boost::result_of and boost::tr1_result_of to eliminate
412 // some unnecessary template instantiations
415 template<typename Expr>
416 struct result_of<proto::default_domain(Expr)>
421 template<typename Expr>
422 struct result_of<proto::basic_default_domain(Expr)>
427 template<typename Expr>
428 struct result_of<proto::default_generator(Expr)>
433 template<typename Expr>
434 struct result_of<proto::basic_default_generator(Expr)>
439 #if BOOST_VERSION >= 104400
440 template<typename Expr>
441 struct tr1_result_of<proto::default_domain(Expr)>
446 template<typename Expr>
447 struct tr1_result_of<proto::basic_default_domain(Expr)>
452 template<typename Expr>
453 struct tr1_result_of<proto::default_generator(Expr)>
458 template<typename Expr>
459 struct tr1_result_of<proto::basic_default_generator(Expr)>
466 #if defined(_MSC_VER)
467 # pragma warning(pop)
470 #endif // BOOST_PROTO_GENERATE_HPP_EAN_02_13_2007