fold_impl.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #if !defined(BOOST_PROTO_DONT_USE_PREPROCESSED_FILES)
  2. #include <boost/proto/transform/detail/preprocessed/fold_impl.hpp>
  3. #elif !defined(BOOST_PP_IS_ITERATING)
  4. #define BOOST_PROTO_CHILD_N_TYPE(N) \
  5. BOOST_PP_CAT(proto_child, N) \
  6. /**/
  7. #define BOOST_PROTO_FOLD_STATE_TYPE(Z, N, DATA) \
  8. typedef \
  9. typename when<_, Fun>::template impl< \
  10. typename result_of::child_c<Expr, N>::type \
  11. , BOOST_PP_CAT(state, N) \
  12. , Data \
  13. >::result_type \
  14. BOOST_PP_CAT(state, BOOST_PP_INC(N)); \
  15. /**/
  16. #define BOOST_PROTO_FOLD_STATE(Z, N, DATA) \
  17. BOOST_PP_CAT(state, BOOST_PP_INC(N)) \
  18. BOOST_PP_CAT(s, BOOST_PP_INC(N)) \
  19. = typename when<_, Fun>::template impl< \
  20. typename result_of::child_c<Expr, N>::type \
  21. , BOOST_PP_CAT(state, N) \
  22. , Data \
  23. >()( \
  24. proto::child_c<N>(e) \
  25. , BOOST_PP_CAT(s, N) \
  26. , d \
  27. ); \
  28. /**/
  29. #define BOOST_PROTO_REVERSE_FOLD_STATE_TYPE(Z, N, DATA) \
  30. typedef \
  31. typename when<_, Fun>::template impl< \
  32. typename result_of::child_c< \
  33. Expr \
  34. , BOOST_PP_SUB(DATA, BOOST_PP_INC(N)) \
  35. >::type \
  36. , BOOST_PP_CAT(state, BOOST_PP_SUB(DATA, N)) \
  37. , Data \
  38. >::result_type \
  39. BOOST_PP_CAT(state, BOOST_PP_SUB(DATA, BOOST_PP_INC(N))); \
  40. /**/
  41. #define BOOST_PROTO_REVERSE_FOLD_STATE(Z, N, DATA) \
  42. BOOST_PP_CAT(state, BOOST_PP_SUB(DATA, BOOST_PP_INC(N))) \
  43. BOOST_PP_CAT(s, BOOST_PP_SUB(DATA, BOOST_PP_INC(N))) \
  44. = typename when<_, Fun>::template impl< \
  45. typename result_of::child_c< \
  46. Expr \
  47. , BOOST_PP_SUB(DATA, BOOST_PP_INC(N)) \
  48. >::type \
  49. , BOOST_PP_CAT(state, BOOST_PP_SUB(DATA, N)) \
  50. , Data \
  51. >()( \
  52. proto::child_c<BOOST_PP_SUB(DATA, BOOST_PP_INC(N))>(e) \
  53. , BOOST_PP_CAT(s, BOOST_PP_SUB(DATA, N)) \
  54. , d \
  55. ); \
  56. /**/
  57. #if defined(__WAVE__) && defined(BOOST_PROTO_CREATE_PREPROCESSED_FILES)
  58. #pragma wave option(preserve: 2, line: 0, output: "preprocessed/fold_impl.hpp")
  59. #endif
  60. ///////////////////////////////////////////////////////////////////////////////
  61. /// \file fold_impl.hpp
  62. /// Contains definition of fold_impl<> and reverse_fold_impl<> templates.
  63. //
  64. // Copyright 2008 Eric Niebler. Distributed under the Boost
  65. // Software License, Version 1.0. (See accompanying file
  66. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  67. #if defined(__WAVE__) && defined(BOOST_PROTO_CREATE_PREPROCESSED_FILES)
  68. #pragma wave option(preserve: 1)
  69. #endif
  70. #define BOOST_PP_ITERATION_PARAMS_1 \
  71. (3, (1, BOOST_PROTO_MAX_ARITY, <boost/proto/transform/detail/fold_impl.hpp>))
  72. #include BOOST_PP_ITERATE()
  73. #if defined(__WAVE__) && defined(BOOST_PROTO_CREATE_PREPROCESSED_FILES)
  74. #pragma wave option(output: null)
  75. #endif
  76. #undef BOOST_PROTO_REVERSE_FOLD_STATE
  77. #undef BOOST_PROTO_REVERSE_FOLD_STATE_TYPE
  78. #undef BOOST_PROTO_FOLD_STATE
  79. #undef BOOST_PROTO_FOLD_STATE_TYPE
  80. #undef BOOST_PROTO_CHILD_N_TYPE
  81. #else
  82. #define N BOOST_PP_ITERATION()
  83. template<typename State0, typename Fun, typename Expr, typename State, typename Data>
  84. struct fold_impl<State0, Fun, Expr, State, Data, N>
  85. : transform_impl<Expr, State, Data>
  86. {
  87. typedef typename when<_, State0>::template impl<Expr, State, Data>::result_type state0;
  88. BOOST_PP_REPEAT(N, BOOST_PROTO_FOLD_STATE_TYPE, N)
  89. typedef BOOST_PP_CAT(state, N) result_type;
  90. result_type operator ()(
  91. typename fold_impl::expr_param e
  92. , typename fold_impl::state_param s
  93. , typename fold_impl::data_param d
  94. ) const
  95. {
  96. state0 s0 =
  97. typename when<_, State0>::template impl<Expr, State, Data>()(e, s, d);
  98. BOOST_PP_REPEAT(N, BOOST_PROTO_FOLD_STATE, N)
  99. return BOOST_PP_CAT(s, N);
  100. }
  101. };
  102. template<typename State0, typename Fun, typename Expr, typename State, typename Data>
  103. struct reverse_fold_impl<State0, Fun, Expr, State, Data, N>
  104. : transform_impl<Expr, State, Data>
  105. {
  106. typedef typename when<_, State0>::template impl<Expr, State, Data>::result_type BOOST_PP_CAT(state, N);
  107. BOOST_PP_REPEAT(N, BOOST_PROTO_REVERSE_FOLD_STATE_TYPE, N)
  108. typedef state0 result_type;
  109. result_type operator ()(
  110. typename reverse_fold_impl::expr_param e
  111. , typename reverse_fold_impl::state_param s
  112. , typename reverse_fold_impl::data_param d
  113. ) const
  114. {
  115. BOOST_PP_CAT(state, N) BOOST_PP_CAT(s, N) =
  116. typename when<_, State0>::template impl<Expr, State, Data>()(e, s, d);
  117. BOOST_PP_REPEAT(N, BOOST_PROTO_REVERSE_FOLD_STATE, N)
  118. return s0;
  119. }
  120. };
  121. #undef N
  122. #endif