9
3

less.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*!
  2. @file
  3. Defines `boost::hana::less`.
  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_LESS_HPP
  9. #define BOOST_HANA_LESS_HPP
  10. #include <boost/hana/fwd/less.hpp>
  11. #include <boost/hana/and.hpp>
  12. #include <boost/hana/bool.hpp>
  13. #include <boost/hana/concept/constant.hpp>
  14. #include <boost/hana/concept/orderable.hpp>
  15. #include <boost/hana/concept/product.hpp>
  16. #include <boost/hana/concept/sequence.hpp>
  17. #include <boost/hana/config.hpp>
  18. #include <boost/hana/core/common.hpp>
  19. #include <boost/hana/core/to.hpp>
  20. #include <boost/hana/core/dispatch.hpp>
  21. #include <boost/hana/detail/concepts.hpp>
  22. #include <boost/hana/detail/has_common_embedding.hpp>
  23. #include <boost/hana/detail/nested_than.hpp> // required by fwd decl
  24. #include <boost/hana/equal.hpp>
  25. #include <boost/hana/first.hpp>
  26. #include <boost/hana/if.hpp>
  27. #include <boost/hana/less_equal.hpp>
  28. #include <boost/hana/lexicographical_compare.hpp>
  29. #include <boost/hana/or.hpp>
  30. #include <boost/hana/second.hpp>
  31. #include <boost/hana/value.hpp>
  32. BOOST_HANA_NAMESPACE_BEGIN
  33. //! @cond
  34. template <typename X, typename Y>
  35. constexpr auto less_t::operator()(X&& x, Y&& y) const {
  36. using T = typename hana::tag_of<X>::type;
  37. using U = typename hana::tag_of<Y>::type;
  38. using Less = BOOST_HANA_DISPATCH_IF(decltype(less_impl<T, U>{}),
  39. hana::Orderable<T>::value &&
  40. hana::Orderable<U>::value &&
  41. !is_default<less_impl<T, U>>::value
  42. );
  43. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  44. static_assert(hana::Orderable<T>::value,
  45. "hana::less(x, y) requires 'x' to be Orderable");
  46. static_assert(hana::Orderable<U>::value,
  47. "hana::less(x, y) requires 'y' to be Orderable");
  48. static_assert(!is_default<less_impl<T, U>>::value,
  49. "hana::less(x, y) requires 'x' and 'y' to be embeddable "
  50. "in a common Orderable");
  51. #endif
  52. return Less::apply(static_cast<X&&>(x), static_cast<Y&&>(y));
  53. }
  54. //! @endcond
  55. template <typename T, typename U, bool condition>
  56. struct less_impl<T, U, when<condition>> : default_ {
  57. template <typename ...Args>
  58. static constexpr auto apply(Args&& ...) = delete;
  59. };
  60. // Cross-type overload
  61. template <typename T, typename U>
  62. struct less_impl<T, U, when<
  63. detail::has_nontrivial_common_embedding<Orderable, T, U>::value &&
  64. !detail::LessThanComparable<T, U>::value
  65. >> {
  66. using C = typename hana::common<T, U>::type;
  67. template <typename X, typename Y>
  68. static constexpr decltype(auto) apply(X&& x, Y&& y) {
  69. return hana::less(hana::to<C>(static_cast<X&&>(x)),
  70. hana::to<C>(static_cast<Y&&>(y)));
  71. }
  72. };
  73. //////////////////////////////////////////////////////////////////////////
  74. // Model for LessThanComparable data types
  75. //////////////////////////////////////////////////////////////////////////
  76. template <typename T, typename U>
  77. struct less_impl<T, U, when<detail::LessThanComparable<T, U>::value>> {
  78. template <typename X, typename Y>
  79. static constexpr decltype(auto) apply(X&& x, Y&& y)
  80. { return static_cast<X&&>(x) < static_cast<Y&&>(y); }
  81. };
  82. //////////////////////////////////////////////////////////////////////////
  83. // Model for Constants wrapping an Orderable
  84. //////////////////////////////////////////////////////////////////////////
  85. template <typename C>
  86. struct less_impl<C, C, when<
  87. hana::Constant<C>::value &&
  88. Orderable<typename C::value_type>::value
  89. >> {
  90. template <typename X, typename Y>
  91. static constexpr auto apply(X const&, Y const&) {
  92. constexpr auto is_less = hana::less(hana::value<X>(), hana::value<Y>());
  93. constexpr bool truth_value = hana::if_(is_less, true, false);
  94. return hana::bool_c<truth_value>;
  95. }
  96. };
  97. //////////////////////////////////////////////////////////////////////////
  98. // Model for Products
  99. //////////////////////////////////////////////////////////////////////////
  100. template <typename T, typename U>
  101. struct less_impl<T, U, when<hana::Product<T>::value && hana::Product<U>::value>> {
  102. template <typename X, typename Y>
  103. static constexpr decltype(auto) apply(X const& x, Y const& y) {
  104. return hana::or_(
  105. hana::less(hana::first(x), hana::first(y)),
  106. hana::and_(
  107. hana::less_equal(hana::first(x), hana::first(y)),
  108. hana::less(hana::second(x), hana::second(y))
  109. )
  110. );
  111. }
  112. };
  113. //////////////////////////////////////////////////////////////////////////
  114. // Model for Sequences
  115. //////////////////////////////////////////////////////////////////////////
  116. template <typename T, typename U>
  117. struct less_impl<T, U, when<
  118. hana::Sequence<T>::value && hana::Sequence<U>::value
  119. >> {
  120. template <typename Xs, typename Ys>
  121. static constexpr auto apply(Xs const& xs, Ys const& ys)
  122. { return hana::lexicographical_compare(xs, ys); }
  123. };
  124. BOOST_HANA_NAMESPACE_END
  125. #endif // !BOOST_HANA_LESS_HPP