1 /*! 2 @file 3 Defines `boost::hana::drop_back`. 4 5 @copyright Louis Dionne 2013-2017 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_BACK_HPP 11 #define BOOST_HANA_DROP_BACK_HPP 12 13 #include <boost/hana/fwd/drop_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> operator ()(Xs && xs,N const & n) const31 constexpr auto drop_back_t::operator()(Xs&& xs, N const& n) const { 32 using S = typename hana::tag_of<Xs>::type; 33 using DropBack = BOOST_HANA_DISPATCH_IF(drop_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::drop_back(xs, n) requires 'xs' to be a Sequence"); 41 42 static_assert(hana::IntegralConstant<N>::value, 43 "hana::drop_back(xs, n) requires 'n' to be an IntegralConstant"); 44 #endif 45 46 static_assert(N::value >= 0, 47 "hana::drop_back(xs, n) requires 'n' to be non-negative"); 48 49 return DropBack::apply(static_cast<Xs&&>(xs), n); 50 } 51 52 template <typename Xs> operator ()(Xs && xs) const53 constexpr auto drop_back_t::operator()(Xs&& xs) const { 54 return (*this)(static_cast<Xs&&>(xs), hana::size_c<1>); 55 } 56 //! @endcond 57 58 template <typename S, bool condition> 59 struct drop_back_impl<S, when<condition>> : default_ { 60 template <typename Xs, std::size_t ...n> drop_back_helperdrop_back_impl61 static constexpr auto drop_back_helper(Xs&& xs, std::index_sequence<n...>) { 62 return hana::make<S>(hana::at_c<n>(static_cast<Xs&&>(xs))...); 63 } 64 65 template <typename Xs, typename N> applydrop_back_impl66 static constexpr auto apply(Xs&& xs, N const&) { 67 constexpr std::size_t n = N::value; 68 constexpr std::size_t len = decltype(hana::length(xs))::value; 69 return drop_back_helper(static_cast<Xs&&>(xs), 70 std::make_index_sequence<(n > len ? 0 : len - n)>{}); 71 } 72 }; 73 BOOST_HANA_NAMESPACE_END 74 75 #endif // !BOOST_HANA_DROP_BACK_HPP 76