]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*! |
2 | @file | |
3 | Defines `boost::hana::take_back`. | |
4 | ||
5 | @copyright Louis Dionne 2013-2016 | |
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_TAKE_BACK_HPP | |
11 | #define BOOST_HANA_TAKE_BACK_HPP | |
12 | ||
13 | #include <boost/hana/fwd/take_back.hpp> | |
14 | ||
15 | #include <boost/hana/at.hpp> | |
16 | #include <boost/hana/concept/integral_constant.hpp> | |
17 | #include <boost/hana/concept/sequence.hpp> | |
18 | #include <boost/hana/config.hpp> | |
19 | #include <boost/hana/core/dispatch.hpp> | |
20 | #include <boost/hana/core/make.hpp> | |
21 | #include <boost/hana/integral_constant.hpp> | |
22 | #include <boost/hana/length.hpp> | |
23 | ||
24 | #include <cstddef> | |
25 | #include <utility> | |
26 | ||
27 | ||
28 | BOOST_HANA_NAMESPACE_BEGIN | |
29 | //! @cond | |
30 | template <typename Xs, typename N> | |
31 | constexpr auto take_back_t::operator()(Xs&& xs, N const& n) const { | |
32 | using S = typename hana::tag_of<Xs>::type; | |
33 | using TakeBack = BOOST_HANA_DISPATCH_IF(take_back_impl<S>, | |
34 | hana::Sequence<S>::value && | |
35 | hana::IntegralConstant<N>::value | |
36 | ); | |
37 | ||
38 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS | |
39 | static_assert(hana::Sequence<S>::value, | |
40 | "hana::take_back(xs, n) requires 'xs' to be a Sequence"); | |
41 | ||
42 | static_assert(hana::IntegralConstant<N>::value, | |
43 | "hana::take_back(xs, n) requires 'n' to be an IntegralConstant"); | |
44 | #endif | |
45 | ||
46 | return TakeBack::apply(static_cast<Xs&&>(xs), n); | |
47 | } | |
48 | //! @endcond | |
49 | ||
50 | template <typename S, bool condition> | |
51 | struct take_back_impl<S, when<condition>> : default_ { | |
52 | template <std::size_t start, typename Xs, std::size_t ...n> | |
53 | static constexpr auto take_back_helper(Xs&& xs, std::index_sequence<n...>) { | |
54 | return hana::make<S>(hana::at_c<start + n>(static_cast<Xs&&>(xs))...); | |
55 | } | |
56 | ||
57 | template <typename Xs, typename N> | |
58 | static constexpr auto apply(Xs&& xs, N const&) { | |
59 | constexpr std::size_t n = N::value; | |
60 | constexpr std::size_t size = decltype(hana::length(xs))::value; | |
61 | constexpr std::size_t start = n < size ? size - n : 0; | |
62 | return take_back_helper<start>(static_cast<Xs&&>(xs), | |
63 | std::make_index_sequence<(n < size ? n : size)>{}); | |
64 | } | |
65 | }; | |
66 | BOOST_HANA_NAMESPACE_END | |
67 | ||
68 | #endif // !BOOST_HANA_TAKE_BACK_HPP |