From ae8db07d6f3af066932129bee60349119fa99ff1 Mon Sep 17 00:00:00 2001 From: Caleb Burke Date: Wed, 23 Sep 2026 23:24:23 -0700 Subject: [PATCH] Simplified handles --- engine/include/sophia/handles.hpp | 18 ++++++++-------- .../include/sophia/resource_registry_ref.hpp | 21 +++---------------- engine/include/sophia/scene_registry.hpp | 2 +- engine/include/sophia/ui/panel_registry.hpp | 2 +- engine/include/sophia/util/pool.hpp | 21 ++++++++++--------- engine/src/resources/mesh.hpp | 2 -- .../resources/pools/render_target_pool.hpp | 2 +- engine/src/resources/resource_registry.hpp | 2 +- 8 files changed, 27 insertions(+), 43 deletions(-) diff --git a/engine/include/sophia/handles.hpp b/engine/include/sophia/handles.hpp index acee081..3a97886 100644 --- a/engine/include/sophia/handles.hpp +++ b/engine/include/sophia/handles.hpp @@ -6,7 +6,7 @@ namespace sophia { -template +template struct Handle { static constexpr u32 INVALID_INDEX = (1u << 20) - 1; @@ -24,14 +24,14 @@ struct Handle { [[nodiscard]] auto operator<=>(const Handle&) const = default; }; -struct MeshTag {}; -struct PanelTag {}; -struct RenderTargetTag {}; -struct SceneTag {}; +class Mesh; +class Panel; +class RenderTarget; +class Scene; -using MeshHandle = Handle; -using PanelHandle = Handle; -using RenderTargetHandle = Handle; -using SceneHandle = Handle; +using MeshHandle = Handle; +using PanelHandle = Handle; +using RenderTargetHandle = Handle; +using SceneHandle = Handle; } // namespace sophia diff --git a/engine/include/sophia/resource_registry_ref.hpp b/engine/include/sophia/resource_registry_ref.hpp index 34d4785..f63bf09 100644 --- a/engine/include/sophia/resource_registry_ref.hpp +++ b/engine/include/sophia/resource_registry_ref.hpp @@ -14,30 +14,15 @@ namespace sophia { -class Mesh; -class RenderTarget; class ResourceRegistry; -template -struct ResourceTraits; - -template <> -struct ResourceTraits { - using Handle = MeshHandle; -}; - -template <> -struct ResourceTraits { - using Handle = RenderTargetHandle; -}; - class ResourceRegistryRef { public: template - typename ResourceTraits::Handle load(const std::string& path); + Handle load(const std::string& path); template - typename ResourceTraits::Handle create(Args&&... args) { + Handle create(Args&&... args) { return createResource(std::type_identity{}, std::forward(args)...); } @@ -48,7 +33,7 @@ class ResourceRegistryRef { bool isAlive(RenderTargetHandle handle) const; template - void release(typename ResourceTraits::Handle handle); + void release(Handle handle); private: friend class Application; diff --git a/engine/include/sophia/scene_registry.hpp b/engine/include/sophia/scene_registry.hpp index 70e2739..2403b79 100644 --- a/engine/include/sophia/scene_registry.hpp +++ b/engine/include/sophia/scene_registry.hpp @@ -25,7 +25,7 @@ class SceneRegistry { u32 size() const { return pool.size(); } private: - Pool pool{"Scene"}; + Pool pool{"Scene"}; }; } // namespace sophia diff --git a/engine/include/sophia/ui/panel_registry.hpp b/engine/include/sophia/ui/panel_registry.hpp index b53b11c..5836fee 100644 --- a/engine/include/sophia/ui/panel_registry.hpp +++ b/engine/include/sophia/ui/panel_registry.hpp @@ -93,7 +93,7 @@ class PanelRegistry { const DockLayout& getDefaultLayout() const { return defaultLayout; } private: - Pool pool{"Panel"}; + Pool pool{"Panel"}; DockLayout defaultLayout; }; diff --git a/engine/include/sophia/util/pool.hpp b/engine/include/sophia/util/pool.hpp index f5b8a53..083aa95 100644 --- a/engine/include/sophia/util/pool.hpp +++ b/engine/include/sophia/util/pool.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -8,7 +9,7 @@ namespace sophia { -template +template class Pool { public: explicit Pool(const char* name) : name{name} {} @@ -17,29 +18,29 @@ class Pool { Pool& operator=(const Pool&) = delete; template - HandleType create(Args&&... args) { + Handle create(Args&&... args) { return add(std::make_unique(std::forward(args)...)); } - HandleType add(std::unique_ptr item) { + Handle add(std::unique_ptr item) { if (!freeList.empty()) { u32 index = freeList.back(); freeList.pop_back(); slots[index].item = std::move(item); - return HandleType{index, slots[index].generation}; + return Handle{index, slots[index].generation}; } slots.push_back(Slot{std::move(item), 0}); - return HandleType{static_cast(slots.size() - 1), 0}; + return Handle{static_cast(slots.size() - 1), 0}; } - T& get(HandleType handle) { + T& get(Handle handle) { return const_cast(std::as_const(*this).get(handle)); } - const T& get(HandleType handle) const { return *validate(handle).item; } + const T& get(Handle handle) const { return *validate(handle).item; } - void release(HandleType handle) { + void release(Handle handle) { validate(handle); Slot& slot = slots[handle.index]; @@ -48,7 +49,7 @@ class Pool { freeList.push_back(handle.index); } - bool isAlive(HandleType handle) const { + bool isAlive(Handle handle) const { return handle.isValid() && handle.index < slots.size() && slots[handle.index].generation == handle.generation && slots[handle.index].item != nullptr; @@ -98,7 +99,7 @@ class Pool { u32 generation = 0; }; - const Slot& validate(HandleType handle) const { + const Slot& validate(Handle handle) const { SOPHIA_ASSERT(handle.isValid(), "invalid handle:", name); SOPHIA_ASSERT(handle.index < slots.size(), name, "handle index out of range:", handle.index); diff --git a/engine/src/resources/mesh.hpp b/engine/src/resources/mesh.hpp index 3c64513..54b8306 100644 --- a/engine/src/resources/mesh.hpp +++ b/engine/src/resources/mesh.hpp @@ -16,8 +16,6 @@ namespace sophia { class Mesh { public: - using HandleType = MeshHandle; - struct Vertex { glm::vec2 position{}; glm::vec3 color{}; diff --git a/engine/src/resources/pools/render_target_pool.hpp b/engine/src/resources/pools/render_target_pool.hpp index 7a8b870..5f4b525 100644 --- a/engine/src/resources/pools/render_target_pool.hpp +++ b/engine/src/resources/pools/render_target_pool.hpp @@ -30,7 +30,7 @@ class RenderTargetPool { Device& device; vk::Format format; - Pool pool{"RenderTarget"}; + Pool pool{"RenderTarget"}; }; } // namespace sophia diff --git a/engine/src/resources/resource_registry.hpp b/engine/src/resources/resource_registry.hpp index 24882ef..3912cf7 100644 --- a/engine/src/resources/resource_registry.hpp +++ b/engine/src/resources/resource_registry.hpp @@ -18,7 +18,7 @@ class ResourceRegistry { : device{device}, renderTargetPool{device, renderTargetFormat} {} Device& device; - Pool meshPool{"Mesh"}; + Pool meshPool{"Mesh"}; RenderTargetPool renderTargetPool; // TexturePool textures; // MaterialPool materials;