less_equal.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*!
  2. @file
  3. Defines `boost::hana::less_equal`.
  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_EQUAL_HPP
  9. #define BOOST_HANA_LESS_EQUAL_HPP
  10. #include <boost/hana/fwd/less_equal.hpp>
  11. #include <boost/hana/concept/orderable.hpp>
  12. #include <boost/hana/config.hpp>
  13. #include <boost/hana/core/common.hpp>
  14. #include <boost/hana/core/to.hpp>
  15. #include <boost/hana/core/dispatch.hpp>
  16. #include <boost/hana/detail/has_common_embedding.hpp>
  17. #include <boost/hana/detail/nested_than.hpp> // required by fwd decl
  18. #include <boost/hana/less.hpp>
  19. #include <boost/hana/not.hpp>
  20. #include <type_traits>
  21. BOOST_HANA_NAMESPACE_BEGIN
  22. //! @cond
  23. template <typename X, typename Y>
  24. constexpr auto less_equal_t::operator()(X&& x, Y&& y) const {
  25. using T = typename hana::tag_of<X>::type;
  26. using U = typename hana::tag_of<Y>::type;
  27. using LessEqual = BOOST_HANA_DISPATCH_IF(
  28. decltype(less_equal_impl<T, U>{}),
  29. hana::Orderable<T>::value &&
  30. hana::Orderable<U>::value
  31. );
  32. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  33. static_assert(hana::Orderable<T>::value,
  34. "hana::less_equal(x, y) requires 'x' to be Orderable");
  35. static_assert(hana::Orderable<U>::value,
  36. "hana::less_equal(x, y) requires 'y' to be Orderable");
  37. #endif
  38. return LessEqual::apply(static_cast<X&&>(x), static_cast<Y&&>(y));
  39. }
  40. //! @endcond
  41. template <typename T, typename U, bool condition>
  42. struct less_equal_impl<T, U, when<condition>> : default_ {
  43. template <typename X, typename Y>
  44. static constexpr decltype(auto) apply(X&& x, Y&& y) {
  45. return hana::not_(hana::less(static_cast<Y&&>(y),
  46. static_cast<X&&>(x)));
  47. }
  48. };
  49. // Cross-type overload
  50. template <typename T, typename U>
  51. struct less_equal_impl<T, U, when<
  52. detail::has_nontrivial_common_embedding<Orderable, T, U>::value
  53. >> {
  54. using C = typename hana::common<T, U>::type;
  55. template <typename X, typename Y>
  56. static constexpr decltype(auto) apply(X&& x, Y&& y) {
  57. return hana::less_equal(hana::to<C>(static_cast<X&&>(x)),
  58. hana::to<C>(static_cast<Y&&>(y)));
  59. }
  60. };
  61. BOOST_HANA_NAMESPACE_END
  62. #endif // !BOOST_HANA_LESS_EQUAL_HPP