1 /*! 2 @file 3 Defines `boost::hana::is_subset`. 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_IS_SUBSET_HPP 11 #define BOOST_HANA_IS_SUBSET_HPP 12 13 #include <boost/hana/fwd/is_subset.hpp> 14 15 #include <boost/hana/all_of.hpp> 16 #include <boost/hana/concept/searchable.hpp> 17 #include <boost/hana/config.hpp> 18 #include <boost/hana/contains.hpp> 19 #include <boost/hana/core/common.hpp> 20 #include <boost/hana/core/to.hpp> 21 #include <boost/hana/core/dispatch.hpp> 22 #include <boost/hana/detail/has_common_embedding.hpp> 23 #include <boost/hana/functional/partial.hpp> 24 25 26 BOOST_HANA_NAMESPACE_BEGIN 27 //! @cond 28 template <typename Xs, typename Ys> operator ()(Xs && xs,Ys && ys) const29 constexpr auto is_subset_t::operator()(Xs&& xs, Ys&& ys) const { 30 using S1 = typename hana::tag_of<Xs>::type; 31 using S2 = typename hana::tag_of<Ys>::type; 32 using IsSubset = BOOST_HANA_DISPATCH_IF( 33 decltype(is_subset_impl<S1, S2>{}), 34 hana::Searchable<S1>::value && 35 hana::Searchable<S2>::value && 36 !is_default<is_subset_impl<S1, S2>>::value 37 ); 38 39 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS 40 static_assert(hana::Searchable<S1>::value, 41 "hana::is_subset(xs, ys) requires 'xs' to be Searchable"); 42 43 static_assert(hana::Searchable<S2>::value, 44 "hana::is_subset(xs, ys) requires 'ys' to be Searchable"); 45 46 static_assert(!is_default<is_subset_impl<S1, S2>>::value, 47 "hana::is_subset(xs, ys) requires 'xs' and 'ys' to be embeddable " 48 "in a common Searchable"); 49 #endif 50 51 return IsSubset::apply(static_cast<Xs&&>(xs), static_cast<Ys&&>(ys)); 52 } 53 //! @endcond 54 55 template <typename S1, typename S2, bool condition> 56 struct is_subset_impl<S1, S2, when<condition>> : default_ { 57 template <typename ...Args> 58 static constexpr auto apply(Args&& ...) = delete; 59 }; 60 61 template <typename S, bool condition> 62 struct is_subset_impl<S, S, when<condition>> { 63 template <typename Xs, typename Ys> applyis_subset_impl64 static constexpr decltype(auto) apply(Xs&& xs, Ys&& ys) { 65 return hana::all_of(static_cast<Xs&&>(xs), 66 hana::partial(hana::contains, static_cast<Ys&&>(ys))); 67 } 68 }; 69 70 // Cross-type overload 71 template <typename S1, typename S2> 72 struct is_subset_impl<S1, S2, when< 73 detail::has_nontrivial_common_embedding<Searchable, S1, S2>::value 74 >> { 75 using C = typename common<S1, S2>::type; 76 template <typename Xs, typename Ys> applyis_subset_impl77 static constexpr decltype(auto) apply(Xs&& xs, Ys&& ys) { 78 return hana::is_subset(hana::to<C>(static_cast<Xs&&>(xs)), 79 hana::to<C>(static_cast<Ys&&>(ys))); 80 } 81 }; 82 BOOST_HANA_NAMESPACE_END 83 84 #endif // !BOOST_HANA_IS_SUBSET_HPP 85