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