GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Pair.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_DATA_PAIR_HPP
5#define GREM_CORE_DATA_PAIR_HPP
6
8
9#include <compare> // std::three_way_comparable_with, std::weak_ordering, std::common_comparison_category_t
10#include <cstddef> // std::size_t
11#include <tuple> // std::tuple
12#include <type_traits> // std::is_..._v, std::basic_common_reference, std::common_reference_t
13#include <utility> // std::piecewise_construct_t, std::index_sequence, std::make_index_sequence, std::forward, std::swap
14
15namespace grem {
16
17namespace detail {
18
19template <typename T>
20concept default_copy_list_initializable = std::is_default_constructible_v<T> && requires(void (*f)(T)) { f({}); };
21
22template <typename B>
23concept boolean_testable_implementation = std::is_convertible_v<B, bool>;
24
25template <typename B>
26concept boolean_testable = boolean_testable_implementation<B> && requires(B&& b) {
27 { !std::forward<B>(b) } -> boolean_testable_implementation;
28};
29
30constexpr auto synthThreeWay = []<typename T, typename U>(const T& t, const U& u) requires(requires {
31 { t < u } -> boolean_testable;
32 { u < t } -> boolean_testable;
33}) {
34 if constexpr (std::three_way_comparable_with<T, U>) {
35 return t <=> u;
36 } else {
37 if (t < u) {
38 return std::weak_ordering::less;
39 }
40 if (u < t) {
41 return std::weak_ordering::greater;
42 }
43 return std::weak_ordering::equivalent;
44 }
45};
46
47template <typename T, typename U = T>
48using SynthThreeWayResult = decltype(synthThreeWay(std::declval<T&>(), std::declval<U&>()));
49
50} // namespace detail
51
52template <typename T1, typename T2 = T1>
53struct Pair {
54 using first_type = T1;
55 using second_type = T2;
56
59
60 constexpr explicit(!detail::default_copy_list_initializable<T1> || !detail::default_copy_list_initializable<T2>) Pair()
61 requires(std::is_default_constructible_v<T1> && std::is_default_constructible_v<T2>)
62 : first()
63 , second() {}
64
65 constexpr explicit(!std::is_convertible_v<const T1&, T1> || !std::is_convertible_v<const T2&, T2>) Pair(const T1& x, const T2& y)
66 requires(std::is_copy_constructible_v<T1> && std::is_copy_constructible_v<T2>)
67 : first(x)
68 , second(y) {}
69
70 template <typename U1, typename U2>
71 constexpr explicit(!std::is_convertible_v<U1, T1> || !std::is_convertible_v<U2, T2>) Pair(U1&& x, U2&& y)
72 requires(std::is_constructible_v<T1, U1> && std::is_constructible_v<T2, U2>)
73 : first(std::forward<U1>(x))
74 , second(std::forward<U2>(y)) {}
75
76 template <typename U1, typename U2>
77 constexpr explicit(!std::is_convertible_v<U1&, T1> || !std::is_convertible_v<U2&, T2>) Pair(Pair<U1, U2>& p)
78 requires(std::is_constructible_v<T1, U1&> && std::is_constructible_v<T2, U2&>)
79 : first(p.first)
81
82 template <typename U1, typename U2>
83 constexpr explicit(!std::is_convertible_v<const U1&, T1> || !std::is_convertible_v<const U2&, T2>) Pair(const Pair<U1, U2>& p)
84 requires(std::is_constructible_v<T1, const U1&> && std::is_constructible_v<T2, const U2&>)
85 : first(p.first)
87
88 template <typename U1, typename U2>
89 constexpr explicit(!std::is_convertible_v<U1, T1> || !std::is_convertible_v<U2, T2>) Pair(Pair<U1, U2>&& p) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
90 requires(std::is_constructible_v<T1, U1> && std::is_constructible_v<T2, U2>)
91 : first(std::forward<U1>(p.first))
92 , second(std::forward<U2>(p.second)) {}
93
94 template <typename... Args1, typename... Args2>
95 constexpr Pair(std::piecewise_construct_t piecewiseConstruct, std::tuple<Args1...> firstArgs, std::tuple<Args2...> secondArgs)
96 : Pair(piecewiseConstruct, firstArgs, std::make_index_sequence<sizeof...(Args1)>{}, secondArgs, std::make_index_sequence<sizeof...(Args2)>{}) {}
97
98 constexpr Pair(const Pair& p) = default;
99 constexpr Pair(Pair&& p) = default;
100
101 constexpr ~Pair() = default;
102
103 constexpr Pair& operator=(const Pair& other) requires(std::is_copy_assignable_v<T1> && std::is_copy_assignable_v<T2>) = default;
104 constexpr Pair& operator=(const Pair& other) requires(!std::is_copy_assignable_v<T1> || !std::is_copy_assignable_v<T2>) = delete;
105
106 template <typename U1, typename U2>
107 constexpr Pair& operator=(const Pair<U1, U2>& other) requires(std::is_assignable_v<T1&, const U1&> && std::is_assignable_v<T2&, const U2&>) {
108 first = other.first;
109 second = other.second;
110 return *this;
111 }
112
113 constexpr Pair& operator=(Pair&& other) noexcept(std::is_nothrow_move_assignable_v<T1> && std::is_nothrow_move_assignable_v<T2>)
114 requires(std::is_move_assignable_v<T1> && std::is_move_assignable_v<T2>) = default;
115
116 template <typename U1, typename U2>
117 constexpr Pair& operator=(Pair<U1, U2>&& p) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
118 requires(std::is_assignable_v<T1&, U1> && std::is_assignable_v<T2&, U2>) {
119 first = std::forward<U1>(p.first);
120 second = std::forward<U2>(p.second);
121 }
122
123 constexpr void swap(Pair& other) noexcept(std::is_nothrow_swappable_v<T1> && std::is_nothrow_swappable_v<T2>) {
124 using std::swap;
125 swap(first, other.first);
126 swap(second, other.second);
127 }
128
129 friend void swap(Pair& a, Pair& b) noexcept(noexcept(a.swap(b))) requires(std::is_swappable_v<T1> && std::is_swappable_v<T2>) {
130 a.swap(b);
131 }
132
133 [[nodiscard]] friend constexpr Pair<T2, T1> reversed(const Pair& pair) {
134 return {pair.second, pair.first};
135 }
136
137private:
138 template <typename... Args1, typename... Args2, std::size_t... Indices1, std::size_t... Indices2>
139 constexpr Pair(std::piecewise_construct_t, std::tuple<Args1...>& firstArgs, std::index_sequence<Indices1...>, std::tuple<Args2...>& secondArgs,
140 std::index_sequence<Indices2...>)
141 : first(std::forward<Args1>(std::get<Indices1>(firstArgs))...)
142 , second(std::forward<Args2>(std::get<Indices2>(secondArgs))...) {}
143};
144
145template <typename T1, typename T2>
147
148} // namespace grem
149
150template <typename T1, typename T2>
151struct std::tuple_size<grem::Pair<T1, T2>> : std::integral_constant<std::size_t, 2> {};
152
153template <typename T1, typename T2>
154struct std::tuple_element<0, grem::Pair<T1, T2>> {
155 using type = T1;
156};
157
158template <typename T1, typename T2>
159struct std::tuple_element<1, grem::Pair<T1, T2>> {
160 using type = T2;
161};
162
163template <typename T1, typename T2, typename U1, typename U2>
164requires(requires { typename grem::Pair<std::common_type_t<T1, U1>, std::common_type_t<T2, U2>>; }) struct std::common_type<grem::Pair<T1, T2>, grem::Pair<U1, U2>> {
165 using type = grem::Pair<std::common_type_t<T1, U1>, std::common_type_t<T2, U2>>;
166};
167
168template <typename T1, typename T2, typename U1, typename U2, template <typename> typename TQual, template <typename> typename UQual>
169requires(requires { typename grem::Pair<std::common_reference_t<TQual<T1>, UQual<U1>>, std::common_reference_t<TQual<T2>, UQual<U2>>>; })
170struct std::basic_common_reference<grem::Pair<T1, T2>, grem::Pair<U1, U2>, TQual, UQual> {
171 using type = grem::Pair<std::common_reference_t<TQual<T1>, UQual<U1>>, std::common_reference_t<TQual<T2>, UQual<U2>>>;
172};
173
174namespace grem {
175
176using std::tuple_element;
177using std::tuple_element_t;
178using std::tuple_size;
179using std::tuple_size_v;
180
181template <typename T1, typename T2, typename U1, typename U2>
182[[nodiscard]] constexpr bool operator==(const Pair<T1, T2>& a, const Pair<U1, U2>& b) {
183 return a.first == b.first && a.second == b.second;
184}
185
186template <typename T1, typename T2, typename U1, typename U2>
187[[nodiscard]] constexpr std::common_comparison_category_t<detail::SynthThreeWayResult<T1, U1>, detail::SynthThreeWayResult<T2, U2>> operator<=>(const Pair<T1, T2>& a,
188 const Pair<U1, U2>& b) {
189 std::common_comparison_category_t<detail::SynthThreeWayResult<T1, U1>, detail::SynthThreeWayResult<T2, U2>> result = a.first <=> b.first;
190 if (result == 0) {
191 result = a.second <=> b.second;
192 }
193 return result;
194}
195
196template <typename T1, typename T2>
197constexpr Pair<std::unwrap_ref_decay_t<T1>, std::unwrap_ref_decay_t<T2>> make_pair(T1&& x, T2&& y) {
198 return {std::forward<T1>(x), std::forward<T2>(y)};
199}
200
201template <std::size_t Index, typename T1, typename T2>
202[[nodiscard]] constexpr tuple_element_t<Index, Pair<T1, T2>>& get(Pair<T1, T2>& t) noexcept {
203 static_assert(Index == 0 || Index == 1);
204 if constexpr (Index == 0) {
205 return t.first;
206 } else {
207 return t.second;
208 }
209}
210
211template <std::size_t Index, typename T1, typename T2>
212[[nodiscard]] constexpr const tuple_element_t<Index, Pair<T1, T2>>& get(const Pair<T1, T2>& t) noexcept {
213 static_assert(Index == 0 || Index == 1);
214 if constexpr (Index == 0) {
215 return t.first;
216 } else {
217 return t.second;
218 }
219}
220
221template <std::size_t Index, typename T1, typename T2>
222[[nodiscard]] constexpr tuple_element_t<Index, Pair<T1, T2>>&& get(Pair<T1, T2>&& t) noexcept {
223 static_assert(Index == 0 || Index == 1);
224 if constexpr (Index == 0) {
225 return std::move(t).first;
226 } else {
227 return std::move(t).second;
228 }
229}
230
231template <std::size_t Index, typename T1, typename T2>
232[[nodiscard]] constexpr const tuple_element_t<Index, Pair<T1, T2>>&& get(const Pair<T1, T2>&& t) noexcept {
233 static_assert(Index == 0 || Index == 1);
234 if constexpr (Index == 0) {
235 return std::move(t).first;
236 } else {
237 return std::move(t).second;
238 }
239}
240
241template <typename T, typename U>
242[[nodiscard]] constexpr T& get(Pair<T, U>& t) noexcept {
243 return t.first;
244}
245
246template <typename T, typename U>
247[[nodiscard]] constexpr const T& get(const Pair<T, U>& t) noexcept {
248 return t.first;
249}
250
251template <typename T, typename U>
252[[nodiscard]] constexpr T&& get(Pair<T, U>&& t) noexcept {
253 return std::move(t).first;
254}
255
256template <typename T, typename U>
257[[nodiscard]] constexpr const T&& get(const Pair<T, U>&& t) noexcept {
258 return std::move(t).first;
259}
260
261template <typename T, typename U>
262[[nodiscard]] constexpr T& get(Pair<U, T>& t) noexcept {
263 return t.second;
264}
265
266template <typename T, typename U>
267[[nodiscard]] constexpr const T& get(const Pair<U, T>& t) noexcept {
268 return t.second;
269}
270
271template <typename T, typename U>
272[[nodiscard]] constexpr T&& get(Pair<U, T>&& t) noexcept {
273 return std::move(t).second;
274}
275
276template <typename T, typename U>
277[[nodiscard]] constexpr const T&& get(const Pair<U, T>&& t) noexcept {
278 return std::move(t).second;
279}
280
281} // namespace grem
282
283#endif
@ U
'u'
Definition keyboard.hpp:91
@ B
'b'
Definition keyboard.hpp:72
@ T
't'
Definition keyboard.hpp:90
Definition aliases.hpp:9
constexpr tuple_element_t< Index, Pair< T1, T2 > > & get(Pair< T1, T2 > &t) noexcept
Definition Pair.hpp:202
constexpr Pair< std::unwrap_ref_decay_t< T1 >, std::unwrap_ref_decay_t< T2 > > make_pair(T1 &&x, T2 &&y)
Definition Pair.hpp:197
constexpr bool operator==(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:246
Pair(T1, T2) -> Pair< T1, T2 >
constexpr std::compare_three_way_result_t< A, B > operator<=>(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:273
Definition Pair.hpp:53
constexpr ~Pair()=default
T second
Definition Pair.hpp:58
const Key first_type
Definition Pair.hpp:54
constexpr Pair(Pair &&p)=default
T second_type
Definition Pair.hpp:55
friend void swap(Pair &a, Pair &b) noexcept(noexcept(a.swap(b)))
Definition Pair.hpp:129
constexpr Pair(const Pair &p)=default
constexpr Pair(std::piecewise_construct_t piecewiseConstruct, std::tuple< Args1... > firstArgs, std::tuple< Args2... > secondArgs)
Definition Pair.hpp:95
constexpr U2 & p
Definition Pair.hpp:80
const Key first
Definition Pair.hpp:57
constexpr const T & y
Definition Pair.hpp:68
friend constexpr Pair< T2, T1 > reversed(const Pair &pair)
Definition Pair.hpp:133
constexpr Pair & operator=(const Pair &other)=delete
constexpr void swap(Pair &other) noexcept(std::is_nothrow_swappable_v< T1 > &&std::is_nothrow_swappable_v< T2 >)
Definition Pair.hpp:123
constexpr Pair & operator=(const Pair &other)=default
constexpr Pair & operator=(Pair &&other) noexcept(std::is_nothrow_move_assignable_v< T1 > &&std::is_nothrow_move_assignable_v< T2 >)=default
constexpr Pair & operator=(Pair< U1, U2 > &&p)
Definition Pair.hpp:117
constexpr Pair & operator=(const Pair< U1, U2 > &other)
Definition Pair.hpp:107
grem::Pair< std::common_reference_t< TQual< T1 >, UQual< U1 > >, std::common_reference_t< TQual< T2 >, UQual< U2 > > > type
Definition Pair.hpp:171
grem::Pair< std::common_type_t< T1, U1 >, std::common_type_t< T2, U2 > > type
Definition Pair.hpp:165