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_C4180 \
78 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
79 typename BOOST_PROTO_RESULT_OF< \
81 typename boost::proto::result_of::BOOST_PP_CAT(funop, N)< \
82 proto_derived_expr Const() \
84 BOOST_PP_ENUM_TRAILING_PARAMS_Z(Z, N, const A) \
88 operator ()(BOOST_PP_ENUM_BINARY_PARAMS_Z(Z, N, A, const &a)) Const() \
90 typedef boost::proto::result_of::BOOST_PP_CAT(funop, N)< \
91 proto_derived_expr Const() \
93 BOOST_PP_ENUM_TRAILING_PARAMS_Z(Z, N, const A) \
95 return proto_generator()( \
97 *static_cast<proto_derived_expr Const() *>(this) \
98 BOOST_PP_ENUM_TRAILING_PARAMS_Z(Z, N, a) \
106 #define BOOST_PROTO_DEFINE_FUN_OP_VARIADIC_IMPL_(Const) \
107 template<typename... A> \
108 BOOST_PROTO_DISABLE_MSVC_C4180 \
109 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
110 typename BOOST_PROTO_RESULT_OF< \
112 typename boost::proto::result_of::funop< \
113 proto_derived_expr Const()(A const &...) \
114 , proto_derived_expr \
119 operator ()(A const &...a) Const() \
121 typedef boost::proto::result_of::funop< \
122 proto_derived_expr Const()(A const &...) \
123 , proto_derived_expr \
126 return proto_generator()( \
128 *static_cast<proto_derived_expr Const() *>(this) \
137 #define BOOST_PROTO_DEFINE_FUN_OP_CONST(Z, N, DATA) \
138 BOOST_PROTO_DEFINE_FUN_OP_IMPL_(Z, N, DATA, BOOST_PROTO_CONST) \
143 #define BOOST_PROTO_DEFINE_FUN_OP_NON_CONST(Z, N, DATA) \
144 BOOST_PROTO_DEFINE_FUN_OP_IMPL_(Z, N, DATA, BOOST_PP_EMPTY) \
149 #define BOOST_PROTO_DEFINE_FUN_OP(Z, N, DATA) \
150 BOOST_PROTO_DEFINE_FUN_OP_CONST(Z, N, DATA) \
151 BOOST_PROTO_DEFINE_FUN_OP_NON_CONST(Z, N, DATA) \
156 #define BOOST_PROTO_EXTENDS_CHILD(Z, N, DATA) \
158 typename proto_base_expr::BOOST_PP_CAT(proto_child, N) \
159 BOOST_PP_CAT(proto_child, N); \
162 #define BOOST_PROTO_BASIC_EXTENDS_(Expr, Derived, Domain) \
165 typedef Expr proto_base_expr_; /**< INTERNAL ONLY */ \
166 typedef typename proto_base_expr_::proto_base_expr proto_base_expr; \
167 typedef BOOST_PROTO_REMOVE_TYPENAME(Domain) proto_domain; \
168 typedef Derived proto_derived_expr; \
169 typedef Domain::proto_generator proto_generator; \
170 typedef typename proto_base_expr::proto_tag proto_tag; \
171 typedef typename proto_base_expr::proto_args proto_args; \
172 typedef typename proto_base_expr::proto_arity proto_arity; \
173 typedef typename proto_base_expr::proto_grammar proto_grammar; \
174 typedef typename proto_base_expr::address_of_hack_type_ proto_address_of_hack_type_; \
175 typedef void proto_is_expr_; /**< INTERNAL ONLY */ \
176 static const long proto_arity_c = proto_base_expr::proto_arity_c; \
177 typedef boost::proto::tag::proto_expr<proto_tag, proto_domain> fusion_tag; \
178 BOOST_PP_REPEAT(BOOST_PROTO_MAX_ARITY, BOOST_PROTO_EXTENDS_CHILD, ~) \
180 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
181 static proto_derived_expr const make(Expr const &e) \
183 proto_derived_expr that = {e}; \
187 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
188 proto_base_expr &proto_base() \
190 return this->proto_expr_.proto_base(); \
193 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
194 proto_base_expr const &proto_base() const \
196 return this->proto_expr_.proto_base(); \
199 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
200 operator proto_address_of_hack_type_() const \
202 return boost::addressof(this->proto_base().child0); \
206 #define BOOST_PROTO_BASIC_EXTENDS(Expr, Derived, Domain) \
207 BOOST_PROTO_BASIC_EXTENDS_(Expr, Derived, Domain) \
208 typedef void proto_is_aggregate_; \
209 /**< INTERNAL ONLY */
211 #define BOOST_PROTO_EXTENDS_COPY_ASSIGN_IMPL_(This, Const, Typename) \
212 BOOST_PROTO_DISABLE_MSVC_C4522 \
213 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
214 Typename() BOOST_PROTO_RESULT_OF< \
215 Typename() This::proto_generator( \
216 Typename() boost::proto::base_expr< \
217 Typename() This::proto_domain \
218 , boost::proto::tag::assign \
219 , boost::proto::list2< \
226 operator =(This Const() &a) \
229 Typename() boost::proto::base_expr< \
230 Typename() This::proto_domain \
231 , boost::proto::tag::assign \
232 , boost::proto::list2< \
238 that_type const that = { \
242 return Typename() This::proto_generator()(that); \
246 // MSVC 8.0 and higher seem to need copy-assignment operator to be overloaded on *both*
247 // const and non-const rhs arguments.
248 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600)) && (BOOST_MSVC > 1310)
249 #define BOOST_PROTO_EXTENDS_COPY_ASSIGN_(This, Typename) \
250 BOOST_PROTO_EXTENDS_COPY_ASSIGN_IMPL_(This, BOOST_PP_EMPTY, Typename) \
251 BOOST_PROTO_EXTENDS_COPY_ASSIGN_IMPL_(This, BOOST_PROTO_CONST, Typename) \
254 #define BOOST_PROTO_EXTENDS_COPY_ASSIGN_(This, Typename) \
255 BOOST_PROTO_EXTENDS_COPY_ASSIGN_IMPL_(This, BOOST_PROTO_CONST, Typename) \
261 #define BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(ThisConst, ThatConst) \
262 template<typename A> \
263 BOOST_PROTO_DISABLE_MSVC_C4180 \
264 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
265 typename BOOST_PROTO_RESULT_OF< \
267 typename boost::proto::base_expr< \
269 , boost::proto::tag::assign \
270 , boost::proto::list2< \
271 proto_derived_expr ThisConst() & \
272 , typename boost::proto::result_of::as_child<A ThatConst(), proto_domain>::type \
277 operator =(A ThatConst() &a) ThisConst() \
280 typename boost::proto::base_expr< \
282 , boost::proto::tag::assign \
283 , boost::proto::list2< \
284 proto_derived_expr ThisConst() & \
285 , typename boost::proto::result_of::as_child<A ThatConst(), proto_domain>::type \
289 that_type const that = { \
290 *static_cast<proto_derived_expr ThisConst() *>(this) \
291 , boost::proto::as_child<proto_domain>(a) \
293 return proto_generator()(that); \
297 #define BOOST_PROTO_EXTENDS_ASSIGN_CONST_() \
298 BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(BOOST_PROTO_CONST, BOOST_PP_EMPTY) \
299 BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(BOOST_PROTO_CONST, BOOST_PROTO_CONST) \
302 #define BOOST_PROTO_EXTENDS_ASSIGN_NON_CONST_() \
303 BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(BOOST_PP_EMPTY, BOOST_PP_EMPTY) \
304 BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(BOOST_PP_EMPTY, BOOST_PROTO_CONST) \
307 #define BOOST_PROTO_EXTENDS_ASSIGN_() \
308 BOOST_PROTO_EXTENDS_ASSIGN_CONST_() \
309 BOOST_PROTO_EXTENDS_ASSIGN_NON_CONST_() \
312 #define BOOST_PROTO_EXTENDS_ASSIGN_CONST() \
313 BOOST_PROTO_EXTENDS_COPY_ASSIGN_(proto_derived_expr, BOOST_PROTO_TYPENAME) \
314 BOOST_PROTO_EXTENDS_ASSIGN_CONST_() \
317 #define BOOST_PROTO_EXTENDS_ASSIGN_NON_CONST() \
318 BOOST_PROTO_EXTENDS_COPY_ASSIGN_(proto_derived_expr, BOOST_PROTO_TYPENAME) \
319 BOOST_PROTO_EXTENDS_ASSIGN_NON_CONST_() \
322 #define BOOST_PROTO_EXTENDS_ASSIGN() \
323 BOOST_PROTO_EXTENDS_COPY_ASSIGN_(proto_derived_expr, BOOST_PROTO_TYPENAME) \
324 BOOST_PROTO_EXTENDS_ASSIGN_() \
329 #define BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(ThisConst, ThatConst) \
330 template<typename A> \
331 BOOST_PROTO_DISABLE_MSVC_C4180 \
332 BOOST_PROTO_DISABLE_MSVC_C4714 BOOST_FORCEINLINE \
333 typename BOOST_PROTO_RESULT_OF< \
335 typename boost::proto::base_expr< \
337 , boost::proto::tag::subscript \
338 , boost::proto::list2< \
339 proto_derived_expr ThisConst() & \
340 , typename boost::proto::result_of::as_child<A ThatConst(), proto_domain>::type \
345 operator [](A ThatConst() &a) ThisConst() \
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 \
357 that_type const that = { \
358 *static_cast<proto_derived_expr ThisConst() *>(this) \
359 , boost::proto::as_child<proto_domain>(a) \
361 return proto_generator()(that); \
365 #define BOOST_PROTO_EXTENDS_SUBSCRIPT_CONST() \
366 BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(BOOST_PROTO_CONST, BOOST_PP_EMPTY) \
367 BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(BOOST_PROTO_CONST, BOOST_PROTO_CONST) \
370 #define BOOST_PROTO_EXTENDS_SUBSCRIPT_NON_CONST() \
371 BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(BOOST_PP_EMPTY, BOOST_PP_EMPTY) \
372 BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(BOOST_PP_EMPTY, BOOST_PROTO_CONST) \
375 #define BOOST_PROTO_EXTENDS_SUBSCRIPT() \
376 BOOST_PROTO_EXTENDS_SUBSCRIPT_CONST() \
377 BOOST_PROTO_EXTENDS_SUBSCRIPT_NON_CONST() \
382 #define BOOST_PROTO_EXTENDS_FUNCTION_() \
383 template<typename Sig> \
387 typename BOOST_PROTO_RESULT_OF< \
389 typename boost::proto::result_of::funop< \
391 , proto_derived_expr \
400 #ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
401 #define BOOST_PROTO_EXTENDS_FUNCTION_CONST() \
402 BOOST_PROTO_EXTENDS_FUNCTION_() \
403 BOOST_PROTO_DEFINE_FUN_OP_VARIADIC_IMPL_(BOOST_PROTO_CONST) \
406 #define BOOST_PROTO_EXTENDS_FUNCTION_NON_CONST() \
407 BOOST_PROTO_EXTENDS_FUNCTION_() \
408 BOOST_PROTO_DEFINE_FUN_OP_VARIADIC_IMPL_(BOOST_PP_EMPTY) \
411 #define BOOST_PROTO_EXTENDS_FUNCTION() \
412 BOOST_PROTO_EXTENDS_FUNCTION_() \
413 BOOST_PROTO_DEFINE_FUN_OP_VARIADIC_IMPL_(BOOST_PP_EMPTY) \
414 BOOST_PROTO_DEFINE_FUN_OP_VARIADIC_IMPL_(BOOST_PROTO_CONST) \
417 #define BOOST_PROTO_EXTENDS_FUNCTION_CONST() \
418 BOOST_PROTO_EXTENDS_FUNCTION_() \
419 BOOST_PP_REPEAT_FROM_TO( \
421 , BOOST_PROTO_MAX_FUNCTION_CALL_ARITY \
422 , BOOST_PROTO_DEFINE_FUN_OP_CONST \
427 #define BOOST_PROTO_EXTENDS_FUNCTION_NON_CONST() \
428 BOOST_PROTO_EXTENDS_FUNCTION_() \
429 BOOST_PP_REPEAT_FROM_TO( \
431 , BOOST_PROTO_MAX_FUNCTION_CALL_ARITY \
432 , BOOST_PROTO_DEFINE_FUN_OP_NON_CONST \
437 #define BOOST_PROTO_EXTENDS_FUNCTION() \
438 BOOST_PROTO_EXTENDS_FUNCTION_() \
439 BOOST_PP_REPEAT_FROM_TO( \
441 , BOOST_PROTO_MAX_FUNCTION_CALL_ARITY \
442 , BOOST_PROTO_DEFINE_FUN_OP \
448 #define BOOST_PROTO_EXTENDS(Expr, Derived, Domain) \
449 BOOST_PROTO_BASIC_EXTENDS(Expr, Derived, Domain) \
450 BOOST_PROTO_EXTENDS_ASSIGN() \
451 BOOST_PROTO_EXTENDS_SUBSCRIPT() \
452 BOOST_PROTO_EXTENDS_FUNCTION() \
455 #define BOOST_PROTO_EXTENDS_USING_ASSIGN(Derived) \
456 typedef typename Derived::proto_extends proto_extends; \
457 using proto_extends::operator =; \
458 BOOST_PROTO_EXTENDS_COPY_ASSIGN_(Derived, BOOST_PROTO_TYPENAME) \
461 #define BOOST_PROTO_EXTENDS_USING_ASSIGN_NON_DEPENDENT(Derived) \
462 typedef Derived::proto_extends proto_extends; \
463 using proto_extends::operator =; \
464 BOOST_PROTO_EXTENDS_COPY_ASSIGN_(Derived, BOOST_PP_EMPTY) \
469 /// \brief Empty type to be used as a dummy template parameter of
470 /// POD expression wrappers. It allows argument-dependent lookup
471 /// to find Proto's operator overloads.
473 /// \c proto::is_proto_expr allows argument-dependent lookup
474 /// to find Proto's operator overloads. For example:
477 /// template<typename T, typename Dummy = proto::is_proto_expr>
478 /// struct my_terminal
480 /// BOOST_PROTO_BASIC_EXTENDS(
481 /// typename proto::terminal<T>::type
488 /// my_terminal<int> _1, _2;
489 /// _1 + _2; // OK, uses proto::operator+
492 /// Without the second \c Dummy template parameter, Proto's operator
493 /// overloads would not be considered by name lookup.
497 /// \brief extends\<\> class template for adding behaviors to a Proto expression template
502 , typename Domain // = proto::default_domain
503 , long Arity // = Expr::proto_arity_c
513 extends(extends const &that)
514 : proto_expr_(that.proto_expr_)
518 extends(Expr const &expr_)
522 typedef extends proto_extends;
523 BOOST_PROTO_BASIC_EXTENDS_(Expr, Derived, typename Domain)
524 BOOST_PROTO_EXTENDS_ASSIGN_CONST_()
525 BOOST_PROTO_EXTENDS_SUBSCRIPT_CONST()
527 // Instead of using BOOST_PROTO_EXTENDS_FUNCTION, which uses
528 // nested preprocessor loops, use file iteration here to generate
529 // the operator() overloads, which is more efficient.
530 #include <boost/proto/detail/extends_funop_const.hpp>
533 /// \brief extends\<\> class template for adding behaviors to a Proto expression template
535 template<typename Expr, typename Derived, typename Domain>
536 struct extends<Expr, Derived, Domain, 0>
544 extends(extends const &that)
545 : proto_expr_(that.proto_expr_)
549 extends(Expr const &expr_)
553 typedef extends proto_extends;
554 BOOST_PROTO_BASIC_EXTENDS_(Expr, Derived, typename Domain)
555 BOOST_PROTO_EXTENDS_ASSIGN_()
556 BOOST_PROTO_EXTENDS_SUBSCRIPT()
558 // Instead of using BOOST_PROTO_EXTENDS_FUNCTION, which uses
559 // nested preprocessor loops, use file iteration here to generate
560 // the operator() overloads, which is more efficient.
561 #include <boost/proto/detail/extends_funop.hpp>
566 template<typename This, typename Fun, typename Domain>
567 struct virtual_member
569 typedef Domain proto_domain;
570 typedef typename Domain::proto_generator proto_generator;
571 typedef virtual_member<This, Fun, Domain> proto_derived_expr;
572 typedef tag::member proto_tag;
573 typedef list2<This &, expr<tag::terminal, term<Fun> > const &> proto_args;
574 typedef mpl::long_<2> proto_arity;
575 typedef detail::not_a_valid_type proto_address_of_hack_type_;
576 typedef void proto_is_expr_; /**< INTERNAL ONLY */
577 static const long proto_arity_c = 2;
578 typedef boost::proto::tag::proto_expr<proto_tag, Domain> fusion_tag;
579 typedef This &proto_child0;
580 typedef expr<tag::terminal, term<Fun> > const &proto_child1;
581 typedef expr<proto_tag, proto_args, proto_arity_c> proto_base_expr;
582 typedef basic_expr<proto_tag, proto_args, proto_arity_c> proto_grammar;
583 typedef void proto_is_aggregate_; /**< INTERNAL ONLY */
585 BOOST_PROTO_EXTENDS_ASSIGN_()
586 BOOST_PROTO_EXTENDS_SUBSCRIPT()
588 // Instead of using BOOST_PROTO_EXTENDS_FUNCTION, which uses
589 // nested preprocessor loops, use file iteration here to generate
590 // the operator() overloads, which is more efficient.
591 #define BOOST_PROTO_NO_WAVE_OUTPUT
592 #include <boost/proto/detail/extends_funop.hpp>
593 #undef BOOST_PROTO_NO_WAVE_OUTPUT
596 proto_base_expr const proto_base() const
598 proto_base_expr that = {this->child0(), this->child1()};
603 proto_child0 child0() const
606 return *(This *)((char *)this - BOOST_PROTO_OFFSETOF(This, proto_member_union_start_));
610 proto_child1 child1() const
612 static expr<tag::terminal, term<Fun>, 0> const that = {Fun()};
619 #define BOOST_PROTO_EXTENDS_MEMBER_(R, DOMAIN, ELEM) \
620 boost::proto::exprns_::virtual_member< \
622 , BOOST_PP_TUPLE_ELEM(2, 0, ELEM) \
624 > BOOST_PP_TUPLE_ELEM(2, 1, ELEM); \
627 /// \brief For declaring virtual data members in an extension class.
629 #define BOOST_PROTO_EXTENDS_MEMBERS_WITH_DOMAIN(SEQ, DOMAIN) \
632 char proto_member_union_start_; \
633 BOOST_PP_SEQ_FOR_EACH(BOOST_PROTO_EXTENDS_MEMBER_, DOMAIN, SEQ) \
637 /// \brief For declaring virtual data members in an extension class.
639 #define BOOST_PROTO_EXTENDS_MEMBERS(SEQ) \
640 BOOST_PROTO_EXTENDS_MEMBERS_WITH_DOMAIN(SEQ, proto_domain) \
647 #if defined(_MSC_VER)
648 # pragma warning(pop)