ops.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright (c) 2016-2021 Antony Polukhin
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PFR_OPS_HPP
  6. #define BOOST_PFR_OPS_HPP
  7. #pragma once
  8. #include <boost/pfr/detail/config.hpp>
  9. #include <boost/pfr/detail/detectors.hpp>
  10. #include <boost/pfr/ops_fields.hpp>
  11. /// \file boost/pfr/ops.hpp
  12. /// Contains comparison and hashing functions.
  13. /// If type is comparable using its own operator or its conversion operator, then the types operator is used. Otherwise
  14. /// the operation is done via corresponding function from boost/pfr/ops.hpp header.
  15. ///
  16. /// \b Example:
  17. /// \code
  18. /// #include <boost/pfr/ops.hpp>
  19. /// struct comparable_struct { // No operators defined for that structure
  20. /// int i; short s; char data[7]; bool bl; int a,b,c,d,e,f;
  21. /// };
  22. /// // ...
  23. ///
  24. /// comparable_struct s1 {0, 1, "Hello", false, 6,7,8,9,10,11};
  25. /// comparable_struct s2 {0, 1, "Hello", false, 6,7,8,9,10,11111};
  26. /// assert(boost::pfr::lt(s1, s2));
  27. /// \endcode
  28. ///
  29. /// \podops for other ways to define operators and more details.
  30. ///
  31. /// \b Synopsis:
  32. namespace boost { namespace pfr {
  33. namespace detail {
  34. ///////////////////// Helper typedefs that are used by all the ops
  35. template <template <class, class> class Detector, class T, class U>
  36. using enable_not_comp_base_t = std::enable_if_t<
  37. not_appliable<Detector, T const&, U const&>::value,
  38. bool
  39. >;
  40. template <template <class, class> class Detector, class T, class U>
  41. using enable_comp_base_t = std::enable_if_t<
  42. !not_appliable<Detector, T const&, U const&>::value,
  43. bool
  44. >;
  45. ///////////////////// std::enable_if_t like functions that enable only if types do not support operation
  46. template <class T, class U> using enable_not_eq_comp_t = enable_not_comp_base_t<comp_eq_detector, T, U>;
  47. template <class T, class U> using enable_not_ne_comp_t = enable_not_comp_base_t<comp_ne_detector, T, U>;
  48. template <class T, class U> using enable_not_lt_comp_t = enable_not_comp_base_t<comp_lt_detector, T, U>;
  49. template <class T, class U> using enable_not_le_comp_t = enable_not_comp_base_t<comp_le_detector, T, U>;
  50. template <class T, class U> using enable_not_gt_comp_t = enable_not_comp_base_t<comp_gt_detector, T, U>;
  51. template <class T, class U> using enable_not_ge_comp_t = enable_not_comp_base_t<comp_ge_detector, T, U>;
  52. template <class T> using enable_not_hashable_t = std::enable_if_t<
  53. not_appliable<hash_detector, const T&, const T&>::value,
  54. std::size_t
  55. >;
  56. ///////////////////// std::enable_if_t like functions that enable only if types do support operation
  57. template <class T, class U> using enable_eq_comp_t = enable_comp_base_t<comp_eq_detector, T, U>;
  58. template <class T, class U> using enable_ne_comp_t = enable_comp_base_t<comp_ne_detector, T, U>;
  59. template <class T, class U> using enable_lt_comp_t = enable_comp_base_t<comp_lt_detector, T, U>;
  60. template <class T, class U> using enable_le_comp_t = enable_comp_base_t<comp_le_detector, T, U>;
  61. template <class T, class U> using enable_gt_comp_t = enable_comp_base_t<comp_gt_detector, T, U>;
  62. template <class T, class U> using enable_ge_comp_t = enable_comp_base_t<comp_ge_detector, T, U>;
  63. template <class T> using enable_hashable_t = std::enable_if_t<
  64. !not_appliable<hash_detector, const T&, const T&>::value,
  65. std::size_t
  66. >;
  67. } // namespace detail
  68. /// \brief Compares lhs and rhs for equality using their own comparison and conversion operators; if no operators avalable returns \forcedlink{eq_fields}(lhs, rhs).
  69. ///
  70. /// \returns true if lhs is equal to rhs; false otherwise
  71. template <class T, class U>
  72. constexpr detail::enable_not_eq_comp_t<T, U> eq(const T& lhs, const U& rhs) noexcept {
  73. return boost::pfr::eq_fields(lhs, rhs);
  74. }
  75. /// \overload eq
  76. template <class T, class U>
  77. constexpr detail::enable_eq_comp_t<T, U> eq(const T& lhs, const U& rhs) {
  78. return lhs == rhs;
  79. }
  80. /// \brief Compares lhs and rhs for inequality using their own comparison and conversion operators; if no operators avalable returns \forcedlink{ne_fields}(lhs, rhs).
  81. ///
  82. /// \returns true if lhs is not equal to rhs; false otherwise
  83. template <class T, class U>
  84. constexpr detail::enable_not_ne_comp_t<T, U> ne(const T& lhs, const U& rhs) noexcept {
  85. return boost::pfr::ne_fields(lhs, rhs);
  86. }
  87. /// \overload ne
  88. template <class T, class U>
  89. constexpr detail::enable_ne_comp_t<T, U> ne(const T& lhs, const U& rhs) {
  90. return lhs != rhs;
  91. }
  92. /// \brief Compares lhs and rhs for less-than using their own comparison and conversion operators; if no operators avalable returns \forcedlink{lt_fields}(lhs, rhs).
  93. ///
  94. /// \returns true if lhs is less than rhs; false otherwise
  95. template <class T, class U>
  96. constexpr detail::enable_not_lt_comp_t<T, U> lt(const T& lhs, const U& rhs) noexcept {
  97. return boost::pfr::lt_fields(lhs, rhs);
  98. }
  99. /// \overload lt
  100. template <class T, class U>
  101. constexpr detail::enable_lt_comp_t<T, U> lt(const T& lhs, const U& rhs) {
  102. return lhs < rhs;
  103. }
  104. /// \brief Compares lhs and rhs for greater-than using their own comparison and conversion operators; if no operators avalable returns \forcedlink{lt_fields}(lhs, rhs).
  105. ///
  106. /// \returns true if lhs is greater than rhs; false otherwise
  107. template <class T, class U>
  108. constexpr detail::enable_not_gt_comp_t<T, U> gt(const T& lhs, const U& rhs) noexcept {
  109. return boost::pfr::gt_fields(lhs, rhs);
  110. }
  111. /// \overload gt
  112. template <class T, class U>
  113. constexpr detail::enable_gt_comp_t<T, U> gt(const T& lhs, const U& rhs) {
  114. return lhs > rhs;
  115. }
  116. /// \brief Compares lhs and rhs for less-equal using their own comparison and conversion operators; if no operators avalable returns \forcedlink{le_fields}(lhs, rhs).
  117. ///
  118. /// \returns true if lhs is less or equal to rhs; false otherwise
  119. template <class T, class U>
  120. constexpr detail::enable_not_le_comp_t<T, U> le(const T& lhs, const U& rhs) noexcept {
  121. return boost::pfr::le_fields(lhs, rhs);
  122. }
  123. /// \overload le
  124. template <class T, class U>
  125. constexpr detail::enable_le_comp_t<T, U> le(const T& lhs, const U& rhs) {
  126. return lhs <= rhs;
  127. }
  128. /// \brief Compares lhs and rhs for greater-equal using their own comparison and conversion operators; if no operators avalable returns \forcedlink{ge_fields}(lhs, rhs).
  129. ///
  130. /// \returns true if lhs is greater or equal to rhs; false otherwise
  131. template <class T, class U>
  132. constexpr detail::enable_not_ge_comp_t<T, U> ge(const T& lhs, const U& rhs) noexcept {
  133. return boost::pfr::ge_fields(lhs, rhs);
  134. }
  135. /// \overload ge
  136. template <class T, class U>
  137. constexpr detail::enable_ge_comp_t<T, U> ge(const T& lhs, const U& rhs) {
  138. return lhs >= rhs;
  139. }
  140. /// \brief Hashes value using its own std::hash specialization; if no std::hash specialization avalable returns \forcedlink{hash_fields}(value).
  141. ///
  142. /// \returns std::size_t with hash of the value
  143. template <class T>
  144. constexpr detail::enable_not_hashable_t<T> hash_value(const T& value) noexcept {
  145. return boost::pfr::hash_fields(value);
  146. }
  147. /// \overload hash_value
  148. template <class T>
  149. constexpr detail::enable_hashable_t<T> hash_value(const T& value) {
  150. return std::hash<T>{}(value);
  151. }
  152. }} // namespace boost::pfr
  153. #endif // BOOST_PFR_OPS_HPP