lambda.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 1,675 行 · 第 1/5 页

HPP
1,675
字号
#ifndef BOOST_PP_IS_ITERATING    #ifndef BOOST_LAMBDA_HPP_EAN_04_19_2008    #define BOOST_LAMBDA_HPP_EAN_04_19_2008    #define BOOST_MPL_LIMIT_METAFUNCTION_ARITY 10    #define BOOST_PROTO_MAX_ARITY 10    #define BOOST_PROTO_MAX_FUNCTION_CALL_ARITY 5    #include <iosfwd>    #include <typeinfo>    #include <algorithm>    #include <boost/ref.hpp>    #include <boost/assert.hpp>    #include <boost/mpl/or.hpp>    #include <boost/mpl/int.hpp>    #include <boost/mpl/void.hpp>    #include <boost/mpl/identity.hpp>    #include <boost/mpl/next_prior.hpp>    #include <boost/mpl/min_max.hpp>    #include <boost/mpl/assert.hpp>    #include <boost/mpl/apply_wrap.hpp>    #include <boost/preprocessor.hpp>    #include <boost/utility/enable_if.hpp>    #include <boost/utility/result_of.hpp>    #include <boost/fusion/include/vector.hpp>    #include <boost/type_traits/add_reference.hpp>    #include <boost/type_traits/remove_reference.hpp>    #include <boost/type_traits/remove_const.hpp>    #include <boost/type_traits/is_same.hpp>    #include <boost/type_traits/is_abstract.hpp>    #include <boost/type_traits/is_array.hpp>    #include <boost/type_traits/is_function.hpp>    #include <boost/proto/proto.hpp>    #ifndef BOOST_LAMBDA_MAX_ARITY    # define BOOST_LAMBDA_MAX_ARITY 3    #endif    #ifdef _MSC_VER    # pragma warning(push)    # pragma warning(disable: 4355) // 'this' : used in base member initializer list    # pragma warning(disable: 4065) // switch statement contains 'default' but no 'case' labels    #endif    namespace boost { namespace lambda    {        namespace tag        {            struct if_ {};            struct if_else_ {};            struct for_ {};            struct while_ {};            struct do_while_ {};            struct protect {};            struct try_ {};            struct throw_ {};            struct rethrow_ {};            struct switch_ {};            struct default_ {};            template<int I> struct case_ { BOOST_STATIC_CONSTANT(int, value = I); };            template<typename E> struct catch_ { typedef E exception_type; };            struct catch_all_ { typedef catch_all_ exception_type; };        };        template<typename Int>        struct placeholder        {            typedef typename Int::tag tag;            typedef typename Int::value_type value_type;            typedef placeholder<Int> type;            typedef placeholder<typename Int::next> next;            typedef placeholder<typename Int::prior> prior;            BOOST_STATIC_CONSTANT(value_type, value = Int::value);            friend std::ostream &operator<<(std::ostream &sout, placeholder)            {                return sout << "boost::lambda::_" << (Int::value+1);            }        };        struct exception_placeholder        {};        struct no_exception_type {};        no_exception_type const no_exception = {};        // Calculate the arity of a lambda expression        struct Arity          : proto::or_<                proto::when<proto::terminal<placeholder<proto::_> >, mpl::next<proto::_value>()>              , proto::when<proto::terminal<proto::_>, mpl::int_<0>()>              , proto::otherwise<proto::fold<proto::_, mpl::int_<0>(), mpl::max<proto::_state, Arity>()> >            >        {};        // True when a lambda expression can be applied with no arguments and        // without an active exception object        struct IsNullary          : proto::or_<                proto::when<proto::terminal<placeholder<proto::_> >, mpl::false_()>              , proto::when<proto::terminal<exception_placeholder>, mpl::false_()>              , proto::when<proto::terminal<proto::_>, mpl::true_()>              , proto::otherwise<proto::fold<proto::_, mpl::true_(), mpl::and_<proto::_state, IsNullary>()> >            >        {};                struct at : proto::callable        {            template<class Sig>            struct result;            template<class This, class Cont, class Int>            struct result<This(Cont, Int)>              : fusion::result_of::at<                    typename remove_reference<Cont>::type                  , typename remove_reference<Int>::type                >            {};            template<typename Cont, typename Int>            typename fusion::result_of::at<Cont, Int>::type            operator ()(Cont &cont, Int const &) const            {                return fusion::at<Int>(cont);            }        };        struct Eval;        struct EvalWhile : proto::transform<EvalWhile>        {            template<typename Expr, typename State, typename Data>            struct impl : proto::transform_impl<Expr, State, Data>            {                typedef mpl::void_ result_type;                result_type operator()(                    typename impl::expr_param expr                  , typename impl::state_param state                  , typename impl::data_param data                ) const                {                    while(Eval()(proto::left(expr), state, data))                    {                        Eval()(proto::right(expr), state, data);                    }                    return result_type();                }            };        };        struct EvalDoWhile : proto::transform<EvalDoWhile>        {            template<typename Expr, typename State, typename Data>            struct impl : proto::transform_impl<Expr, State, Data>            {                typedef mpl::void_ result_type;                result_type operator()(                    typename impl::expr_param expr                  , typename impl::state_param state                  , typename impl::data_param data                ) const                {                    do                    {                        Eval()(proto::child_c<0>(expr), state, data);                    }                    while(Eval()(proto::child_c<1>(expr), state, data));                    return result_type();                }            };        };        struct EvalFor : proto::transform<EvalFor>        {            template<typename Expr, typename State, typename Data>            struct impl : proto::transform_impl<Expr, State, Data>            {                typedef mpl::void_ result_type;                result_type operator()(                    typename impl::expr_param expr                  , typename impl::state_param state                  , typename impl::data_param data                ) const                {                    for(Eval()(proto::child_c<0>(expr), state, data)                      ; Eval()(proto::child_c<1>(expr), state, data)                      ; Eval()(proto::child_c<2>(expr), state, data))                    {                        Eval()(proto::child_c<3>(expr), state, data);                    }                    return result_type();                }            };        };        struct EvalIf : proto::transform<EvalIf>        {            template<typename Expr, typename State, typename Data>            struct impl : proto::transform_impl<Expr, State, Data>            {                typedef mpl::void_ result_type;                result_type operator()(                    typename impl::expr_param expr                  , typename impl::state_param state                  , typename impl::data_param data                ) const                {                    if(Eval()(proto::left(expr), state, data))                    {                        Eval()(proto::right(expr), state, data);                    }                    return result_type();                }            };        };        struct EvalIfElse : proto::transform<EvalIfElse>        {            template<typename Expr, typename State, typename Data>            struct impl : proto::transform_impl<Expr, State, Data>            {                typedef mpl::void_ result_type;                result_type operator()(                    typename impl::expr_param expr                  , typename impl::state_param state                  , typename impl::data_param data                ) const                {                    if(Eval()(proto::child_c<0>(expr), state, data))                    {                        Eval()(proto::child_c<1>(expr), state, data);                    }                    else                    {                        Eval()(proto::child_c<2>(expr), state, data);                    }                    return result_type();                }            };        };        struct EvalException : proto::transform<EvalException>        {            template<typename Expr, typename State, typename Data>            struct impl : proto::transform_impl<Expr, State, Data>            {                typedef typename remove_const<typename impl::state>::type result_type;                BOOST_MPL_ASSERT_NOT((is_same<result_type, no_exception_type>));                BOOST_MPL_ASSERT_NOT((is_same<result_type, tag::catch_all_>));                typename impl::state_param operator()(                    typename impl::expr_param                  , typename impl::state_param state                  , typename impl::data_param                ) const                {                    return state;                }            };        };        struct EvalSwitch : proto::transform<EvalSwitch>        {            template<typename Expr, typename State, typename Data, long Arity, typename BackTag>            struct impl2;                        #define M0(Z, N, DATA)                                                                  \                case proto::tag_of<typename proto::result_of::child_c<Expr, N>::type>::type::value: \                    Eval()(proto::child_c<N>(expr), state, data);                                   \                    break;                                                                          \                    /**/            #define M1(Z, N, DATA)                                                                  \            template<typename Expr, typename State, typename Data, typename BackTag>                \            struct impl2<Expr, State, Data, N, BackTag>                                             \              : proto::transform_impl<Expr, State, Data>                                            \            {                                                                                       \                typedef void result_type;                                                           \                                                                                                    \                void operator()(                                                                    \                    typename impl2::expr_param expr                                                 \                  , typename impl2::state_param state                                               \                  , typename impl2::data_param data                                                 \                ) const                                                                             \                {                                                                                   \                    switch(Eval()(proto::child_c<0>(expr), state, data))                            \                    {                                                                               \                        BOOST_PP_REPEAT_FROM_TO_ ## Z(1, N, M0, ~)                                  \                    default:                                                                        \                        break;                                                                      \                    }                                                                               \                }                                                                                   \            };                                                                                      \                                                                                                    \            template<typename Expr, typename State, typename Data>                                  \            struct impl2<Expr, State, Data, N, tag::default_>                                       \              : proto::transform_impl<Expr, State, Data>                                            \            {                                                                                       \                typedef void result_type;                                                           \                                                                                                    \                void operator()(                                                                    \                    typename impl2::expr_param expr                                                 \                  , typename impl2::state_param state                                               \                  , typename impl2::data_param data                                                 \                ) const                                                                             \                {                                                                                   \                    switch(Eval()(proto::child_c<0>(expr), state, data))                            \                    {                                                                               \                        BOOST_PP_REPEAT_FROM_TO_ ## Z(1, BOOST_PP_DEC(N), M0, ~)                    \                    default:;                                                                       \                        Eval()(proto::child_c<BOOST_PP_DEC(N)>(expr), state, data);                 \                        break;                                                                      \                    }                                                                               \                }                                                                                   \            };                                                                                      \            /**/            BOOST_PP_REPEAT_FROM_TO(2, BOOST_PP_INC(BOOST_PROTO_MAX_ARITY), M1, ~)            #undef M0            #undef M1            template<typename Expr, typename State, typename Data>            struct impl              : impl2<                    Expr                  , State                  , Data                  , proto::arity_of<Expr>::value                  , typename proto::tag_of<                        typename proto::result_of::child_c<

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?