equal.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*!
  2. @file
  3. Defines `boost::hana::equal`.
  4. Copyright Louis Dionne 2013-2022
  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_EQUAL_HPP
  9. #define BOOST_HANA_EQUAL_HPP
  10. #include <boost/hana/fwd/equal.hpp>
  11. #include <boost/hana/accessors.hpp>
  12. #include <boost/hana/all_of.hpp>
  13. #include <boost/hana/and.hpp>
  14. #include <boost/hana/at.hpp>
  15. #include <boost/hana/bool.hpp>
  16. #include <boost/hana/concept/comparable.hpp>
  17. #include <boost/hana/concept/constant.hpp>
  18. #include <boost/hana/concept/product.hpp>
  19. #include <boost/hana/concept/sequence.hpp>
  20. #include <boost/hana/concept/struct.hpp>
  21. #include <boost/hana/config.hpp>
  22. #include <boost/hana/core/common.hpp>
  23. #include <boost/hana/core/to.hpp>
  24. #include <boost/hana/core/dispatch.hpp>
  25. #include <boost/hana/core/tag_of.hpp>
  26. #include <boost/hana/core/when.hpp>
  27. #include <boost/hana/detail/concepts.hpp>
  28. #include <boost/hana/detail/has_common_embedding.hpp>
  29. #include <boost/hana/detail/nested_to.hpp> // required by fwd decl
  30. #include <boost/hana/first.hpp>
  31. #include <boost/hana/if.hpp>
  32. #include <boost/hana/length.hpp>
  33. #include <boost/hana/second.hpp>
  34. #include <boost/hana/value.hpp>
  35. #include <cstddef>
  36. namespace boost { namespace hana {
  37. //! @cond
  38. template <typename X, typename Y>
  39. constexpr auto equal_t::operator()(X&& x, Y&& y) const {
  40. using T = typename hana::tag_of<X>::type;
  41. using U = typename hana::tag_of<Y>::type;
  42. using Equal = equal_impl<T, U>;
  43. return Equal::apply(static_cast<X&&>(x), static_cast<Y&&>(y));
  44. }
  45. //! @endcond
  46. template <typename T, typename U, bool condition>
  47. struct equal_impl<T, U, when<condition>> : default_ {
  48. template <typename X, typename Y>
  49. static constexpr auto apply(X const&, Y const&) {
  50. // Delay the static_assert by ensuring T_ is dependent.
  51. using T_ = typename hana::tag_of<X>::type;
  52. static_assert(!hana::is_convertible<T_, U>::value &&
  53. !hana::is_convertible<U, T_>::value,
  54. "No default implementation of hana::equal is provided for related "
  55. "types that can't be safely embedded into a common type, because "
  56. "those are most likely programming errors. If this is really what "
  57. "you want, you can manually convert both objects to a common "
  58. "Comparable type before performing the comparison. If you think "
  59. "you have made your types Comparable but you see this, perhaps you "
  60. "forgot to define some of the necessary methods for an automatic "
  61. "model of Comparable to kick in. A possible culprit is defining "
  62. "'operator==' but not 'operator!='.");
  63. return hana::false_c;
  64. }
  65. };
  66. // Cross-type overload
  67. template <typename T, typename U>
  68. struct equal_impl<T, U, when<
  69. detail::has_nontrivial_common_embedding<Comparable, T, U>::value &&
  70. !detail::EqualityComparable<T, U>::value
  71. >> {
  72. using C = typename hana::common<T, U>::type;
  73. template <typename X, typename Y>
  74. static constexpr auto apply(X&& x, Y&& y) {
  75. return hana::equal(hana::to<C>(static_cast<X&&>(x)),
  76. hana::to<C>(static_cast<Y&&>(y)));
  77. }
  78. };
  79. //////////////////////////////////////////////////////////////////////////
  80. // Model for EqualityComparable data types
  81. //////////////////////////////////////////////////////////////////////////
  82. template <typename T, typename U>
  83. struct equal_impl<T, U, when<detail::EqualityComparable<T, U>::value>> {
  84. template <typename X, typename Y>
  85. static constexpr auto apply(X&& x, Y&& y)
  86. { return static_cast<X&&>(x) == static_cast<Y&&>(y); }
  87. };
  88. //////////////////////////////////////////////////////////////////////////
  89. // Model for Constants wrapping a Comparable
  90. //////////////////////////////////////////////////////////////////////////
  91. template <typename C>
  92. struct equal_impl<C, C, when<
  93. hana::Constant<C>::value &&
  94. Comparable<typename C::value_type>::value
  95. >> {
  96. template <typename X, typename Y>
  97. static constexpr auto apply(X const&, Y const&) {
  98. constexpr auto eq = hana::equal(hana::value<X>(), hana::value<Y>());
  99. constexpr bool truth_value = hana::if_(eq, true, false);
  100. return hana::bool_<truth_value>{};
  101. }
  102. };
  103. //////////////////////////////////////////////////////////////////////////
  104. // Comparable for Products
  105. //////////////////////////////////////////////////////////////////////////
  106. template <typename T, typename U>
  107. struct equal_impl<T, U, when<hana::Product<T>::value && hana::Product<U>::value>> {
  108. template <typename X, typename Y>
  109. static constexpr auto apply(X const& x, Y const& y) {
  110. return hana::and_(
  111. hana::equal(hana::first(x), hana::first(y)),
  112. hana::equal(hana::second(x), hana::second(y))
  113. );
  114. }
  115. };
  116. //////////////////////////////////////////////////////////////////////////
  117. // Comparable for Sequences
  118. //////////////////////////////////////////////////////////////////////////
  119. namespace detail {
  120. template <typename Xs, typename Ys, std::size_t Length>
  121. struct compare_finite_sequences {
  122. Xs const& xs;
  123. Ys const& ys;
  124. template <std::size_t i>
  125. constexpr auto apply(hana::false_, hana::true_) const {
  126. return compare_finite_sequences::apply<i+1>(
  127. hana::bool_<i+1 == Length>{},
  128. hana::if_(hana::equal(hana::at_c<i>(xs), hana::at_c<i>(ys)),
  129. hana::true_c, hana::false_c)
  130. );
  131. }
  132. template <std::size_t i>
  133. constexpr auto apply(hana::false_, hana::false_) const
  134. { return hana::false_c; }
  135. template <std::size_t i, typename Result>
  136. constexpr auto apply(hana::true_, Result r) const
  137. { return r; }
  138. template <std::size_t i>
  139. constexpr bool apply(hana::false_, bool b) const {
  140. return b && compare_finite_sequences::apply<i+1>(
  141. hana::bool_<i+1 == Length>{},
  142. hana::if_(hana::equal(hana::at_c<i>(xs), hana::at_c<i>(ys)),
  143. hana::true_c, hana::false_c)
  144. );
  145. }
  146. };
  147. }
  148. template <typename T, typename U>
  149. struct equal_impl<T, U, when<Sequence<T>::value && hana::Sequence<U>::value>> {
  150. template <typename Xs, typename Ys>
  151. static constexpr auto apply(Xs const& xs, Ys const& ys) {
  152. constexpr std::size_t xs_size = decltype(hana::length(xs))::value;
  153. constexpr std::size_t ys_size = decltype(hana::length(ys))::value;
  154. detail::compare_finite_sequences<Xs, Ys, xs_size> comp{xs, ys};
  155. return comp.template apply<0>(hana::bool_<xs_size == 0>{},
  156. hana::bool_<xs_size == ys_size>{});
  157. }
  158. };
  159. namespace detail {
  160. template <typename X, typename Y>
  161. struct compare_struct_members {
  162. X const& x;
  163. Y const& y;
  164. template <typename Member>
  165. constexpr auto operator()(Member&& member) const {
  166. auto accessor = hana::second(static_cast<Member&&>(member));
  167. return hana::equal(accessor(x), accessor(y));
  168. }
  169. };
  170. }
  171. template <typename S>
  172. struct equal_impl<S, S, when<
  173. hana::Struct<S>::value &&
  174. !detail::EqualityComparable<S, S>::value
  175. >> {
  176. template <typename X, typename Y>
  177. static constexpr auto apply(X const& x, Y const& y) {
  178. return hana::all_of(hana::accessors<S>(),
  179. detail::compare_struct_members<X, Y>{x, y});
  180. }
  181. };
  182. }} // end namespace boost::hana
  183. #endif // !BOOST_HANA_EQUAL_HPP