drop_back.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*!
  2. @file
  3. Defines `boost::hana::drop_back`.
  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_DROP_BACK_HPP
  9. #define BOOST_HANA_DROP_BACK_HPP
  10. #include <boost/hana/fwd/drop_back.hpp>
  11. #include <boost/hana/at.hpp>
  12. #include <boost/hana/concept/integral_constant.hpp>
  13. #include <boost/hana/concept/sequence.hpp>
  14. #include <boost/hana/config.hpp>
  15. #include <boost/hana/core/dispatch.hpp>
  16. #include <boost/hana/core/make.hpp>
  17. #include <boost/hana/integral_constant.hpp>
  18. #include <boost/hana/length.hpp>
  19. #include <cstddef>
  20. #include <utility>
  21. BOOST_HANA_NAMESPACE_BEGIN
  22. //! @cond
  23. template <typename Xs, typename N>
  24. constexpr auto drop_back_t::operator()(Xs&& xs, N const& n) const {
  25. using S = typename hana::tag_of<Xs>::type;
  26. using DropBack = BOOST_HANA_DISPATCH_IF(drop_back_impl<S>,
  27. hana::Sequence<S>::value &&
  28. hana::IntegralConstant<N>::value
  29. );
  30. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  31. static_assert(hana::Sequence<S>::value,
  32. "hana::drop_back(xs, n) requires 'xs' to be a Sequence");
  33. static_assert(hana::IntegralConstant<N>::value,
  34. "hana::drop_back(xs, n) requires 'n' to be an IntegralConstant");
  35. #endif
  36. static_assert(N::value >= 0,
  37. "hana::drop_back(xs, n) requires 'n' to be non-negative");
  38. return DropBack::apply(static_cast<Xs&&>(xs), n);
  39. }
  40. template <typename Xs>
  41. constexpr auto drop_back_t::operator()(Xs&& xs) const {
  42. return (*this)(static_cast<Xs&&>(xs), hana::size_c<1>);
  43. }
  44. //! @endcond
  45. template <typename S, bool condition>
  46. struct drop_back_impl<S, when<condition>> : default_ {
  47. template <typename Xs, std::size_t ...n>
  48. static constexpr auto drop_back_helper(Xs&& xs, std::index_sequence<n...>) {
  49. return hana::make<S>(hana::at_c<n>(static_cast<Xs&&>(xs))...);
  50. }
  51. template <typename Xs, typename N>
  52. static constexpr auto apply(Xs&& xs, N const&) {
  53. constexpr std::size_t n = N::value;
  54. constexpr std::size_t len = decltype(hana::length(xs))::value;
  55. return drop_back_helper(static_cast<Xs&&>(xs),
  56. std::make_index_sequence<(n > len ? 0 : len - n)>{});
  57. }
  58. };
  59. BOOST_HANA_NAMESPACE_END
  60. #endif // !BOOST_HANA_DROP_BACK_HPP