| 1 | /*! |
| 2 | @file |
| 3 | Defines `boost::hana::drop_front_exactly`. |
| 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_DROP_FRONT_EXACTLY_HPP |
| 11 | #define BOOST_HANA_DROP_FRONT_EXACTLY_HPP |
| 12 | |
| 13 | #include <boost/hana/fwd/drop_front_exactly.hpp> |
| 14 | |
| 15 | #include <boost/hana/bool.hpp> |
| 16 | #include <boost/hana/concept/integral_constant.hpp> |
| 17 | #include <boost/hana/concept/iterable.hpp> |
| 18 | #include <boost/hana/config.hpp> |
| 19 | #include <boost/hana/core/dispatch.hpp> |
| 20 | #include <boost/hana/drop_front.hpp> |
| 21 | #include <boost/hana/integral_constant.hpp> |
| 22 | #include <boost/hana/is_empty.hpp> |
| 23 | |
| 24 | |
| 25 | namespace boost { namespace hana { |
| 26 | //! @cond |
| 27 | template <typename Xs, typename N> |
| 28 | constexpr auto drop_front_exactly_t::operator()(Xs&& xs, N const& n) const { |
| 29 | using It = typename hana::tag_of<Xs>::type; |
| 30 | using DropFrontExactly = BOOST_HANA_DISPATCH_IF(drop_front_exactly_impl<It>, |
| 31 | hana::Iterable<It>::value && |
| 32 | hana::IntegralConstant<N>::value |
| 33 | ); |
| 34 | |
| 35 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS |
| 36 | static_assert(hana::Iterable<It>::value, |
| 37 | "hana::drop_front_exactly(xs, n) requires 'xs' to be an Iterable" ); |
| 38 | |
| 39 | static_assert(hana::IntegralConstant<N>::value, |
| 40 | "hana::drop_front_exactly(xs, n) requires 'n' to be an IntegralConstant" ); |
| 41 | #endif |
| 42 | |
| 43 | static_assert(N::value >= 0, |
| 44 | "hana::drop_front_exactly(xs, n) requires 'n' to be non-negative" ); |
| 45 | |
| 46 | return DropFrontExactly::apply(static_cast<Xs&&>(xs), n); |
| 47 | } |
| 48 | |
| 49 | template <typename Xs> |
| 50 | constexpr auto drop_front_exactly_t::operator()(Xs&& xs) const { |
| 51 | return (*this)(static_cast<Xs&&>(xs), hana::size_c<1>); |
| 52 | } |
| 53 | //! @endcond |
| 54 | |
| 55 | namespace detail { |
| 56 | template <typename Xs, typename N> |
| 57 | constexpr void check_dfe_overflow(Xs const& xs, N const&, hana::true_) { |
| 58 | constexpr bool n_overflew_length = decltype( |
| 59 | hana::is_empty(hana::drop_front(xs, hana::size_c<N::value - 1>)) |
| 60 | )::value; |
| 61 | static_assert(!n_overflew_length, |
| 62 | "hana::drop_front_exactly(xs, n) requires 'n' to be less than or " |
| 63 | "equal to the number of elements in 'xs'" ); |
| 64 | } |
| 65 | |
| 66 | template <typename Xs, typename N> |
| 67 | constexpr void check_dfe_overflow(Xs const&, N const&, hana::false_) { } |
| 68 | } |
| 69 | |
| 70 | template <typename It, bool condition> |
| 71 | struct drop_front_exactly_impl<It, when<condition>> : default_ { |
| 72 | template <typename Xs, typename N> |
| 73 | static constexpr auto apply(Xs&& xs, N const& n) { |
| 74 | auto result = hana::drop_front(static_cast<Xs&&>(xs), n); |
| 75 | constexpr bool check_for_overflow = |
| 76 | decltype(hana::is_empty(result))::value && N::value != 0; |
| 77 | |
| 78 | detail::check_dfe_overflow(xs, n, hana::bool_c<check_for_overflow>); |
| 79 | |
| 80 | return result; // NRVO applied |
| 81 | } |
| 82 | }; |
| 83 | }} // end namespace boost::hana |
| 84 | |
| 85 | #endif // !BOOST_HANA_DROP_FRONT_EXACTLY_HPP |
| 86 | |