| 1 | /*! |
| 2 | @file |
| 3 | Defines `boost::hana::unfold_right`. |
| 4 | |
| 5 | Copyright Louis Dionne 2013-2022 |
| 6 | Distributed under the Boost Software License, Version 1.0. |
| 7 | (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) |
| 8 | */ |
| 9 | |
| 10 | #ifndef BOOST_HANA_UNFOLD_RIGHT_HPP |
| 11 | #define BOOST_HANA_UNFOLD_RIGHT_HPP |
| 12 | |
| 13 | #include <boost/hana/fwd/unfold_right.hpp> |
| 14 | |
| 15 | #include <boost/hana/concept/sequence.hpp> |
| 16 | #include <boost/hana/config.hpp> |
| 17 | #include <boost/hana/core/dispatch.hpp> |
| 18 | #include <boost/hana/empty.hpp> |
| 19 | #include <boost/hana/first.hpp> |
| 20 | #include <boost/hana/functional/partial.hpp> |
| 21 | #include <boost/hana/optional.hpp> |
| 22 | #include <boost/hana/prepend.hpp> |
| 23 | #include <boost/hana/second.hpp> |
| 24 | |
| 25 | |
| 26 | namespace boost { namespace hana { |
| 27 | //! @cond |
| 28 | template <typename S> |
| 29 | struct unfold_right_t { |
| 30 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS |
| 31 | static_assert(hana::Sequence<S>::value, |
| 32 | "hana::unfold_right<S> requires 'S' to be a Sequence" ); |
| 33 | #endif |
| 34 | |
| 35 | template <typename State, typename F> |
| 36 | constexpr auto operator()(State&& state, F&& f) const { |
| 37 | return unfold_right_impl<S>::apply( |
| 38 | static_cast<State&&>(state), |
| 39 | static_cast<F&&>(f) |
| 40 | ); |
| 41 | } |
| 42 | }; |
| 43 | //! @endcond |
| 44 | |
| 45 | template <typename S, bool condition> |
| 46 | struct unfold_right_impl<S, when<condition>> : default_ { |
| 47 | struct unfold_right_helper { |
| 48 | template <typename F, typename P> |
| 49 | constexpr auto operator()(F&& f, P&& p) const { |
| 50 | return hana::prepend( |
| 51 | unfold_right_impl::apply( |
| 52 | hana::second(static_cast<P&&>(p)), |
| 53 | static_cast<F&&>(f) |
| 54 | ), |
| 55 | hana::first(static_cast<P&&>(p)) |
| 56 | ); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | template <typename Init, typename F> |
| 61 | static constexpr auto apply(Init&& init, F&& f) { |
| 62 | decltype(auto) elt = f(static_cast<Init&&>(init)); |
| 63 | return hana::maybe(hana::empty<S>(), |
| 64 | hana::partial(unfold_right_helper{}, static_cast<F&&>(f)), |
| 65 | static_cast<decltype(elt)&&>(elt) |
| 66 | ); |
| 67 | } |
| 68 | }; |
| 69 | }} // end namespace boost::hana |
| 70 | |
| 71 | #endif // !BOOST_HANA_UNFOLD_RIGHT_HPP |
| 72 | |