Skip to main content

ErasedUnique.ixx File

Type-erased unique ownership wrapper. More...

Included Headers

#include <memory>

Namespaces Index

namespacehelios
namespacecore

Core utilities shared across the helios engine. More...

namespacememory

Low-level memory utilities for type-erased ownership. More...

Classes Index

structErasedUnique

Type-erased unique ownership wrapper. More...

Description

Type-erased unique ownership wrapper.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file ErasedUnique.ixx
3 * @brief Type-erased unique ownership wrapper.
4 */
5module;
6
7#include <memory>
8
9export module helios.core.memory.ErasedUnique;
10
11export namespace helios::core::memory {
12
13 /**
14 * @brief Type-erased unique ownership wrapper.
15 *
16 * @details ErasedUnique provides `unique_ptr`-like ownership semantics
17 * without requiring a common base class or virtual destructor. It stores
18 * a `void*` and a stateless deleter function pointer, totaling 16 bytes.
19 *
20 * Used by ResourceRegistry to store heterogeneous resource types
21 * (Managers, CommandBuffers) in a single owning collection without
22 * vtable overhead for destruction.
23 *
24 * ## Move-Only Semantics
25 *
26 * ErasedUnique is move-only. Moving transfers ownership and nulls the
27 * source. Copy construction and assignment are implicitly deleted.
28 *
29 * @see ResourceRegistry
30 */
31 struct ErasedUnique {
32
33 /**
34 * @brief Raw pointer to the owned object.
35 */
36 void* ptr = nullptr;
37
38 /**
39 * @brief Stateless deleter that knows the concrete type.
40 */
41 void (*destroy)(void*) noexcept = nullptr;
42
43 /**
44 * @brief Default constructor. Creates an empty (non-owning) instance.
45 */
46 ErasedUnique() = default;
47
48 /**
49 * @brief Constructs from a unique_ptr, taking ownership.
50 *
51 * @details Releases the unique_ptr and captures a typed deleter.
52 *
53 * @tparam T The owned type.
54 *
55 * @param p The unique_ptr to take ownership from.
56 */
57 template<class T>
58 explicit ErasedUnique(std::unique_ptr<T> p) noexcept
59 : ptr(p.release()),
60 destroy([](void* q) noexcept { delete static_cast<T*>(q); })
61 {}
62
63 /**
64 * @brief Move constructor. Transfers ownership.
65 *
66 * @param obj The source instance (nulled after move).
67 */
68 ErasedUnique(ErasedUnique&& obj) noexcept : ptr(obj.ptr), destroy(obj.destroy) {
69 obj.ptr = nullptr;
70 obj.destroy = nullptr;
71 }
72
73 /**
74 * @brief Move assignment. Transfers ownership after releasing current.
75 *
76 * @param obj The source instance (nulled after move).
77 *
78 * @return Reference to this instance.
79 */
81 if (this != &obj) {
82 reset();
83 ptr = obj.ptr; destroy = obj.destroy;
84 obj.ptr = nullptr;
85 obj.destroy = nullptr;
86 }
87 return *this;
88 }
89
90 /**
91 * @brief Destructor. Destroys the owned object if present.
92 */
94
95 /**
96 * @brief Destroys the owned object and resets to empty.
97 */
98 void reset() noexcept {
99 if (ptr && destroy) {
100 destroy(ptr);
101 }
102 ptr = nullptr;
103 destroy = nullptr;
104 }
105 };
106
107
108}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.