foldl.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #ifndef BOOST_METAPARSE_V1_FOLDL_HPP
  2. #define BOOST_METAPARSE_V1_FOLDL_HPP
  3. // Copyright Abel Sinkovics (abel@sinkovics.hu) 2011.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <boost/metaparse/v1/accept.hpp>
  8. #include <boost/metaparse/v1/is_error.hpp>
  9. #include <boost/metaparse/v1/get_position.hpp>
  10. #include <boost/metaparse/v1/get_result.hpp>
  11. #include <boost/metaparse/v1/get_remaining.hpp>
  12. #include <boost/mpl/eval_if.hpp>
  13. namespace boost
  14. {
  15. namespace metaparse
  16. {
  17. namespace v1
  18. {
  19. template <class P, class State, class ForwardOp>
  20. struct foldl
  21. {
  22. private:
  23. template <class Res>
  24. struct apply_unchecked :
  25. // foldl never returns error
  26. // I need to use apply_wrap, and not apply, because apply would
  27. // build a metafunction class from foldl<P, State, ForwardOp>
  28. // when ForwardOp is a lambda expression.
  29. foldl<
  30. P,
  31. typename ForwardOp::template apply<
  32. typename State::type,
  33. typename get_result<Res>::type
  34. >,
  35. ForwardOp
  36. >::template apply<
  37. typename get_remaining<Res>::type,
  38. typename get_position<Res>::type
  39. >
  40. {};
  41. template <class S, class Pos>
  42. struct next_iteration : accept<typename State::type, S, Pos> {};
  43. public:
  44. typedef foldl type;
  45. template <class S, class Pos>
  46. struct apply :
  47. boost::mpl::eval_if<
  48. typename is_error<typename P::template apply<S, Pos> >::type,
  49. next_iteration<S, Pos>,
  50. apply_unchecked<typename P::template apply<S, Pos> >
  51. >
  52. {};
  53. };
  54. }
  55. }
  56. }
  57. #endif