functors.hpp 6.9 KB

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