| 1 | /*! |
|---|---|
| 2 | @file |
| 3 | Defines `boost::hana::reverse`. |
| 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_REVERSE_HPP |
| 11 | #define BOOST_HANA_REVERSE_HPP |
| 12 | |
| 13 | #include <boost/hana/fwd/reverse.hpp> |
| 14 | |
| 15 | #include <boost/hana/at.hpp> |
| 16 | #include <boost/hana/concept/sequence.hpp> |
| 17 | #include <boost/hana/config.hpp> |
| 18 | #include <boost/hana/core/dispatch.hpp> |
| 19 | #include <boost/hana/core/make.hpp> |
| 20 | #include <boost/hana/length.hpp> |
| 21 | |
| 22 | #include <cstddef> |
| 23 | #include <utility> |
| 24 | |
| 25 | |
| 26 | namespace boost { namespace hana { |
| 27 | //! @cond |
| 28 | template <typename Xs> |
| 29 | constexpr auto reverse_t::operator()(Xs&& xs) const { |
| 30 | using S = typename hana::tag_of<Xs>::type; |
| 31 | using Reverse = BOOST_HANA_DISPATCH_IF(reverse_impl<S>, |
| 32 | hana::Sequence<S>::value |
| 33 | ); |
| 34 | |
| 35 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS |
| 36 | static_assert(hana::Sequence<S>::value, |
| 37 | "hana::reverse(xs) requires 'xs' to be a Sequence"); |
| 38 | #endif |
| 39 | |
| 40 | return Reverse::apply(static_cast<Xs&&>(xs)); |
| 41 | } |
| 42 | //! @endcond |
| 43 | |
| 44 | template <typename S, bool condition> |
| 45 | struct reverse_impl<S, when<condition>> : default_ { |
| 46 | template <typename Xs, std::size_t ...i> |
| 47 | static constexpr auto reverse_helper(Xs&& xs, std::index_sequence<i...>) { |
| 48 | return hana::make<S>( |
| 49 | hana::at_c<sizeof...(i) - i - 1>(static_cast<Xs&&>(xs))... |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | template <typename Xs> |
| 54 | static constexpr auto apply(Xs&& xs) { |
| 55 | constexpr std::size_t N = decltype(hana::length(xs))::value; |
| 56 | return reverse_helper(static_cast<Xs&&>(xs), std::make_index_sequence<N>{}); |
| 57 | } |
| 58 | }; |
| 59 | }} // end namespace boost::hana |
| 60 | |
| 61 | #endif // !BOOST_HANA_REVERSE_HPP |
| 62 |
