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_PUSH_WARNINGS \
78 BOOST_PROTO_DISABLE_MSVC_C4180 \
79 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
80 typename BOOST_PROTO_RESULT_OF< \
82 typename boost::proto::result_of::BOOST_PP_CAT(funop, N)< \
83 proto_derived_expr Const() \
85 BOOST_PP_ENUM_TRAILING_PARAMS_Z(Z, N, const A) \
89 operator ()(BOOST_PP_ENUM_BINARY_PARAMS_Z(Z, N, A, const &a)) Const() \
91 typedef boost::proto::result_of::BOOST_PP_CAT(funop, N)< \
92 proto_derived_expr Const() \
94 BOOST_PP_ENUM_TRAILING_PARAMS_Z(Z, N, const A) \
96 return proto_generator()( \
98 *static_cast<proto_derived_expr Const() *>(this) \
99 BOOST_PP_ENUM_TRAILING_PARAMS_Z(Z, N, a) \
103 BOOST_PROTO_POP_WARNINGS \
108 #define BOOST_PROTO_DEFINE_FUN_OP_VARIADIC_IMPL_(Const) \
109 template<typename... A> \
110 BOOST_PROTO_PUSH_WARNINGS \
111 BOOST_PROTO_DISABLE_MSVC_C4180 \
112 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
113 typename BOOST_PROTO_RESULT_OF< \
115 typename boost::proto::result_of::funop< \
116 proto_derived_expr Const()(A const &...) \
117 , proto_derived_expr \
122 operator ()(A const &...a) Const() \
124 typedef boost::proto::result_of::funop< \
125 proto_derived_expr Const()(A const &...) \
126 , proto_derived_expr \
129 return proto_generator()( \
131 *static_cast<proto_derived_expr Const() *>(this) \
136 BOOST_PROTO_POP_WARNINGS \
141 #define BOOST_PROTO_DEFINE_FUN_OP_CONST(Z, N, DATA) \
142 BOOST_PROTO_DEFINE_FUN_OP_IMPL_(Z, N, DATA, BOOST_PROTO_CONST) \
147 #define BOOST_PROTO_DEFINE_FUN_OP_NON_CONST(Z, N, DATA) \
148 BOOST_PROTO_DEFINE_FUN_OP_IMPL_(Z, N, DATA, BOOST_PP_EMPTY) \
153 #define BOOST_PROTO_DEFINE_FUN_OP(Z, N, DATA) \
154 BOOST_PROTO_DEFINE_FUN_OP_CONST(Z, N, DATA) \
155 BOOST_PROTO_DEFINE_FUN_OP_NON_CONST(Z, N, DATA) \
160 #define BOOST_PROTO_EXTENDS_CHILD(Z, N, DATA) \
162 typename proto_base_expr::BOOST_PP_CAT(proto_child, N) \
163 BOOST_PP_CAT(proto_child, N); \
166 #define BOOST_PROTO_BASIC_EXTENDS_(Expr, Derived, Domain) \
169 typedef Expr proto_base_expr_; /**< INTERNAL ONLY */ \
170 typedef typename proto_base_expr_::proto_base_expr proto_base_expr; \
171 typedef BOOST_PROTO_REMOVE_TYPENAME(Domain) proto_domain; \
172 typedef Derived proto_derived_expr; \
173 typedef Domain::proto_generator proto_generator; \
174 typedef typename proto_base_expr::proto_tag proto_tag; \
175 typedef typename proto_base_expr::proto_args proto_args; \
176 typedef typename proto_base_expr::proto_arity proto_arity; \
177 typedef typename proto_base_expr::proto_grammar proto_grammar; \
178 typedef typename proto_base_expr::address_of_hack_type_ proto_address_of_hack_type_; \
179 typedef void proto_is_expr_; /**< INTERNAL ONLY */ \
180 static const long proto_arity_c = proto_base_expr::proto_arity_c; \
181 typedef boost::proto::tag::proto_expr<proto_tag, proto_domain> fusion_tag; \
182 BOOST_PP_REPEAT(BOOST_PROTO_MAX_ARITY, BOOST_PROTO_EXTENDS_CHILD, ~) \
184 BOOST_PROTO_PUSH_WARNINGS \
186 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
187 static proto_derived_expr const make(Expr const &e) \
189 proto_derived_expr that = {e}; \
193 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
194 proto_base_expr &proto_base() \
196 return this->proto_expr_.proto_base(); \
199 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
200 proto_base_expr const &proto_base() const \
202 return this->proto_expr_.proto_base(); \
205 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
206 operator proto_address_of_hack_type_() const \
208 return boost::addressof(this->proto_base().child0); \
211 BOOST_PROTO_POP_WARNINGS \
214 #define BOOST_PROTO_BASIC_EXTENDS(Expr, Derived, Domain) \
215 BOOST_PROTO_BASIC_EXTENDS_(Expr, Derived, Domain) \
216 typedef void proto_is_aggregate_; \
217 /**< INTERNAL ONLY */
219 #define BOOST_PROTO_EXTENDS_COPY_ASSIGN_IMPL_(This, Const, Typename) \
220 BOOST_PROTO_PUSH_WARNINGS \
221 BOOST_PROTO_DISABLE_MSVC_C4522 \
222 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
223 Typename() BOOST_PROTO_RESULT_OF< \
224 Typename() This::proto_generator( \
225 Typename() boost::proto::base_expr< \
226 Typename() This::proto_domain \
227 , boost::proto::tag::assign \
228 , boost::proto::list2< \
235 operator =(This Const() &a) \
238 Typename() boost::proto::base_expr< \
239 Typename() This::proto_domain \
240 , boost::proto::tag::assign \
241 , boost::proto::list2< \
247 that_type const that = { \
251 return Typename() This::proto_generator()(that); \
253 BOOST_PROTO_POP_WARNINGS \
256 // MSVC 8.0 and higher seem to need copy-assignment operator to be overloaded on *both*
257 // const and non-const rhs arguments.
258 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600)) && (BOOST_MSVC > 1310)
259 #define BOOST_PROTO_EXTENDS_COPY_ASSIGN_(This, Typename) \
260 BOOST_PROTO_EXTENDS_COPY_ASSIGN_IMPL_(This, BOOST_PP_EMPTY, Typename) \
261 BOOST_PROTO_EXTENDS_COPY_ASSIGN_IMPL_(This, BOOST_PROTO_CONST, Typename) \
264 #define BOOST_PROTO_EXTENDS_COPY_ASSIGN_(This, Typename) \
265 BOOST_PROTO_EXTENDS_COPY_ASSIGN_IMPL_(This, BOOST_PROTO_CONST, Typename) \
271 #define BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(ThisConst, ThatConst) \
272 template<typename A> \
273 BOOST_PROTO_PUSH_WARNINGS \
274 BOOST_PROTO_DISABLE_MSVC_C4180 \
275 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
276 typename BOOST_PROTO_RESULT_OF< \
278 typename boost::proto::base_expr< \
280 , boost::proto::tag::assign \
281 , boost::proto::list2< \
282 proto_derived_expr ThisConst() & \
283 , typename boost::proto::result_of::as_child<A ThatConst(), proto_domain>::type \
288 operator =(A ThatConst() &a) ThisConst() \
291 typename boost::proto::base_expr< \
293 , boost::proto::tag::assign \
294 , boost::proto::list2< \
295 proto_derived_expr ThisConst() & \
296 , typename boost::proto::result_of::as_child<A ThatConst(), proto_domain>::type \
300 that_type const that = { \
301 *static_cast<proto_derived_expr ThisConst() *>(this) \
302 , boost::proto::as_child<proto_domain>(a) \
304 return proto_generator()(that); \
306 BOOST_PROTO_POP_WARNINGS \
309 #define BOOST_PROTO_EXTENDS_ASSIGN_CONST_() \
310 BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(BOOST_PROTO_CONST, BOOST_PP_EMPTY) \
311 BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(BOOST_PROTO_CONST, BOOST_PROTO_CONST) \
314 #define BOOST_PROTO_EXTENDS_ASSIGN_NON_CONST_() \
315 BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(BOOST_PP_EMPTY, BOOST_PP_EMPTY) \
316 BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(BOOST_PP_EMPTY, BOOST_PROTO_CONST) \
319 #define BOOST_PROTO_EXTENDS_ASSIGN_() \
320 BOOST_PROTO_EXTENDS_ASSIGN_CONST_() \
321 BOOST_PROTO_EXTENDS_ASSIGN_NON_CONST_() \
324 #define BOOST_PROTO_EXTENDS_ASSIGN_CONST() \
325 BOOST_PROTO_EXTENDS_COPY_ASSIGN_(proto_derived_expr, BOOST_PROTO_TYPENAME) \
326 BOOST_PROTO_EXTENDS_ASSIGN_CONST_() \
329 #define BOOST_PROTO_EXTENDS_ASSIGN_NON_CONST() \
330 BOOST_PROTO_EXTENDS_COPY_ASSIGN_(proto_derived_expr, BOOST_PROTO_TYPENAME) \
331 BOOST_PROTO_EXTENDS_ASSIGN_NON_CONST_() \
334 #define BOOST_PROTO_EXTENDS_ASSIGN() \
335 BOOST_PROTO_EXTENDS_COPY_ASSIGN_(proto_derived_expr, BOOST_PROTO_TYPENAME) \
336 BOOST_PROTO_EXTENDS_ASSIGN_() \
341 #define BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(ThisConst, ThatConst) \
342 template<typename A> \
343 BOOST_PROTO_PUSH_WARNINGS \
344 BOOST_PROTO_DISABLE_MSVC_C4180 \
345 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
346 typename BOOST_PROTO_RESULT_OF< \
348 typename boost::proto::base_expr< \
350 , boost::proto::tag::subscript \
351 , boost::proto::list2< \
352 proto_derived_expr ThisConst() & \
353 , typename boost::proto::result_of::as_child<A ThatConst(), proto_domain>::type \
358 operator [](A ThatConst() &a) ThisConst() \
361 typename boost::proto::base_expr< \
363 , boost::proto::tag::subscript \
364 , boost::proto::list2< \
365 proto_derived_expr ThisConst() & \
366 , typename boost::proto::result_of::as_child<A ThatConst(), proto_domain>::type \
370 that_type const that = { \
371 *static_cast<proto_derived_expr ThisConst() *>(this) \
372 , boost::proto::as_child<proto_domain>(a) \
374 return proto_generator()(that); \
376 BOOST_PROTO_POP_WARNINGS \
379 #define BOOST_PROTO_EXTENDS_SUBSCRIPT_CONST() \
380 BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(BOOST_PROTO_CONST, BOOST_PP_EMPTY) \
381 BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(BOOST_PROTO_CONST, BOOST_PROTO_CONST) \
384 #define BOOST_PROTO_EXTENDS_SUBSCRIPT_NON_CONST() \
385 BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(BOOST_PP_EMPTY, BOOST_PP_EMPTY) \
386 BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(BOOST_PP_EMPTY, BOOST_PROTO_CONST) \
389 #define BOOST_PROTO_EXTENDS_SUBSCRIPT() \
390 BOOST_PROTO_EXTENDS_SUBSCRIPT_CONST() \
391 BOOST_PROTO_EXTENDS_SUBSCRIPT_NON_CONST() \
396 #define BOOST_PROTO_EXTENDS_FUNCTION_() \
397 template<typename Sig> \
401 typename BOOST_PROTO_RESULT_OF< \
403 typename boost::proto::result_of::funop< \
405 , proto_derived_expr \
414 #ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
415 #define BOOST_PROTO_EXTENDS_FUNCTION_CONST() \
416 BOOST_PROTO_EXTENDS_FUNCTION_() \
417 BOOST_PROTO_DEFINE_FUN_OP_VARIADIC_IMPL_(BOOST_PROTO_CONST) \
420 #define BOOST_PROTO_EXTENDS_FUNCTION_NON_CONST() \
421 BOOST_PROTO_EXTENDS_FUNCTION_() \
422 BOOST_PROTO_DEFINE_FUN_OP_VARIADIC_IMPL_(BOOST_PP_EMPTY) \
425 #define BOOST_PROTO_EXTENDS_FUNCTION() \
426 BOOST_PROTO_EXTENDS_FUNCTION_() \
427 BOOST_PROTO_DEFINE_FUN_OP_VARIADIC_IMPL_(BOOST_PP_EMPTY) \
428 BOOST_PROTO_DEFINE_FUN_OP_VARIADIC_IMPL_(BOOST_PROTO_CONST) \
431 #define BOOST_PROTO_EXTENDS_FUNCTION_CONST() \
432 BOOST_PROTO_EXTENDS_FUNCTION_() \
433 BOOST_PP_REPEAT_FROM_TO( \
435 , BOOST_PROTO_MAX_FUNCTION_CALL_ARITY \
436 , BOOST_PROTO_DEFINE_FUN_OP_CONST \
441 #define BOOST_PROTO_EXTENDS_FUNCTION_NON_CONST() \
442 BOOST_PROTO_EXTENDS_FUNCTION_() \
443 BOOST_PP_REPEAT_FROM_TO( \
445 , BOOST_PROTO_MAX_FUNCTION_CALL_ARITY \
446 , BOOST_PROTO_DEFINE_FUN_OP_NON_CONST \
451 #define BOOST_PROTO_EXTENDS_FUNCTION() \
452 BOOST_PROTO_EXTENDS_FUNCTION_() \
453 BOOST_PP_REPEAT_FROM_TO( \
455 , BOOST_PROTO_MAX_FUNCTION_CALL_ARITY \
456 , BOOST_PROTO_DEFINE_FUN_OP \
462 #define BOOST_PROTO_EXTENDS(Expr, Derived, Domain) \
463 BOOST_PROTO_BASIC_EXTENDS(Expr, Derived, Domain) \
464 BOOST_PROTO_EXTENDS_ASSIGN() \
465 BOOST_PROTO_EXTENDS_SUBSCRIPT() \
466 BOOST_PROTO_EXTENDS_FUNCTION() \
469 #define BOOST_PROTO_EXTENDS_USING_ASSIGN(Derived) \
470 typedef typename Derived::proto_extends proto_extends; \
471 using proto_extends::operator =; \
472 BOOST_PROTO_EXTENDS_COPY_ASSIGN_(Derived, BOOST_PROTO_TYPENAME) \
475 #define BOOST_PROTO_EXTENDS_USING_ASSIGN_NON_DEPENDENT(Derived) \
476 typedef Derived::proto_extends proto_extends; \
477 using proto_extends::operator =; \
478 BOOST_PROTO_EXTENDS_COPY_ASSIGN_(Derived, BOOST_PP_EMPTY) \
483 /// \brief Empty type to be used as a dummy template parameter of
484 /// POD expression wrappers. It allows argument-dependent lookup
485 /// to find Proto's operator overloads.
487 /// \c proto::is_proto_expr allows argument-dependent lookup
488 /// to find Proto's operator overloads. For example:
491 /// template<typename T, typename Dummy = proto::is_proto_expr>
492 /// struct my_terminal
494 /// BOOST_PROTO_BASIC_EXTENDS(
495 /// typename proto::terminal<T>::type
502 /// my_terminal<int> _1, _2;
503 /// _1 + _2; // OK, uses proto::operator+
506 /// Without the second \c Dummy template parameter, Proto's operator
507 /// overloads would not be considered by name lookup.
511 /// \brief extends\<\> class template for adding behaviors to a Proto expression template
516 , typename Domain // = proto::default_domain
517 , long Arity // = Expr::proto_arity_c
527 extends(Expr const &expr_)
531 typedef extends proto_extends;
532 BOOST_PROTO_BASIC_EXTENDS_(Expr, Derived, typename Domain)
533 BOOST_PROTO_EXTENDS_ASSIGN_CONST_()
534 BOOST_PROTO_EXTENDS_SUBSCRIPT_CONST()
536 // Instead of using BOOST_PROTO_EXTENDS_FUNCTION, which uses
537 // nested preprocessor loops, use file iteration here to generate
538 // the operator() overloads, which is more efficient.
539 #include <boost/proto/detail/extends_funop_const.hpp>
542 /// \brief extends\<\> class template for adding behaviors to a Proto expression template
544 template<typename Expr, typename Derived, typename Domain>
545 struct extends<Expr, Derived, Domain, 0>
553 extends(Expr const &expr_)
557 typedef extends proto_extends;
558 BOOST_PROTO_BASIC_EXTENDS_(Expr, Derived, typename Domain)
559 BOOST_PROTO_EXTENDS_ASSIGN_()
560 BOOST_PROTO_EXTENDS_SUBSCRIPT()
562 // Instead of using BOOST_PROTO_EXTENDS_FUNCTION, which uses
563 // nested preprocessor loops, use file iteration here to generate
564 // the operator() overloads, which is more efficient.
565 #include <boost/proto/detail/extends_funop.hpp>
570 template<typename This, typename Fun, typename Domain>
571 struct virtual_member
573 typedef Domain proto_domain;
574 typedef typename Domain::proto_generator proto_generator;
575 typedef virtual_member<This, Fun, Domain> proto_derived_expr;
576 typedef tag::member proto_tag;
577 typedef list2<This &, expr<tag::terminal, term<Fun> > const &> proto_args;
578 typedef mpl::long_<2> proto_arity;
579 typedef detail::not_a_valid_type proto_address_of_hack_type_;
580 typedef void proto_is_expr_; /**< INTERNAL ONLY */
581 static const long proto_arity_c = 2;
582 typedef boost::proto::tag::proto_expr<proto_tag, Domain> fusion_tag;
583 typedef This &proto_child0;
584 typedef expr<tag::terminal, term<Fun> > const &proto_child1;
585 typedef expr<proto_tag, proto_args, proto_arity_c> proto_base_expr;
586 typedef basic_expr<proto_tag, proto_args, proto_arity_c> proto_grammar;
587 typedef void proto_is_aggregate_; /**< INTERNAL ONLY */
589 BOOST_PROTO_EXTENDS_ASSIGN_()
590 BOOST_PROTO_EXTENDS_SUBSCRIPT()
592 // Instead of using BOOST_PROTO_EXTENDS_FUNCTION, which uses
593 // nested preprocessor loops, use file iteration here to generate
594 // the operator() overloads, which is more efficient.
595 #define BOOST_PROTO_NO_WAVE_OUTPUT
596 #include <boost/proto/detail/extends_funop.hpp>
597 #undef BOOST_PROTO_NO_WAVE_OUTPUT
600 proto_base_expr const proto_base() const
602 proto_base_expr that = {this->child0(), this->child1()};
607 proto_child0 child0() const
610 return *(This *)((char *)this - BOOST_PROTO_OFFSETOF(This, proto_member_union_start_));
614 proto_child1 child1() const
616 static expr<tag::terminal, term<Fun>, 0> const that = {Fun()};
623 #define BOOST_PROTO_EXTENDS_MEMBER_(R, DOMAIN, ELEM) \
624 boost::proto::exprns_::virtual_member< \
626 , BOOST_PP_TUPLE_ELEM(2, 0, ELEM) \
628 > BOOST_PP_TUPLE_ELEM(2, 1, ELEM); \
631 /// \brief For declaring virtual data members in an extension class.
633 #define BOOST_PROTO_EXTENDS_MEMBERS_WITH_DOMAIN(SEQ, DOMAIN) \
636 char proto_member_union_start_; \
637 BOOST_PP_SEQ_FOR_EACH(BOOST_PROTO_EXTENDS_MEMBER_, DOMAIN, SEQ) \
641 /// \brief For declaring virtual data members in an extension class.
643 #define BOOST_PROTO_EXTENDS_MEMBERS(SEQ) \
644 BOOST_PROTO_EXTENDS_MEMBERS_WITH_DOMAIN(SEQ, proto_domain) \
651 #if defined(_MSC_VER)
652 # pragma warning(pop)