From 424bf5f533e50148a6c194eead7f9e20d2094761 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Fri, 10 Oct 2025 16:01:17 +0300 Subject: [PATCH] Add ecs::container::detach_finally/destroy_finally helpers --- libs/ecs/include/psemek/ecs/container.hpp | 16 ++++++++++++++++ libs/ecs/source/container.cpp | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/libs/ecs/include/psemek/ecs/container.hpp b/libs/ecs/include/psemek/ecs/container.hpp index c112bb1d..8ecd12e7 100644 --- a/libs/ecs/include/psemek/ecs/container.hpp +++ b/libs/ecs/include/psemek/ecs/container.hpp @@ -79,6 +79,9 @@ namespace psemek::ecs */ void destroy(handle entity); + /** Same as destroy(), but via finally() */ + void destroy_finally(handle entity); + /** Get an accessor for an entity, which provides access to the entity's components. * It is designed to be a single-use object; it cannot be stored as a reference to * the entity. Use handle for that instead. @@ -169,6 +172,10 @@ namespace psemek::ecs template void detach(handle entity); + /** Same as detach(), but via finally() */ + template + void detach_finally(handle entity); + /** Create a query cache that can be used to speed up `apply()` calls. * * The constness of the component types is ignored. @@ -611,6 +618,15 @@ namespace psemek::ecs --method_recursion_depth_; } + template + void container::detach_finally(handle entity) + { + finally([entity](container & world){ + if (world.alive(entity)) + world.detach(entity); + }); + } + template query_cache container::cache() { diff --git a/libs/ecs/source/container.cpp b/libs/ecs/source/container.cpp index b913960e..5396c6e0 100644 --- a/libs/ecs/source/container.cpp +++ b/libs/ecs/source/container.cpp @@ -36,6 +36,13 @@ namespace psemek::ecs --method_recursion_depth_; } + + void container::destroy_finally(handle entity) + { + finally([entity](container & world){ + if (world.alive(entity)) + world.destroy(entity); + }); } accessor container::get(handle entity)