find.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*!
  2. @file
  3. Defines `boost::hana::find`.
  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_FIND_HPP
  9. #define BOOST_HANA_FIND_HPP
  10. #include <boost/hana/fwd/find.hpp>
  11. #include <boost/hana/concept/searchable.hpp>
  12. #include <boost/hana/config.hpp>
  13. #include <boost/hana/core/dispatch.hpp>
  14. #include <boost/hana/equal.hpp>
  15. #include <boost/hana/find_if.hpp>
  16. BOOST_HANA_NAMESPACE_BEGIN
  17. //! @cond
  18. template <typename Xs, typename Key>
  19. constexpr auto find_t::operator()(Xs&& xs, Key const& key) const {
  20. using S = typename hana::tag_of<Xs>::type;
  21. using Find = BOOST_HANA_DISPATCH_IF(find_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::find(xs, key) requires 'xs' to be Searchable");
  27. #endif
  28. return Find::apply(static_cast<Xs&&>(xs), key);
  29. }
  30. //! @endcond
  31. namespace detail {
  32. template <typename T>
  33. struct equal_to {
  34. T const& t;
  35. template <typename U>
  36. constexpr auto operator()(U const& u) const {
  37. return hana::equal(t, u);
  38. }
  39. };
  40. }
  41. template <typename S, bool condition>
  42. struct find_impl<S, when<condition>> : default_ {
  43. template <typename Xs, typename Key>
  44. static constexpr auto apply(Xs&& xs, Key const& key) {
  45. return hana::find_if(static_cast<Xs&&>(xs),
  46. detail::equal_to<Key>{key});
  47. }
  48. };
  49. BOOST_HANA_NAMESPACE_END
  50. #endif // !BOOST_HANA_FIND_HPP