functors.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright (c) 2016-2025 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_FUNCTORS_HPP
  6. #define BOOST_PFR_FUNCTORS_HPP
  7. #pragma once
  8. #include <boost/pfr/detail/config.hpp>
  9. #if !defined(BOOST_USE_MODULES) || defined(BOOST_PFR_INTERFACE_UNIT)
  10. #include <boost/pfr/ops.hpp>
  11. #include <boost/pfr/detail/functional.hpp>
  12. /// \file boost/pfr/functors.hpp
  13. /// Contains functors that are close to the Standard Library ones.
  14. /// Each functor calls corresponding Boost.PFR function from boost/pfr/ops.hpp
  15. ///
  16. /// \b Example:
  17. /// \code
  18. /// #include <boost/pfr/functors.hpp>
  19. /// struct my_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. /// std::unordered_set<
  25. /// my_struct,
  26. /// boost::pfr::hash<>,
  27. /// boost::pfr::equal_to<>
  28. /// > my_set;
  29. /// \endcode
  30. ///
  31. /// \b Synopsis:
  32. namespace boost { namespace pfr {
  33. BOOST_PFR_BEGIN_MODULE_EXPORT
  34. ///////////////////// Comparisons
  35. /// \brief std::equal_to like comparator that returns \forcedlink{eq}(x, y)
  36. template <class T = void> struct equal_to {
  37. /// \return \b true if each field of \b x equals the field with same index of \b y.
  38. bool operator()(const T& x, const T& y) const {
  39. return boost::pfr::eq(x, y);
  40. }
  41. #ifdef BOOST_PFR_DOXYGEN_INVOKED
  42. /// This typedef exists only if T \b is void
  43. typedef std::true_type is_transparent;
  44. /// This operator allows comparison of \b x and \b y that have different type.
  45. /// \pre Exists only if T \b is void.
  46. template <class V, class U> bool operator()(const V& x, const U& y) const;
  47. #endif
  48. };
  49. /// @cond
  50. template <> struct equal_to<void> {
  51. template <class T, class U>
  52. bool operator()(const T& x, const U& y) const {
  53. return boost::pfr::eq(x, y);
  54. }
  55. typedef std::true_type is_transparent;
  56. };
  57. /// @endcond
  58. /// \brief std::not_equal like comparator that returns \forcedlink{ne}(x, y)
  59. template <class T = void> struct not_equal {
  60. /// \return \b true if at least one field \b x not equals the field with same index of \b y.
  61. bool operator()(const T& x, const T& y) const {
  62. return boost::pfr::ne(x, y);
  63. }
  64. #ifdef BOOST_PFR_DOXYGEN_INVOKED
  65. /// This typedef exists only if T \b is void
  66. typedef std::true_type is_transparent;
  67. /// This operator allows comparison of \b x and \b y that have different type.
  68. /// \pre Exists only if T \b is void.
  69. template <class V, class U> bool operator()(const V& x, const U& y) const;
  70. #endif
  71. };
  72. /// @cond
  73. template <> struct not_equal<void> {
  74. template <class T, class U>
  75. bool operator()(const T& x, const U& y) const {
  76. return boost::pfr::ne(x, y);
  77. }
  78. typedef std::true_type is_transparent;
  79. };
  80. /// @endcond
  81. /// \brief std::greater like comparator that returns \forcedlink{gt}(x, y)
  82. template <class T = void> struct greater {
  83. /// \return \b true if field of \b x greater than the field with same index of \b y and all previous fields of \b x equal to the same fields of \b y.
  84. bool operator()(const T& x, const T& y) const {
  85. return boost::pfr::gt(x, y);
  86. }
  87. #ifdef BOOST_PFR_DOXYGEN_INVOKED
  88. /// This typedef exists only if T \b is void
  89. typedef std::true_type is_transparent;
  90. /// This operator allows comparison of \b x and \b y that have different type.
  91. /// \pre Exists only if T \b is void.
  92. template <class V, class U> bool operator()(const V& x, const U& y) const;
  93. #endif
  94. };
  95. /// @cond
  96. template <> struct greater<void> {
  97. template <class T, class U>
  98. bool operator()(const T& x, const U& y) const {
  99. return boost::pfr::gt(x, y);
  100. }
  101. typedef std::true_type is_transparent;
  102. };
  103. /// @endcond
  104. /// \brief std::less like comparator that returns \forcedlink{lt}(x, y)
  105. template <class T = void> struct less {
  106. /// \return \b true if field of \b x less than the field with same index of \b y and all previous fields of \b x equal to the same fields of \b y.
  107. bool operator()(const T& x, const T& y) const {
  108. return boost::pfr::lt(x, y);
  109. }
  110. #ifdef BOOST_PFR_DOXYGEN_INVOKED
  111. /// This typedef exists only if T \b is void
  112. typedef std::true_type is_transparent;
  113. /// This operator allows comparison of \b x and \b y that have different type.
  114. /// \pre Exists only if T \b is void.
  115. template <class V, class U> bool operator()(const V& x, const U& y) const;
  116. #endif
  117. };
  118. /// @cond
  119. template <> struct less<void> {
  120. template <class T, class U>
  121. bool operator()(const T& x, const U& y) const {
  122. return boost::pfr::lt(x, y);
  123. }
  124. typedef std::true_type is_transparent;
  125. };
  126. /// @endcond
  127. /// \brief std::greater_equal like comparator that returns \forcedlink{ge}(x, y)
  128. template <class T = void> struct greater_equal {
  129. /// \return \b true if field of \b x greater than the field with same index of \b y and all previous fields of \b x equal to the same fields of \b y;
  130. /// or if each field of \b x equals the field with same index of \b y.
  131. bool operator()(const T& x, const T& y) const {
  132. return boost::pfr::ge(x, y);
  133. }
  134. #ifdef BOOST_PFR_DOXYGEN_INVOKED
  135. /// This typedef exists only if T \b is void
  136. typedef std::true_type is_transparent;
  137. /// This operator allows comparison of \b x and \b y that have different type.
  138. /// \pre Exists only if T \b is void.
  139. template <class V, class U> bool operator()(const V& x, const U& y) const;
  140. #endif
  141. };
  142. /// @cond
  143. template <> struct greater_equal<void> {
  144. template <class T, class U>
  145. bool operator()(const T& x, const U& y) const {
  146. return boost::pfr::ge(x, y);
  147. }
  148. typedef std::true_type is_transparent;
  149. };
  150. /// @endcond
  151. /// \brief std::less_equal like comparator that returns \forcedlink{le}(x, y)
  152. template <class T = void> struct less_equal {
  153. /// \return \b true if field of \b x less than the field with same index of \b y and all previous fields of \b x equal to the same fields of \b y;
  154. /// or if each field of \b x equals the field with same index of \b y.
  155. bool operator()(const T& x, const T& y) const {
  156. return boost::pfr::le(x, y);
  157. }
  158. #ifdef BOOST_PFR_DOXYGEN_INVOKED
  159. /// This typedef exists only if T \b is void
  160. typedef std::true_type is_transparent;
  161. /// This operator allows comparison of \b x and \b y that have different type.
  162. /// \pre Exists only if T \b is void.
  163. template <class V, class U> bool operator()(const V& x, const U& y) const;
  164. #endif
  165. };
  166. /// @cond
  167. template <> struct less_equal<void> {
  168. template <class T, class U>
  169. bool operator()(const T& x, const U& y) const {
  170. return boost::pfr::le(x, y);
  171. }
  172. typedef std::true_type is_transparent;
  173. };
  174. /// @endcond
  175. /// \brief std::hash like functor that returns \forcedlink{hash_value}(x)
  176. template <class T> struct hash {
  177. /// \return hash value of \b x.
  178. std::size_t operator()(const T& x) const {
  179. return boost::pfr::hash_value(x);
  180. }
  181. };
  182. BOOST_PFR_END_MODULE_EXPORT
  183. }} // namespace boost::pfr
  184. #endif // #if !defined(BOOST_USE_MODULES) || defined(BOOST_PFR_INTERFACE_UNIT)
  185. #endif // BOOST_PFR_FUNCTORS_HPP