From 713a479eedb0e86898cf0dbb40f2682ff5bfe0fc Mon Sep 17 00:00:00 2001 From: lisyarus Date: Mon, 20 May 2024 16:43:25 +0300 Subject: [PATCH] Fix registering ecs constructors & destructors --- libs/ecs/include/psemek/ecs/container.hpp | 44 ++++++++++++++--------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/libs/ecs/include/psemek/ecs/container.hpp b/libs/ecs/include/psemek/ecs/container.hpp index 65606fa8..edc0f298 100644 --- a/libs/ecs/include/psemek/ecs/container.hpp +++ b/libs/ecs/include/psemek/ecs/container.hpp @@ -390,6 +390,9 @@ namespace psemek::ecs std::vector callback_caches_; + template + query_cache cache_impl(Constructor && constructor, Destructor && destructor); + detail::table * insert_table(std::vector> columns); void do_destroy(handle const & entity); void remove_row(detail::table & table, std::uint32_t row, util::span entities); @@ -538,16 +541,7 @@ namespace psemek::ecs template query_cache container::cache() { - typename detail::filter_with ...>>::type with_uuids; - typename detail::filter_without...>>::type without_uuids; - - auto result = query_cache_container_.create(with_uuids.get(), without_uuids.get()); - - table_container_.apply([&](detail::table & table){ - result->add(&table); - }, with_uuids.get(), without_uuids.get()); - - return result; + return cache_impl(nullptr, nullptr); } template @@ -632,9 +626,7 @@ namespace psemek::ecs static_assert(invocable_type::value, "function is not invocable with these components"); - query_cache cache = this->cache(); - - cache->constructor_factory = [function = std::move(function)](std::vector const & column_indices) -> detail::table_callback { + auto constructor = [function = std::move(function)](std::vector const & column_indices) -> detail::table_callback { return [function, column_indices](container & container, detail::table & table, std::uint32_t row){ typename detail::filter_with>::type apply_helper(container, table.entity_handles()); @@ -646,6 +638,8 @@ namespace psemek::ecs }; }; + query_cache cache = this->cache_impl(std::move(constructor), nullptr); + callback_caches_.push_back(cache); } @@ -658,9 +652,7 @@ namespace psemek::ecs static_assert(invocable_type::value, "function is not invocable with these components"); - query_cache cache = this->cache(); - - cache->destructor_factory = [function = std::move(function)](std::vector const & column_indices) -> detail::table_callback { + auto destructor = [function = std::move(function)](std::vector const & column_indices) -> detail::table_callback { return [function, column_indices](container & container, detail::table & table, std::uint32_t row){ typename detail::filter_with>::type apply_helper(container, table.entity_handles()); @@ -672,6 +664,8 @@ namespace psemek::ecs }; }; + query_cache cache = this->cache_impl(nullptr, std::move(destructor)); + callback_caches_.push_back(cache); } @@ -681,4 +675,22 @@ namespace psemek::ecs return index_container_.get(*this, std::forward(args)...); } + template + query_cache container::cache_impl(Constructor && constructor, Destructor && destructor) + { + typename detail::filter_with ...>>::type with_uuids; + typename detail::filter_without...>>::type without_uuids; + + auto result = query_cache_container_.create(with_uuids.get(), without_uuids.get()); + + result->constructor_factory = std::move(constructor); + result->destructor_factory = std::move(destructor); + + table_container_.apply([&](detail::table & table){ + result->add(&table); + }, with_uuids.get(), without_uuids.get()); + + return result; + } + }