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