Skip to content

Commit f538681

Browse files
committed
Implement a world-local component id caching API and make use of it in cpp components
* Fixes potential conflicting component id issues when initializing different worlds with a different order. * Closes SanderMertens#1032
1 parent 808e510 commit f538681

19 files changed

+314
-333
lines changed

include/flecs.h

+52
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,16 @@ typedef struct ecs_type_hooks_t ecs_type_hooks_t;
432432
* alignment and type hooks. */
433433
typedef struct ecs_type_info_t ecs_type_info_t;
434434

435+
/** Cached Type information.
436+
* Contains information about a component type, such as its id, size and alignment */
437+
typedef struct ecs_cached_component_info_t ecs_cached_component_info_t;
438+
439+
/** An index to a cached component id information.
440+
* Can be used to map a typed component from object-oriented language
441+
* fast to a dynamically-per-world generated component id.
442+
* Components are resolved by name lookup and subsequently cached. */
443+
typedef int32_t ecs_component_cache_index_t;
444+
435445
/** Information about an entity, like its table and row. */
436446
typedef struct ecs_record_t ecs_record_t;
437447

@@ -864,6 +874,16 @@ struct ecs_type_info_t {
864874
const char *name; /**< Type name. */
865875
};
866876

877+
/** Type that contains cache component information
878+
*
879+
* \ingroup components
880+
*/
881+
struct ecs_cached_component_info_t {
882+
ecs_entity_t component; /**< Handle to component */
883+
ecs_size_t size; /**< Size of type */
884+
ecs_size_t alignment; /**< Alignment of type */
885+
};
886+
867887
#include "flecs/private/api_types.h" /* Supporting API types */
868888
#include "flecs/private/api_support.h" /* Supporting API functions */
869889
#include "flecs/private/vec.h" /* Vector */
@@ -3690,6 +3710,38 @@ const ecs_type_hooks_t* ecs_get_hooks_id(
36903710
ecs_world_t *world,
36913711
ecs_entity_t id);
36923712

3713+
/** Get the cached information for a specific component cache index.
3714+
*
3715+
* @param world The world.
3716+
* @param component_cache_index The component cache index to lookup.
3717+
* @return The cached component info for the specific component, always returns a present entry.
3718+
*/
3719+
FLECS_API
3720+
ecs_cached_component_info_t* ecs_get_or_create_cached_component_info(
3721+
ecs_world_t* world,
3722+
ecs_component_cache_index_t component_cache_index);
3723+
3724+
/** Get the valid cached information for a specific component cache index.
3725+
*
3726+
* @param world The world.
3727+
* @param component_cache_index The component cache index to lookup.
3728+
* @return The valid cached component info for the specific component or NULL if invalid.
3729+
*/
3730+
FLECS_API
3731+
const ecs_cached_component_info_t* ecs_lookup_cached_component_info(
3732+
const ecs_world_t* world,
3733+
ecs_component_cache_index_t component_cache_index);
3734+
3735+
3736+
/** Test if the cached component info is valid (set)
3737+
*
3738+
* @param component_info The component cache index to lookup.
3739+
* @return True if the info is valid.
3740+
*/
3741+
FLECS_API
3742+
bool ecs_is_cached_component_info_valid(
3743+
const ecs_cached_component_info_t* component_info);
3744+
36933745
/** @} */
36943746

36953747
/**

0 commit comments

Comments
 (0)