ops_fields.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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_FIELDS_HPP
  6. #define BOOST_PFR_OPS_FIELDS_HPP
  7. #pragma once
  8. #include <boost/pfr/detail/config.hpp>
  9. #include <boost/pfr/core.hpp>
  10. #include <boost/pfr/detail/functional.hpp>
  11. /// \file boost/pfr/ops_fields.hpp
  12. /// Contains field-by-fields comparison and hash functions.
  13. ///
  14. /// \b Example:
  15. /// \code
  16. /// #include <boost/pfr/ops_fields.hpp>
  17. /// struct comparable_struct { // No operators defined for that structure
  18. /// int i; short s;
  19. /// };
  20. /// // ...
  21. ///
  22. /// comparable_struct s1 {0, 1};
  23. /// comparable_struct s2 {0, 2};
  24. /// assert(boost::pfr::lt_fields(s1, s2));
  25. /// \endcode
  26. ///
  27. /// \podops for other ways to define operators and more details.
  28. ///
  29. /// \b Synopsis:
  30. namespace boost { namespace pfr {
  31. /// Does a field-by-field equality comparison.
  32. ///
  33. /// \returns `L == R && tuple_size_v<T> == tuple_size_v<U>`, where `L` and
  34. /// `R` are the results of calling `std::tie` on first `N` fields of `lhs` and
  35. // `rhs` respectively; `N` is `std::min(tuple_size_v<T>, tuple_size_v<U>)`.
  36. template <class T, class U>
  37. constexpr bool eq_fields(const T& lhs, const U& rhs) noexcept {
  38. return detail::binary_visit<detail::equal_impl>(lhs, rhs);
  39. }
  40. /// Does a field-by-field inequality comparison.
  41. ///
  42. /// \returns `L != R || tuple_size_v<T> != tuple_size_v<U>`, where `L` and
  43. /// `R` are the results of calling `std::tie` on first `N` fields of `lhs` and
  44. // `rhs` respectively; `N` is `std::min(tuple_size_v<T>, tuple_size_v<U>)`.
  45. template <class T, class U>
  46. constexpr bool ne_fields(const T& lhs, const U& rhs) noexcept {
  47. return detail::binary_visit<detail::not_equal_impl>(lhs, rhs);
  48. }
  49. /// Does a field-by-field greter comparison.
  50. ///
  51. /// \returns `L > R || (L == R && tuple_size_v<T> > tuple_size_v<U>)`, where `L` and
  52. /// `R` are the results of calling `std::tie` on first `N` fields of `lhs` and
  53. // `rhs` respectively; `N` is `std::min(tuple_size_v<T>, tuple_size_v<U>)`.
  54. template <class T, class U>
  55. constexpr bool gt_fields(const T& lhs, const U& rhs) noexcept {
  56. return detail::binary_visit<detail::greater_impl>(lhs, rhs);
  57. }
  58. /// Does a field-by-field less comparison.
  59. ///
  60. /// \returns `L < R || (L == R && tuple_size_v<T> < tuple_size_v<U>)`, where `L` and
  61. /// `R` are the results of calling `std::tie` on first `N` fields of `lhs` and
  62. // `rhs` respectively; `N` is `std::min(tuple_size_v<T>, tuple_size_v<U>)`.
  63. template <class T, class U>
  64. constexpr bool lt_fields(const T& lhs, const U& rhs) noexcept {
  65. return detail::binary_visit<detail::less_impl>(lhs, rhs);
  66. }
  67. /// Does a field-by-field greater equal comparison.
  68. ///
  69. /// \returns `L > R || (L == R && tuple_size_v<T> >= tuple_size_v<U>)`, where `L` and
  70. /// `R` are the results of calling `std::tie` on first `N` fields of `lhs` and
  71. // `rhs` respectively; `N` is `std::min(tuple_size_v<T>, tuple_size_v<U>)`.
  72. template <class T, class U>
  73. constexpr bool ge_fields(const T& lhs, const U& rhs) noexcept {
  74. return detail::binary_visit<detail::greater_equal_impl>(lhs, rhs);
  75. }
  76. /// Does a field-by-field less equal comparison.
  77. ///
  78. /// \returns `L < R || (L == R && tuple_size_v<T> <= tuple_size_v<U>)`, where `L` and
  79. /// `R` are the results of calling `std::tie` on first `N` fields of `lhs` and
  80. // `rhs` respectively; `N` is `std::min(tuple_size_v<T>, tuple_size_v<U>)`.
  81. template <class T, class U>
  82. constexpr bool le_fields(const T& lhs, const U& rhs) noexcept {
  83. return detail::binary_visit<detail::less_equal_impl>(lhs, rhs);
  84. }
  85. /// Does a field-by-field hashing.
  86. ///
  87. /// \returns combined hash of all the fields
  88. template <class T>
  89. std::size_t hash_fields(const T& x) {
  90. constexpr std::size_t fields_count_val = boost::pfr::detail::fields_count<std::remove_reference_t<T>>();
  91. #if BOOST_PFR_USE_CPP17 || BOOST_PFR_USE_LOOPHOLE
  92. return detail::hash_impl<0, fields_count_val>::compute(detail::tie_as_tuple(x));
  93. #else
  94. std::size_t result = 0;
  95. ::boost::pfr::detail::for_each_field_dispatcher(
  96. x,
  97. [&result](const auto& lhs) {
  98. // We can not reuse `fields_count_val` in lambda because compilers had issues with
  99. // passing constexpr variables into lambdas. Computing is again is the most portable solution.
  100. constexpr std::size_t fields_count_val_lambda = boost::pfr::detail::fields_count<std::remove_reference_t<T>>();
  101. result = detail::hash_impl<0, fields_count_val_lambda>::compute(lhs);
  102. },
  103. detail::make_index_sequence<fields_count_val>{}
  104. );
  105. return result;
  106. #endif
  107. }
  108. }} // namespace boost::pfr
  109. #endif // BOOST_PFR_OPS_HPP