contains.hpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*!
  2. @file
  3. Defines `boost::hana::contains` and `boost::hana::in`.
  4. @copyright Louis Dionne 2013-2017
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_CONTAINS_HPP
  9. #define BOOST_HANA_CONTAINS_HPP
  10. #include <boost/hana/fwd/contains.hpp>
  11. #include <boost/hana/any_of.hpp>
  12. #include <boost/hana/concept/searchable.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/dispatch.hpp>
  15. #include <boost/hana/equal.hpp>
  16. BOOST_HANA_NAMESPACE_BEGIN
  17. //! @cond
  18. template <typename Xs, typename Key>
  19. constexpr auto contains_t::operator()(Xs&& xs, Key&& key) const {
  20. using S = typename hana::tag_of<Xs>::type;
  21. using Contains = BOOST_HANA_DISPATCH_IF(contains_impl<S>,
  22. hana::Searchable<S>::value
  23. );
  24. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  25. static_assert(hana::Searchable<S>::value,
  26. "hana::contains(xs, key) requires 'xs' to be a Searchable");
  27. #endif
  28. return Contains::apply(static_cast<Xs&&>(xs),
  29. static_cast<Key&&>(key));
  30. }
  31. //! @endcond
  32. template <typename S, bool condition>
  33. struct contains_impl<S, when<condition>> : default_ {
  34. template <typename Xs, typename X>
  35. static constexpr auto apply(Xs&& xs, X&& x) {
  36. return hana::any_of(static_cast<Xs&&>(xs),
  37. hana::equal.to(static_cast<X&&>(x)));
  38. }
  39. };
  40. BOOST_HANA_NAMESPACE_END
  41. #endif // !BOOST_HANA_CONTAINS_HPP