From 2a97a467aa4793d7658d6ae9da6dadef390eac1f Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sat, 16 Dec 2023 21:33:13 +0300 Subject: [PATCH] Return query cache from ecs::container::apply and batch_apply --- libs/ecs/include/psemek/ecs/container.hpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/libs/ecs/include/psemek/ecs/container.hpp b/libs/ecs/include/psemek/ecs/container.hpp index ce923420..1cff650b 100644 --- a/libs/ecs/include/psemek/ecs/container.hpp +++ b/libs/ecs/include/psemek/ecs/container.hpp @@ -143,10 +143,13 @@ namespace psemek::ecs * The function is guaranteed not to visit destroyed entities (unless it did * so before the entity was destroyed). * - * An optional query cache can be supplied to speed up iteration. + * An optional query cache can be supplied to speed up iteration. If the caller doesn't + * supply a query cache, a new query cache is created as if by calling `cache()`. + * In any case, the (user-provided or newly-created) query cache is returned. * * @param function The function to apply to all entities with the specified components * @param cache A query cache + * @return The query cache, either user-provided, or newly-created * @pre The query query cache was created with the exact same sequence of component types * @warning If any two of the passed component types are equal, the call fails with * a compilation error @@ -156,7 +159,7 @@ namespace psemek::ecs * behavior is undefined */ template - void apply(Function && function, query_cache cache = {}); + query_cache apply(Function && function, query_cache cache = {}); /** Apply a function to all entities having the specified components, * in unspecified order. Instead of applying the function to each @@ -175,10 +178,13 @@ namespace psemek::ecs * the function should use the entity handles array view to compute the number * of visited entities. * - * An optional query cache can be supplied to speed up iteration. + * An optional query cache can be supplied to speed up iteration. If the caller doesn't + * supply a query cache, a new query cache is created as if by calling `cache()`. + * In any case, the (user-provided or newly-created) query cache is returned. * * @param function The function to batch-apply to all entities with the specified components * @param cache A query cache + * @return The query cache, either user-provided, or newly-created * @pre The query query cache was created with the exact same sequence of component types * @warning If any two of the passed component types are equal, the call fails with * a compilation error @@ -187,7 +193,7 @@ namespace psemek::ecs * @warning If the function recursively calls `apply()` or `batch_apply()`, the behavior is undefined */ template - void batch_apply(Function && function, query_cache cache = {}); + query_cache batch_apply(Function && function, query_cache cache = {}); /** Register a constructor. Each time an entity is created that has * the specified set of components, the constructor is called for @@ -413,7 +419,7 @@ namespace psemek::ecs } template - void container::apply(Function && function, query_cache cache) + query_cache container::apply(Function && function, query_cache cache) { static_assert(detail::all_different_types_v, "all component types must be different"); @@ -452,10 +458,12 @@ namespace psemek::ecs data.row = row++; } } + + return cache; } template - void container::batch_apply(Function && function, query_cache cache) + query_cache container::batch_apply(Function && function, query_cache cache) { static_assert(detail::all_different_types_v, "all component types must be different"); @@ -471,6 +479,8 @@ namespace psemek::ecs apply_helper.batch_apply(function); } + + return cache; } }