GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Indirect.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_INDIRECT_HPP
5#define GREM_CORE_DATA_INDIRECT_HPP
6
8
11
12#include <compare> // std::strong_ordering, std::compare_three_way_result_t
13#include <functional> // std::hash
14#include <memory> // std::allocator, std::allocator_traits, std::to_address, std::allocator_arg...
15#include <memory_resource> // std::pmr::polymorphic_allocator
16#include <type_traits> // std::is_..._v, std::remove_..._t, std::decay_t
17#include <utility> // std::move, std::forward
18
19namespace grem {
20
21template <typename T, typename Allocator = std::allocator<T>>
22class Indirect {
23public:
24 using value_type = T;
26 using reference = T&;
27 using const_reference = const T&;
28 using pointer = typename std::allocator_traits<Allocator>::pointer;
29 using const_pointer = typename std::allocator_traits<Allocator>::const_pointer;
30
31 constexpr Indirect() requires(std::is_default_constructible_v<T> && std::is_default_constructible_v<Allocator>)
32 : allocator() {
33 emplace();
34 }
35
36 constexpr Indirect(std::allocator_arg_t, const Allocator& allocator = Allocator()) requires(std::is_default_constructible_v<T>)
37 : allocator(allocator) {
38 emplace();
39 }
40
41 template <typename Arg, typename... Args>
42 explicit(!std::is_convertible_v<Arg&&, T>) constexpr Indirect(std::allocator_arg_t, const Allocator& allocator, Arg&& arg, Args&&... args)
43 requires(std::is_constructible_v<T, Arg &&, Args && ...>)
44 : allocator(allocator) {
45 emplace(std::forward<Arg>(arg), std::forward<Args>(args)...);
46 }
47
48 template <typename U, typename... Args>
49 constexpr Indirect(std::allocator_arg_t, const Allocator& allocator, std::initializer_list<U> ilist, Args&&... args)
50 requires(std::is_constructible_v<T, std::initializer_list<U>&, Args && ...>)
51 : allocator(allocator) {
52 emplace(ilist, std::forward<Args>(args)...);
53 }
54
55 template <typename Arg, typename... Args>
56 explicit(!std::is_convertible_v<Arg&&, T>) constexpr Indirect(Arg&& arg, Args&&... args)
57 requires(!std::is_same_v<std::remove_cvref_t<Arg>, Indirect> && !std::is_convertible_v<Arg &&, std::allocator_arg_t> && std::is_constructible_v<T, Arg &&, Args && ...>)
58 : Indirect(std::allocator_arg, Allocator(), std::forward<Arg>(arg), std::forward<Args>(args)...) {}
59
60 template <typename U, typename... Args>
61 explicit constexpr Indirect(std::initializer_list<U> ilist, Args&&... args) requires(std::is_constructible_v<T, std::initializer_list<U>&, Args && ...>)
62 : Indirect(std::allocator_arg, Allocator(), ilist, std::forward<Args>(args)...) {}
63
64 constexpr Indirect(const Indirect& other)
65 : Indirect(std::allocator_arg, std::allocator_traits<Allocator>::select_on_container_copy_construction(other.get_allocator()), other) {}
66
67 constexpr Indirect(std::allocator_arg_t, const Allocator& allocator, const Indirect& other)
68 : allocator(allocator) {
69 if (other.object) {
70 emplace(*other);
71 }
72 }
73
74 constexpr Indirect(Indirect&& other) noexcept
75 : Indirect(std::allocator_arg, other.get_allocator(), std::move(other)) {}
76
77 constexpr Indirect(std::allocator_arg_t, const Allocator& allocator, Indirect&& other) noexcept // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
78 : object(std::exchange(other.object, nullptr))
79 , allocator(allocator) {}
80
82 reset();
83 }
84
85 constexpr Indirect& operator=(const Indirect& other) {
86 if (this == &other) {
87 return *this;
88 }
89 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
90 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
91 if (allocator != other.get_allocator()) {
92 reset();
93 }
94 }
95 allocator = other.get_allocator();
96 }
97 if (other.object) {
98 if (object) {
99 **this = *other;
100 } else {
101 *this = Indirect(std::allocator_arg, allocator, other);
102 }
103 } else {
104 reset();
105 }
106 return *this;
107 }
108
109 constexpr Indirect& operator=(Indirect&& other) noexcept(
110 std::allocator_traits<
111 Allocator>::propagate_on_container_move_assignment::value || // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
112 std::allocator_traits<Allocator>::is_always_equal::value) {
113 if (this == &other) {
114 return *this;
115 }
116 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value || std::allocator_traits<Allocator>::is_always_equal::value) {
117 reset();
118 object = std::exchange(other.object, nullptr);
119 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
120 allocator = other.allocator;
121 }
122 } else {
123 if (allocator == other.allocator) {
124 reset();
125 object = std::exchange(other.object, nullptr);
126 } else {
127 GREM_ASSERT(other.object);
128 *this = Indirect(std::allocator_arg, allocator, other);
129 }
130 }
131 return *this;
132 }
133
134 template <typename U = T>
135 constexpr Indirect& operator=(U&& value)
136 requires(!std::is_same_v<std::remove_cvref_t<U>, Indirect> && std::is_constructible_v<T, U> && std::is_assignable_v<T&, U> &&
137 (!std::is_scalar_v<T> || !std::is_same_v<T, std::decay_t<U>>)) {
138 if (object) {
139#ifdef __clang__
140#pragma clang diagnostic push
141#pragma clang diagnostic ignored "-Wshorten-64-to-32"
142#pragma clang diagnostic ignored "-Wfloat-conversion"
143#elif defined(__GNUC__)
144#pragma GCC diagnostic push
145#pragma GCC diagnostic ignored "-Wconversion"
146#pragma GCC diagnostic ignored "-Wfloat-conversion"
147#endif
148 **this = std::forward<U>(value);
149#ifdef __clang__
150#pragma clang diagnostic pop
151#elif defined(__GNUC__)
152#pragma GCC diagnostic pop
153#endif
154 } else {
155 emplace(std::forward<U>(value));
156 }
157 return *this;
158 }
159
160 template <typename U>
161 constexpr Indirect& operator=(const Indirect<U>& other)
162 requires(std::is_constructible_v<T, const U&> && std::is_assignable_v<T&, const U&> && !std::is_constructible_v<T, Indirect<U>&> &&
163 !std::is_constructible_v<T, const Indirect<U>&> && !std::is_constructible_v<T, Indirect<U> &&> && !std::is_constructible_v<T, const Indirect<U> &&> &&
164 !std::is_convertible_v<Indirect<U>&, T> && !std::is_convertible_v<const Indirect<U>&, T> && !std::is_convertible_v<Indirect<U> &&, T> &&
165 !std::is_convertible_v<const Indirect<U> &&, T> && !std::is_assignable_v<T&, Indirect<U>&> && !std::is_assignable_v<T&, const Indirect<U>&> &&
166 !std::is_assignable_v<T&, Indirect<U> &&> && !std::is_assignable_v<T&, const Indirect<U> &&>) {
167 if (object) {
168 if (other.object) {
169 **this = **other;
170 } else {
171 reset();
172 }
173 } else if (other.object) {
174 emplace(*other);
175 }
176 return *this;
177 }
178
179 template <typename U>
180 constexpr Indirect& operator=(Indirect<U>&& other) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
181 requires(std::is_constructible_v<T, U> && std::is_assignable_v<T&, U> && !std::is_constructible_v<T, Indirect<U>&> && !std::is_constructible_v<T, const Indirect<U>&> &&
182 !std::is_constructible_v<T, Indirect<U> &&> && !std::is_constructible_v<T, const Indirect<U> &&> && !std::is_convertible_v<Indirect<U>&, T> &&
183 !std::is_convertible_v<const Indirect<U>&, T> && !std::is_convertible_v<Indirect<U> &&, T> && !std::is_convertible_v<const Indirect<U> &&, T> &&
184 !std::is_assignable_v<T&, Indirect<U>&> && !std::is_assignable_v<T&, const Indirect<U>&> && !std::is_assignable_v<T&, Indirect<U> &&> &&
185 !std::is_assignable_v<T&, const Indirect<U> &&>) {
186 if (object) {
187 if (other.object) {
188 **this = std::move(**other);
189 } else {
190 reset();
191 }
192 } else if (other.object) {
193 emplace(std::move(*other));
194 }
195 return *this;
196 }
197
198 [[nodiscard]] allocator_type get_allocator() const noexcept {
199 return allocator;
200 }
201
202 [[nodiscard]] constexpr pointer get() const noexcept {
203 return object;
204 }
205
206 [[nodiscard]] constexpr pointer operator->() const noexcept {
207 GREM_ASSERT(object);
208 return object;
209 }
210
211 [[nodiscard]] constexpr reference operator*() const noexcept {
212 GREM_ASSERT(object);
213 return *object;
214 }
215
216private:
217 template <typename OtherT, typename OtherAllocator>
218 friend class Indirect;
219
220 void reset() noexcept {
221 if (object) {
222 std::allocator_traits<Allocator>::destroy(allocator, std::to_address(object));
223 std::allocator_traits<Allocator>::deallocate(allocator, object, 1);
224 object = nullptr;
225 }
226 }
227
228 template <typename... Args>
229 reference emplace(Args&&... args) {
230 reset();
231 object = std::allocator_traits<Allocator>::allocate(allocator, 1);
232 try {
233 std::allocator_traits<Allocator>::construct(allocator, std::to_address(object), std::forward<Args>(args)...);
234 } catch (...) {
235 std::allocator_traits<Allocator>::deallocate(allocator, object, 1);
236 throw;
237 }
238 return *object;
239 }
240
241 pointer object = nullptr;
242 [[no_unique_address]] Allocator allocator;
243};
244
245template <typename A, typename AllocatorA, typename B, typename AllocatorB>
246[[nodiscard]] constexpr bool operator==(const Indirect<A, AllocatorA>& a, const Indirect<B, AllocatorB>& b) requires(requires(const A x, const B y) { x == y; }) {
247 if (a.get() && b.get()) {
248 return *a == *b;
249 }
250 return static_cast<bool>(a.get()) == static_cast<bool>(b.get());
251}
252
253template <typename A, typename AllocatorA, typename B>
254[[nodiscard]] constexpr bool operator==(const Indirect<A, AllocatorA>& a, const B& b)
255 requires(!std::is_same_v<std::remove_cvref_t<B>, Indirect<A, AllocatorA>> &&
256 (std::is_same_v<std::remove_cv_t<A>, bool> || !template_specialization_of<std::remove_cvref_t<B>, Indirect>) && equality_comparable<A, B>) {
257#ifdef __clang__
258#pragma clang diagnostic push
259#pragma clang diagnostic ignored "-Wsign-compare"
260#elif defined(__GNUC__)
261#pragma GCC diagnostic push
262#pragma GCC diagnostic ignored "-Wsign-compare"
263#endif
264 return (a.get()) ? *a == b : false;
265#ifdef __clang__
266#pragma clang diagnostic pop
267#elif defined(__GNUC__)
268#pragma GCC diagnostic pop
269#endif
270}
271
272template <typename A, typename AllocatorA, typename B, typename AllocatorB>
273[[nodiscard]] constexpr std::compare_three_way_result_t<A, B> operator<=>(const Indirect<A, AllocatorA>& a, const Indirect<B, AllocatorB>& b) requires three_way_comparable<A, B> {
274 if (a.get() && b.get()) {
275 return *a <=> *b;
276 }
277 return static_cast<bool>(a.get()) <=> static_cast<bool>(b.get());
278}
279
280template <typename A, typename AllocatorA, typename B>
281[[nodiscard]] constexpr std::compare_three_way_result_t<A, B> operator<=>(const Indirect<A, AllocatorA>& a, const B& b)
282 requires(!std::is_same_v<std::remove_cvref_t<B>, Indirect<A, AllocatorA>> &&
283 (std::is_same_v<std::remove_cv_t<A>, bool> || !template_specialization_of<std::remove_cvref_t<B>, Indirect>) && three_way_comparable<A, B>) {
284 return (a.get()) ? *a <=> b : std::strong_ordering::less;
285}
286
287} // namespace grem
288
289template <typename T, typename Allocator>
290struct std::hash<grem::Indirect<T, Allocator>> {
291 [[nodiscard]] std::size_t operator()(const grem::Indirect<T, Allocator>& indirect) const {
292 return (indirect.get()) ? hasher(*indirect) : 0;
293 }
294
295private:
296 [[no_unique_address]] std::hash<T> hasher;
297};
298
299namespace grem::pmr {
300
301template <typename T>
303
304} // namespace grem::pmr
305
306#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition Indirect.hpp:22
constexpr Indirect(std::allocator_arg_t, const Allocator &allocator, const Indirect &other)
Definition Indirect.hpp:67
typename std::allocator_traits< Allocator >::const_pointer const_pointer
Definition Indirect.hpp:29
constexpr Indirect(const Indirect &other)
Definition Indirect.hpp:64
T value_type
Definition Indirect.hpp:24
constexpr Indirect()
Definition Indirect.hpp:31
const T & const_reference
Definition Indirect.hpp:27
constexpr Indirect(std::allocator_arg_t, const Allocator &allocator, std::initializer_list< U > ilist, Args &&... args)
Definition Indirect.hpp:49
constexpr Indirect(Indirect &&other) noexcept
Definition Indirect.hpp:74
constexpr Indirect(std::initializer_list< U > ilist, Args &&... args)
Definition Indirect.hpp:61
constexpr Indirect(std::allocator_arg_t, const Allocator &allocator=Allocator())
Definition Indirect.hpp:36
constexpr Indirect & operator=(const Indirect &other)
Definition Indirect.hpp:85
~Indirect()
Definition Indirect.hpp:81
constexpr Indirect & operator=(const Indirect< U > &other)
Definition Indirect.hpp:161
constexpr pointer operator->() const noexcept
Definition Indirect.hpp:206
Allocator allocator_type
Definition Indirect.hpp:25
allocator_type get_allocator() const noexcept
Definition Indirect.hpp:198
constexpr Indirect & operator=(Indirect &&other) noexcept(std::allocator_traits< Allocator >::propagate_on_container_move_assignment::value||//NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor) std::allocator_traits< Allocator >::is_always_equal::value)
Definition Indirect.hpp:109
constexpr Indirect & operator=(U &&value)
Definition Indirect.hpp:135
friend class Indirect
Definition Indirect.hpp:218
constexpr Indirect(std::allocator_arg_t, const Allocator &allocator, Indirect &&other) noexcept
Definition Indirect.hpp:77
T & reference
Definition Indirect.hpp:26
typename std::allocator_traits< Allocator >::pointer pointer
Definition Indirect.hpp:28
constexpr reference operator*() const noexcept
Definition Indirect.hpp:211
constexpr pointer get() const noexcept
Definition Indirect.hpp:202
constexpr Indirect & operator=(Indirect< U > &&other)
Definition Indirect.hpp:180
Definition concepts.hpp:160
Definition concepts.hpp:106
Definition concepts.hpp:41
Definition concepts.hpp:163
Definition Allocation.hpp:554
grem::Indirect< T, std::pmr::polymorphic_allocator< T > > Indirect
Definition Indirect.hpp:302
Definition aliases.hpp:9
constexpr bool operator==(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:246
constexpr std::compare_three_way_result_t< A, B > operator<=>(const Indirect< A, AllocatorA > &a, const Indirect< B, AllocatorB > &b)
Definition Indirect.hpp:273
std::size_t operator()(const grem::Indirect< T, Allocator > &indirect) const
Definition Indirect.hpp:291