Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
compare_three_way.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_COMPARE_COMPARE_THREE_WAY_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_COMPARE_COMPARE_THREE_WAY_HPP_
7
8// IWYU pragma: private, include "arene/base/compare.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
15#include "arene/base/stdlib_choice/declval.hpp"
16#include "arene/base/stdlib_choice/enable_if.hpp"
17#include "arene/base/stdlib_choice/equal_to.hpp"
18#include "arene/base/stdlib_choice/is_integral.hpp"
19#include "arene/base/stdlib_choice/is_pointer.hpp"
20#include "arene/base/stdlib_choice/is_same.hpp"
21#include "arene/base/stdlib_choice/less.hpp"
24// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
25
26// parasoft-begin-suppress AUTOSAR-A15_4_3-a-2 "False positive: This file is the only declaration of these functions"
27
28namespace arene {
29namespace base {
30
31/// @brief Type trait to check if an instance of type @c T can be compared to an instance of type @c U with a static
32/// three-way-compare member function that returns @c strong_ordering.
33/// @tparam T The type of the lhs
34/// @tparam U The type of the rhs. Defaults to @c T
35/// @return true If @c T::three_way_compare(T,U) is defined and returns @c strong_ordering .
36/// @return false Otherwise.
37template <typename T, typename U = T, typename = constraints<>>
38extern constexpr bool is_three_way_comparable_v = false;
39
40/// @brief Specialization for the case that @c T and @c U can be compared.
41template <typename T, typename U>
42extern constexpr bool is_three_way_comparable_v<
43 T,
44 U,
45 constraints<std::enable_if_t<std::is_same<
46 decltype(T::three_way_compare(std::declval<T const&>(), std::declval<U const&>())),
47 strong_ordering>::value>>> = true;
48
49namespace three_way_compare_detail {
50
51/// @brief Class for callable objects that performs three-way comparison between two values. It provides a transparent
52/// comparison that depends on the comparisons provided by the underlying types.
53///
54/// Provides @c operator() that takes two values and returns a @c strong_ordering with the result of the comparison.
55///
56/// The order of preference for performing the comparison is as follows:
57/// 1. If the type of @c lhs side provides a @c three_way_compare static member function which is well-formed when
58/// applied to the type of @c rhs, that is used
59/// 1. If both <c>lhs < rhs</c> and <c>lhs == rhs</c> are well-formed, they are used
60/// 1. if only <c> lhs < rhs</c> and <c> rhs < lhs</c> are well-formed, they are used
61/// If none of those overloads are valid, the comparison cannot be made.
62///
63class compare_three_way {
64 public:
65 /// @brief Member that declares this is a transparent comparator.
66 using is_transparent = void;
67
68 /// @brief Compare two pointers using @c std::less
69 /// @tparam T The type of @c lhs
70 /// @tparam U The type of @c rhs
71 /// @param lhs The left hand operand to compare against
72 /// @param rhs The right hand operand to compare against
73 /// @return @c strong_ordering::equal if the values are equal, @c
74 /// strong_ordering::less if the first operand is less than the second
75 /// operand, @c strong_ordering::greater otherwise
76 template <
77 typename T,
78 typename U,
79 constraints<std::enable_if_t<std::is_pointer<T>::value>, std::enable_if_t<std::is_pointer<U>::value>> = nullptr>
80 constexpr auto operator()(T const& lhs, U const& rhs) const noexcept -> strong_ordering {
81 if (std::less<>{}(lhs, rhs)) {
82 return strong_ordering::less;
83 }
84 if (std::less<>{}(rhs, lhs)) {
85 return strong_ordering::greater;
86 }
87 return strong_ordering::equal;
88 }
89
90 /// @brief Compare two integral objects using less-than and equality in a sign-conversion safe way.
91 /// @tparam T The type of @c lhs
92 /// @tparam U The type of @c rhs
93 /// @param lhs The left hand operand to compare against
94 /// @param rhs The right hand operand to compare against
95 /// @return @c strong_ordering::equal if the values are equal
96 /// @return @c strong_ordering::less if the first operand is less than the second operand. Negative signed values
97 /// always compare less than unsigned values.
98 /// @return @c strong_ordering::greater Otherwise.
99 template <
100 typename T,
101 typename U,
102 constraints<std::enable_if_t<std::is_integral<T>::value>, std::enable_if_t<std::is_integral<U>::value>> = nullptr>
103 constexpr auto operator()(T const& lhs, U const& rhs) const noexcept -> strong_ordering {
104 if (::arene::base::cmp_equal(lhs, rhs)) {
105 return strong_ordering::equal;
106 }
107 if (::arene::base::cmp_less(lhs, rhs)) {
108 return strong_ordering::less;
109 }
110 return strong_ordering::greater;
111 }
112
114 ARENE_IGNORE_ALL("-Wfloat-equal", "Three-way comparison requires equality check");
115
116 // parasoft-begin-suppress AUTOSAR-A8_4_12-a "False positive: 'lhs' nor 'rhs' are always 'std::unique_ptr'"
117 /// @brief Compare two objects using less-than and equality.
118 /// @tparam T The type of @c lhs
119 /// @tparam U The type of @c rhs
120 /// @param lhs The left hand operand to compare against
121 /// @param rhs The right hand operand to compare against
122 /// @return @c strong_ordering::equal if the values are equal, @c strong_ordering::less if the first operand is less
123 /// than the second operand, @c strong_ordering::greater otherwise
124 template <
125 typename T,
126 typename U,
130 std::enable_if_t<!std::is_pointer<T>::value || !std::is_pointer<U>::value>,
131 std::enable_if_t<!std::is_integral<T>::value || !std::is_integral<U>::value>,
134 constexpr auto operator()(T const& lhs, U const& rhs) const
136 -> strong_ordering {
137 if (std::equal_to<>{}(lhs, rhs)) {
138 return strong_ordering::equal;
139 }
140 if (std::less<>{}(lhs, rhs)) {
141 return strong_ordering::less;
142 }
143 return strong_ordering::greater;
144 }
145 // parasoft-end-suppress AUTOSAR-A8_4_12-a
146
148
149 /// @brief Compare two objects using less-than
150 /// @tparam T The type of @c lhs
151 /// @tparam U The type of @c rhs
152 /// @param lhs The left hand operand to compare against
153 /// @param rhs The right hand operand to compare against
154 /// @return @c strong_ordering::less if the first operand is less than the second operand, @c
155 /// strong_ordering::greater if the second is less than the first, @c strong_ordering::equal otherwise.
156 template <
157 typename T,
158 typename U,
162 std::enable_if_t<!std::is_pointer<T>::value || !std::is_pointer<U>::value>,
166 constexpr auto operator()(T const& lhs, U const& rhs) const
168 -> strong_ordering {
169 if (std::less<>{}(lhs, rhs)) {
170 return strong_ordering::less;
171 }
172 if (std::less<>{}(rhs, lhs)) {
173 return strong_ordering::greater;
174 }
175 return strong_ordering::equal;
176 }
177
178 /// @brief Compare two objects using a @c three_way_compare static member function.
179 /// @tparam T The type of @c lhs
180 /// @tparam U The type of @c rhs
181 /// @param lhs The left hand operand to compare against
182 /// @param rhs The right hand operand to compare against
183 /// @return The result of @c T::three_way_compare(t,u)
184 template <typename T, typename U, constraints<std::enable_if_t<is_three_way_comparable_v<T, U>>> = nullptr>
185 constexpr auto operator()(T const& lhs, U const& rhs) const
186 noexcept(noexcept(T::three_way_compare(std::declval<T const&>(), std::declval<U const&>()))) -> strong_ordering {
187 return T::three_way_compare(lhs, rhs);
188 }
189
190 // parasoft-begin-suppress AUTOSAR-A8_4_12-a "False positive: 'lhs' is not always a 'std::unique_ptr'"
191 /// @brief Compare two objects using a @c three_way_compare static member function.
192 /// @tparam T The type of @c lhs
193 /// @tparam U The type of @c rhs
194 /// @param lhs The left hand operand to compare against
195 /// @param rhs The right hand operand to compare against
196 /// @return strong_ordering The opposite result of @c U::three_way_compare(u,t)
197 template <
198 typename T,
199 typename U,
203 constexpr auto operator()(T const& lhs, U const& rhs) const
204 noexcept(noexcept(U::three_way_compare(std::declval<U const&>(), std::declval<T const&>()))) -> strong_ordering {
205 return opposite_ordering(U::three_way_compare(rhs, lhs));
206 }
207};
208// parasoft-end-suppress AUTOSAR-A8_4_12-a
209
210} // namespace three_way_compare_detail
211
212// parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: @brief tag is present"
213/// @brief Function object that does a three-way comparison between operands, equivalent to @c std::compare_three_way .
214using compare_three_way = three_way_compare_detail::compare_three_way;
215// parasoft-end-suppress AUTOSAR-A2_7_3-a-2
216
217/// @brief Boolean constant indicating if instances of @c Lhs and @c Rhs can be compared using @c compare_three_way
218/// @tparam Lhs The type of the left-hand operand
219/// @tparam Rhs The type of the right-hand operand
220/// @{
221template <typename Lhs, typename Rhs = Lhs, typename = constraints<>>
222extern constexpr bool compare_three_way_supported_v = false;
223
224template <typename Lhs, typename Rhs>
225extern constexpr bool compare_three_way_supported_v<
226 Lhs,
227 Rhs,
229/// @}
230
231} // namespace base
232} // namespace arene
233
234#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_COMPARE_COMPARE_THREE_WAY_HPP_
static constexpr value_type value
Definition integral_constant.hpp:34
Provides macros used to disable and enable compiler specific diagnostics using the _Pragma operator.
#define ARENE_IGNORE_END()
Ends a region of a suppressed diagnostic.
Definition diagnostics.hpp:136
#define ARENE_IGNORE_START()
Begins a region of a suppressed diagnostic.
Definition diagnostics.hpp:131
#define ARENE_IGNORE_ALL(s, comment)
Pushes an ignore for a diagnostic into the compiler for any compiler.
Definition diagnostics.hpp:122
Definition accumulate.hpp:37
constexpr bool compare_three_way_supported_v
Boolean constant indicating if instances of Lhs and Rhs can be compared using compare_three_way.
strong_ordering_detail::strong_ordering strong_ordering
The enumeration specifying the result of an ordering comparison.
Definition strong_ordering.hpp:147
three_way_compare_detail::compare_three_way compare_three_way
Function object that does a three-way comparison between operands, equivalent to std::compare_three_w...
Definition compare_three_way.hpp:214
constexpr auto opposite_ordering(strong_ordering const ordering) noexcept -> strong_ordering
Return the opposite ordering for a strong_ordering three-way-comparison result.
Definition strong_ordering.hpp:209
constexpr bool is_nothrow_equality_comparable_v
constexpr bool is_three_way_comparable_v
Type trait to check if an instance of type T can be compared to an instance of type U with a static t...
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
constexpr bool is_nothrow_less_than_comparable_v
Definition accumulate.hpp:36
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
Provides backports of the safe integer comparison functions from C++20.