/////////////////////////////////////////////////////////////////////////////// /// \file operators.hpp /// Contains all the overloaded operators that make it possible to build /// Proto expression trees. // // Copyright 2008 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_OPERATORS_HPP_EAN_04_01_2005 #define BOOST_PROTO_OPERATORS_HPP_EAN_04_01_2005 #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace proto { namespace detail { template struct generate_if : lazy_enable_if_c< matches::value , typename Domain::template result > {}; // Optimization, generate fewer templates... template struct generate_if { typedef Expr type; }; template struct generate_if_left : lazy_enable_if_c< matches, 2>, typename Domain::proto_grammar>::value , typename Domain::template result::type>, 2> )> > {}; // Optimization, generate fewer templates... template struct generate_if_left { typedef proto::expr, 2> type; }; template struct generate_if_right : lazy_enable_if_c< matches, 2>, typename Domain::proto_grammar>::value , typename Domain::template result::type, Right &>, 2> )> > {}; // Optimization, generate fewer templates... template struct generate_if_right { typedef proto::expr, 2> type; }; template struct as_expr_if2 {}; template struct as_expr_if2 : generate_if_left< typename Left::proto_domain , Tag , Left , proto::expr, 0> > { typedef proto::expr, 0> term_type; typedef typename Left::proto_domain proto_domain; typedef proto::expr::type>, 2> expr_type; static typename proto_domain::template result::type make(Left &left, Right &right) { term_type term = {right}; expr_type that = {left, proto_domain()(term)}; return proto_domain()(that); } }; template struct as_expr_if2 : generate_if_right< typename Right::proto_domain , Tag , proto::expr, 0> , Right > { typedef proto::expr, 0> term_type; typedef typename Right::proto_domain proto_domain; typedef proto::expr::type, Right &>, 2> expr_type; static typename proto_domain::template result::type make(Left &left, Right &right) { term_type term = {left}; expr_type that = {proto_domain()(term), right}; return proto_domain()(that); } }; template struct as_expr_if : as_expr_if2 {}; template struct as_expr_if : generate_if< typename Left::proto_domain , proto::expr, 2> > { typedef proto::expr, 2> expr_type; typedef typename Left::proto_domain proto_domain; BOOST_MPL_ASSERT((is_same)); static typename proto_domain::template result::type make(Left &left, Right &right) { expr_type that = {left, right}; return proto_domain()(that); } }; template struct arg_weight { BOOST_STATIC_CONSTANT(int, value = 1 + Trait::value); }; template struct arg_weight { BOOST_STATIC_CONSTANT(int, value = 0); }; template struct enable_unary : boost::enable_if_c< boost::mpl::and_ >::value , Expr > {}; template struct enable_unary : boost::enable_if_c< boost::mpl::and_< Trait , boost::proto::matches::type::proto_grammar> >::value , Expr > {}; template struct enable_unary : boost::enable_if_c {}; template struct enable_binary : boost::enable_if_c< boost::mpl::and_< mpl::bool_<(3 <= (arg_weight::value + arg_weight::value))> , boost::proto::matches >::value , Expr > {}; template struct enable_binary : boost::enable_if_c< boost::mpl::and_< mpl::bool_<(3 <= (arg_weight::value + arg_weight::value))> , boost::proto::matches::type::proto_grammar> >::value , Expr > {}; template struct enable_binary : boost::enable_if_c< (3 <= (arg_weight::value + arg_weight::value)) , Expr > {}; } // detail #define BOOST_PROTO_UNARY_OP_IS_POSTFIX_0 #define BOOST_PROTO_UNARY_OP_IS_POSTFIX_1 , int #define BOOST_PROTO_DEFINE_UNARY_OPERATOR(OP, TAG, POST) \ template \ typename detail::generate_if< \ typename Arg::proto_domain \ , proto::expr, 1> \ , typename Arg::proto_is_expr_ \ >::type const \ operator OP(Arg &arg BOOST_PROTO_UNARY_OP_IS_POSTFIX_ ## POST) \ { \ typedef proto::expr, 1> that_type; \ that_type that = {arg}; \ return typename Arg::proto_domain()(that); \ } \ template \ typename detail::generate_if< \ typename Arg::proto_domain \ , proto::expr, 1> \ , typename Arg::proto_is_expr_ \ >::type const \ operator OP(Arg const &arg BOOST_PROTO_UNARY_OP_IS_POSTFIX_ ## POST) \ { \ typedef proto::expr, 1> that_type; \ that_type that = {arg}; \ return typename Arg::proto_domain()(that); \ } \ /**/ #define BOOST_PROTO_DEFINE_BINARY_OPERATOR(OP, TAG) \ template \ inline typename detail::as_expr_if::type const \ operator OP(Left &left, Right &right) \ { \ return detail::as_expr_if::make(left, right); \ } \ template \ inline typename detail::as_expr_if::type const \ operator OP(Left &left, Right const &right) \ { \ return detail::as_expr_if::make(left, right); \ } \ template \ inline typename detail::as_expr_if::type const \ operator OP(Left const &left, Right &right) \ { \ return detail::as_expr_if::make(left, right); \ } \ template \ inline typename detail::as_expr_if::type const \ operator OP(Left const &left, Right const &right) \ { \ return detail::as_expr_if::make(left, right); \ } \ /**/ BOOST_PROTO_BEGIN_ADL_NAMESPACE(exprns_) BOOST_PROTO_DEFINE_UNARY_OPERATOR(+, tag::unary_plus, 0) BOOST_PROTO_DEFINE_UNARY_OPERATOR(-, tag::negate, 0) BOOST_PROTO_DEFINE_UNARY_OPERATOR(*, tag::dereference, 0) BOOST_PROTO_DEFINE_UNARY_OPERATOR(~, tag::complement, 0) BOOST_PROTO_DEFINE_UNARY_OPERATOR(&, tag::address_of, 0) BOOST_PROTO_DEFINE_UNARY_OPERATOR(!, tag::logical_not, 0) BOOST_PROTO_DEFINE_UNARY_OPERATOR(++, tag::pre_inc, 0) BOOST_PROTO_DEFINE_UNARY_OPERATOR(--, tag::pre_dec, 0) BOOST_PROTO_DEFINE_UNARY_OPERATOR(++, tag::post_inc, 1) BOOST_PROTO_DEFINE_UNARY_OPERATOR(--, tag::post_dec, 1) BOOST_PROTO_DEFINE_BINARY_OPERATOR(<<, tag::shift_left) BOOST_PROTO_DEFINE_BINARY_OPERATOR(>>, tag::shift_right) BOOST_PROTO_DEFINE_BINARY_OPERATOR(*, tag::multiplies) BOOST_PROTO_DEFINE_BINARY_OPERATOR(/, tag::divides) BOOST_PROTO_DEFINE_BINARY_OPERATOR(%, tag::modulus) BOOST_PROTO_DEFINE_BINARY_OPERATOR(+, tag::plus) BOOST_PROTO_DEFINE_BINARY_OPERATOR(-, tag::minus) BOOST_PROTO_DEFINE_BINARY_OPERATOR(<, tag::less) BOOST_PROTO_DEFINE_BINARY_OPERATOR(>, tag::greater) BOOST_PROTO_DEFINE_BINARY_OPERATOR(<=, tag::less_equal) BOOST_PROTO_DEFINE_BINARY_OPERATOR(>=, tag::greater_equal) BOOST_PROTO_DEFINE_BINARY_OPERATOR(==, tag::equal_to) BOOST_PROTO_DEFINE_BINARY_OPERATOR(!=, tag::not_equal_to) BOOST_PROTO_DEFINE_BINARY_OPERATOR(||, tag::logical_or) BOOST_PROTO_DEFINE_BINARY_OPERATOR(&&, tag::logical_and) BOOST_PROTO_DEFINE_BINARY_OPERATOR(&, tag::bitwise_and) BOOST_PROTO_DEFINE_BINARY_OPERATOR(|, tag::bitwise_or) BOOST_PROTO_DEFINE_BINARY_OPERATOR(^, tag::bitwise_xor) BOOST_PROTO_DEFINE_BINARY_OPERATOR(BOOST_PP_COMMA(), tag::comma) BOOST_PROTO_DEFINE_BINARY_OPERATOR(->*, tag::mem_ptr) BOOST_PROTO_DEFINE_BINARY_OPERATOR(<<=, tag::shift_left_assign) BOOST_PROTO_DEFINE_BINARY_OPERATOR(>>=, tag::shift_right_assign) BOOST_PROTO_DEFINE_BINARY_OPERATOR(*=, tag::multiplies_assign) BOOST_PROTO_DEFINE_BINARY_OPERATOR(/=, tag::divides_assign) BOOST_PROTO_DEFINE_BINARY_OPERATOR(%=, tag::modulus_assign) BOOST_PROTO_DEFINE_BINARY_OPERATOR(+=, tag::plus_assign) BOOST_PROTO_DEFINE_BINARY_OPERATOR(-=, tag::minus_assign) BOOST_PROTO_DEFINE_BINARY_OPERATOR(&=, tag::bitwise_and_assign) BOOST_PROTO_DEFINE_BINARY_OPERATOR(|=, tag::bitwise_or_assign) BOOST_PROTO_DEFINE_BINARY_OPERATOR(^=, tag::bitwise_xor_assign) /// if_else /// template typename functional::make_expr::impl::result_type const if_else(A0 const &a0, A1 const &a1, A2 const &a2) { return functional::make_expr::impl()(a0, a1, a2); } BOOST_PROTO_END_ADL_NAMESPACE(exprns_) BOOST_PROTO_WHEN_NOT_BUILDING_DOCS(using exprns_::if_else;) #undef BOOST_PROTO_DEFINE_UNARY_OPERATOR #undef BOOST_PROTO_DEFINE_BINARY_OPERATOR #define BOOST_PROTO_DEFINE_UNARY_OPERATOR(OP, TAG, TRAIT, DOMAIN, POST) \ template \ typename boost::proto::detail::enable_unary, Arg \ , typename boost::proto::functional::make_expr::impl::result_type \ >::type const \ operator OP(Arg &arg BOOST_PROTO_UNARY_OP_IS_POSTFIX_ ## POST) \ { \ return boost::proto::functional::make_expr::impl()(arg); \ } \ template \ typename boost::proto::detail::enable_unary, Arg \ , typename boost::proto::functional::make_expr::impl::result_type \ >::type const \ operator OP(Arg const &arg BOOST_PROTO_UNARY_OP_IS_POSTFIX_ ## POST) \ { \ return boost::proto::functional::make_expr::impl()(arg); \ } \ /**/ #define BOOST_PROTO_DEFINE_BINARY_OPERATOR(OP, TAG, TRAIT, DOMAIN) \ template \ typename boost::proto::detail::enable_binary, Left, TRAIT, Right \ , typename boost::proto::functional::make_expr::impl::result_type\ >::type const \ operator OP(Left &left, Right &right) \ { \ return boost::proto::functional::make_expr::impl()(left, right);\ } \ template \ typename boost::proto::detail::enable_binary, Left, TRAIT, Right \ , typename boost::proto::functional::make_expr::impl::result_type\ >::type const \ operator OP(Left &left, Right const &right) \ { \ return boost::proto::functional::make_expr::impl()(left, right);\ } \ template \ typename boost::proto::detail::enable_binary, Left, TRAIT, Right \ , typename boost::proto::functional::make_expr::impl::result_type\ >::type const \ operator OP(Left const &left, Right &right) \ { \ return boost::proto::functional::make_expr::impl()(left, right);\ } \ template \ typename boost::proto::detail::enable_binary, Left, TRAIT, Right \ , typename boost::proto::functional::make_expr::impl::result_type\ >::type const \ operator OP(Left const &left, Right const &right) \ { \ return boost::proto::functional::make_expr::impl()(left, right);\ } \ /**/ #define BOOST_PROTO_DEFINE_OPERATORS(TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_UNARY_OPERATOR(+, boost::proto::tag::unary_plus, TRAIT, DOMAIN, 0) \ BOOST_PROTO_DEFINE_UNARY_OPERATOR(-, boost::proto::tag::negate, TRAIT, DOMAIN, 0) \ BOOST_PROTO_DEFINE_UNARY_OPERATOR(*, boost::proto::tag::dereference, TRAIT, DOMAIN, 0) \ BOOST_PROTO_DEFINE_UNARY_OPERATOR(~, boost::proto::tag::complement, TRAIT, DOMAIN, 0) \ BOOST_PROTO_DEFINE_UNARY_OPERATOR(&, boost::proto::tag::address_of, TRAIT, DOMAIN, 0) \ BOOST_PROTO_DEFINE_UNARY_OPERATOR(!, boost::proto::tag::logical_not, TRAIT, DOMAIN, 0) \ BOOST_PROTO_DEFINE_UNARY_OPERATOR(++, boost::proto::tag::pre_inc, TRAIT, DOMAIN, 0) \ BOOST_PROTO_DEFINE_UNARY_OPERATOR(--, boost::proto::tag::pre_dec, TRAIT, DOMAIN, 0) \ BOOST_PROTO_DEFINE_UNARY_OPERATOR(++, boost::proto::tag::post_inc, TRAIT, DOMAIN, 1) \ BOOST_PROTO_DEFINE_UNARY_OPERATOR(--, boost::proto::tag::post_dec, TRAIT, DOMAIN, 1) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(<<, boost::proto::tag::shift_left, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(>>, boost::proto::tag::shift_right, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(*, boost::proto::tag::multiplies, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(/, boost::proto::tag::divides, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(%, boost::proto::tag::modulus, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(+, boost::proto::tag::plus, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(-, boost::proto::tag::minus, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(<, boost::proto::tag::less, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(>, boost::proto::tag::greater, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(<=, boost::proto::tag::less_equal, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(>=, boost::proto::tag::greater_equal, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(==, boost::proto::tag::equal_to, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(!=, boost::proto::tag::not_equal_to, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(||, boost::proto::tag::logical_or, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(&&, boost::proto::tag::logical_and, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(&, boost::proto::tag::bitwise_and, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(|, boost::proto::tag::bitwise_or, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(^, boost::proto::tag::bitwise_xor, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(BOOST_PP_COMMA(), boost::proto::tag::comma, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(->*, boost::proto::tag::mem_ptr, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(<<=, boost::proto::tag::shift_left_assign, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(>>=, boost::proto::tag::shift_right_assign, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(*=, boost::proto::tag::multiplies_assign, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(/=, boost::proto::tag::divides_assign, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(%=, boost::proto::tag::modulus_assign, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(+=, boost::proto::tag::plus_assign, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(-=, boost::proto::tag::minus_assign, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(&=, boost::proto::tag::bitwise_and_assign, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(|=, boost::proto::tag::bitwise_or_assign, TRAIT, DOMAIN) \ BOOST_PROTO_DEFINE_BINARY_OPERATOR(^=, boost::proto::tag::bitwise_xor_assign, TRAIT, DOMAIN) \ /**/ template struct is_extension : mpl::false_ {}; #ifndef BOOST_PROTO_BUILDING_DOCS namespace exops { BOOST_PROTO_DEFINE_OPERATORS(is_extension, default_domain) using proto::if_else; } #endif }} #endif