/////////////////////////////////////////////////////////////////////////////// /// \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 #include #include #include #include namespace boost { namespace proto { namespace detail { template struct as_callable { as_callable(Data d) : d_(d) {} template struct result; template struct result { typedef typename when<_, Transform>::template impl::result_type type; }; template typename when<_, Transform>::template impl::result_type operator ()(State const &s, Expr &e) const { return typename when<_, Transform>::template impl()(e, s, this->d_); } private: Data d_; }; template< typename State0 , typename Fun , typename Expr , typename State , typename Data , long Arity = arity_of::value > struct fold_impl {}; template< typename State0 , typename Fun , typename Expr , typename State , typename Data , long Arity = arity_of::value > struct reverse_fold_impl {}; #include } // namespace detail /// \brief A PrimitiveTransform that invokes the fusion::fold\<\> /// algorithm to accumulate template struct fold : transform > { template struct impl : transform_impl { /// \brief A Fusion sequence. typedef typename remove_reference< typename when<_, Sequence>::template impl::result_type >::type sequence; /// \brief An initial state for the fold. typedef typename remove_reference< typename when<_, State0>::template impl::result_type >::type state0; /// \brief fun(d)(e,s) == when\<_,Fun\>()(e,s,d) typedef detail::as_callable fun; typedef typename fusion::result_of::fold< sequence , state0 , fun >::type result_type; /// Let \c seq be when\<_, Sequence\>()(e, s, d), let /// \c state0 be when\<_, State0\>()(e, s, d), and /// let \c fun(d) be an object such that fun(d)(e, s) /// is equivalent to when\<_, Fun\>()(e, s, d). Then, this /// function returns fusion::fold(seq, state0, fun(d)). /// /// \param e The current expression /// \param s The current state /// \param d An arbitrary data result_type operator ()( typename impl::expr_param e , typename impl::state_param s , typename impl::data_param d ) const { typename when<_, Sequence>::template impl seq; detail::as_callable f(d); return fusion::fold( seq(e, s, d) , typename when<_, State0>::template impl()(e, s, d) , f ); } }; }; /// \brief A PrimitiveTransform that is the same as the /// fold\<\> transform, except that it folds /// back-to-front instead of front-to-back. template struct reverse_fold : transform > { template struct impl : transform_impl { /// \brief A Fusion sequence. typedef typename remove_reference< typename when<_, Sequence>::template impl::result_type >::type sequence; /// \brief An initial state for the fold. typedef typename remove_reference< typename when<_, State0>::template impl::result_type >::type state0; /// \brief fun(d)(e,s) == when\<_,Fun\>()(e,s,d) typedef detail::as_callable fun; typedef typename fusion::result_of::reverse_fold< sequence , state0 , fun >::type result_type; /// Let \c seq be when\<_, Sequence\>()(e, s, d), let /// \c state0 be when\<_, State0\>()(e, s, d), and /// let \c fun(d) be an object such that fun(d)(e, s) /// is equivalent to when\<_, Fun\>()(e, s, d). Then, this /// function returns fusion::fold(seq, state0, fun(d)). /// /// \param e The current expression /// \param s The current state /// \param d An arbitrary data result_type operator ()( typename impl::expr_param e , typename impl::state_param s , typename impl::data_param d ) const { typename when<_, Sequence>::template impl seq; detail::as_callable f(d); return fusion::reverse_fold( seq(e, s, d) , typename when<_, State0>::template impl()(e, s, d) , f ); } }; }; // 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> : transform > { template struct impl : detail::fold_impl {}; }; // 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> : transform > { template struct impl : detail::reverse_fold_impl {}; }; /// INTERNAL ONLY /// template struct is_callable > : mpl::true_ {}; /// INTERNAL ONLY /// template struct is_callable > : mpl::true_ {}; }} #endif