1 /*! 2 @file 3 Defines `boost::hana::take_front` and `boost::hana::take_front_c`. 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_TAKE_FRONT_HPP 11 #define BOOST_HANA_TAKE_FRONT_HPP 12 13 #include <boost/hana/fwd/take_front.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 take_front_t::operator()(Xs&& xs, N const& n) const { 32 using S = typename hana::tag_of<Xs>::type; 33 using TakeFront = BOOST_HANA_DISPATCH_IF(take_front_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_front(xs, n) requires 'xs' to be a Sequence"); 41 42 static_assert(hana::IntegralConstant<N>::value, 43 "hana::take_front(xs, n) requires 'n' to be an IntegralConstant"); 44 #endif 45 46 return TakeFront::apply(static_cast<Xs&&>(xs), n); 47 } 48 //! @endcond 49 50 template <typename S, bool condition> 51 struct take_front_impl<S, when<condition>> : default_ { 52 template <typename Xs, std::size_t ...n> take_front_helpertake_front_impl53 static constexpr auto take_front_helper(Xs&& xs, std::index_sequence<n...>) { 54 return hana::make<S>(hana::at_c<n>(static_cast<Xs&&>(xs))...); 55 } 56 57 template <typename Xs, typename N> applytake_front_impl58 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 return take_front_helper(static_cast<Xs&&>(xs), 62 std::make_index_sequence<(n < size ? n : size)>{}); 63 } 64 }; 65 66 template <std::size_t n> 67 struct take_front_c_t { 68 template <typename Xs> operator ()take_front_c_t69 constexpr auto operator()(Xs&& xs) const { 70 return hana::take_front(static_cast<Xs&&>(xs), hana::size_c<n>); 71 } 72 }; 73 BOOST_HANA_NAMESPACE_END 74 75 #endif // !BOOST_HANA_TAKE_FRONT_HPP 76