File indexing completed on 2025-01-18 09:38:15
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_HOF_GUARD_FOLD_H
0009 #define BOOST_HOF_GUARD_FOLD_H
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080 #include <boost/hof/detail/callable_base.hpp>
0081 #include <boost/hof/detail/delegate.hpp>
0082 #include <boost/hof/detail/compressed_pair.hpp>
0083 #include <boost/hof/detail/move.hpp>
0084 #include <boost/hof/detail/make.hpp>
0085 #include <boost/hof/detail/static_const_var.hpp>
0086
0087 namespace boost { namespace hof { namespace detail {
0088
0089 struct v_fold
0090 {
0091 BOOST_HOF_RETURNS_CLASS(v_fold);
0092 template<class F, class State, class T, class... Ts>
0093 constexpr BOOST_HOF_SFINAE_MANUAL_RESULT(const v_fold&, id_<const F&>, result_of<const F&, id_<State>, id_<T>>, id_<Ts>...)
0094 operator()(const F& f, State&& state, T&& x, Ts&&... xs) const BOOST_HOF_SFINAE_MANUAL_RETURNS
0095 (
0096 (*BOOST_HOF_CONST_THIS)(f, f(BOOST_HOF_FORWARD(State)(state), BOOST_HOF_FORWARD(T)(x)), BOOST_HOF_FORWARD(Ts)(xs)...)
0097 );
0098
0099 template<class F, class State>
0100 constexpr State operator()(const F&, State&& state) const noexcept
0101 {
0102 return BOOST_HOF_FORWARD(State)(state);
0103 }
0104 };
0105
0106 }
0107
0108 template<class F, class State=void>
0109 struct fold_adaptor
0110 : detail::compressed_pair<detail::callable_base<F>, State>
0111 {
0112 typedef detail::compressed_pair<detail::callable_base<F>, State> base_type;
0113 BOOST_HOF_INHERIT_CONSTRUCTOR(fold_adaptor, base_type)
0114
0115 template<class... Ts>
0116 constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const noexcept
0117 {
0118 return this->first(xs...);
0119 }
0120
0121 template<class... Ts>
0122 constexpr State get_state(Ts&&... xs) const noexcept
0123 {
0124 return this->second(xs...);
0125 }
0126
0127 BOOST_HOF_RETURNS_CLASS(fold_adaptor);
0128
0129 template<class... Ts>
0130 constexpr BOOST_HOF_SFINAE_RESULT(detail::v_fold, id_<const detail::callable_base<F>&>, id_<State>, id_<Ts>...)
0131 operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
0132 (
0133 detail::v_fold()(
0134 BOOST_HOF_MANGLE_CAST(const detail::callable_base<F>&)(BOOST_HOF_CONST_THIS->base_function(xs...)),
0135 BOOST_HOF_MANGLE_CAST(State)(BOOST_HOF_CONST_THIS->get_state(xs...)),
0136 BOOST_HOF_FORWARD(Ts)(xs)...
0137 )
0138 )
0139 };
0140
0141
0142 template<class F>
0143 struct fold_adaptor<F, void>
0144 : detail::callable_base<F>
0145 {
0146 BOOST_HOF_INHERIT_CONSTRUCTOR(fold_adaptor, detail::callable_base<F>)
0147
0148 template<class... Ts>
0149 constexpr const detail::callable_base<F>& base_function(Ts&&... xs) const noexcept
0150 {
0151 return boost::hof::always_ref(*this)(xs...);
0152 }
0153
0154 BOOST_HOF_RETURNS_CLASS(fold_adaptor);
0155
0156 template<class... Ts>
0157 constexpr BOOST_HOF_SFINAE_RESULT(detail::v_fold, id_<const detail::callable_base<F>&>, id_<Ts>...)
0158 operator()(Ts&&... xs) const BOOST_HOF_SFINAE_RETURNS
0159 (
0160 detail::v_fold()(
0161 BOOST_HOF_MANGLE_CAST(const detail::callable_base<F>&)(BOOST_HOF_CONST_THIS->base_function(xs...)),
0162 BOOST_HOF_FORWARD(Ts)(xs)...
0163 )
0164 )
0165 };
0166
0167 BOOST_HOF_DECLARE_STATIC_VAR(fold, detail::make<fold_adaptor>);
0168
0169 }}
0170
0171 #endif