Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
Handle.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
15#pragma once
16
17// =============================================================================
18// Includes
19// =============================================================================
20
21// Project includes
22#include "../core/Types.hpp"
23
24// Standard library
25#include <functional>
26
27namespace esengine::resource {
28
29// =============================================================================
30// Handle Template
31// =============================================================================
32
49template<typename T>
50class Handle {
51public:
52 using IdType = u32;
53
54 // Bit-packing math is shared with Entity via PackedId (single source). The
55 // resource handle split stays 20+12 (independent of the ECS entity split).
57 static constexpr u32 INDEX_BITS = Layout::INDEX_BITS;
58 static constexpr u32 GEN_BITS = Layout::GEN_BITS;
59 static constexpr u32 INDEX_MASK = Layout::INDEX_MASK;
60 static constexpr u32 GEN_MASK = Layout::GEN_MASK;
61 static constexpr IdType INVALID = 0;
62
64 Handle() = default;
65
70 explicit Handle(IdType id) : id_(id) {}
71
80
82 bool isValid() const { return id_ != INVALID; }
83
85 IdType id() const { return id_; }
86
88 u32 index() const { return Layout::indexOf(id_); }
89
91 u32 generation() const { return Layout::generationOf(id_); }
92
94 static u32 extractIndex(IdType packed) { return Layout::indexOf(packed); }
95
97 static u32 extractGeneration(IdType packed) { return Layout::generationOf(packed); }
98
100 bool operator==(const Handle& other) const { return id_ == other.id_; }
101
103 bool operator!=(const Handle& other) const { return id_ != other.id_; }
104
106 explicit operator bool() const { return isValid(); }
107
108private:
109 IdType id_ = INVALID;
110};
111
112// =============================================================================
113// SharedHandle (RAII wrapper)
114// =============================================================================
115
134template<typename T>
136public:
137 using ReleaseFunc = std::function<void(Handle<T>)>;
138
139 SharedHandle() = default;
140
142 static SharedHandle make(Handle<T> handle, ReleaseFunc release) {
143 SharedHandle sh;
144 if (handle.isValid()) {
145 sh.state_ = std::make_shared<State>(handle, std::move(release));
146 }
147 return sh;
148 }
149
151 Handle<T> get() const { return state_ ? state_->handle : Handle<T>{}; }
152
154 bool isValid() const { return state_ && state_->handle.isValid(); }
155 explicit operator bool() const { return isValid(); }
156
158 void reset() { state_.reset(); }
159
160private:
161 struct State {
162 Handle<T> handle;
163 ReleaseFunc release;
164
165 State(Handle<T> h, ReleaseFunc r) : handle(h), release(std::move(r)) {}
166 ~State() {
167 if (handle.isValid() && release) {
168 release(handle);
169 }
170 }
171
172 State(const State&) = delete;
173 State& operator=(const State&) = delete;
174 };
175
176 Shared<State> state_;
177};
178
179} // namespace esengine::resource
180
181// =============================================================================
182// Forward Declarations
183// =============================================================================
184
185namespace esengine {
186 class Shader;
187 class Texture;
188 class VertexBuffer;
189 class IndexBuffer;
190 class Mesh;
191 class Environment;
192 class ProbeVolume;
193}
194
195// Forward-declared only — the native spine runtime was removed (spine is fully
196// side-module now), but SpineAnimation keeps its (vestigial) skeleton-data handle
197// field so the component layout/hash is unchanged. Handle<T> stores just an id,
198// so T never needs a definition here.
200 struct SpineSkeletonData;
201}
202
203namespace esengine::text {
204 class BitmapFont;
205}
206
207// =============================================================================
208// Handle Type Aliases
209// =============================================================================
210
211namespace esengine::resource {
212
215
218
221
224
227
230
233
236
239
242
245
248
249} // namespace esengine::resource
250
251// =============================================================================
252// Std Hash Support
253// =============================================================================
254
255namespace std {
256
257template<typename T>
258struct hash<esengine::resource::Handle<T>> {
259 size_t operator()(const esengine::resource::Handle<T>& handle) const noexcept {
260 return hash<esengine::u32>{}(handle.id());
261 }
262};
263
264} // namespace std
Core type definitions and utilities for ESEngine.
The two halves of an image-based light, as the importer baked them.
GPU buffer for index data.
Definition Buffer.hpp:337
Buffers, the layout that describes them, and the bounds culling reads.
Definition Mesh.hpp:85
Irradiance at a grid of points — an Environment sampled, not held.
GPU shader program for rendering.
Definition Shader.hpp:86
2D texture for GPU rendering
Definition Texture.hpp:116
GPU buffer for vertex data, static or dynamic; all GPU work goes through the device.
Definition Buffer.hpp:188
Type-safe resource handle.
Definition Handle.hpp:50
static u32 extractIndex(IdType packed)
Extracts index from a raw packed ID.
Definition Handle.hpp:94
Handle(IdType id)
Creates a handle from a packed ID (index + generation).
Definition Handle.hpp:70
static Handle fromParts(u32 index, u32 generation)
Creates a handle from separate index and generation.
Definition Handle.hpp:77
bool isValid() const
Checks if the handle references a valid resource.
Definition Handle.hpp:82
bool operator==(const Handle &other) const
Equality comparison.
Definition Handle.hpp:100
bool operator!=(const Handle &other) const
Inequality comparison.
Definition Handle.hpp:103
PackedId< 20, 12 > Layout
Definition Handle.hpp:56
Handle()=default
Creates an invalid handle.
static u32 extractGeneration(IdType packed)
Extracts generation from a raw packed ID.
Definition Handle.hpp:97
RAII wrapper around Handle that auto-releases on destruction.
Definition Handle.hpp:135
Handle< T > get() const
Gets the underlying raw handle (no ownership transfer).
Definition Handle.hpp:151
std::function< void(Handle< T >)> ReleaseFunc
Definition Handle.hpp:137
bool isValid() const
Checks if valid.
Definition Handle.hpp:154
static SharedHandle make(Handle< T > handle, ReleaseFunc release)
Creates a SharedHandle that will auto-release.
Definition Handle.hpp:142
void reset()
Manually release (all copies become invalid).
Definition Handle.hpp:158
Handle< esengine::Shader > ShaderHandle
Handle to a shader resource.
Definition Handle.hpp:214
Handle< esengine::text::BitmapFont > BitmapFontHandle
Handle to a bitmap font resource.
Definition Handle.hpp:238
Handle< esengine::Environment > EnvironmentHandle
Handle to a baked environment (irradiance + prefiltered reflection).
Definition Handle.hpp:229
SharedHandle< esengine::text::BitmapFont > SharedBitmapFontHandle
RAII handle to a bitmap font resource.
Definition Handle.hpp:247
Handle< esengine::IndexBuffer > IndexBufferHandle
Handle to an index buffer resource.
Definition Handle.hpp:223
Handle< esengine::VertexBuffer > VertexBufferHandle
Handle to a vertex buffer resource.
Definition Handle.hpp:220
Handle< esengine::Texture > TextureHandle
Handle to a texture resource.
Definition Handle.hpp:217
Handle< esengine::ProbeVolume > ProbeVolumeHandle
Handle to a grid of baked irradiance.
Definition Handle.hpp:232
Handle< esengine::Mesh > MeshHandle
Handle to GPU-resident geometry.
Definition Handle.hpp:226
Handle< esengine::spine::SpineSkeletonData > SpineDataHandle
Handle to a Spine skeleton data resource (vestigial — see fwd decl).
Definition Handle.hpp:235
SharedHandle< esengine::Texture > SharedTextureHandle
RAII handle to a texture resource.
Definition Handle.hpp:244
SharedHandle< esengine::Shader > SharedShaderHandle
RAII handle to a shader resource.
Definition Handle.hpp:241
std::shared_ptr< T > Shared
Shared ownership smart pointer.
Definition Types.hpp:82
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
Index+generation packing into a u32, parameterized by the bit split.
Definition PackedId.hpp:42
static constexpr std::uint32_t generationOf(std::uint32_t raw)
Definition PackedId.hpp:62
static constexpr std::uint32_t INDEX_MASK
Definition PackedId.hpp:51
static constexpr std::uint32_t pack(std::uint32_t index, std::uint32_t generation)
Definition PackedId.hpp:71
static constexpr std::uint32_t INDEX_BITS
Definition PackedId.hpp:49
static constexpr std::uint32_t indexOf(std::uint32_t raw)
Definition PackedId.hpp:60
static constexpr std::uint32_t GEN_BITS
Definition PackedId.hpp:50
static constexpr std::uint32_t GEN_MASK
Definition PackedId.hpp:52
size_t operator()(const esengine::resource::Handle< T > &handle) const noexcept
Definition Handle.hpp:259