GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
UniquePointer.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_UNIQUE_POINTER_HPP
5#define GREM_CORE_DATA_UNIQUE_POINTER_HPP
6
8
10
11#include <cstddef> // std::size_t, std::nullptr_t
12#include <type_traits> // std::is_same_v, std::is_convertible_v, std::is_void_v, std::remove_reference_t
13#include <utility> // std::move, std::forward
14
15namespace grem {
16
17namespace detail {
18
19template <typename T, typename Deleter>
20struct deleter_pointer_type {
21 using type = T*;
22};
23
24template <typename T, typename Deleter>
25requires(requires { typename Deleter::pointer; }) struct deleter_pointer_type<T, Deleter> {
26 using type = typename Deleter::pointer;
27};
28
29}; // namespace detail
30
36template <typename T>
38public:
39 constexpr DefaultDelete() noexcept = default;
40
41 template <typename U>
42 constexpr DefaultDelete(const DefaultDelete<U>&) noexcept requires(std::is_convertible_v<U*, T*>) {}
43
44 constexpr void operator()(T* handle) const noexcept {
45 delete handle; // NOLINT(cppcoreguidelines-owning-memory)
46 }
47};
48
54template <typename T>
55class DefaultDelete<T[]> {
56public:
57 constexpr DefaultDelete() noexcept = default;
58
59 template <typename U>
60 constexpr DefaultDelete(const DefaultDelete<U[]>&) noexcept requires(std::is_convertible_v<U (*)[], T (*)[]>) {}
61
62 template <typename U>
63 constexpr void operator()(U* handle) const noexcept requires(std::is_convertible_v<U (*)[], T (*)[]>) {
64 delete[] handle; // NOLINT(cppcoreguidelines-owning-memory)
65 }
66};
67
76template <typename T, typename Deleter = DefaultDelete<T>>
78public:
80 using pointer = typename detail::deleter_pointer_type<T, std::remove_reference_t<Deleter>>::type;
81
83 using element_type = T;
84
86 using deleter_type = Deleter;
87
100 template <typename... Args>
101 [[nodiscard]] static constexpr UniquePointer create(Args&&... args) {
102 return UniquePointer{new T(std::forward<Args>(args)...)};
103 }
104
108 constexpr UniquePointer() noexcept = default;
109
115 constexpr UniquePointer(std::nullptr_t pointer) noexcept {
116 (void)pointer;
117 }
118
125 template <typename U, typename OtherDeleter>
126 constexpr UniquePointer(UniquePointer<U, OtherDeleter>&& other) noexcept // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
127 requires(!std::is_same_v<T, U> && std::is_convertible_v<typename UniquePointer<U, OtherDeleter>::pointer, pointer>)
128 : handle(other.release(), std::move(other.get_deleter())) {}
129
137 constexpr explicit UniquePointer(pointer pointer, Deleter deleter = Deleter()) noexcept
138 : handle(pointer, std::move(deleter)) {}
139
146 constexpr explicit operator bool() const noexcept {
147 return static_cast<bool>(handle);
148 }
149
161 [[nodiscard]] constexpr bool operator==(const UniquePointer& other) const noexcept {
162 return handle.get() == other.handle.get();
163 }
164
172 [[nodiscard]] constexpr bool operator==(std::nullptr_t other) const noexcept {
173 (void)other;
174 return !handle;
175 }
176
187 constexpr void reset(pointer newPointer = nullptr) noexcept {
188 handle.reset(newPointer);
189 }
190
210 constexpr pointer release() noexcept {
211 return handle.release();
212 }
213
222 [[nodiscard]] constexpr pointer get() const noexcept {
223 return handle.get();
224 }
225
231 [[nodiscard]] constexpr Deleter& get_deleter() noexcept {
232 return handle.get_deleter();
233 }
234
240 [[nodiscard]] constexpr const Deleter& get_deleter() const noexcept {
241 return handle.get_deleter();
242 }
243
254 [[nodiscard]] constexpr pointer operator->() const noexcept {
255 return handle.get();
256 }
257
268 [[nodiscard]] constexpr decltype(auto) operator*() const noexcept requires(!std::is_void_v<T>) {
269 return *handle.get();
270 }
271
272private:
274};
275
284template <typename T, typename Deleter>
285class UniquePointer<T[], Deleter> {
286public:
288 using pointer = typename detail::deleter_pointer_type<T, std::remove_reference_t<Deleter>>::type;
289
291 using element_type = T;
292
294 using deleter_type = Deleter;
295
308 [[nodiscard]] static constexpr UniquePointer create(std::size_t size) {
309 return UniquePointer{new T[size]};
310 }
311
315 constexpr UniquePointer() noexcept = default;
316
322 constexpr UniquePointer(std::nullptr_t pointer) noexcept {
323 (void)pointer;
324 }
325
333 constexpr explicit UniquePointer(pointer pointer, Deleter deleter = Deleter()) noexcept
334 : handle(pointer, std::move(deleter)) {}
335
342 constexpr explicit operator bool() const noexcept {
343 return static_cast<bool>(handle);
344 }
345
357 [[nodiscard]] constexpr bool operator==(const UniquePointer& other) const noexcept {
358 return handle.get() == other.handle.get();
359 }
360
368 [[nodiscard]] constexpr bool operator==(std::nullptr_t other) const noexcept {
369 (void)other;
370 return !handle;
371 }
372
383 constexpr void reset(pointer newPointer = nullptr) noexcept {
384 handle.reset(newPointer);
385 }
386
406 constexpr pointer release() noexcept {
407 return handle.release();
408 }
409
419 [[nodiscard]] constexpr pointer get() const noexcept {
420 return handle.get();
421 }
422
428 [[nodiscard]] constexpr Deleter& get_deleter() noexcept {
429 return handle.get_deleter();
430 }
431
437 [[nodiscard]] constexpr const Deleter& get_deleter() const noexcept {
438 return handle.get_deleter();
439 }
440
453 [[nodiscard]] constexpr decltype(auto) operator[](std::size_t index) const noexcept requires(!std::is_void_v<T>) {
454 return handle.get()[index];
455 }
456
457private:
459};
460
461} // namespace grem
462
463#endif
constexpr DefaultDelete() noexcept=default
constexpr void operator()(U *handle) const noexcept
Definition UniquePointer.hpp:63
constexpr DefaultDelete() noexcept=default
constexpr void operator()(T *handle) const noexcept
Definition UniquePointer.hpp:44
Generic nullable RAII resource handle with exclusive ownership of a resource that is automatically de...
Definition UniqueHandle.hpp:26
Deleter deleter_type
Deleter type.
Definition UniquePointer.hpp:294
T element_type
Object type.
Definition UniquePointer.hpp:291
constexpr pointer get() const noexcept
Get the underlying array pointer.
Definition UniquePointer.hpp:419
constexpr void reset(pointer newPointer=nullptr) noexcept
Destroy the array associated with this pointer, if any, and take ownership of a new array,...
Definition UniquePointer.hpp:383
static constexpr UniquePointer create(std::size_t size)
Allocate an array of default-initialized elements of the associated type and wrap it in a pointer wit...
Definition UniquePointer.hpp:308
constexpr Deleter & get_deleter() noexcept
Get the current deleter.
Definition UniquePointer.hpp:428
constexpr pointer release() noexcept
Relinquish ownership of the associated array.
Definition UniquePointer.hpp:406
constexpr const Deleter & get_deleter() const noexcept
Get the current deleter.
Definition UniquePointer.hpp:437
constexpr bool operator==(const UniquePointer &other) const noexcept
Compare this pointer against another for equality of the underlying address.
Definition UniquePointer.hpp:357
constexpr UniquePointer(pointer pointer, Deleter deleter=Deleter()) noexcept
Construct a pointer that takes ownership of an existing array.
Definition UniquePointer.hpp:333
constexpr UniquePointer() noexcept=default
Construct a null pointer without an associated array.
constexpr bool operator==(std::nullptr_t other) const noexcept
Check if this pointer is null and has no associated array.
Definition UniquePointer.hpp:368
constexpr pointer get() const noexcept
Get the underlying pointer.
Definition UniquePointer.hpp:222
constexpr bool operator==(std::nullptr_t other) const noexcept
Check if this pointer is null and has no associated object.
Definition UniquePointer.hpp:172
T element_type
Object type.
Definition UniquePointer.hpp:83
constexpr UniquePointer(UniquePointer< U, OtherDeleter > &&other) noexcept
Construct a pointer from another pointer of a derived type, taking ownership of its underlying object...
Definition UniquePointer.hpp:126
constexpr const Deleter & get_deleter() const noexcept
Get the current deleter.
Definition UniquePointer.hpp:240
constexpr UniquePointer() noexcept=default
Construct a null pointer without an associated object.
constexpr pointer release() noexcept
Relinquish ownership of the associated object.
Definition UniquePointer.hpp:210
constexpr UniquePointer(pointer pointer, Deleter deleter=Deleter()) noexcept
Construct a pointer that takes ownership of an existing object pointer.
Definition UniquePointer.hpp:137
constexpr bool operator==(const UniquePointer &other) const noexcept
Compare this pointer against another for equality of the underlying address.
Definition UniquePointer.hpp:161
Deleter deleter_type
Deleter type.
Definition UniquePointer.hpp:86
constexpr Deleter & get_deleter() noexcept
Get the current deleter.
Definition UniquePointer.hpp:231
static constexpr UniquePointer create(Args &&... args)
Allocate and construct an object of the associated type and wrap it in a pointer with unique ownershi...
Definition UniquePointer.hpp:101
constexpr void reset(pointer newPointer=nullptr) noexcept
Destroy the object associated with this pointer, if any, and take ownership of a new object pointer,...
Definition UniquePointer.hpp:187
constexpr pointer operator->() const noexcept
Access the associated object by pointer.
Definition UniquePointer.hpp:254
Definition concepts.hpp:106
@ T
't'
Definition keyboard.hpp:90
Definition aliases.hpp:9
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669