reverse.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*!
  2. @file
  3. Defines `boost::hana::reverse`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_REVERSE_HPP
  9. #define BOOST_HANA_REVERSE_HPP
  10. #include <boost/hana/fwd/reverse.hpp>
  11. #include <boost/hana/at.hpp>
  12. #include <boost/hana/concept/sequence.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/dispatch.hpp>
  15. #include <boost/hana/core/make.hpp>
  16. #include <boost/hana/length.hpp>
  17. #include <cstddef>
  18. #include <utility>
  19. BOOST_HANA_NAMESPACE_BEGIN
  20. //! @cond
  21. template <typename Xs>
  22. constexpr auto reverse_t::operator()(Xs&& xs) const {
  23. using S = typename hana::tag_of<Xs>::type;
  24. using Reverse = BOOST_HANA_DISPATCH_IF(reverse_impl<S>,
  25. hana::Sequence<S>::value
  26. );
  27. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  28. static_assert(hana::Sequence<S>::value,
  29. "hana::reverse(xs) requires 'xs' to be a Sequence");
  30. #endif
  31. return Reverse::apply(static_cast<Xs&&>(xs));
  32. }
  33. //! @endcond
  34. template <typename S, bool condition>
  35. struct reverse_impl<S, when<condition>> : default_ {
  36. template <typename Xs, std::size_t ...i>
  37. static constexpr auto reverse_helper(Xs&& xs, std::index_sequence<i...>) {
  38. return hana::make<S>(
  39. hana::at_c<sizeof...(i) - i - 1>(static_cast<Xs&&>(xs))...
  40. );
  41. }
  42. template <typename Xs>
  43. static constexpr auto apply(Xs&& xs) {
  44. constexpr std::size_t N = decltype(hana::length(xs))::value;
  45. return reverse_helper(static_cast<Xs&&>(xs), std::make_index_sequence<N>{});
  46. }
  47. };
  48. BOOST_HANA_NAMESPACE_END
  49. #endif // !BOOST_HANA_REVERSE_HPP