]>
Commit | Line | Data |
---|---|---|
1 | /*! | |
2 | @file | |
3 | Defines `boost::hana::length`. | |
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_LENGTH_HPP | |
11 | #define BOOST_HANA_LENGTH_HPP | |
12 | ||
13 | #include <boost/hana/fwd/length.hpp> | |
14 | ||
15 | #include <boost/hana/concept/foldable.hpp> | |
16 | #include <boost/hana/config.hpp> | |
17 | #include <boost/hana/core/dispatch.hpp> | |
18 | #include <boost/hana/integral_constant.hpp> | |
19 | #include <boost/hana/unpack.hpp> | |
20 | ||
21 | ||
22 | BOOST_HANA_NAMESPACE_BEGIN | |
23 | //! @cond | |
24 | template <typename Xs> | |
25 | constexpr auto length_t::operator()(Xs const& xs) const { | |
26 | using S = typename hana::tag_of<Xs>::type; | |
27 | using Length = BOOST_HANA_DISPATCH_IF(length_impl<S>, | |
28 | hana::Foldable<S>::value | |
29 | ); | |
30 | ||
31 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS | |
32 | static_assert(hana::Foldable<S>::value, | |
33 | "hana::length(xs) requires 'xs' to be Foldable"); | |
34 | #endif | |
35 | ||
36 | return Length::apply(xs); | |
37 | } | |
38 | //! @endcond | |
39 | ||
40 | namespace detail { | |
41 | struct argn { | |
42 | template <typename ...Xs> | |
43 | constexpr hana::size_t<sizeof...(Xs)> operator()(Xs const& ...) const | |
44 | { return {}; } | |
45 | }; | |
46 | } | |
47 | ||
48 | template <typename T, bool condition> | |
49 | struct length_impl<T, when<condition>> : default_ { | |
50 | template <typename Xs> | |
51 | static constexpr auto apply(Xs const& xs) { | |
52 | return hana::unpack(xs, detail::argn{}); | |
53 | } | |
54 | }; | |
55 | BOOST_HANA_NAMESPACE_END | |
56 | ||
57 | #endif // !BOOST_HANA_LENGTH_HPP |