/////////////////////////////////////////////////////////////////////////////// /// \file extends.hpp /// Macros and a base class for defining end-user expression types // // Copyright 2007 Eric Niebler. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_PROTO_EXTENDS_HPP_EAN_11_1_2006 #define BOOST_PROTO_EXTENDS_HPP_EAN_11_1_2006 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace proto { /// INTERNAL ONLY /// #define BOOST_PROTO_CONST0 /// INTERNAL ONLY /// #define BOOST_PROTO_CONST1 const /// INTERNAL ONLY /// #define BOOST_PROTO_TEMPLATE_YES_(Z, N) template /// INTERNAL ONLY /// #define BOOST_PROTO_TEMPLATE_NO_(Z, N) /// INTERNAL ONLY /// #define BOOST_PROTO_DEFINE_FUN_OP_IMPL_(Z, N, Data, Const)\ BOOST_PP_IF(N, BOOST_PROTO_TEMPLATE_YES_, BOOST_PROTO_TEMPLATE_NO_)(Z, N)\ typename boost::mpl::apply_wrap1<\ BOOST_PP_TUPLE_ELEM(3, 2, Data)\ , typename boost::proto::result_of::BOOST_PP_CAT(funop, N)<\ BOOST_PP_TUPLE_ELEM(3, 1, Data) BOOST_PROTO_CONST ## Const\ BOOST_PP_ENUM_TRAILING_PARAMS_Z(Z, N, const A)\ >::type\ >::type const\ operator ()(BOOST_PP_ENUM_BINARY_PARAMS_Z(Z, N, A, const &a)) BOOST_PROTO_CONST ## Const\ {\ typedef boost::proto::result_of::BOOST_PP_CAT(funop, N)<\ BOOST_PP_TUPLE_ELEM(3, 1, Data) BOOST_PROTO_CONST ## Const\ BOOST_PP_ENUM_TRAILING_PARAMS_Z(Z, N, const A)\ > funop;\ return BOOST_PP_TUPLE_ELEM(3, 2, Data)::make(\ funop::call(*static_cast(this) BOOST_PP_ENUM_TRAILING_PARAMS_Z(Z, N, a))\ );\ }\ /**/ /// INTERNAL ONLY /// #define BOOST_PROTO_DEFINE_FUN_OP_CONST(Z, N, Data)\ BOOST_PROTO_DEFINE_FUN_OP_IMPL_(Z, N, Data, 1) /// INTERNAL ONLY /// #define BOOST_PROTO_DEFINE_FUN_OP_NON_CONST(Z, N, Data)\ BOOST_PROTO_DEFINE_FUN_OP_IMPL_(Z, N, Data, 0) /// INTERNAL ONLY /// #define BOOST_PROTO_DEFINE_FUN_OP(Z, N, Data)\ BOOST_PROTO_DEFINE_FUN_OP_CONST(Z, N, Data)\ BOOST_PROTO_DEFINE_FUN_OP_NON_CONST(Z, N, Data)\ /**/ /// INTERNAL ONLY /// #define BOOST_PROTO_EXTENDS_ARG(z, n, Expr)\ typedef\ typename Expr::BOOST_PP_CAT(proto_arg, n)\ BOOST_PP_CAT(proto_arg, n);\ /**/ #define BOOST_PROTO_EXTENDS(Expr, Derived, Domain)\ Expr expr;\ \ typedef Expr proto_base_expr;\ typedef Domain proto_domain;\ typedef Derived proto_derived_expr;\ typedef typename Expr::proto_tag proto_tag;\ typedef typename Expr::proto_args proto_args;\ typedef typename Expr::proto_arity proto_arity;\ typedef void proto_is_expr_;\ typedef boost::proto::tag::proto_expr fusion_tag;\ \ BOOST_PROTO_IDENTITY_TRANSFORM();\ BOOST_PP_REPEAT(BOOST_PROTO_MAX_ARITY, BOOST_PROTO_EXTENDS_ARG, Expr)\ \ static Derived const make(Expr const &expr)\ {\ Derived that = {expr};\ return that;\ }\ \ Expr &proto_base()\ {\ return this->expr;\ }\ \ Expr const &proto_base() const\ {\ return this->expr;\ }\ /**/ /// INTERNAL ONLY /// #define BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(Expr, Derived, Domain, Const)\ template\ typename boost::mpl::apply_wrap1, typename boost::proto::result_of::as_arg::type> > >::type const\ operator =(A &a) BOOST_PROTO_CONST ## Const\ {\ typedef boost::proto::expr, typename boost::proto::result_of::as_arg::type> > that_type;\ that_type that = {{*static_cast(this)}, boost::proto::as_arg(a)};\ return Domain::make(that);\ }\ \ template\ typename boost::mpl::apply_wrap1, typename boost::proto::result_of::as_arg::type> > >::type const\ operator =(A const &a) BOOST_PROTO_CONST ## Const\ {\ typedef boost::proto::expr, typename boost::proto::result_of::as_arg::type> > that_type;\ that_type that = {{*static_cast(this)}, boost::proto::as_arg(a)};\ return Domain::make(that);\ }\ /**/ #define BOOST_PROTO_EXTENDS_ASSIGN_CONST(Expr, Derived, Domain)\ BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(Expr, Derived, Domain, 1) #define BOOST_PROTO_EXTENDS_ASSIGN_NON_CONST(Expr, Derived, Domain)\ BOOST_PROTO_EXTENDS_ASSIGN_IMPL_(Expr, Derived, Domain, 0) #define BOOST_PROTO_EXTENDS_ASSIGN(Expr, Derived, Domain)\ BOOST_PROTO_EXTENDS_ASSIGN_CONST(Expr, Derived, Domain)\ BOOST_PROTO_EXTENDS_ASSIGN_NON_CONST(Expr, Derived, Domain)\ /**/ /// INTERNAL ONLY /// #define BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(Expr, Derived, Domain, Const)\ template\ typename boost::mpl::apply_wrap1, typename boost::proto::result_of::as_arg::type> > >::type const\ operator [](A &a) BOOST_PROTO_CONST ## Const\ {\ typedef boost::proto::expr, typename boost::proto::result_of::as_arg::type> > that_type;\ that_type that = {{*static_cast(this)}, boost::proto::as_arg(a)};\ return Domain::make(that);\ }\ \ template\ typename boost::mpl::apply_wrap1, typename boost::proto::result_of::as_arg::type> > >::type const\ operator [](A const &a) BOOST_PROTO_CONST ## Const\ {\ typedef boost::proto::expr, typename boost::proto::result_of::as_arg::type> > that_type;\ that_type that = {{*static_cast(this)}, boost::proto::as_arg(a)};\ return Domain::make(that);\ }\ /**/ #define BOOST_PROTO_EXTENDS_SUBSCRIPT_CONST(Expr, Derived, Domain)\ BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(Expr, Derived, Domain, 1) #define BOOST_PROTO_EXTENDS_SUBSCRIPT_NON_CONST(Expr, Derived, Domain)\ BOOST_PROTO_EXTENDS_SUBSCRIPT_IMPL_(Expr, Derived, Domain, 0) #define BOOST_PROTO_EXTENDS_SUBSCRIPT(Expr, Derived, Domain)\ BOOST_PROTO_EXTENDS_SUBSCRIPT_CONST(Expr, Derived, Domain)\ BOOST_PROTO_EXTENDS_SUBSCRIPT_NON_CONST(Expr, Derived, Domain)\ /**/ /// INTERNAL ONLY /// #define BOOST_PROTO_EXTENDS_FUNCTION_(Expr, Derived, Domain)\ template\ struct result\ {\ typedef\ typename boost::mpl::apply_wrap1::type>::type\ type;\ };\ /**/ #define BOOST_PROTO_EXTENDS_FUNCTION_CONST(Expr, Derived, Domain)\ BOOST_PROTO_EXTENDS_FUNCTION_(Expr, Derived, Domain)\ BOOST_PP_REPEAT_FROM_TO(0, BOOST_PP_DEC(BOOST_PROTO_MAX_ARITY), BOOST_PROTO_DEFINE_FUN_OP_CONST, (Expr, Derived, Domain))\ /**/ #define BOOST_PROTO_EXTENDS_FUNCTION_NON_CONST(Expr, Derived, Domain)\ BOOST_PROTO_EXTENDS_FUNCTION_(Expr, Derived, Domain)\ BOOST_PP_REPEAT_FROM_TO(0, BOOST_PP_DEC(BOOST_PROTO_MAX_ARITY), BOOST_PROTO_DEFINE_FUN_OP_NON_CONST, (Expr, Derived, Domain))\ /**/ #define BOOST_PROTO_EXTENDS_FUNCTION(Expr, Derived, Domain)\ BOOST_PROTO_EXTENDS_FUNCTION_(Expr, Derived, Domain)\ BOOST_PP_REPEAT_FROM_TO(0, BOOST_PP_DEC(BOOST_PROTO_MAX_ARITY), BOOST_PROTO_DEFINE_FUN_OP, (Expr, Derived, Domain))\ /**/ namespace exprns_ { /// \brief Empty type to be used as a dummy template parameter of /// POD expression wrappers. It allows argument-dependent lookup /// to find Proto's operator overloads. /// /// \c proto::is_proto_expr allows argument-dependent lookup /// to find Proto's operator overloads. For example: /// /// \code /// template /// struct my_terminal /// { /// BOOST_PROTO_EXTENDS( /// typename proto::terminal::type /// , my_terminal /// , default_domain /// ) /// }; /// /// // ... /// my_terminal _1, _2; /// _1 + _2; // OK, uses proto::operator+ /// \endcode /// /// Without the second \c Dummy template parameter, Proto's operator /// overloads would not be considered by name lookup. struct is_proto_expr {}; /// \brief extends\<\> class template for adding behaviors to a proto expression template /// template struct extends { extends() : expr() {} extends(extends const &that) : expr(that.expr) {} extends(Expr const &expr_) : expr(expr_) {} BOOST_PROTO_EXTENDS(Expr, Derived, Domain) BOOST_PROTO_EXTENDS_ASSIGN_CONST(Expr, Derived, Domain) BOOST_PROTO_EXTENDS_SUBSCRIPT_CONST(Expr, Derived, Domain) // Instead of using BOOST_PROTO_EXTENDS_FUNCTION, which uses // nested preprocessor loops, use file iteration here to generate // the operator() overloads, which is more efficient. BOOST_PROTO_EXTENDS_FUNCTION_(Expr, Derived, Domain) /// INTERNAL ONLY /// #define BOOST_PP_LOCAL_MACRO(N) \ BOOST_PROTO_DEFINE_FUN_OP_CONST(1, N, (Expr, Derived, Domain))\ /**/ /// INTERNAL ONLY /// #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_DEC(BOOST_PROTO_MAX_ARITY)) #include BOOST_PP_LOCAL_ITERATE() }; /// \brief extends\<\> class template for adding behaviors to a proto expression template /// template struct extends { extends() : expr() {} extends(extends const &that) : expr(that.expr) {} extends(Expr const &expr_) : expr(expr_) {} BOOST_PROTO_EXTENDS(Expr, Derived, Domain) BOOST_PROTO_EXTENDS_ASSIGN(Expr, Derived, Domain) BOOST_PROTO_EXTENDS_SUBSCRIPT(Expr, Derived, Domain) // Instead of using BOOST_PROTO_EXTENDS_FUNCTION, which uses // nested preprocessor loops, use file iteration here to generate // the operator() overloads, which is more efficient. BOOST_PROTO_EXTENDS_FUNCTION_(Expr, Derived, Domain) /// INTERNAL ONLY /// #define BOOST_PP_LOCAL_MACRO(N) \ BOOST_PROTO_DEFINE_FUN_OP(1, N, (Expr, Derived, Domain))\ /**/ /// INTERNAL ONLY /// #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_DEC(BOOST_PROTO_MAX_ARITY)) #include BOOST_PP_LOCAL_ITERATE() }; } // namespace exprns_ }} #endif