123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- ///////////////////////////////////////////////////////////////////////////////
- /// \file default.hpp
- /// Contains definition of the _default transform, which gives operators their
- /// usual C++ meanings and uses Boost.Typeof to deduce return types.
- //
- // 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_TRANSFORM_DEFAULT_HPP_EAN_04_04_2008
- #define BOOST_PROTO_TRANSFORM_DEFAULT_HPP_EAN_04_04_2008
- #include <boost/preprocessor/iteration/iterate.hpp>
- #include <boost/preprocessor/repetition/repeat.hpp>
- #include <boost/preprocessor/arithmetic/add.hpp>
- #include <boost/preprocessor/arithmetic/sub.hpp>
- #include <boost/preprocessor/repetition/enum.hpp>
- #include <boost/preprocessor/repetition/enum_shifted.hpp>
- #include <boost/preprocessor/repetition/enum_shifted_params.hpp>
- #include <boost/ref.hpp>
- #include <boost/get_pointer.hpp>
- #include <boost/utility/enable_if.hpp>
- #include <boost/type_traits/is_member_pointer.hpp>
- #include <boost/type_traits/is_member_object_pointer.hpp>
- #include <boost/type_traits/is_member_function_pointer.hpp>
- #include <boost/proto/proto_fwd.hpp>
- #include <boost/proto/traits.hpp>
- #include <boost/proto/transform/impl.hpp>
- #include <boost/proto/transform/arg.hpp>
- #include <boost/proto/detail/decltype.hpp>
- namespace boost { namespace proto
- {
- namespace detail
- {
- template<typename Grammar, typename Tag>
- struct default_case
- : not_<_>
- {};
- template<typename Grammar>
- struct default_case<Grammar, tag::terminal>
- : when<terminal<_>, _value>
- {};
- template<typename Grammar>
- struct default_cases
- {
- template<typename Tag>
- struct case_
- : default_case<Grammar, Tag>
- {};
- };
- #define BOOST_PROTO_UNARY_DEFAULT_EVAL(OP, TAG, MAKE) \
- template<typename Grammar> \
- struct BOOST_PP_CAT(default_, TAG) \
- : transform<BOOST_PP_CAT(default_, TAG)<Grammar> > \
- { \
- template<typename Expr, typename State, typename Data> \
- struct impl \
- : transform_impl<Expr, State, Data> \
- { \
- private: \
- typedef typename result_of::child_c<Expr, 0>::type e0; \
- typedef typename Grammar::template impl<e0, State, Data>::result_type r0; \
- public: \
- BOOST_PROTO_DECLTYPE_(OP proto::detail::MAKE<r0>(), result_type) \
- result_type operator ()( \
- typename impl::expr_param e \
- , typename impl::state_param s \
- , typename impl::data_param d \
- ) const \
- { \
- typename Grammar::template impl<e0, State, Data> t0; \
- return OP t0(proto::child_c<0>(e), s, d); \
- } \
- }; \
- }; \
- \
- template<typename Grammar> \
- struct default_case<Grammar, tag::TAG> \
- : when<unary_expr<tag::TAG, Grammar>, BOOST_PP_CAT(default_, TAG)<Grammar> > \
- {}; \
- /**/
- #define BOOST_PROTO_BINARY_DEFAULT_EVAL(OP, TAG, LMAKE, RMAKE) \
- template<typename Grammar> \
- struct BOOST_PP_CAT(default_, TAG) \
- : transform<BOOST_PP_CAT(default_, TAG)<Grammar> > \
- { \
- template<typename Expr, typename State, typename Data> \
- struct impl \
- : transform_impl<Expr, State, Data> \
- { \
- private: \
- typedef typename result_of::child_c<Expr, 0>::type e0; \
- typedef typename result_of::child_c<Expr, 1>::type e1; \
- typedef typename Grammar::template impl<e0, State, Data>::result_type r0; \
- typedef typename Grammar::template impl<e1, State, Data>::result_type r1; \
- public: \
- BOOST_PROTO_DECLTYPE_( \
- proto::detail::LMAKE<r0>() OP proto::detail::RMAKE<r1>() \
- , result_type \
- ) \
- result_type operator ()( \
- typename impl::expr_param e \
- , typename impl::state_param s \
- , typename impl::data_param d \
- ) const \
- { \
- typename Grammar::template impl<e0, State, Data> t0; \
- typename Grammar::template impl<e1, State, Data> t1; \
- return t0(proto::child_c<0>(e), s, d) \
- OP t1(proto::child_c<1>(e), s, d); \
- } \
- }; \
- }; \
- \
- template<typename Grammar> \
- struct default_case<Grammar, tag::TAG> \
- : when<binary_expr<tag::TAG, Grammar, Grammar>, BOOST_PP_CAT(default_, TAG)<Grammar> > \
- {}; \
- /**/
- BOOST_PROTO_UNARY_DEFAULT_EVAL(+, unary_plus, make)
- BOOST_PROTO_UNARY_DEFAULT_EVAL(-, negate, make)
- BOOST_PROTO_UNARY_DEFAULT_EVAL(*, dereference, make)
- BOOST_PROTO_UNARY_DEFAULT_EVAL(~, complement, make)
- BOOST_PROTO_UNARY_DEFAULT_EVAL(&, address_of, make)
- BOOST_PROTO_UNARY_DEFAULT_EVAL(!, logical_not, make)
- BOOST_PROTO_UNARY_DEFAULT_EVAL(++, pre_inc, make_mutable)
- BOOST_PROTO_UNARY_DEFAULT_EVAL(--, pre_dec, make_mutable)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(<<, shift_left, make_mutable, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(>>, shift_right, make_mutable, make_mutable)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(*, multiplies, make, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(/, divides, make, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(%, modulus, make, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(+, plus, make, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(-, minus, make, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(<, less, make, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(>, greater, make, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(<=, less_equal, make, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(>=, greater_equal, make, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(==, equal_to, make, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(!=, not_equal_to, make, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(||, logical_or, make, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(&&, logical_and, make, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(&, bitwise_and, make, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(|, bitwise_or, make, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(^, bitwise_xor, make, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(=, assign, make_mutable, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(<<=, shift_left_assign, make_mutable, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(>>=, shift_right_assign, make_mutable, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(*=, multiplies_assign, make_mutable, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(/=, divides_assign, make_mutable, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(%=, modulus_assign, make_mutable, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(+=, plus_assign, make_mutable, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(-=, minus_assign, make_mutable, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(&=, bitwise_and_assign, make_mutable, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(|=, bitwise_or_assign, make_mutable, make)
- BOOST_PROTO_BINARY_DEFAULT_EVAL(^=, bitwise_xor_assign, make_mutable, make)
- #undef BOOST_PROTO_UNARY_DEFAULT_EVAL
- #undef BOOST_PROTO_BINARY_DEFAULT_EVAL
- /// INTERNAL ONLY
- template<typename Grammar, typename Expr, typename State, typename Data>
- struct is_member_function_invocation
- : is_member_function_pointer<
- typename uncvref<
- typename Grammar::template impl<
- typename result_of::child_c<Expr, 1>::type
- , State
- , Data
- >::result_type
- >::type
- >
- {};
- /// INTERNAL ONLY
- template<typename Grammar, typename Expr, typename State, typename Data, bool IsMemFunCall>
- struct default_mem_ptr_impl
- : transform_impl<Expr, State, Data>
- {
- private:
- typedef typename result_of::child_c<Expr, 0>::type e0;
- typedef typename result_of::child_c<Expr, 1>::type e1;
- typedef typename Grammar::template impl<e0, State, Data>::result_type r0;
- typedef typename Grammar::template impl<e1, State, Data>::result_type r1;
- public:
- typedef typename detail::mem_ptr_fun<r0, r1>::result_type result_type;
- result_type operator ()(
- typename default_mem_ptr_impl::expr_param e
- , typename default_mem_ptr_impl::state_param s
- , typename default_mem_ptr_impl::data_param d
- ) const
- {
- typename Grammar::template impl<e0, State, Data> t0;
- typename Grammar::template impl<e1, State, Data> t1;
- return detail::mem_ptr_fun<r0, r1>()(
- t0(proto::child_c<0>(e), s, d)
- , t1(proto::child_c<1>(e), s, d)
- );
- }
- };
- /// INTERNAL ONLY
- template<typename Grammar, typename Expr, typename State, typename Data>
- struct default_mem_ptr_impl<Grammar, Expr, State, Data, true>
- : transform_impl<Expr, State, Data>
- {
- private:
- typedef typename result_of::child_c<Expr, 0>::type e0;
- typedef typename result_of::child_c<Expr, 1>::type e1;
- typedef typename Grammar::template impl<e0, State, Data>::result_type r0;
- typedef typename Grammar::template impl<e1, State, Data>::result_type r1;
- public:
- typedef detail::memfun<r0, r1> result_type;
- result_type const operator ()(
- typename default_mem_ptr_impl::expr_param e
- , typename default_mem_ptr_impl::state_param s
- , typename default_mem_ptr_impl::data_param d
- ) const
- {
- typename Grammar::template impl<e0, State, Data> t0;
- typename Grammar::template impl<e1, State, Data> t1;
- return detail::memfun<r0, r1>(
- t0(proto::child_c<0>(e), s, d)
- , t1(proto::child_c<1>(e), s, d)
- );
- }
- };
- template<typename Grammar>
- struct default_mem_ptr
- : transform<default_mem_ptr<Grammar> >
- {
- template<typename Expr, typename State, typename Data>
- struct impl
- : default_mem_ptr_impl<
- Grammar
- , Expr
- , State
- , Data
- , is_member_function_invocation<Grammar, Expr, State, Data>::value
- >
- {};
- };
- template<typename Grammar>
- struct default_case<Grammar, tag::mem_ptr>
- : when<mem_ptr<Grammar, Grammar>, default_mem_ptr<Grammar> >
- {};
- template<typename Grammar>
- struct default_post_inc
- : transform<default_post_inc<Grammar> >
- {
- template<typename Expr, typename State, typename Data>
- struct impl
- : transform_impl<Expr, State, Data>
- {
- private:
- typedef typename result_of::child_c<Expr, 0>::type e0;
- typedef typename Grammar::template impl<e0, State, Data>::result_type r0;
- public:
- BOOST_PROTO_DECLTYPE_(proto::detail::make_mutable<r0>() ++, result_type)
- result_type operator ()(
- typename impl::expr_param e
- , typename impl::state_param s
- , typename impl::data_param d
- ) const
- {
- typename Grammar::template impl<e0, State, Data> t0;
- return t0(proto::child_c<0>(e), s, d) ++;
- }
- };
- };
- template<typename Grammar>
- struct default_case<Grammar, tag::post_inc>
- : when<post_inc<Grammar>, default_post_inc<Grammar> >
- {};
- template<typename Grammar>
- struct default_post_dec
- : transform<default_post_dec<Grammar> >
- {
- template<typename Expr, typename State, typename Data>
- struct impl
- : transform_impl<Expr, State, Data>
- {
- private:
- typedef typename result_of::child_c<Expr, 0>::type e0;
- typedef typename Grammar::template impl<e0, State, Data>::result_type r0;
- public:
- BOOST_PROTO_DECLTYPE_(proto::detail::make_mutable<r0>() --, result_type)
- result_type operator ()(
- typename impl::expr_param e
- , typename impl::state_param s
- , typename impl::data_param d
- ) const
- {
- typename Grammar::template impl<e0, State, Data> t0;
- return t0(proto::child_c<0>(e), s, d) --;
- }
- };
- };
- template<typename Grammar>
- struct default_case<Grammar, tag::post_dec>
- : when<post_dec<Grammar>, default_post_dec<Grammar> >
- {};
- template<typename Grammar>
- struct default_subscript
- : transform<default_subscript<Grammar> >
- {
- template<typename Expr, typename State, typename Data>
- struct impl
- : transform_impl<Expr, State, Data>
- {
- private:
- typedef typename result_of::child_c<Expr, 0>::type e0;
- typedef typename result_of::child_c<Expr, 1>::type e1;
- typedef typename Grammar::template impl<e0, State, Data>::result_type r0;
- typedef typename Grammar::template impl<e1, State, Data>::result_type r1;
- public:
- BOOST_PROTO_DECLTYPE_(
- proto::detail::make_subscriptable<r0>() [ proto::detail::make<r1>() ]
- , result_type
- )
- result_type operator ()(
- typename impl::expr_param e
- , typename impl::state_param s
- , typename impl::data_param d
- ) const
- {
- typename Grammar::template impl<e0, State, Data> t0;
- typename Grammar::template impl<e1, State, Data> t1;
- return t0(proto::child_c<0>(e), s, d) [
- t1(proto::child_c<1>(e), s, d) ];
- }
- };
- };
- template<typename Grammar>
- struct default_case<Grammar, tag::subscript>
- : when<subscript<Grammar, Grammar>, default_subscript<Grammar> >
- {};
- template<typename Grammar>
- struct default_if_else_
- {
- template<typename Expr, typename State, typename Data>
- struct impl
- : transform_impl<Expr, State, Data>
- {
- private:
- typedef typename result_of::child_c<Expr, 0>::type e0;
- typedef typename result_of::child_c<Expr, 1>::type e1;
- typedef typename result_of::child_c<Expr, 2>::type e2;
- typedef typename Grammar::template impl<e0, State, Data>::result_type r0;
- typedef typename Grammar::template impl<e1, State, Data>::result_type r1;
- typedef typename Grammar::template impl<e2, State, Data>::result_type r2;
- public:
- BOOST_PROTO_DECLTYPE_(
- proto::detail::make<r0>()
- ? proto::detail::make<r1>()
- : proto::detail::make<r2>()
- , result_type
- )
- result_type operator ()(
- typename impl::expr_param e
- , typename impl::state_param s
- , typename impl::data_param d
- ) const
- {
- typename Grammar::template impl<e0, State, Data> t0;
- typename Grammar::template impl<e1, State, Data> t1;
- typename Grammar::template impl<e2, State, Data> t2;
- return t0(proto::child_c<0>(e), s, d)
- ? t1(proto::child_c<1>(e), s, d)
- : t2(proto::child_c<2>(e), s, d);
- }
- };
- };
- template<typename Grammar>
- struct default_case<Grammar, tag::if_else_>
- : when<if_else_<Grammar, Grammar, Grammar>, default_if_else_<Grammar> >
- {};
- template<typename Grammar>
- struct default_comma
- : transform<default_comma<Grammar> >
- {
- template<typename Expr, typename State, typename Data>
- struct impl
- : transform_impl<Expr, State, Data>
- {
- private:
- typedef typename result_of::child_c<Expr, 0>::type e0;
- typedef typename result_of::child_c<Expr, 1>::type e1;
- typedef typename Grammar::template impl<e0, State, Data>::result_type r0;
- typedef typename Grammar::template impl<e1, State, Data>::result_type r1;
- public:
- typedef typename proto::detail::comma_result<r0, r1>::type result_type;
- result_type operator ()(
- typename impl::expr_param e
- , typename impl::state_param s
- , typename impl::data_param d
- ) const
- {
- typename Grammar::template impl<e0, State, Data> t0;
- typename Grammar::template impl<e1, State, Data> t1;
- return t0(proto::child_c<0>(e), s, d)
- , t1(proto::child_c<1>(e), s, d);
- }
- };
- };
- template<typename Grammar>
- struct default_case<Grammar, tag::comma>
- : when<comma<Grammar, Grammar>, default_comma<Grammar> >
- {};
- template<typename Grammar, typename Expr, typename State, typename Data, long Arity>
- struct default_function_impl;
- template<typename Grammar>
- struct default_function
- : transform<default_function<Grammar> >
- {
- template<typename Expr, typename State, typename Data>
- struct impl
- : default_function_impl<
- Grammar
- , Expr
- , State
- , Data
- , transform_impl<Expr, State, Data>::expr::proto_arity_c
- >
- {};
- };
- template<typename Grammar>
- struct default_case<Grammar, tag::function>
- : when<function<Grammar, vararg<Grammar> >, default_function<Grammar> >
- {};
- #define BOOST_PROTO_DEFAULT_EVAL_TYPE(Z, N, DATA) \
- typedef \
- typename result_of::child_c<DATA, N>::type \
- BOOST_PP_CAT(e, N); \
- \
- typedef \
- typename Grammar::template impl<BOOST_PP_CAT(e, N), State, Data>::result_type \
- BOOST_PP_CAT(r, N); \
- /**/
- #define BOOST_PROTO_DEFAULT_EVAL(Z, N, DATA) \
- typename Grammar::template impl<BOOST_PP_CAT(e, N), State, Data>()( \
- proto::child_c<N>(DATA), s, d \
- ) \
- /**/
- template<typename Grammar, typename Expr, typename State, typename Data>
- struct default_function_impl<Grammar, Expr, State, Data, 1>
- : transform_impl<Expr, State, Data>
- {
- BOOST_PROTO_DEFAULT_EVAL_TYPE(~, 0, Expr)
- typedef
- typename proto::detail::result_of_fixup<r0>::type
- function_type;
- typedef
- typename BOOST_PROTO_RESULT_OF<function_type()>::type
- result_type;
- result_type operator ()(
- typename default_function_impl::expr_param e
- , typename default_function_impl::state_param s
- , typename default_function_impl::data_param d
- ) const
- {
- return BOOST_PROTO_DEFAULT_EVAL(~, 0, e)();
- }
- };
- template<typename Grammar, typename Expr, typename State, typename Data>
- struct default_function_impl<Grammar, Expr, State, Data, 2>
- : transform_impl<Expr, State, Data>
- {
- BOOST_PROTO_DEFAULT_EVAL_TYPE(~, 0, Expr)
- BOOST_PROTO_DEFAULT_EVAL_TYPE(~, 1, Expr)
- typedef
- typename proto::detail::result_of_fixup<r0>::type
- function_type;
- typedef
- typename detail::result_of_<function_type(r1)>::type
- result_type;
- result_type operator ()(
- typename default_function_impl::expr_param e
- , typename default_function_impl::state_param s
- , typename default_function_impl::data_param d
- ) const
- {
- return this->invoke(
- e
- , s
- , d
- , is_member_function_pointer<function_type>()
- , is_member_object_pointer<function_type>()
- );
- }
- private:
- result_type invoke(
- typename default_function_impl::expr_param e
- , typename default_function_impl::state_param s
- , typename default_function_impl::data_param d
- , mpl::false_
- , mpl::false_
- ) const
- {
- return BOOST_PROTO_DEFAULT_EVAL(~, 0, e)(BOOST_PROTO_DEFAULT_EVAL(~, 1, e));
- }
- result_type invoke(
- typename default_function_impl::expr_param e
- , typename default_function_impl::state_param s
- , typename default_function_impl::data_param d
- , mpl::true_
- , mpl::false_
- ) const
- {
- BOOST_PROTO_USE_GET_POINTER();
- typedef typename detail::class_member_traits<function_type>::class_type class_type;
- return (
- BOOST_PROTO_GET_POINTER(class_type, (BOOST_PROTO_DEFAULT_EVAL(~, 1, e))) ->*
- BOOST_PROTO_DEFAULT_EVAL(~, 0, e)
- )();
- }
- result_type invoke(
- typename default_function_impl::expr_param e
- , typename default_function_impl::state_param s
- , typename default_function_impl::data_param d
- , mpl::false_
- , mpl::true_
- ) const
- {
- BOOST_PROTO_USE_GET_POINTER();
- typedef typename detail::class_member_traits<function_type>::class_type class_type;
- return (
- BOOST_PROTO_GET_POINTER(class_type, (BOOST_PROTO_DEFAULT_EVAL(~, 1, e))) ->*
- BOOST_PROTO_DEFAULT_EVAL(~, 0, e)
- );
- }
- };
- #include <boost/proto/transform/detail/default_function_impl.hpp>
- #undef BOOST_PROTO_DEFAULT_EVAL_TYPE
- #undef BOOST_PROTO_DEFAULT_EVAL
- }
- template<typename Grammar /*= detail::_default*/>
- struct _default
- : switch_<detail::default_cases<Grammar> >
- {};
- template<typename Grammar>
- struct is_callable<_default<Grammar> >
- : mpl::true_
- {};
- namespace detail
- {
- // Loopy indirection that allows proto::_default<> to be
- // used without specifying a Grammar argument.
- struct _default
- : proto::_default<>
- {};
- }
- }}
- #endif
|