1 ///////////////////////////////////////////////////////////////////////////////
3 /// Macros and a base class for defining end-user expression types
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_EXTENDS_HPP_EAN_11_1_2006
10 #define BOOST_PROTO_EXTENDS_HPP_EAN_11_1_2006
12 #include <cstddef> // for offsetof
13 #include <boost/config.hpp>
14 #include <boost/detail/workaround.hpp>
15 #include <boost/preprocessor/facilities/empty.hpp>
16 #include <boost/preprocessor/tuple/elem.hpp>
17 #include <boost/preprocessor/control/if.hpp>
18 #include <boost/preprocessor/arithmetic/inc.hpp>
19 #include <boost/preprocessor/arithmetic/dec.hpp>
20 #include <boost/preprocessor/iteration/local.hpp>
21 #include <boost/preprocessor/repetition/enum_params.hpp>
22 #include <boost/preprocessor/repetition/repeat_from_to.hpp>
23 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
24 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
25 #include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
26 #include <boost/preprocessor/seq/for_each.hpp>
27 #include <boost/utility/addressof.hpp>
28 #include <boost/utility/result_of.hpp>
29 #include <boost/proto/proto_fwd.hpp>
30 #include <boost/proto/traits.hpp>
31 #include <boost/proto/expr.hpp>
32 #include <boost/proto/args.hpp>
33 #include <boost/proto/traits.hpp>
34 #include <boost/proto/generate.hpp>
35 #include <boost/proto/detail/remove_typename.hpp>
38 # pragma warning(push)
39 # pragma warning(disable : 4714) // function 'xxx' marked as __forceinline not inlined
42 namespace boost { namespace proto
47 # define BOOST_PROTO_ADDROF(x) ((char const volatile*)boost::addressof(x))
50 # define BOOST_PROTO_OFFSETOF(s,m) (BOOST_PROTO_ADDROF((((s *)this)->m)) - BOOST_PROTO_ADDROF(*((s *)this)))
54 # define BOOST_PROTO_OFFSETOF offsetof
59 #define BOOST_PROTO_CONST() const
63 #define BOOST_PROTO_TYPENAME() typename
67 #define BOOST_PROTO_TEMPLATE_YES_(Z, N) template<BOOST_PP_ENUM_PARAMS_Z(Z, N, typename A)>
71 #define BOOST_PROTO_TEMPLATE_NO_(Z, N)
75 #define BOOST_PROTO_DEFINE_FUN_OP_IMPL_(Z, N, DATA, Const) \
76 BOOST_PP_IF(N, BOOST_PROTO_TEMPLATE_YES_, BOOST_PROTO_TEMPLATE_NO_)(Z, N) \
77 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
78 typename BOOST_PROTO_RESULT_OF< \
80 typename boost::proto::result_of::BOOST_PP_CAT(funop, N)< \
81 proto_derived_expr Const() \
83 BOOST_PP_ENUM_TRAILING_PARAMS_Z(Z, N, const A) \
87 operator ()(BOOST_PP_ENUM_BINARY_PARAMS_Z(Z, N, A, const &a)) Const() \
89 typedef boost::proto::result_of::BOOST_PP_CAT(funop, N)< \
90 proto_derived_expr Const() \
92 BOOST_PP_ENUM_TRAILING_PARAMS_Z(Z, N, const A) \
94 return proto_generator()( \
96 *static_cast<proto_derived_expr Const() *>(this) \
97 BOOST_PP_ENUM_TRAILING_PARAMS_Z(Z, N, a) \
105 #define BOOST_PROTO_DEFINE_FUN_OP_VARIADIC_IMPL_(Const) \
106 template<typename... A> \
107 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
108 typename BOOST_PROTO_RESULT_OF< \
110 typename boost::proto::result_of::funop< \
111 proto_derived_expr Const()(A const &...) \
112 , proto_derived_expr \
117 operator ()(A const &...a) Const() \
119 typedef boost::proto::result_of::funop< \
120 proto_derived_expr Const()(A const &...) \
121 , proto_derived_expr \
124 return proto_generator()( \
126 *static_cast<proto_derived_expr Const() *>(this) \
135 #define BOOST_PROTO_DEFINE_FUN_OP_CONST(Z, N, DATA) \
136 BOOST_PROTO_DEFINE_FUN_OP_IMPL_(Z, N, DATA, BOOST_PROTO_CONST) \
141 #define BOOST_PROTO_DEFINE_FUN_OP_NON_CONST(Z, N, DATA) \
142 BOOST_PROTO_DEFINE_FUN_OP_IMPL_(Z, N, DATA, BOOST_PP_EMPTY) \
147 #define BOOST_PROTO_DEFINE_FUN_OP(Z, N, DATA) \
148 BOOST_PROTO_DEFINE_FUN_OP_CONST(Z, N, DATA) \
149 BOOST_PROTO_DEFINE_FUN_OP_NON_CONST(Z, N, DATA) \
154 #define BOOST_PROTO_EXTENDS_CHILD(Z, N, DATA) \
156 typename proto_base_expr::BOOST_PP_CAT(proto_child, N) \
157 BOOST_PP_CAT(proto_child, N); \
160 #define BOOST_PROTO_BASIC_EXTENDS_(Expr, Derived, Domain) \
163 typedef Expr proto_base_expr_; /**< INTERNAL ONLY */ \
164 typedef typename proto_base_expr_::proto_base_expr proto_base_expr; \
165 typedef BOOST_PROTO_REMOVE_TYPENAME(Domain) proto_domain; \
166 typedef Derived proto_derived_expr; \
167 typedef Domain::proto_generator proto_generator; \
168 typedef typename proto_base_expr::proto_tag proto_tag; \
169 typedef typename proto_base_expr::proto_args proto_args; \
170 typedef typename proto_base_expr::proto_arity proto_arity; \
171 typedef typename proto_base_expr::proto_grammar proto_grammar; \
172 typedef typename proto_base_expr::address_of_hack_type_ proto_address_of_hack_type_; \
173 typedef void proto_is_expr_; /**< INTERNAL ONLY */ \
174 static const long proto_arity_c = proto_base_expr::proto_arity_c; \
175 typedef boost::proto::tag::proto_expr<proto_tag, proto_domain> fusion_tag; \
176 BOOST_PP_REPEAT(BOOST_PROTO_MAX_ARITY, BOOST_PROTO_EXTENDS_CHILD, ~) \
178 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
179 static proto_derived_expr const make(Expr const &e) \
181 proto_derived_expr that = {e}; \
185 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
186 proto_base_expr &proto_base() \
188 return this->proto_expr_.proto_base(); \
191 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
192 proto_base_expr const &proto_base() const \
194 return this->proto_expr_.proto_base(); \
197 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
198 operator proto_address_of_hack_type_() const \
200 return boost::addressof(this->proto_base().child0); \
204 #define BOOST_PROTO_BASIC_EXTENDS(Expr, Derived, Domain) \
205 BOOST_PROTO_BASIC_EXTENDS_(Expr, Derived, Domain) \
206 typedef void proto_is_aggregate_; \
207 /**< INTERNAL ONLY */
209 #define BOOST_PROTO_EXTENDS_COPY_ASSIGN_IMPL_(This, Const, Typename) \
210 BOOST_PROTO_DISABLE_MSVC_C4522 \
211 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
212 Typename() BOOST_PROTO_RESULT_OF< \
213 Typename() This::proto_generator( \
214 Typename() boost::proto::base_expr< \
215 Typename() This::proto_domain \
216 , boost::proto::tag::assign \
217 , boost::proto::list2< \
224 operator =(This Const() &a) \
227 Typename() boost::proto::base_expr< \
228 Typename() This::proto_domain \
229 , boost::proto::tag::assign \
230 , boost::proto::list2< \
236 that_type const that = { \
240 return Typename() This::proto_generator()(that); \
244 // MSVC 8.0 and higher seem to need copy-assignment operator to be overloaded on *both*
245 // const and non-const rhs arguments.
246 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600)) && (BOOST_MSVC > 1310)
247 #define BOOST_PROTO_EXTENDS_COPY_ASSIGN_(This, Typename) \
248 BOOST_PROTO_EXTENDS_COPY_ASSIGN_IMPL_(This, BOOST_PP_EMPTY, Typename) \
249 BOOST_PROTO_EXTENDS_COPY_ASSIGN_IMPL_(This, BOOST_PROTO_CONST, Typename) \
252 #define BOOST_PROTO_EXTENDS_COPY_ASSIGN_(This, Typename) \
253 BOOST_PROTO_EXTENDS_COPY_ASSIGN_IMPL_(This, BOOST_PROTO_CONST, Typename) \
259 #define BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(ThisConst, ThatConst) \
260 template<typename A> \
261 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
262 typename BOOST_PROTO_RESULT_OF< \
264 typename boost::proto::base_expr< \
266 , boost::proto::tag::assign \
267 , boost::proto::list2< \
268 proto_derived_expr ThisConst() & \
269 , typename boost::proto::result_of::as_child<A ThatConst(), proto_domain>::type \
274 operator =(A ThatConst() &a) ThisConst() \
277 typename boost::proto::base_expr< \
279 , boost::proto::tag::assign \
280 , boost::proto::list2< \
281 proto_derived_expr ThisConst() & \
282 , typename boost::proto::result_of::as_child<A ThatConst(), proto_domain>::type \
286 that_type const that = { \
287 *static_cast<proto_derived_expr ThisConst() *>(this) \
288 , boost::proto::as_child<proto_domain>(a) \
290 return proto_generator()(that); \
294 #define BOOST_PROTO_EXTENDS_ASSIGN_CONST_() \
295 BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(BOOST_PROTO_CONST, BOOST_PP_EMPTY) \
296 BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(BOOST_PROTO_CONST, BOOST_PROTO_CONST) \
299 #define BOOST_PROTO_EXTENDS_ASSIGN_NON_CONST_() \
300 BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(BOOST_PP_EMPTY, BOOST_PP_EMPTY) \
301 BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(BOOST_PP_EMPTY, BOOST_PROTO_CONST) \
304 #define BOOST_PROTO_EXTENDS_ASSIGN_() \
305 BOOST_PROTO_EXTENDS_ASSIGN_CONST_() \
306 BOOST_PROTO_EXTENDS_ASSIGN_NON_CONST_() \
309 #define BOOST_PROTO_EXTENDS_ASSIGN_CONST() \
310 BOOST_PROTO_EXTENDS_COPY_ASSIGN_(proto_derived_expr, BOOST_PROTO_TYPENAME) \
311 BOOST_PROTO_EXTENDS_ASSIGN_CONST_() \
314 #define BOOST_PROTO_EXTENDS_ASSIGN_NON_CONST() \
315 BOOST_PROTO_EXTENDS_COPY_ASSIGN_(proto_derived_expr, BOOST_PROTO_TYPENAME) \
316 BOOST_PROTO_EXTENDS_ASSIGN_NON_CONST_() \
319 #define BOOST_PROTO_EXTENDS_ASSIGN() \
320 BOOST_PROTO_EXTENDS_COPY_ASSIGN_(proto_derived_expr, BOOST_PROTO_TYPENAME) \
321 BOOST_PROTO_EXTENDS_ASSIGN_() \
326 #define BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(ThisConst, ThatConst) \
327 template<typename A> \
328 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
329 typename BOOST_PROTO_RESULT_OF< \
331 typename boost::proto::base_expr< \
333 , boost::proto::tag::subscript \
334 , boost::proto::list2< \
335 proto_derived_expr ThisConst() & \
336 , typename boost::proto::result_of::as_child<A ThatConst(), proto_domain>::type \
341 operator [](A ThatConst() &a) ThisConst() \
344 typename boost::proto::base_expr< \
346 , boost::proto::tag::subscript \
347 , boost::proto::list2< \
348 proto_derived_expr ThisConst() & \
349 , typename boost::proto::result_of::as_child<A ThatConst(), proto_domain>::type \
353 that_type const that = { \
354 *static_cast<proto_derived_expr ThisConst() *>(this) \
355 , boost::proto::as_child<proto_domain>(a) \
357 return proto_generator()(that); \
361 #define BOOST_PROTO_EXTENDS_SUBSCRIPT_CONST() \
362 BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(BOOST_PROTO_CONST, BOOST_PP_EMPTY) \
363 BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(BOOST_PROTO_CONST, BOOST_PROTO_CONST) \
366 #define BOOST_PROTO_EXTENDS_SUBSCRIPT_NON_CONST() \
367 BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(BOOST_PP_EMPTY, BOOST_PP_EMPTY) \
368 BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(BOOST_PP_EMPTY, BOOST_PROTO_CONST) \
371 #define BOOST_PROTO_EXTENDS_SUBSCRIPT() \
372 BOOST_PROTO_EXTENDS_SUBSCRIPT_CONST() \
373 BOOST_PROTO_EXTENDS_SUBSCRIPT_NON_CONST() \
378 #define BOOST_PROTO_EXTENDS_FUNCTION_() \
379 template<typename Sig> \
383 typename BOOST_PROTO_RESULT_OF< \
385 typename boost::proto::result_of::funop< \
387 , proto_derived_expr \
396 #ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
397 #define BOOST_PROTO_EXTENDS_FUNCTION_CONST() \
398 BOOST_PROTO_EXTENDS_FUNCTION_() \
399 BOOST_PROTO_DEFINE_FUN_OP_VARIADIC_IMPL_(BOOST_PROTO_CONST) \
402 #define BOOST_PROTO_EXTENDS_FUNCTION_NON_CONST() \
403 BOOST_PROTO_EXTENDS_FUNCTION_() \
404 BOOST_PROTO_DEFINE_FUN_OP_VARIADIC_IMPL_(BOOST_PP_EMPTY) \
407 #define BOOST_PROTO_EXTENDS_FUNCTION() \
408 BOOST_PROTO_EXTENDS_FUNCTION_() \
409 BOOST_PROTO_DEFINE_FUN_OP_VARIADIC_IMPL_(BOOST_PP_EMPTY) \
410 BOOST_PROTO_DEFINE_FUN_OP_VARIADIC_IMPL_(BOOST_PROTO_CONST) \
413 #define BOOST_PROTO_EXTENDS_FUNCTION_CONST() \
414 BOOST_PROTO_EXTENDS_FUNCTION_() \
415 BOOST_PP_REPEAT_FROM_TO( \
417 , BOOST_PROTO_MAX_FUNCTION_CALL_ARITY \
418 , BOOST_PROTO_DEFINE_FUN_OP_CONST \
423 #define BOOST_PROTO_EXTENDS_FUNCTION_NON_CONST() \
424 BOOST_PROTO_EXTENDS_FUNCTION_() \
425 BOOST_PP_REPEAT_FROM_TO( \
427 , BOOST_PROTO_MAX_FUNCTION_CALL_ARITY \
428 , BOOST_PROTO_DEFINE_FUN_OP_NON_CONST \
433 #define BOOST_PROTO_EXTENDS_FUNCTION() \
434 BOOST_PROTO_EXTENDS_FUNCTION_() \
435 BOOST_PP_REPEAT_FROM_TO( \
437 , BOOST_PROTO_MAX_FUNCTION_CALL_ARITY \
438 , BOOST_PROTO_DEFINE_FUN_OP \
444 #define BOOST_PROTO_EXTENDS(Expr, Derived, Domain) \
445 BOOST_PROTO_BASIC_EXTENDS(Expr, Derived, Domain) \
446 BOOST_PROTO_EXTENDS_ASSIGN() \
447 BOOST_PROTO_EXTENDS_SUBSCRIPT() \
448 BOOST_PROTO_EXTENDS_FUNCTION() \
451 #define BOOST_PROTO_EXTENDS_USING_ASSIGN(Derived) \
452 typedef typename Derived::proto_extends proto_extends; \
453 using proto_extends::operator =; \
454 BOOST_PROTO_EXTENDS_COPY_ASSIGN_(Derived, BOOST_PROTO_TYPENAME) \
457 #define BOOST_PROTO_EXTENDS_USING_ASSIGN_NON_DEPENDENT(Derived) \
458 typedef Derived::proto_extends proto_extends; \
459 using proto_extends::operator =; \
460 BOOST_PROTO_EXTENDS_COPY_ASSIGN_(Derived, BOOST_PP_EMPTY) \
465 /// \brief Empty type to be used as a dummy template parameter of
466 /// POD expression wrappers. It allows argument-dependent lookup
467 /// to find Proto's operator overloads.
469 /// \c proto::is_proto_expr allows argument-dependent lookup
470 /// to find Proto's operator overloads. For example:
473 /// template<typename T, typename Dummy = proto::is_proto_expr>
474 /// struct my_terminal
476 /// BOOST_PROTO_BASIC_EXTENDS(
477 /// typename proto::terminal<T>::type
484 /// my_terminal<int> _1, _2;
485 /// _1 + _2; // OK, uses proto::operator+
488 /// Without the second \c Dummy template parameter, Proto's operator
489 /// overloads would not be considered by name lookup.
493 /// \brief extends\<\> class template for adding behaviors to a Proto expression template
498 , typename Domain // = proto::default_domain
499 , long Arity // = Expr::proto_arity_c
509 extends(extends const &that)
510 : proto_expr_(that.proto_expr_)
514 extends(Expr const &expr_)
518 typedef extends proto_extends;
519 BOOST_PROTO_BASIC_EXTENDS_(Expr, Derived, typename Domain)
520 BOOST_PROTO_EXTENDS_ASSIGN_CONST_()
521 BOOST_PROTO_EXTENDS_SUBSCRIPT_CONST()
523 // Instead of using BOOST_PROTO_EXTENDS_FUNCTION, which uses
524 // nested preprocessor loops, use file iteration here to generate
525 // the operator() overloads, which is more efficient.
526 #include <boost/proto/detail/extends_funop_const.hpp>
529 /// \brief extends\<\> class template for adding behaviors to a Proto expression template
531 template<typename Expr, typename Derived, typename Domain>
532 struct extends<Expr, Derived, Domain, 0>
540 extends(extends const &that)
541 : proto_expr_(that.proto_expr_)
545 extends(Expr const &expr_)
549 typedef extends proto_extends;
550 BOOST_PROTO_BASIC_EXTENDS_(Expr, Derived, typename Domain)
551 BOOST_PROTO_EXTENDS_ASSIGN_()
552 BOOST_PROTO_EXTENDS_SUBSCRIPT()
554 // Instead of using BOOST_PROTO_EXTENDS_FUNCTION, which uses
555 // nested preprocessor loops, use file iteration here to generate
556 // the operator() overloads, which is more efficient.
557 #include <boost/proto/detail/extends_funop.hpp>
562 template<typename This, typename Fun, typename Domain>
563 struct virtual_member
565 typedef Domain proto_domain;
566 typedef typename Domain::proto_generator proto_generator;
567 typedef virtual_member<This, Fun, Domain> proto_derived_expr;
568 typedef tag::member proto_tag;
569 typedef list2<This &, expr<tag::terminal, term<Fun> > const &> proto_args;
570 typedef mpl::long_<2> proto_arity;
571 typedef detail::not_a_valid_type proto_address_of_hack_type_;
572 typedef void proto_is_expr_; /**< INTERNAL ONLY */
573 static const long proto_arity_c = 2;
574 typedef boost::proto::tag::proto_expr<proto_tag, Domain> fusion_tag;
575 typedef This &proto_child0;
576 typedef expr<tag::terminal, term<Fun> > const &proto_child1;
577 typedef expr<proto_tag, proto_args, proto_arity_c> proto_base_expr;
578 typedef basic_expr<proto_tag, proto_args, proto_arity_c> proto_grammar;
579 typedef void proto_is_aggregate_; /**< INTERNAL ONLY */
581 BOOST_PROTO_EXTENDS_ASSIGN_()
582 BOOST_PROTO_EXTENDS_SUBSCRIPT()
584 // Instead of using BOOST_PROTO_EXTENDS_FUNCTION, which uses
585 // nested preprocessor loops, use file iteration here to generate
586 // the operator() overloads, which is more efficient.
587 #define BOOST_PROTO_NO_WAVE_OUTPUT
588 #include <boost/proto/detail/extends_funop.hpp>
589 #undef BOOST_PROTO_NO_WAVE_OUTPUT
592 proto_base_expr const proto_base() const
594 proto_base_expr that = {this->child0(), this->child1()};
599 proto_child0 child0() const
602 return *(This *)((char *)this - BOOST_PROTO_OFFSETOF(This, proto_member_union_start_));
606 proto_child1 child1() const
608 static expr<tag::terminal, term<Fun>, 0> const that = {Fun()};
615 #define BOOST_PROTO_EXTENDS_MEMBER_(R, DOMAIN, ELEM) \
616 boost::proto::exprns_::virtual_member< \
618 , BOOST_PP_TUPLE_ELEM(2, 0, ELEM) \
620 > BOOST_PP_TUPLE_ELEM(2, 1, ELEM); \
623 /// \brief For declaring virtual data members in an extension class.
625 #define BOOST_PROTO_EXTENDS_MEMBERS_WITH_DOMAIN(SEQ, DOMAIN) \
628 char proto_member_union_start_; \
629 BOOST_PP_SEQ_FOR_EACH(BOOST_PROTO_EXTENDS_MEMBER_, DOMAIN, SEQ) \
633 /// \brief For declaring virtual data members in an extension class.
635 #define BOOST_PROTO_EXTENDS_MEMBERS(SEQ) \
636 BOOST_PROTO_EXTENDS_MEMBERS_WITH_DOMAIN(SEQ, proto_domain) \
643 #if defined(_MSC_VER)
644 # pragma warning(pop)