From 93aa697347e47141d6b15bdbeb6fdf3055dfbcc0 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sun, 13 Apr 2025 17:36:43 +0300 Subject: [PATCH] Support RAII-wrapping the execution of an ECS system into some context (e.g. for profiling) --- libs/ecs/include/psemek/ecs/dispatcher.hpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/libs/ecs/include/psemek/ecs/dispatcher.hpp b/libs/ecs/include/psemek/ecs/dispatcher.hpp index b534413e..a51aaa39 100644 --- a/libs/ecs/include/psemek/ecs/dispatcher.hpp +++ b/libs/ecs/include/psemek/ecs/dispatcher.hpp @@ -7,6 +7,19 @@ namespace psemek::ecs { + namespace detail + { + + struct default_context_factory + { + int operator()() const noexcept + { + return 0; + } + }; + + } + struct dispatcher { dispatcher() = default; @@ -42,12 +55,14 @@ namespace psemek::ecs }); } - template - void system(System system) + template + void system(System system, ContextFactory && context_factory = ContextFactory{}) { - handlers_[Event::uuid()].push_back([system = std::move(system), this, cache = container_->cache()](void const * event_ptr) mutable { + handlers_[Event::uuid()].push_back([system = std::move(system), this, cache = container_->cache(), context_factory = std::move(context_factory)](void const * event_ptr) mutable { auto const & event = *reinterpret_cast(event_ptr); + [[maybe_unused]] auto context = context_factory(); + container_->apply([&](ecs::container & container, ecs::handle entity, FilteredComponents & ... filtered_components){ if constexpr (std::invocable) {