GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
SharedPointer.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_SHARED_POINTER_HPP
5#define GREM_CORE_DATA_SHARED_POINTER_HPP
6
8
11
12#include <algorithm> // std::max
13#include <cstddef> // std::size_t, std::ptrdiff_t, std::byte, std::nullptr_t
14#include <functional> // std::hash
15#include <memory> // std::construct_at, std::destroy_at, std::destroy_n, std::uninitialized_default_construct_n
16#include <new> // std::launder, std::align_val_t
17#include <type_traits> // std::is_..._v, std::remove_..._t
18#include <utility> // std::move, std::forward, std::exchange, std::swap
19
20namespace grem {
21
22namespace detail {
23
24struct alignas(std::max(std::size_t{__STDCPP_DEFAULT_NEW_ALIGNMENT__}, std::size_t{alignof(std::max_align_t)})) ControlBlockHeader {
25 Atomic<std::size_t> strongReferenceCount = 1;
26 Atomic<std::size_t> totalReferenceCount = 1;
27 std::size_t elementCount;
28 void (*destroyObject)(void* object, std::size_t elementCount) noexcept;
29
30 explicit ControlBlockHeader(std::size_t elementCount, void (*destroyObject)(void* object, std::size_t elementCount) noexcept) noexcept
31 : elementCount(elementCount)
32 , destroyObject(destroyObject) {}
33
34 void incrementStrongReferenceCount() {
35 strongReferenceCount.fetch_add(1, MemoryOrder::RELAXED);
36 }
37
38 void incrementTotalReferenceCount() {
39 totalReferenceCount.fetch_add(1, MemoryOrder::RELAXED);
40 }
41
42 [[nodiscard]] bool decrementStrongReferenceCount() {
43 return strongReferenceCount.fetch_sub(1, MemoryOrder::ACQUIRE_RELEASE) == 1;
44 }
45
46 [[nodiscard]] bool decrementTotalReferenceCount() {
47 return totalReferenceCount.fetch_sub(1, MemoryOrder::ACQUIRE_RELEASE) == 1;
48 }
49
50 [[nodiscard]] bool acquireStrongReference() {
51 return strongReferenceCount.fetch_add(1, MemoryOrder::ACQUIRE) > 0;
52 }
53
54 [[nodiscard]] std::size_t getApproximateStrongReferenceCount() const {
55 return strongReferenceCount.load(MemoryOrder::RELAXED);
56 }
57};
58
59} // namespace detail
60
61template <typename T>
62class WeakPointer;
63
64template <typename T>
66public:
67 using element_type = T;
69
70 template <typename... Args>
71 [[nodiscard]] static SharedPointer create(Args&&... args) {
72 if constexpr (alignof(T) <= alignof(detail::ControlBlockHeader)) {
73 void* const storage = operator new[](sizeof(detail::ControlBlockHeader) + sizeof(T), static_cast<std::align_val_t>(alignof(detail::ControlBlockHeader)));
74 detail::ControlBlockHeader* const controlBlock =
75 std::construct_at(static_cast<detail::ControlBlockHeader*>(storage), 1, [](void* object, std::size_t) noexcept -> void { //
76 std::destroy_at(static_cast<T*>(object));
77 });
78 try {
79 T* const object = std::construct_at(reinterpret_cast<T*>(static_cast<std::byte*>(storage) + sizeof(detail::ControlBlockHeader)), std::forward<Args>(args)...);
80 return SharedPointer{controlBlock, object};
81 } catch (...) {
82 std::destroy_at(controlBlock);
83 operator delete[](storage, static_cast<std::align_val_t>(alignof(detail::ControlBlockHeader)));
84 throw;
85 }
86 } else {
87 void* const storage = operator new[](sizeof(detail::ControlBlockHeader), static_cast<std::align_val_t>(alignof(detail::ControlBlockHeader)));
88 detail::ControlBlockHeader* const controlBlock =
89 std::construct_at(static_cast<detail::ControlBlockHeader*>(storage), 1, [](void* object, std::size_t) noexcept -> void {
90 delete static_cast<T*>(object); // NOLINT(cppcoreguidelines-owning-memory)
91 });
92 try {
93 T* const object = new T(std::forward<Args>(args)...); // NOLINT(cppcoreguidelines-owning-memory)
94 return SharedPointer{controlBlock, object};
95 } catch (...) {
96 std::destroy_at(controlBlock);
97 operator delete[](storage, static_cast<std::align_val_t>(alignof(detail::ControlBlockHeader)));
98 throw;
99 }
100 }
101 }
102
103 SharedPointer() noexcept = default;
104
105 SharedPointer(std::nullptr_t) noexcept
106 : SharedPointer() {}
107
108 SharedPointer(const SharedPointer& other) noexcept
109 : SharedPointer(other, other.object) {}
110
111 template <typename U>
112 SharedPointer(const SharedPointer<U>& other) noexcept requires(std::is_convertible_v<U*, T*>)
113 : SharedPointer(other, other.object) {}
114
115 SharedPointer(SharedPointer&& other) noexcept
116 : SharedPointer(std::move(other), std::exchange(other.object, nullptr)) {}
117
118 template <typename U>
119 SharedPointer(SharedPointer<U>&& other) noexcept requires(std::is_convertible_v<U*, T*>)
120 : SharedPointer(std::move(other), std::exchange(other.object, nullptr)) {}
121
123 reset();
124 }
125
126 SharedPointer& operator=(const SharedPointer& other) noexcept {
127 if (this == &other) {
128 return *this;
129 }
130 reset();
131 controlBlock = other.controlBlock;
132 object = other.object;
133 if (controlBlock) {
134 controlBlock->incrementStrongReferenceCount();
135 controlBlock->incrementTotalReferenceCount();
136 }
137 return *this;
138 }
139
140 template <typename U>
141 SharedPointer& operator=(const SharedPointer<U>& other) noexcept requires(std::is_convertible_v<U*, T*>) {
142 reset();
143 controlBlock = other.controlBlock;
144 object = other.object;
145 if (controlBlock) {
146 controlBlock->incrementStrongReferenceCount();
147 controlBlock->incrementTotalReferenceCount();
148 }
149 return *this;
150 }
151
153 if (this == &other) {
154 return *this;
155 }
156 reset();
157 controlBlock = std::exchange(other.controlBlock, nullptr);
158 object = std::exchange(other.object, nullptr);
159 return *this;
160 }
161
162 template <typename U>
163 SharedPointer& operator=(SharedPointer<U>&& other) noexcept requires(std::is_convertible_v<U*, T*>) { // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
164 reset();
165 controlBlock = std::exchange(other.controlBlock, nullptr);
166 object = std::exchange(other.object, nullptr);
167 return *this;
168 }
169
170 void swap(SharedPointer& other) noexcept {
171 std::swap(controlBlock, other.controlBlock);
172 std::swap(object, other.object);
173 }
174
175 friend void swap(SharedPointer& a, SharedPointer& b) noexcept {
176 a.swap(b);
177 }
178
179 void reset() noexcept {
180 if (controlBlock) {
181 if (controlBlock->decrementStrongReferenceCount()) {
182 controlBlock->destroyObject(object, controlBlock->elementCount);
183 }
184 if (controlBlock->decrementTotalReferenceCount()) {
185 GREM_ASSERT(controlBlock->getApproximateStrongReferenceCount() == 0);
186 std::destroy_at(controlBlock);
187 operator delete[](controlBlock, static_cast<std::align_val_t>(alignof(detail::ControlBlockHeader)));
188 }
189 controlBlock = nullptr;
190 object = nullptr;
191 }
192 }
193
194 [[nodiscard]] std::size_t use_count() const noexcept {
195 return (controlBlock) ? controlBlock->getApproximateStrongReferenceCount() : 0;
196 }
197
198 [[nodiscard]] T* get() const noexcept {
199 return object;
200 }
201
202 explicit operator bool() const noexcept {
203 return object != nullptr;
204 }
205
206 [[nodiscard]] decltype(auto) operator*() const noexcept requires(!std::is_void_v<T>) {
207 GREM_ASSERT(object);
208 return *object;
209 }
210
211 [[nodiscard]] T* operator->() const noexcept {
212 GREM_ASSERT(object);
213 return object;
214 }
215
216 [[nodiscard]] bool operator==(std::nullptr_t) const noexcept {
217 return object == nullptr;
218 }
219
220 template <typename U>
221 [[nodiscard]] bool operator==(const SharedPointer<U>& other) const noexcept {
222 return object == other.object;
223 }
224
225 template <typename U>
226 [[nodiscard]] auto operator<=>(const SharedPointer<U>& other) const noexcept {
227 return object <=> other.object;
228 }
229
230 template <typename U>
231 [[nodiscard]] friend SharedPointer<U> static_pointer_cast(SharedPointer&& p) noexcept {
232 return staticPointerCastImplementation<U>(std::move(p));
233 }
234
235 template <typename U>
236 [[nodiscard]] friend SharedPointer<U> static_pointer_cast(const SharedPointer& p) noexcept {
237 return staticPointerCastImplementation<U>(p);
238 }
239
240 template <typename U>
241 [[nodiscard]] friend SharedPointer<U> dynamic_pointer_cast(SharedPointer&& p) noexcept {
242 return dynamicPointerCastImplementation<U>(std::move(p));
243 }
244
245 template <typename U>
246 [[nodiscard]] friend SharedPointer<U> dynamic_pointer_cast(const SharedPointer& p) noexcept {
247 return dynamicPointerCastImplementation<U>(p);
248 }
249
250 template <typename U>
251 [[nodiscard]] friend SharedPointer<U> const_pointer_cast(SharedPointer&& p) noexcept {
252 return constPointerCastImplementation<U>(std::move(p));
253 }
254
255 template <typename U>
256 [[nodiscard]] friend SharedPointer<U> const_pointer_cast(const SharedPointer& p) noexcept {
257 return constPointerCastImplementation<U>(p);
258 }
259
260 template <typename U>
261 [[nodiscard]] friend SharedPointer<U> reinterpret_pointer_cast(SharedPointer&& p) noexcept {
262 return reinterpretPointerCastImplementation<U>(std::move(p));
263 }
264
265 template <typename U>
266 [[nodiscard]] friend SharedPointer<U> reinterpret_pointer_cast(const SharedPointer& p) noexcept {
267 return reinterpretPointerCastImplementation<U>(p);
268 }
269
270private:
271 template <typename U>
272 friend class SharedPointer;
273
274 template <typename U>
275 friend class WeakPointer;
276
277 template <typename U>
278 [[nodiscard]] static SharedPointer<U> staticPointerCastImplementation(SharedPointer&& p) noexcept {
279 return SharedPointer<U>{std::move(p), static_cast<U*>(std::exchange(p.object, nullptr))};
280 }
281
282 template <typename U>
283 [[nodiscard]] static SharedPointer<U> staticPointerCastImplementation(const SharedPointer& p) noexcept {
284 return SharedPointer<U>{p, static_cast<U*>(p.object)};
285 }
286
287 template <typename U>
288 [[nodiscard]] static SharedPointer<U> dynamicPointerCastImplementation(SharedPointer&& p) noexcept {
289 if (U* const u = dynamic_cast<U*>(std::exchange(p.object, nullptr))) {
290 return SharedPointer<U>{std::move(p), u};
291 }
292 return {};
293 }
294
295 template <typename U>
296 [[nodiscard]] static SharedPointer<U> dynamicPointerCastImplementation(const SharedPointer& p) noexcept {
297 if (U* const u = dynamic_cast<U*>(p.object)) {
298 return SharedPointer<U>{p, u};
299 }
300 return {};
301 }
302
303 template <typename U>
304 [[nodiscard]] static SharedPointer<U> constPointerCastImplementation(SharedPointer&& p) noexcept {
305 return SharedPointer<U>{std::move(p), const_cast<U*>(std::exchange(p.object, nullptr))};
306 }
307
308 template <typename U>
309 [[nodiscard]] static SharedPointer<U> constPointerCastImplementation(const SharedPointer& p) noexcept {
310 return SharedPointer<U>{p, const_cast<U*>(p.object)};
311 }
312
313 template <typename U>
314 [[nodiscard]] static SharedPointer<U> reinterpretPointerCastImplementation(SharedPointer&& p) noexcept {
315 return SharedPointer<U>{std::move(p), reinterpret_cast<U*>(std::exchange(p.object, nullptr))};
316 }
317
318 template <typename U>
319 [[nodiscard]] static SharedPointer<U> reinterpretPointerCastImplementation(const SharedPointer& p) noexcept {
320 return SharedPointer<U>{p, reinterpret_cast<U*>(p.object)};
321 }
322
323 SharedPointer(detail::ControlBlockHeader* controlBlock, T* object) noexcept
324 : controlBlock(controlBlock)
325 , object(object) {}
326
327 template <typename U>
328 SharedPointer(const SharedPointer<U>& other, T* object) noexcept
329 : SharedPointer(other.controlBlock, object) {
330 if (controlBlock) {
331 controlBlock->incrementStrongReferenceCount();
332 controlBlock->incrementTotalReferenceCount();
333 }
334 }
335
336 template <typename U>
337 SharedPointer(SharedPointer<U>&& other, T* object) noexcept // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
338 : SharedPointer(std::exchange(other.controlBlock, nullptr), object) {}
339
340 detail::ControlBlockHeader* controlBlock = nullptr;
341 T* object = nullptr;
342};
343
344template <typename T>
345class SharedPointer<T[]> {
346public:
347 using element_type = T;
349
350 [[nodiscard]] static SharedPointer create(std::size_t size) {
351 if constexpr (alignof(T) <= alignof(detail::ControlBlockHeader)) {
352 void* const storage = operator new[](sizeof(detail::ControlBlockHeader) + sizeof(T) * size, static_cast<std::align_val_t>(alignof(detail::ControlBlockHeader)));
353 detail::ControlBlockHeader* const controlBlock =
354 std::construct_at(static_cast<detail::ControlBlockHeader*>(storage), size, [](void* object, std::size_t elementCount) noexcept -> void { //
355 std::destroy_n(static_cast<T*>(object), elementCount);
356 });
357 try {
358 T* const object = reinterpret_cast<T*>(static_cast<std::byte*>(storage) + sizeof(detail::ControlBlockHeader));
359 std::uninitialized_default_construct_n(object, size);
360 return SharedPointer{controlBlock, std::launder(object)};
361 } catch (...) {
362 std::destroy_at(controlBlock);
363 operator delete[](storage, static_cast<std::align_val_t>(alignof(detail::ControlBlockHeader)));
364 throw;
365 }
366 } else {
367 void* const storage = operator new[](sizeof(detail::ControlBlockHeader), static_cast<std::align_val_t>(alignof(detail::ControlBlockHeader)));
368 detail::ControlBlockHeader* const controlBlock =
369 std::construct_at(static_cast<detail::ControlBlockHeader*>(storage), size, [](void* object, std::size_t) noexcept -> void {
370 delete[] static_cast<T*>(object); // NOLINT(cppcoreguidelines-owning-memory)
371 });
372 try {
373 T* const object = new T[size]; // NOLINT(cppcoreguidelines-owning-memory)
374 return SharedPointer{controlBlock, object};
375 } catch (...) {
376 std::destroy_at(controlBlock);
377 operator delete[](storage, static_cast<std::align_val_t>(alignof(detail::ControlBlockHeader)));
378 throw;
379 }
380 }
381 }
382
383 SharedPointer() noexcept = default;
384
385 SharedPointer(std::nullptr_t) noexcept
386 : SharedPointer() {}
387
388 SharedPointer(const SharedPointer& other) noexcept
389 : SharedPointer(other, other.object) {}
390
391 template <typename U>
392 SharedPointer(const SharedPointer<U>& other) noexcept requires(std::is_convertible_v<U*, T*>)
393 : SharedPointer(other, other.object) {}
394
395 SharedPointer(SharedPointer&& other) noexcept
396 : SharedPointer(std::move(other), std::exchange(other.object, nullptr)) {}
397
398 template <typename U>
399 SharedPointer(SharedPointer<U>&& other) noexcept requires(std::is_convertible_v<U*, T*>)
400 : SharedPointer(std::move(other), std::exchange(other.object, nullptr)) {}
401
403 reset();
404 }
405
406 SharedPointer& operator=(const SharedPointer& other) noexcept {
407 if (this == &other) {
408 return *this;
409 }
410 reset();
411 controlBlock = other.controlBlock;
412 object = other.object;
413 if (controlBlock) {
414 controlBlock->incrementStrongReferenceCount();
415 controlBlock->incrementTotalReferenceCount();
416 }
417 return *this;
418 }
419
420 template <typename U>
421 SharedPointer& operator=(const SharedPointer<U>& other) noexcept requires(std::is_convertible_v<U*, T*>) {
422 reset();
423 controlBlock = other.controlBlock;
424 object = other.object;
425 if (controlBlock) {
426 controlBlock->incrementStrongReferenceCount();
427 controlBlock->incrementTotalReferenceCount();
428 }
429 return *this;
430 }
431
433 if (this == &other) {
434 return *this;
435 }
436 reset();
437 controlBlock = std::exchange(other.controlBlock, nullptr);
438 object = std::exchange(other.object, nullptr);
439 return *this;
440 }
441
442 template <typename U>
443 SharedPointer& operator=(SharedPointer<U>&& other) noexcept requires(std::is_convertible_v<U*, T*>) { // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
444 reset();
445 controlBlock = std::exchange(other.controlBlock, nullptr);
446 object = std::exchange(other.object, nullptr);
447 return *this;
448 }
449
450 void swap(SharedPointer& other) noexcept {
451 std::swap(controlBlock, other.controlBlock);
452 std::swap(object, other.object);
453 }
454
455 friend void swap(SharedPointer& a, SharedPointer& b) noexcept {
456 a.swap(b);
457 }
458
459 void reset() noexcept {
460 if (controlBlock) {
461 if (controlBlock->decrementStrongReferenceCount()) {
462 controlBlock->destroyObject(object, controlBlock->elementCount);
463 }
464 if (controlBlock->decrementTotalReferenceCount()) {
465 GREM_ASSERT(controlBlock->getApproximateStrongReferenceCount() == 0);
466 std::destroy_at(controlBlock);
467 operator delete[](controlBlock, static_cast<std::align_val_t>(alignof(detail::ControlBlockHeader)));
468 }
469 controlBlock = nullptr;
470 object = nullptr;
471 }
472 }
473
474 [[nodiscard]] std::size_t use_count() const noexcept {
475 return (controlBlock) ? controlBlock->getApproximateStrongReferenceCount() : 0;
476 }
477
478 [[nodiscard]] T* get() const noexcept {
479 return object;
480 }
481
482 explicit operator bool() const noexcept {
483 return object != nullptr;
484 }
485
486 [[nodiscard]] decltype(auto) operator[](std::ptrdiff_t index) const noexcept requires(!std::is_void_v<T>) {
487 GREM_ASSERT(object);
488 return object[index];
489 }
490
491 [[nodiscard]] std::size_t size() const noexcept {
492 return (controlBlock) ? controlBlock->elementCount : 0;
493 }
494
495 [[nodiscard]] bool empty() const noexcept {
496 return size() == 0;
497 }
498
499 [[nodiscard]] bool operator==(std::nullptr_t) const noexcept {
500 return object == nullptr;
501 }
502
503 template <typename U>
504 [[nodiscard]] bool operator==(const SharedPointer<U>& other) const noexcept {
505 return object == other.object;
506 }
507
508 template <typename U>
509 [[nodiscard]] auto operator<=>(const SharedPointer<U>& other) const noexcept {
510 return object <=> other.object;
511 }
512
513 template <typename U>
514 [[nodiscard]] friend SharedPointer<U> static_pointer_cast(SharedPointer&& p) noexcept {
515 return SharedPointer<U>{std::move(p), static_cast<U*>(std::exchange(p.object, nullptr))};
516 }
517
518 template <typename U>
519 [[nodiscard]] friend SharedPointer<U> static_pointer_cast(const SharedPointer& p) noexcept {
520 return SharedPointer<U>{p, static_cast<U*>(p.object)};
521 }
522
523 template <typename U>
524 [[nodiscard]] friend SharedPointer<U> dynamic_pointer_cast(SharedPointer&& p) noexcept {
525 if (U* const u = dynamic_cast<U*>(std::exchange(p.object, nullptr))) {
526 return SharedPointer<U>{std::move(p), u};
527 }
528 return {};
529 }
530
531 template <typename U>
532 [[nodiscard]] friend SharedPointer<U> dynamic_pointer_cast(const SharedPointer& p) noexcept {
533 if (U* const u = dynamic_cast<U*>(p.object)) {
534 return SharedPointer<U>{p, u};
535 }
536 return {};
537 }
538
539 template <typename U>
540 [[nodiscard]] friend SharedPointer<U> const_pointer_cast(SharedPointer&& p) noexcept {
541 return SharedPointer<U>{std::move(p), const_cast<U*>(std::exchange(p.object, nullptr))};
542 }
543
544 template <typename U>
545 [[nodiscard]] friend SharedPointer<U> const_pointer_cast(const SharedPointer& p) noexcept {
546 return SharedPointer<U>{p, const_cast<U*>(p.object)};
547 }
548
549 template <typename U>
550 [[nodiscard]] friend SharedPointer<U> reinterpret_pointer_cast(SharedPointer&& p) noexcept {
551 return SharedPointer<U>{std::move(p), reinterpret_cast<U*>(std::exchange(p.object, nullptr))};
552 }
553
554 template <typename U>
555 [[nodiscard]] friend SharedPointer<U> reinterpret_pointer_cast(const SharedPointer& p) noexcept {
556 return SharedPointer<U>{p, reinterpret_cast<U*>(p.object)};
557 }
558
559private:
560 template <typename U>
561 friend class SharedPointer;
562
563 template <typename U>
564 friend class WeakPointer;
565
566 SharedPointer(detail::ControlBlockHeader* controlBlock, T* object) noexcept
567 : controlBlock(controlBlock)
568 , object(object) {}
569
570 template <typename U>
571 SharedPointer(const SharedPointer<U>& other, T* object) noexcept
572 : SharedPointer(other.controlBlock, object) {
573 if (controlBlock) {
574 controlBlock->incrementStrongReferenceCount();
575 controlBlock->incrementTotalReferenceCount();
576 }
577 }
578
579 template <typename U>
580 SharedPointer(SharedPointer<U>&& other, T* object) noexcept // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
581 : SharedPointer(std::exchange(other.controlBlock, nullptr), object) {}
582
583 detail::ControlBlockHeader* controlBlock = nullptr;
584 T* object = nullptr;
585};
586
587template <typename T>
589public:
590 using element_type = T;
591
592 WeakPointer() noexcept = default;
593
594 template <typename U>
595 WeakPointer(const SharedPointer<U>& shared) noexcept requires(std::is_convertible_v<U*, T*>)
596 : controlBlock(shared.controlBlock)
597 , object(shared.object) {
598 if (controlBlock) {
599 controlBlock->incrementTotalReferenceCount();
600 }
601 }
602
603 WeakPointer(const WeakPointer& other) noexcept
604 : controlBlock(other.controlBlock)
605 , object(other.object) {
606 if (controlBlock) {
607 controlBlock->incrementTotalReferenceCount();
608 }
609 }
610
611 template <typename U>
612 WeakPointer(const WeakPointer<U>& other) noexcept requires(std::is_convertible_v<U*, T*>)
613 : controlBlock(other.controlBlock)
614 , object(other.object) {
615 if (controlBlock) {
616 controlBlock->incrementTotalReferenceCount();
617 }
618 }
619
620 WeakPointer(WeakPointer&& other) noexcept // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
621 : controlBlock(std::exchange(other.controlBlock, nullptr))
622 , object(std::exchange(other.object, nullptr)) {}
623
624 template <typename U>
625 WeakPointer(WeakPointer<U>&& other) noexcept requires(std::is_convertible_v<U*, T*>) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
626 : controlBlock(std::exchange(other.controlBlock, nullptr))
627 , object(std::exchange(other.object, nullptr)) {}
628
630 reset();
631 }
632
633 WeakPointer& operator=(const WeakPointer& other) noexcept {
634 if (this == &other) {
635 return *this;
636 }
637 reset();
638 controlBlock = other.controlBlock;
639 object = other.object;
640 if (controlBlock) {
641 controlBlock->incrementTotalReferenceCount();
642 }
643 return *this;
644 }
645
646 template <typename U>
647 WeakPointer& operator=(const SharedPointer<U>& other) noexcept requires(std::is_convertible_v<U*, T*>) {
648 reset();
649 controlBlock = other.controlBlock;
650 object = other.object;
651 if (controlBlock) {
652 controlBlock->incrementTotalReferenceCount();
653 }
654 return *this;
655 }
656
657 template <typename U>
658 WeakPointer& operator=(const WeakPointer<U>& other) noexcept requires(std::is_convertible_v<U*, T*>) {
659 reset();
660 controlBlock = other.controlBlock;
661 object = other.object;
662 if (controlBlock) {
663 controlBlock->incrementTotalReferenceCount();
664 }
665 return *this;
666 }
667
668 WeakPointer& operator=(WeakPointer&& other) noexcept {
669 if (this == &other) {
670 return *this;
671 }
672 reset();
673 controlBlock = std::exchange(other.controlBlock, nullptr);
674 object = std::exchange(other.object, nullptr);
675 return *this;
676 }
677
678 template <typename U>
679 WeakPointer& operator=(WeakPointer<U>&& other) noexcept requires(std::is_convertible_v<U*, T*>) { // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
680 reset();
681 controlBlock = std::exchange(other.controlBlock, nullptr);
682 object = std::exchange(other.object, nullptr);
683 return *this;
684 }
685
686 void swap(WeakPointer& other) noexcept {
687 std::swap(controlBlock, other.controlBlock);
688 std::swap(object, other.object);
689 }
690
691 friend void swap(WeakPointer& a, WeakPointer& b) noexcept {
692 a.swap(b);
693 }
694
695 void reset() noexcept {
696 if (controlBlock) {
697 if (controlBlock->decrementTotalReferenceCount()) {
698 GREM_ASSERT(controlBlock->getApproximateStrongReferenceCount() == 0);
699 std::destroy_at(controlBlock);
700 operator delete[](controlBlock, static_cast<std::align_val_t>(alignof(detail::ControlBlockHeader)));
701 }
702 controlBlock = nullptr;
703 object = nullptr;
704 }
705 }
706
707 [[nodiscard]] std::size_t use_count() const noexcept {
708 return (controlBlock) ? controlBlock->getApproximateStrongReferenceCount() : 0;
709 }
710
711 [[nodiscard]] bool expired() const noexcept {
712 return use_count() == 0;
713 }
714
715 [[nodiscard]] SharedPointer<T> lock() const noexcept {
716 if (controlBlock && controlBlock->acquireStrongReference()) {
717 controlBlock->incrementTotalReferenceCount();
718 return SharedPointer<T>{controlBlock, object};
719 }
720 return {};
721 }
722
723 [[nodiscard]] T* get() const noexcept {
724 return object;
725 }
726
727 explicit operator bool() const noexcept {
728 return object != nullptr;
729 }
730
731 [[nodiscard]] bool operator==(std::nullptr_t) const noexcept {
732 return object == nullptr;
733 }
734
735 template <typename U>
736 [[nodiscard]] bool operator==(const WeakPointer<U>& other) const noexcept {
737 return object == other.object;
738 }
739
740 template <typename U>
741 [[nodiscard]] bool operator==(const SharedPointer<U>& other) const noexcept {
742 return object == other.object;
743 }
744
745 template <typename U>
746 [[nodiscard]] auto operator<=>(const WeakPointer<U>& other) const noexcept {
747 return object <=> other.object;
748 }
749
750 template <typename U>
751 [[nodiscard]] auto operator<=>(const SharedPointer<U>& other) const noexcept {
752 return object <=> other.object;
753 }
754
755private:
756 template <typename U>
757 friend class WeakPointer;
758
759 detail::ControlBlockHeader* controlBlock = nullptr;
760 T* object = nullptr;
761};
762
763template <typename T>
764class WeakPointer<T[]> {
765public:
766 using element_type = T;
767
768 WeakPointer() noexcept = default;
769
770 template <typename U>
771 WeakPointer(const SharedPointer<U>& shared) noexcept requires(std::is_convertible_v<U*, T*>)
772 : controlBlock(shared.controlBlock)
773 , object(shared.object) {
774 if (controlBlock) {
775 controlBlock->incrementTotalReferenceCount();
776 }
777 }
778
779 WeakPointer(const WeakPointer& other) noexcept
780 : controlBlock(other.controlBlock)
781 , object(other.object) {
782 if (controlBlock) {
783 controlBlock->incrementTotalReferenceCount();
784 }
785 }
786
787 template <typename U>
788 WeakPointer(const WeakPointer<U>& other) noexcept requires(std::is_convertible_v<U*, T*>)
789 : controlBlock(other.controlBlock)
790 , object(other.object) {
791 if (controlBlock) {
792 controlBlock->incrementTotalReferenceCount();
793 }
794 }
795
796 WeakPointer(WeakPointer&& other) noexcept // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
797 : controlBlock(std::exchange(other.controlBlock, nullptr))
798 , object(std::exchange(other.object, nullptr)) {}
799
800 template <typename U>
801 WeakPointer(WeakPointer<U>&& other) noexcept requires(std::is_convertible_v<U*, T*>) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
802 : controlBlock(std::exchange(other.controlBlock, nullptr))
803 , object(std::exchange(other.object, nullptr)) {}
804
806 reset();
807 }
808
809 WeakPointer& operator=(const WeakPointer& other) noexcept {
810 if (this == &other) {
811 return *this;
812 }
813 reset();
814 controlBlock = other.controlBlock;
815 object = other.object;
816 if (controlBlock) {
817 controlBlock->incrementTotalReferenceCount();
818 }
819 return *this;
820 }
821
822 template <typename U>
823 WeakPointer& operator=(const SharedPointer<U>& other) noexcept requires(std::is_convertible_v<U*, T*>) {
824 reset();
825 controlBlock = other.controlBlock;
826 object = other.object;
827 if (controlBlock) {
828 controlBlock->incrementTotalReferenceCount();
829 }
830 return *this;
831 }
832
833 template <typename U>
834 WeakPointer& operator=(const WeakPointer<U>& other) noexcept requires(std::is_convertible_v<U*, T*>) {
835 reset();
836 controlBlock = other.controlBlock;
837 object = other.object;
838 if (controlBlock) {
839 controlBlock->incrementTotalReferenceCount();
840 }
841 return *this;
842 }
843
844 WeakPointer& operator=(WeakPointer&& other) noexcept {
845 if (this == &other) {
846 return *this;
847 }
848 reset();
849 controlBlock = std::exchange(other.controlBlock, nullptr);
850 object = std::exchange(other.object, nullptr);
851 return *this;
852 }
853
854 template <typename U>
855 WeakPointer& operator=(WeakPointer<U>&& other) noexcept requires(std::is_convertible_v<U*, T*>) { // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
856 reset();
857 controlBlock = std::exchange(other.controlBlock, nullptr);
858 object = std::exchange(other.object, nullptr);
859 return *this;
860 }
861
862 void swap(WeakPointer& other) noexcept {
863 std::swap(controlBlock, other.controlBlock);
864 std::swap(object, other.object);
865 }
866
867 friend void swap(WeakPointer& a, WeakPointer& b) noexcept {
868 a.swap(b);
869 }
870
871 void reset() noexcept {
872 if (controlBlock) {
873 if (controlBlock->decrementTotalReferenceCount()) {
874 GREM_ASSERT(controlBlock->getApproximateStrongReferenceCount() == 0);
875 std::destroy_at(controlBlock);
876 operator delete[](controlBlock, static_cast<std::align_val_t>(alignof(detail::ControlBlockHeader)));
877 }
878 controlBlock = nullptr;
879 object = nullptr;
880 }
881 }
882
883 [[nodiscard]] std::size_t use_count() const noexcept {
884 return (controlBlock) ? controlBlock->getApproximateStrongReferenceCount() : 0;
885 }
886
887 [[nodiscard]] bool expired() const noexcept {
888 return use_count() == 0;
889 }
890
891 [[nodiscard]] SharedPointer<T> lock() const noexcept {
892 if (controlBlock && controlBlock->acquireStrongReference()) {
893 controlBlock->incrementTotalReferenceCount();
894 return SharedPointer<T>{controlBlock, object};
895 }
896 return {};
897 }
898
899 [[nodiscard]] T* get() const noexcept {
900 return object;
901 }
902
903 explicit operator bool() const noexcept {
904 return object != nullptr;
905 }
906
907 [[nodiscard]] bool operator==(std::nullptr_t) const noexcept {
908 return object == nullptr;
909 }
910
911 template <typename U>
912 [[nodiscard]] bool operator==(const WeakPointer<U>& other) const noexcept {
913 return object == other.object;
914 }
915
916 template <typename U>
917 [[nodiscard]] bool operator==(const SharedPointer<U>& other) const noexcept {
918 return object == other.object;
919 }
920
921 template <typename U>
922 [[nodiscard]] auto operator<=>(const WeakPointer<U>& other) const noexcept {
923 return object <=> other.object;
924 }
925
926 template <typename U>
927 [[nodiscard]] auto operator<=>(const SharedPointer<U>& other) const noexcept {
928 return object <=> other.object;
929 }
930
931private:
932 template <typename U>
933 friend class WeakPointer;
934
935 detail::ControlBlockHeader* controlBlock = nullptr;
936 T* object = nullptr;
937};
938
939} // namespace grem
940
941template <typename T>
942struct std::hash<grem::SharedPointer<T>> {
943 [[nodiscard]] std::size_t operator()(const grem::SharedPointer<T>& p) const {
944 return hasher(p.get());
945 }
946
947private:
948 [[no_unique_address]] std::hash<std::remove_const_t<T>*> hasher;
949};
950
951template <typename T>
952struct std::hash<grem::WeakPointer<T>> {
953 [[nodiscard]] std::size_t operator()(const grem::WeakPointer<T>& p) const {
954 return hasher(p.get());
955 }
956
957private:
958 [[no_unique_address]] std::hash<std::remove_const_t<T>*> hasher;
959};
960
961#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
friend SharedPointer< U > reinterpret_pointer_cast(const SharedPointer &p) noexcept
Definition SharedPointer.hpp:555
friend SharedPointer< U > static_pointer_cast(const SharedPointer &p) noexcept
Definition SharedPointer.hpp:519
friend SharedPointer< U > static_pointer_cast(SharedPointer &&p) noexcept
Definition SharedPointer.hpp:514
SharedPointer & operator=(SharedPointer &&other) noexcept
Definition SharedPointer.hpp:432
SharedPointer & operator=(const SharedPointer< U > &other) noexcept
Definition SharedPointer.hpp:421
SharedPointer(const SharedPointer< U > &other) noexcept
Definition SharedPointer.hpp:392
auto operator<=>(const SharedPointer< U > &other) const noexcept
Definition SharedPointer.hpp:509
T * get() const noexcept
Definition SharedPointer.hpp:478
friend SharedPointer< U > dynamic_pointer_cast(const SharedPointer &p) noexcept
Definition SharedPointer.hpp:532
std::size_t size() const noexcept
Definition SharedPointer.hpp:491
friend void swap(SharedPointer &a, SharedPointer &b) noexcept
Definition SharedPointer.hpp:455
SharedPointer() noexcept=default
friend class SharedPointer
Definition SharedPointer.hpp:561
WeakPointer< T[]> weak_type
Definition SharedPointer.hpp:348
friend SharedPointer< U > const_pointer_cast(const SharedPointer &p) noexcept
Definition SharedPointer.hpp:545
SharedPointer & operator=(const SharedPointer &other) noexcept
Definition SharedPointer.hpp:406
friend class WeakPointer
Definition SharedPointer.hpp:564
bool empty() const noexcept
Definition SharedPointer.hpp:495
SharedPointer & operator=(SharedPointer< U > &&other) noexcept
Definition SharedPointer.hpp:443
static SharedPointer create(std::size_t size)
Definition SharedPointer.hpp:350
std::size_t use_count() const noexcept
Definition SharedPointer.hpp:474
friend SharedPointer< U > reinterpret_pointer_cast(SharedPointer &&p) noexcept
Definition SharedPointer.hpp:550
SharedPointer(SharedPointer< U > &&other) noexcept
Definition SharedPointer.hpp:399
T element_type
Definition SharedPointer.hpp:347
SharedPointer(const SharedPointer &other) noexcept
Definition SharedPointer.hpp:388
friend SharedPointer< U > const_pointer_cast(SharedPointer &&p) noexcept
Definition SharedPointer.hpp:540
~SharedPointer()
Definition SharedPointer.hpp:402
void swap(SharedPointer &other) noexcept
Definition SharedPointer.hpp:450
friend SharedPointer< U > dynamic_pointer_cast(SharedPointer &&p) noexcept
Definition SharedPointer.hpp:524
bool operator==(std::nullptr_t) const noexcept
Definition SharedPointer.hpp:499
bool operator==(const SharedPointer< U > &other) const noexcept
Definition SharedPointer.hpp:504
SharedPointer(SharedPointer &&other) noexcept
Definition SharedPointer.hpp:395
void reset() noexcept
Definition SharedPointer.hpp:459
Definition SharedPointer.hpp:65
friend SharedPointer< U > reinterpret_pointer_cast(const SharedPointer &p) noexcept
Definition SharedPointer.hpp:266
friend SharedPointer< U > static_pointer_cast(const SharedPointer &p) noexcept
Definition SharedPointer.hpp:236
friend SharedPointer< U > static_pointer_cast(SharedPointer &&p) noexcept
Definition SharedPointer.hpp:231
SharedPointer(SharedPointer< U > &&other) noexcept
Definition SharedPointer.hpp:119
T * get() const noexcept
Definition SharedPointer.hpp:198
SharedPointer(const SharedPointer< U > &other) noexcept
Definition SharedPointer.hpp:112
friend SharedPointer< U > dynamic_pointer_cast(const SharedPointer &p) noexcept
Definition SharedPointer.hpp:246
SharedPointer & operator=(SharedPointer< U > &&other) noexcept
Definition SharedPointer.hpp:163
bool operator==(std::nullptr_t) const noexcept
Definition SharedPointer.hpp:216
friend void swap(SharedPointer &a, SharedPointer &b) noexcept
Definition SharedPointer.hpp:175
SharedPointer & operator=(const SharedPointer &other) noexcept
Definition SharedPointer.hpp:126
void swap(SharedPointer &other) noexcept
Definition SharedPointer.hpp:170
friend class SharedPointer
Definition SharedPointer.hpp:272
~SharedPointer()
Definition SharedPointer.hpp:122
friend SharedPointer< U > const_pointer_cast(const SharedPointer &p) noexcept
Definition SharedPointer.hpp:256
std::size_t use_count() const noexcept
Definition SharedPointer.hpp:194
friend class WeakPointer
Definition SharedPointer.hpp:275
static SharedPointer create(Args &&... args)
Definition SharedPointer.hpp:71
SharedPointer(SharedPointer &&other) noexcept
Definition SharedPointer.hpp:115
friend SharedPointer< U > reinterpret_pointer_cast(SharedPointer &&p) noexcept
Definition SharedPointer.hpp:261
SharedPointer & operator=(SharedPointer &&other) noexcept
Definition SharedPointer.hpp:152
bool operator==(const SharedPointer< U > &other) const noexcept
Definition SharedPointer.hpp:221
SharedPointer() noexcept=default
T * operator->() const noexcept
Definition SharedPointer.hpp:211
T element_type
Definition SharedPointer.hpp:67
SharedPointer(const SharedPointer &other) noexcept
Definition SharedPointer.hpp:108
friend SharedPointer< U > const_pointer_cast(SharedPointer &&p) noexcept
Definition SharedPointer.hpp:251
auto operator<=>(const SharedPointer< U > &other) const noexcept
Definition SharedPointer.hpp:226
WeakPointer< T > weak_type
Definition SharedPointer.hpp:68
friend SharedPointer< U > dynamic_pointer_cast(SharedPointer &&p) noexcept
Definition SharedPointer.hpp:241
SharedPointer & operator=(const SharedPointer< U > &other) noexcept
Definition SharedPointer.hpp:141
void reset() noexcept
Definition SharedPointer.hpp:179
WeakPointer() noexcept=default
WeakPointer & operator=(WeakPointer &&other) noexcept
Definition SharedPointer.hpp:844
WeakPointer(WeakPointer< U > &&other) noexcept
Definition SharedPointer.hpp:801
friend void swap(WeakPointer &a, WeakPointer &b) noexcept
Definition SharedPointer.hpp:867
~WeakPointer()
Definition SharedPointer.hpp:805
SharedPointer< T > lock() const noexcept
Definition SharedPointer.hpp:891
WeakPointer & operator=(const WeakPointer< U > &other) noexcept
Definition SharedPointer.hpp:834
bool operator==(std::nullptr_t) const noexcept
Definition SharedPointer.hpp:907
friend class WeakPointer
Definition SharedPointer.hpp:933
WeakPointer & operator=(const WeakPointer &other) noexcept
Definition SharedPointer.hpp:809
WeakPointer & operator=(const SharedPointer< U > &other) noexcept
Definition SharedPointer.hpp:823
WeakPointer(WeakPointer &&other) noexcept
Definition SharedPointer.hpp:796
void swap(WeakPointer &other) noexcept
Definition SharedPointer.hpp:862
WeakPointer & operator=(WeakPointer< U > &&other) noexcept
Definition SharedPointer.hpp:855
auto operator<=>(const SharedPointer< U > &other) const noexcept
Definition SharedPointer.hpp:927
T * get() const noexcept
Definition SharedPointer.hpp:899
bool operator==(const WeakPointer< U > &other) const noexcept
Definition SharedPointer.hpp:912
auto operator<=>(const WeakPointer< U > &other) const noexcept
Definition SharedPointer.hpp:922
WeakPointer(const WeakPointer &other) noexcept
Definition SharedPointer.hpp:779
WeakPointer(const WeakPointer< U > &other) noexcept
Definition SharedPointer.hpp:788
T element_type
Definition SharedPointer.hpp:766
bool expired() const noexcept
Definition SharedPointer.hpp:887
std::size_t use_count() const noexcept
Definition SharedPointer.hpp:883
void reset() noexcept
Definition SharedPointer.hpp:871
bool operator==(const SharedPointer< U > &other) const noexcept
Definition SharedPointer.hpp:917
Definition SharedPointer.hpp:588
WeakPointer() noexcept=default
WeakPointer(WeakPointer< U > &&other) noexcept
Definition SharedPointer.hpp:625
std::size_t use_count() const noexcept
Definition SharedPointer.hpp:707
friend void swap(WeakPointer &a, WeakPointer &b) noexcept
Definition SharedPointer.hpp:691
void swap(WeakPointer &other) noexcept
Definition SharedPointer.hpp:686
friend class WeakPointer
Definition SharedPointer.hpp:757
~WeakPointer()
Definition SharedPointer.hpp:629
void reset() noexcept
Definition SharedPointer.hpp:695
WeakPointer & operator=(WeakPointer< U > &&other) noexcept
Definition SharedPointer.hpp:679
bool expired() const noexcept
Definition SharedPointer.hpp:711
WeakPointer(const WeakPointer< U > &other) noexcept
Definition SharedPointer.hpp:612
WeakPointer & operator=(WeakPointer &&other) noexcept
Definition SharedPointer.hpp:668
WeakPointer & operator=(const SharedPointer< U > &other) noexcept
Definition SharedPointer.hpp:647
SharedPointer< T > lock() const noexcept
Definition SharedPointer.hpp:715
T * get() const noexcept
Definition SharedPointer.hpp:723
WeakPointer & operator=(const WeakPointer &other) noexcept
Definition SharedPointer.hpp:633
WeakPointer & operator=(const WeakPointer< U > &other) noexcept
Definition SharedPointer.hpp:658
WeakPointer(WeakPointer &&other) noexcept
Definition SharedPointer.hpp:620
T element_type
Definition SharedPointer.hpp:590
auto operator<=>(const WeakPointer< U > &other) const noexcept
Definition SharedPointer.hpp:746
bool operator==(std::nullptr_t) const noexcept
Definition SharedPointer.hpp:731
WeakPointer(const WeakPointer &other) noexcept
Definition SharedPointer.hpp:603
bool operator==(const SharedPointer< U > &other) const noexcept
Definition SharedPointer.hpp:741
bool operator==(const WeakPointer< U > &other) const noexcept
Definition SharedPointer.hpp:736
auto operator<=>(const SharedPointer< U > &other) const noexcept
Definition SharedPointer.hpp:751
@ U
'u'
Definition keyboard.hpp:91
@ T
't'
Definition keyboard.hpp:90
Definition aliases.hpp:9
@ ACQUIRE_RELEASE
Definition synchronization.hpp:44
@ ACQUIRE
Definition synchronization.hpp:42
@ RELAXED
Definition synchronization.hpp:40
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669
std::size_t operator()(const grem::SharedPointer< T > &p) const
Definition SharedPointer.hpp:943
std::size_t operator()(const grem::WeakPointer< T > &p) const
Definition SharedPointer.hpp:953