Skip to content

Commit

Permalink
Implement a world-local component id caching API and make use of it i…
Browse files Browse the repository at this point in the history
…n cpp components

* Fixes potential conflicting component id issues when initializing
  different worlds with a different order.
* Closes #1032
  • Loading branch information
Naios committed Oct 3, 2023
1 parent 808e510 commit f538681
Show file tree
Hide file tree
Showing 19 changed files with 314 additions and 333 deletions.
52 changes: 52 additions & 0 deletions include/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,16 @@ typedef struct ecs_type_hooks_t ecs_type_hooks_t;
* alignment and type hooks. */
typedef struct ecs_type_info_t ecs_type_info_t;

/** Cached Type information.
* Contains information about a component type, such as its id, size and alignment */
typedef struct ecs_cached_component_info_t ecs_cached_component_info_t;

/** An index to a cached component id information.
* Can be used to map a typed component from object-oriented language
* fast to a dynamically-per-world generated component id.
* Components are resolved by name lookup and subsequently cached. */
typedef int32_t ecs_component_cache_index_t;

/** Information about an entity, like its table and row. */
typedef struct ecs_record_t ecs_record_t;

Expand Down Expand Up @@ -864,6 +874,16 @@ struct ecs_type_info_t {
const char *name; /**< Type name. */
};

/** Type that contains cache component information
*
* \ingroup components
*/
struct ecs_cached_component_info_t {
ecs_entity_t component; /**< Handle to component */
ecs_size_t size; /**< Size of type */
ecs_size_t alignment; /**< Alignment of type */
};

#include "flecs/private/api_types.h" /* Supporting API types */
#include "flecs/private/api_support.h" /* Supporting API functions */
#include "flecs/private/vec.h" /* Vector */
Expand Down Expand Up @@ -3690,6 +3710,38 @@ const ecs_type_hooks_t* ecs_get_hooks_id(
ecs_world_t *world,
ecs_entity_t id);

/** Get the cached information for a specific component cache index.
*
* @param world The world.
* @param component_cache_index The component cache index to lookup.
* @return The cached component info for the specific component, always returns a present entry.
*/
FLECS_API
ecs_cached_component_info_t* ecs_get_or_create_cached_component_info(
ecs_world_t* world,
ecs_component_cache_index_t component_cache_index);

/** Get the valid cached information for a specific component cache index.
*
* @param world The world.
* @param component_cache_index The component cache index to lookup.
* @return The valid cached component info for the specific component or NULL if invalid.
*/
FLECS_API
const ecs_cached_component_info_t* ecs_lookup_cached_component_info(
const ecs_world_t* world,
ecs_component_cache_index_t component_cache_index);


/** Test if the cached component info is valid (set)
*
* @param component_info The component cache index to lookup.
* @return True if the info is valid.
*/
FLECS_API
bool ecs_is_cached_component_info_valid(
const ecs_cached_component_info_t* component_info);

/** @} */

/**
Expand Down
Loading

0 comments on commit f538681

Please sign in to comment.