]>
Commit | Line | Data |
---|---|---|
1 | /*! | |
2 | @file | |
3 | Defines `boost::hana::contains` and `boost::hana::in`. | |
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_CONTAINS_HPP | |
11 | #define BOOST_HANA_CONTAINS_HPP | |
12 | ||
13 | #include <boost/hana/fwd/contains.hpp> | |
14 | ||
15 | #include <boost/hana/any_of.hpp> | |
16 | #include <boost/hana/concept/searchable.hpp> | |
17 | #include <boost/hana/config.hpp> | |
18 | #include <boost/hana/core/dispatch.hpp> | |
19 | #include <boost/hana/equal.hpp> | |
20 | ||
21 | ||
22 | BOOST_HANA_NAMESPACE_BEGIN | |
23 | //! @cond | |
24 | template <typename Xs, typename Key> | |
25 | constexpr auto contains_t::operator()(Xs&& xs, Key&& key) const { | |
26 | using S = typename hana::tag_of<Xs>::type; | |
27 | using Contains = BOOST_HANA_DISPATCH_IF(contains_impl<S>, | |
28 | hana::Searchable<S>::value | |
29 | ); | |
30 | ||
31 | #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS | |
32 | static_assert(hana::Searchable<S>::value, | |
33 | "hana::contains(xs, key) requires 'xs' to be a Searchable"); | |
34 | #endif | |
35 | ||
36 | return Contains::apply(static_cast<Xs&&>(xs), | |
37 | static_cast<Key&&>(key)); | |
38 | } | |
39 | //! @endcond | |
40 | ||
41 | template <typename S, bool condition> | |
42 | struct contains_impl<S, when<condition>> : default_ { | |
43 | template <typename Xs, typename X> | |
44 | static constexpr auto apply(Xs&& xs, X&& x) { | |
45 | return hana::any_of(static_cast<Xs&&>(xs), | |
46 | hana::equal.to(static_cast<X&&>(x))); | |
47 | } | |
48 | }; | |
49 | BOOST_HANA_NAMESPACE_END | |
50 | ||
51 | #endif // !BOOST_HANA_CONTAINS_HPP |