#ifndef BOOST_PP_IS_ITERATING /////////////////////////////////////////////////////////////////////////////// /// \file fold.hpp /// Contains definition of the fold<> and reverse_fold<> transforms. // // 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_FOLD_HPP_EAN_11_04_2007 #define BOOST_PROTO_TRANSFORM_FOLD_HPP_EAN_11_04_2007 #include #include #include #include #include #include #include #if BOOST_VERSION >= 103500 #include #else #include #endif #include #include #include #include #include namespace boost { namespace proto { namespace transform { namespace detail { template struct as_callable { as_callable(Visitor &v) : v_(v) {} template struct result; template struct result { typedef typename when<_, Transform>::template result::type type; }; #if BOOST_VERSION < 103500 template struct apply : result {}; #endif template typename when<_, Transform>::template result::type operator ()(Expr const &expr, State const &state) const { return when<_, Transform>()(expr, state, this->v_); } private: Visitor &v_; }; #if BOOST_VERSION < 103500 template struct as_fusion_sequence_type { typedef Sequence const type; }; template Sequence const &as_fusion_sequence(Sequence const &sequence, ...) { return sequence; } template struct as_fusion_sequence_type { typedef typename Sequence::proto_base_expr const type; }; template typename Sequence::proto_base_expr const &as_fusion_sequence(Sequence const &sequence, int) { return sequence.proto_base(); } #define BOOST_PROTO_AS_FUSION_SEQUENCE_TYPE(X) typename detail::as_fusion_sequence_type::type #define BOOST_PROTO_AS_FUSION_SEQUENCE(X) detail::as_fusion_sequence(X, 0) #else #define BOOST_PROTO_AS_FUSION_SEQUENCE_TYPE(X) X #define BOOST_PROTO_AS_FUSION_SEQUENCE(X) X #endif template struct fold_impl {}; template struct reverse_fold_impl {}; #define BOOST_PROTO_ARG_N_TYPE(n)\ BOOST_PP_CAT(proto_arg, n)\ /**/ #define BOOST_PROTO_FOLD_STATE_TYPE(z, n, data)\ typedef\ typename when<_, Fun>::template result::type\ BOOST_PP_CAT(state, BOOST_PP_INC(n));\ /**/ #define BOOST_PROTO_FOLD_STATE(z, n, data)\ BOOST_PP_CAT(state, BOOST_PP_INC(n)) const &BOOST_PP_CAT(s, BOOST_PP_INC(n)) =\ when<_, Fun>()(expr.BOOST_PP_CAT(arg, n).proto_base(), BOOST_PP_CAT(s, n), visitor);\ /**/ #define BOOST_PROTO_REVERSE_FOLD_STATE_TYPE(z, n, data)\ typedef\ typename when<_, Fun>::template result::type\ BOOST_PP_CAT(state, BOOST_PP_SUB(data, BOOST_PP_INC(n)));\ /**/ #define BOOST_PROTO_REVERSE_FOLD_STATE(z, n, data)\ BOOST_PP_CAT(state, BOOST_PP_SUB(data, BOOST_PP_INC(n))) const &BOOST_PP_CAT(s, BOOST_PP_SUB(data, BOOST_PP_INC(n))) =\ when<_, Fun>()(expr.BOOST_PP_CAT(arg, BOOST_PP_SUB(data, BOOST_PP_INC(n))).proto_base(), BOOST_PP_CAT(s, BOOST_PP_SUB(data, n)), visitor);\ /**/ #define BOOST_PP_ITERATION_PARAMS_1 (3, (1, BOOST_PROTO_MAX_ARITY, )) #include BOOST_PP_ITERATE() #undef BOOST_PROTO_REVERSE_FOLD_STATE #undef BOOST_PROTO_REVERSE_FOLD_STATE_TYPE #undef BOOST_PROTO_FOLD_STATE #undef BOOST_PROTO_FOLD_STATE_TYPE #undef BOOST_PROTO_ARG_N_TYPE } // namespace detail /// \brief A PrimitiveTransform that invokes the fusion::fold\<\> /// algorithm to accumulate template struct fold : proto::callable { template struct result; template struct result { /// \brief A Fusion sequence. typedef typename when<_, Sequence>::template result::type sequence; /// \brief An initial state for the fold. typedef typename when<_, State0>::template result::type state0; /// \brief fun(v)(e,s) == when\<_,Fun\>()(e,s,v) typedef detail::as_callable fun; typedef typename fusion::BOOST_PROTO_FUSION_RESULT_OF::fold< BOOST_PROTO_AS_FUSION_SEQUENCE_TYPE(sequence) , state0 , fun >::type type; }; /// Let \c seq be when\<_, Sequence\>()(expr, state, visitor), let /// \c state0 be when\<_, State0\>()(expr, state, visitor), and /// let \c fun(visitor) be an object such that fun(visitor)(expr, state) /// is equivalent to when\<_, Fun\>()(expr, state, visitor). Then, this /// function returns fusion::fold(seq, state0, fun(visitor)). /// /// \param expr The current expression /// \param state The current state /// \param visitor An arbitrary visitor template typename result::type operator ()(Expr const &expr, State const &state, Visitor &visitor) const { when<_, Sequence> sequence; detail::as_callable fun(visitor); return fusion::fold( BOOST_PROTO_AS_FUSION_SEQUENCE(sequence(expr, state, visitor)) , when<_, State0>()(expr, state, visitor) , fun ); } }; /// \brief A PrimitiveTransform that is the same as the /// fold\<\> transform, except that it folds /// back-to-front instead of front-to-back. It uses /// the \c _reverse callable PolymorphicFunctionObject /// to create a fusion::reverse_view\<\> of the /// sequence before invoking fusion::fold\<\>. template struct reverse_fold : fold, State0, Fun> {}; // This specialization is only for improved compile-time performance // in the commom case when the Sequence transform is \c proto::_. // /// INTERNAL ONLY /// template struct fold<_, State0, Fun> : proto::callable { template struct result; template struct result { typedef typename detail::fold_impl< Fun , typename Expr::proto_base_expr , typename when<_, State0>::template result::type , Visitor >::type type; }; template typename result::type operator ()(Expr const &expr, State const &state, Visitor &visitor) const { typedef detail::fold_impl< Fun , typename Expr::proto_base_expr , typename when<_, State0>::template result::type , Visitor > impl; return impl::call( expr.proto_base() , when<_, State0>()(expr, state, visitor) , visitor ); } }; // This specialization is only for improved compile-time performance // in the commom case when the Sequence transform is \c proto::_. // /// INTERNAL ONLY /// template struct reverse_fold<_, State0, Fun> : proto::callable { template struct result; template struct result { typedef typename detail::reverse_fold_impl< Fun , typename Expr::proto_base_expr , typename when<_, State0>::template result::type , Visitor >::type type; }; template typename result::type operator ()(Expr const &expr, State const &state, Visitor &visitor) const { typedef detail::reverse_fold_impl< Fun , typename Expr::proto_base_expr , typename when<_, State0>::template result::type , Visitor > impl; return impl::call( expr.proto_base() , when<_, State0>()(expr, state, visitor) , visitor ); } }; } /// INTERNAL ONLY /// template struct is_callable > : mpl::true_ {}; /// INTERNAL ONLY /// template struct is_callable > : mpl::true_ {}; }} #endif #else #define N BOOST_PP_ITERATION() template struct fold_impl { BOOST_PP_REPEAT(N, BOOST_PROTO_FOLD_STATE_TYPE, N) typedef BOOST_PP_CAT(state, N) type; static type call(Expr const &expr, state0 const &s0, Visitor &visitor) { BOOST_PP_REPEAT(N, BOOST_PROTO_FOLD_STATE, N) return BOOST_PP_CAT(s, N); } }; template struct reverse_fold_impl { BOOST_PP_REPEAT(N, BOOST_PROTO_REVERSE_FOLD_STATE_TYPE, N) typedef state0 type; static type call(Expr const &expr, BOOST_PP_CAT(state, N) const &BOOST_PP_CAT(s, N), Visitor &visitor) { BOOST_PP_REPEAT(N, BOOST_PROTO_REVERSE_FOLD_STATE, N) return s0; } }; #undef N #endif