Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: /// \file default.hpp Chris@16: /// Definintion of default_context, a default evaluation context for Chris@16: /// proto::eval() that uses Boost.Typeof to deduce return types Chris@16: /// of the built-in operators. Chris@16: // Chris@16: // Copyright 2008 Eric Niebler. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_PROTO_CONTEXT_DEFAULT_HPP_EAN_01_08_2007 Chris@16: #define BOOST_PROTO_CONTEXT_DEFAULT_HPP_EAN_01_08_2007 Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include // for proto::child_c() Chris@16: #include Chris@16: Chris@16: namespace boost { namespace proto Chris@16: { Chris@16: /// INTERNAL ONLY Chris@16: /// Chris@16: #define UNREF(x) typename boost::remove_reference::type Chris@16: Chris@16: namespace context Chris@16: { Chris@16: template< Chris@16: typename Expr Chris@16: , typename Context Chris@16: , typename Tag // = typename Expr::proto_tag Chris@16: , long Arity // = Expr::proto_arity_c Chris@16: > Chris@16: struct default_eval Chris@16: {}; Chris@16: Chris@16: template Chris@16: struct default_eval Chris@16: { Chris@16: typedef Chris@16: typename proto::result_of::value::type Chris@16: result_type; Chris@16: Chris@16: result_type operator ()(Expr &expr, Context &) const Chris@16: { Chris@16: return proto::value(expr); Chris@16: } Chris@16: }; Chris@16: Chris@16: /// INTERNAL ONLY Chris@16: /// Chris@16: #define BOOST_PROTO_UNARY_DEFAULT_EVAL(OP, TAG, MAKE) \ Chris@16: template \ Chris@16: struct default_eval \ Chris@16: { \ Chris@16: private: \ Chris@16: typedef typename proto::result_of::child_c::type e0; \ Chris@16: typedef typename proto::result_of::eval::type r0; \ Chris@16: public: \ Chris@16: BOOST_PROTO_DECLTYPE_(OP proto::detail::MAKE(), result_type) \ Chris@16: result_type operator ()(Expr &expr, Context &ctx) const \ Chris@16: { \ Chris@16: return OP proto::eval(proto::child_c<0>(expr), ctx); \ Chris@16: } \ Chris@16: }; \ Chris@16: /**/ Chris@16: Chris@16: /// INTERNAL ONLY Chris@16: /// Chris@16: #define BOOST_PROTO_BINARY_DEFAULT_EVAL(OP, TAG, LMAKE, RMAKE) \ Chris@16: template \ Chris@16: struct default_eval \ Chris@16: { \ Chris@16: private: \ Chris@16: typedef typename proto::result_of::child_c::type e0; \ Chris@16: typedef typename proto::result_of::child_c::type e1; \ Chris@16: typedef typename proto::result_of::eval::type r0; \ Chris@16: typedef typename proto::result_of::eval::type r1; \ Chris@16: public: \ Chris@16: BOOST_PROTO_DECLTYPE_( \ Chris@16: proto::detail::LMAKE() OP proto::detail::RMAKE() \ Chris@16: , result_type \ Chris@16: ) \ Chris@16: result_type operator ()(Expr &expr, Context &ctx) const \ Chris@16: { \ Chris@16: return proto::eval( \ Chris@16: proto::child_c<0>(expr), ctx) OP proto::eval(proto::child_c<1>(expr) \ Chris@16: , ctx \ Chris@16: ); \ Chris@16: } \ Chris@16: }; \ Chris@16: /**/ Chris@16: Chris@16: BOOST_PROTO_UNARY_DEFAULT_EVAL(+, proto::tag::unary_plus, make) Chris@16: BOOST_PROTO_UNARY_DEFAULT_EVAL(-, proto::tag::negate, make) Chris@16: BOOST_PROTO_UNARY_DEFAULT_EVAL(*, proto::tag::dereference, make) Chris@16: BOOST_PROTO_UNARY_DEFAULT_EVAL(~, proto::tag::complement, make) Chris@16: BOOST_PROTO_UNARY_DEFAULT_EVAL(&, proto::tag::address_of, make) Chris@16: BOOST_PROTO_UNARY_DEFAULT_EVAL(!, proto::tag::logical_not, make) Chris@16: BOOST_PROTO_UNARY_DEFAULT_EVAL(++, proto::tag::pre_inc, make_mutable) Chris@16: BOOST_PROTO_UNARY_DEFAULT_EVAL(--, proto::tag::pre_dec, make_mutable) Chris@16: Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(<<, proto::tag::shift_left, make_mutable, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(>>, proto::tag::shift_right, make_mutable, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(*, proto::tag::multiplies, make, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(/, proto::tag::divides, make, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(%, proto::tag::modulus, make, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(+, proto::tag::plus, make, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(-, proto::tag::minus, make, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(<, proto::tag::less, make, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(>, proto::tag::greater, make, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(<=, proto::tag::less_equal, make, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(>=, proto::tag::greater_equal, make, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(==, proto::tag::equal_to, make, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(!=, proto::tag::not_equal_to, make, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(||, proto::tag::logical_or, make, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(&&, proto::tag::logical_and, make, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(&, proto::tag::bitwise_and, make, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(|, proto::tag::bitwise_or, make, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(^, proto::tag::bitwise_xor, make, make) Chris@16: Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(=, proto::tag::assign, make_mutable, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(<<=, proto::tag::shift_left_assign, make_mutable, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(>>=, proto::tag::shift_right_assign, make_mutable, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(*=, proto::tag::multiplies_assign, make_mutable, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(/=, proto::tag::divides_assign, make_mutable, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(%=, proto::tag::modulus_assign, make_mutable, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(+=, proto::tag::plus_assign, make_mutable, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(-=, proto::tag::minus_assign, make_mutable, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(&=, proto::tag::bitwise_and_assign, make_mutable, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(|=, proto::tag::bitwise_or_assign, make_mutable, make) Chris@16: BOOST_PROTO_BINARY_DEFAULT_EVAL(^=, proto::tag::bitwise_xor_assign, make_mutable, make) Chris@16: Chris@16: #undef BOOST_PROTO_UNARY_DEFAULT_EVAL Chris@16: #undef BOOST_PROTO_BINARY_DEFAULT_EVAL Chris@16: Chris@16: /// INTERNAL ONLY Chris@16: template Chris@16: struct is_member_function_eval Chris@16: : is_member_function_pointer< Chris@16: typename detail::uncvref< Chris@16: typename proto::result_of::eval< Chris@16: typename remove_reference< Chris@16: typename proto::result_of::child_c::type Chris@16: >::type Chris@16: , Context Chris@16: >::type Chris@16: >::type Chris@16: > Chris@16: {}; Chris@16: Chris@16: /// INTERNAL ONLY Chris@16: template Chris@16: struct memfun_eval Chris@16: { Chris@16: private: Chris@16: typedef typename result_of::child_c::type e0; Chris@16: typedef typename result_of::child_c::type e1; Chris@16: typedef typename proto::result_of::eval::type r0; Chris@16: typedef typename proto::result_of::eval::type r1; Chris@16: public: Chris@16: typedef typename detail::mem_ptr_fun::result_type result_type; Chris@16: result_type operator ()(Expr &expr, Context &ctx) const Chris@16: { Chris@16: return detail::mem_ptr_fun()( Chris@16: proto::eval(proto::child_c<0>(expr), ctx) Chris@16: , proto::eval(proto::child_c<1>(expr), ctx) Chris@16: ); Chris@16: } Chris@16: }; Chris@16: Chris@16: /// INTERNAL ONLY Chris@16: template Chris@16: struct memfun_eval Chris@16: { Chris@16: private: Chris@16: typedef typename result_of::child_c::type e0; Chris@16: typedef typename result_of::child_c::type e1; Chris@16: typedef typename proto::result_of::eval::type r0; Chris@16: typedef typename proto::result_of::eval::type r1; Chris@16: public: Chris@16: typedef detail::memfun result_type; Chris@16: result_type const operator ()(Expr &expr, Context &ctx) const Chris@16: { Chris@16: return detail::memfun( Chris@16: proto::eval(proto::child_c<0>(expr), ctx) Chris@16: , proto::eval(proto::child_c<1>(expr), ctx) Chris@16: ); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct default_eval Chris@16: : memfun_eval::value> Chris@16: {}; Chris@16: Chris@16: // Handle post-increment specially. Chris@16: template Chris@16: struct default_eval Chris@16: { Chris@16: private: Chris@16: typedef typename proto::result_of::child_c::type e0; Chris@16: typedef typename proto::result_of::eval::type r0; Chris@16: public: Chris@16: BOOST_PROTO_DECLTYPE_(proto::detail::make_mutable() ++, result_type) Chris@16: result_type operator ()(Expr &expr, Context &ctx) const Chris@16: { Chris@16: return proto::eval(proto::child_c<0>(expr), ctx) ++; Chris@16: } Chris@16: }; Chris@16: Chris@16: // Handle post-decrement specially. Chris@16: template Chris@16: struct default_eval Chris@16: { Chris@16: private: Chris@16: typedef typename proto::result_of::child_c::type e0; Chris@16: typedef typename proto::result_of::eval::type r0; Chris@16: public: Chris@16: BOOST_PROTO_DECLTYPE_(proto::detail::make_mutable() --, result_type) Chris@16: result_type operator ()(Expr &expr, Context &ctx) const Chris@16: { Chris@16: return proto::eval(proto::child_c<0>(expr), ctx) --; Chris@16: } Chris@16: }; Chris@16: Chris@16: // Handle subscript specially. Chris@16: template Chris@16: struct default_eval Chris@16: { Chris@16: private: Chris@16: typedef typename proto::result_of::child_c::type e0; Chris@16: typedef typename proto::result_of::child_c::type e1; Chris@16: typedef typename proto::result_of::eval::type r0; Chris@16: typedef typename proto::result_of::eval::type r1; Chris@16: public: Chris@16: BOOST_PROTO_DECLTYPE_(proto::detail::make_subscriptable()[proto::detail::make()], result_type) Chris@16: result_type operator ()(Expr &expr, Context &ctx) const Chris@16: { Chris@16: return proto::eval(proto::child_c<0>(expr), ctx)[proto::eval(proto::child_c<1>(expr), ctx)]; Chris@16: } Chris@16: }; Chris@16: Chris@16: // Handle if_else_ specially. Chris@16: template Chris@16: struct default_eval Chris@16: { Chris@16: private: Chris@16: typedef typename proto::result_of::child_c::type e0; Chris@16: typedef typename proto::result_of::child_c::type e1; Chris@16: typedef typename proto::result_of::child_c::type e2; Chris@16: typedef typename proto::result_of::eval::type r0; Chris@16: typedef typename proto::result_of::eval::type r1; Chris@16: typedef typename proto::result_of::eval::type r2; Chris@16: public: Chris@16: BOOST_PROTO_DECLTYPE_( Chris@16: proto::detail::make() Chris@16: ? proto::detail::make() Chris@16: : proto::detail::make() Chris@16: , result_type Chris@16: ) Chris@16: result_type operator ()(Expr &expr, Context &ctx) const Chris@16: { Chris@16: return proto::eval(proto::child_c<0>(expr), ctx) Chris@16: ? proto::eval(proto::child_c<1>(expr), ctx) Chris@16: : proto::eval(proto::child_c<2>(expr), ctx); Chris@16: } Chris@16: }; Chris@16: Chris@16: // Handle comma specially. Chris@16: template Chris@16: struct default_eval Chris@16: { Chris@16: private: Chris@16: typedef typename proto::result_of::child_c::type e0; Chris@16: typedef typename proto::result_of::child_c::type e1; Chris@16: typedef typename proto::result_of::eval::type r0; Chris@16: typedef typename proto::result_of::eval::type r1; Chris@16: public: Chris@16: typedef typename proto::detail::comma_result::type result_type; Chris@16: result_type operator ()(Expr &expr, Context &ctx) const Chris@16: { Chris@16: return proto::eval(proto::child_c<0>(expr), ctx), proto::eval(proto::child_c<1>(expr), ctx); Chris@16: } Chris@16: }; Chris@16: Chris@16: // Handle function specially Chris@16: #define BOOST_PROTO_DEFAULT_EVAL_TYPE(Z, N, DATA) \ Chris@16: typename proto::result_of::eval< \ Chris@16: typename remove_reference< \ Chris@16: typename proto::result_of::child_c::type \ Chris@16: >::type \ Chris@16: , Context \ Chris@16: >::type \ Chris@16: /**/ Chris@16: Chris@16: #define BOOST_PROTO_DEFAULT_EVAL(Z, N, DATA) \ Chris@16: proto::eval(proto::child_c(DATA), context) \ Chris@16: /**/ Chris@16: Chris@16: template Chris@16: struct default_eval Chris@16: { Chris@16: typedef Chris@16: typename proto::detail::result_of_fixup< Chris@16: BOOST_PROTO_DEFAULT_EVAL_TYPE(~, 0, Expr) Chris@16: >::type Chris@16: function_type; Chris@16: Chris@16: typedef Chris@16: typename BOOST_PROTO_RESULT_OF::type Chris@16: result_type; Chris@16: Chris@16: result_type operator ()(Expr &expr, Context &context) const Chris@16: { Chris@16: return BOOST_PROTO_DEFAULT_EVAL(~, 0, expr)(); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct default_eval Chris@16: { Chris@16: typedef Chris@16: typename proto::detail::result_of_fixup< Chris@16: BOOST_PROTO_DEFAULT_EVAL_TYPE(~, 0, Expr) Chris@16: >::type Chris@16: function_type; Chris@16: Chris@16: typedef Chris@16: typename detail::result_of_< Chris@16: function_type(BOOST_PROTO_DEFAULT_EVAL_TYPE(~, 1, Expr)) Chris@16: >::type Chris@16: result_type; Chris@16: Chris@16: result_type operator ()(Expr &expr, Context &context) const Chris@16: { Chris@16: return this->invoke( Chris@16: expr Chris@16: , context Chris@16: , is_member_function_pointer() Chris@16: , is_member_object_pointer() Chris@16: ); Chris@16: } Chris@16: Chris@16: private: Chris@16: result_type invoke(Expr &expr, Context &context, mpl::false_, mpl::false_) const Chris@16: { Chris@16: return BOOST_PROTO_DEFAULT_EVAL(~, 0, expr)(BOOST_PROTO_DEFAULT_EVAL(~, 1, expr)); Chris@16: } Chris@16: Chris@16: result_type invoke(Expr &expr, Context &context, mpl::true_, mpl::false_) const Chris@16: { Chris@16: BOOST_PROTO_USE_GET_POINTER(); Chris@16: typedef typename detail::class_member_traits::class_type class_type; Chris@16: return ( Chris@16: BOOST_PROTO_GET_POINTER(class_type, (BOOST_PROTO_DEFAULT_EVAL(~, 1, expr))) ->* Chris@16: BOOST_PROTO_DEFAULT_EVAL(~, 0, expr) Chris@16: )(); Chris@16: } Chris@16: Chris@16: result_type invoke(Expr &expr, Context &context, mpl::false_, mpl::true_) const Chris@16: { Chris@16: BOOST_PROTO_USE_GET_POINTER(); Chris@16: typedef typename detail::class_member_traits::class_type class_type; Chris@16: return ( Chris@16: BOOST_PROTO_GET_POINTER(class_type, (BOOST_PROTO_DEFAULT_EVAL(~, 1, expr))) ->* Chris@16: BOOST_PROTO_DEFAULT_EVAL(~, 0, expr) Chris@16: ); Chris@16: } Chris@16: }; Chris@16: Chris@16: // Additional specialization are generated by the preprocessor Chris@16: #include Chris@16: Chris@16: #undef BOOST_PROTO_DEFAULT_EVAL_TYPE Chris@16: #undef BOOST_PROTO_DEFAULT_EVAL Chris@16: Chris@16: /// default_context Chris@16: /// Chris@16: struct default_context Chris@16: { Chris@16: /// default_context::eval Chris@16: /// Chris@16: template Chris@16: struct eval Chris@16: : default_eval Chris@16: {}; Chris@16: }; Chris@16: Chris@16: } // namespace context Chris@16: Chris@16: }} // namespace boost::proto Chris@16: Chris@16: #undef UNREF Chris@16: Chris@16: #endif