Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: /// \file fold.hpp Chris@16: /// Contains definition of the fold<> and reverse_fold<> transforms. Chris@16: // Chris@16: // Copyright 2008 Eric Niebler. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_PROTO_TRANSFORM_FOLD_HPP_EAN_11_04_2007 Chris@16: #define BOOST_PROTO_TRANSFORM_FOLD_HPP_EAN_11_04_2007 Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace proto Chris@16: { Chris@16: namespace detail Chris@16: { Chris@16: template Chris@16: struct as_callable Chris@16: { Chris@16: as_callable(Data d) Chris@16: : d_(d) Chris@16: {} Chris@16: Chris@16: template Chris@16: struct result; Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef Chris@16: typename when<_, Transform>::template impl::result_type Chris@16: type; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename when<_, Transform>::template impl::result_type Chris@16: operator ()(State const &s, Expr &e) const Chris@16: { Chris@16: return typename when<_, Transform>::template impl()(e, s, this->d_); Chris@16: } Chris@16: Chris@16: private: Chris@16: Data d_; Chris@16: }; Chris@16: Chris@16: template< Chris@16: typename State0 Chris@16: , typename Fun Chris@16: , typename Expr Chris@16: , typename State Chris@16: , typename Data Chris@16: , long Arity = arity_of::value Chris@16: > Chris@16: struct fold_impl Chris@16: {}; Chris@16: Chris@16: template< Chris@16: typename State0 Chris@16: , typename Fun Chris@16: , typename Expr Chris@16: , typename State Chris@16: , typename Data Chris@16: , long Arity = arity_of::value Chris@16: > Chris@16: struct reverse_fold_impl Chris@16: {}; Chris@16: Chris@16: #include Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: /// \brief A PrimitiveTransform that invokes the fusion::fold\<\> Chris@16: /// algorithm to accumulate Chris@16: template Chris@16: struct fold : transform > Chris@16: { Chris@16: template Chris@16: struct impl : transform_impl Chris@16: { Chris@16: /// \brief A Fusion sequence. Chris@16: typedef Chris@16: typename remove_reference< Chris@16: typename when<_, Sequence>::template impl::result_type Chris@16: >::type Chris@16: sequence; Chris@16: Chris@16: /// \brief An initial state for the fold. Chris@16: typedef Chris@16: typename remove_reference< Chris@16: typename when<_, State0>::template impl::result_type Chris@16: >::type Chris@16: state0; Chris@16: Chris@16: /// \brief fun(d)(e,s) == when\<_,Fun\>()(e,s,d) Chris@16: typedef Chris@16: detail::as_callable Chris@16: fun; Chris@16: Chris@16: typedef Chris@16: typename fusion::result_of::fold< Chris@16: sequence Chris@16: , state0 Chris@16: , fun Chris@16: >::type Chris@16: result_type; Chris@16: Chris@16: /// Let \c seq be when\<_, Sequence\>()(e, s, d), let Chris@16: /// \c state0 be when\<_, State0\>()(e, s, d), and Chris@16: /// let \c fun(d) be an object such that fun(d)(e, s) Chris@16: /// is equivalent to when\<_, Fun\>()(e, s, d). Then, this Chris@16: /// function returns fusion::fold(seq, state0, fun(d)). Chris@16: /// Chris@16: /// \param e The current expression Chris@16: /// \param s The current state Chris@16: /// \param d An arbitrary data Chris@16: result_type operator ()( Chris@16: typename impl::expr_param e Chris@16: , typename impl::state_param s Chris@16: , typename impl::data_param d Chris@16: ) const Chris@16: { Chris@16: typename when<_, Sequence>::template impl seq; Chris@16: detail::as_callable f(d); Chris@16: return fusion::fold( Chris@16: seq(e, s, d) Chris@16: , typename when<_, State0>::template impl()(e, s, d) Chris@16: , f Chris@16: ); Chris@16: } Chris@16: }; Chris@16: }; Chris@16: Chris@16: /// \brief A PrimitiveTransform that is the same as the Chris@16: /// fold\<\> transform, except that it folds Chris@16: /// back-to-front instead of front-to-back. Chris@16: template Chris@16: struct reverse_fold : transform > Chris@16: { Chris@16: template Chris@16: struct impl : transform_impl Chris@16: { Chris@16: /// \brief A Fusion sequence. Chris@16: typedef Chris@16: typename remove_reference< Chris@16: typename when<_, Sequence>::template impl::result_type Chris@16: >::type Chris@16: sequence; Chris@16: Chris@16: /// \brief An initial state for the fold. Chris@16: typedef Chris@16: typename remove_reference< Chris@16: typename when<_, State0>::template impl::result_type Chris@16: >::type Chris@16: state0; Chris@16: Chris@16: /// \brief fun(d)(e,s) == when\<_,Fun\>()(e,s,d) Chris@16: typedef Chris@16: detail::as_callable Chris@16: fun; Chris@16: Chris@16: typedef Chris@16: typename fusion::result_of::reverse_fold< Chris@16: sequence Chris@16: , state0 Chris@16: , fun Chris@16: >::type Chris@16: result_type; Chris@16: Chris@16: /// Let \c seq be when\<_, Sequence\>()(e, s, d), let Chris@16: /// \c state0 be when\<_, State0\>()(e, s, d), and Chris@16: /// let \c fun(d) be an object such that fun(d)(e, s) Chris@16: /// is equivalent to when\<_, Fun\>()(e, s, d). Then, this Chris@16: /// function returns fusion::fold(seq, state0, fun(d)). Chris@16: /// Chris@16: /// \param e The current expression Chris@16: /// \param s The current state Chris@16: /// \param d An arbitrary data Chris@16: result_type operator ()( Chris@16: typename impl::expr_param e Chris@16: , typename impl::state_param s Chris@16: , typename impl::data_param d Chris@16: ) const Chris@16: { Chris@16: typename when<_, Sequence>::template impl seq; Chris@16: detail::as_callable f(d); Chris@16: return fusion::reverse_fold( Chris@16: seq(e, s, d) Chris@16: , typename when<_, State0>::template impl()(e, s, d) Chris@16: , f Chris@16: ); Chris@16: } Chris@16: }; Chris@16: }; Chris@16: Chris@16: // This specialization is only for improved compile-time performance Chris@16: // in the commom case when the Sequence transform is \c proto::_. Chris@16: // Chris@16: /// INTERNAL ONLY Chris@16: /// Chris@16: template Chris@16: struct fold<_, State0, Fun> : transform > Chris@16: { Chris@16: template Chris@16: struct impl Chris@16: : detail::fold_impl Chris@16: {}; Chris@16: }; Chris@16: Chris@16: // This specialization is only for improved compile-time performance Chris@16: // in the commom case when the Sequence transform is \c proto::_. Chris@16: // Chris@16: /// INTERNAL ONLY Chris@16: /// Chris@16: template Chris@16: struct reverse_fold<_, State0, Fun> : transform > Chris@16: { Chris@16: template Chris@16: struct impl Chris@16: : detail::reverse_fold_impl Chris@16: {}; Chris@16: }; Chris@16: Chris@16: /// INTERNAL ONLY Chris@16: /// Chris@16: template Chris@16: struct is_callable > Chris@16: : mpl::true_ Chris@16: {}; Chris@16: Chris@16: /// INTERNAL ONLY Chris@16: /// Chris@16: template Chris@16: struct is_callable > Chris@16: : mpl::true_ Chris@16: {}; Chris@16: Chris@16: }} Chris@16: Chris@16: #endif