Skip to content

Commit

Permalink
Added const variants for flecs::ref methods
Browse files Browse the repository at this point in the history
  • Loading branch information
BHolman-LBI authored and SanderMertens committed Oct 7, 2021
1 parent 3df3f60 commit c9823f3
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
35 changes: 35 additions & 0 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -8045,6 +8045,41 @@ const void* ecs_get_ref_w_id(
return ref->ptr;
}

const void* ecs_get_const_ref_w_id(
const ecs_world_t * world,
const ecs_ref_t * ref,
ecs_entity_t entity,
ecs_id_t id)
{
ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(ref != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(!entity || !ref->entity || entity == ref->entity, ECS_INVALID_PARAMETER, NULL);
ecs_assert(!id || !ref->component || id == ref->component, ECS_INVALID_PARAMETER, NULL);
ecs_record_t *record = ref->record;
/* Make sure we're not working with a stage */
world = ecs_get_world(world);
entity |= ref->entity;
if (!record) {
record = ecs_eis_get(world, entity);
}
if (!record || !record->table) {
return NULL;
}
ecs_table_t *table = record->table;
if (ref->record == record &&
ref->table == table &&
ref->row == record->row &&
ref->alloc_count == table->alloc_count)
{
return ref->ptr;
}
id |= ref->component;
int32_t row = record->row;
bool is_monitored;
row = flecs_record_to_row(row, &is_monitored);
return get_component(world, table, row, id);
}

void* ecs_get_mut_id(
ecs_world_t *world,
ecs_entity_t entity,
Expand Down
34 changes: 34 additions & 0 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5112,6 +5112,23 @@ const void* ecs_get_ref_w_id(
ecs_ref_t *ref,
ecs_entity_t entity,
ecs_id_t id);

/** Get an immutable reference to a component.
* Same as ecs_get_ref_w_id, but with const ref and thus
* won't update the ref if stale.
*
* @param world The world.
* @param ref Pointer to a ecs_ref_t value. Must be initialized.
* @param entity The entity.
* @param component The entity id of the component to obtain.
* @return The component pointer, NULL if the entity does not have the component.
*/
FLECS_API
const void* ecs_get_const_ref_w_id (
const ecs_world_t *world,
const ecs_ref_t *ref,
ecs_entity_t entity,
ecs_id_t id);

/** Get an immutable reference to a component.
* Same as ecs_get_ref_w_id, but accepts the typename of a component.
Expand Down Expand Up @@ -13650,13 +13667,30 @@ class ref {
return result;
}

const T* operator->() const {
const T* result = static_cast<const T*>(ecs_get_const_ref_w_id(
m_world, &m_ref, m_entity, _::cpp_type<T>::id(m_world)));

ecs_assert(result != NULL, ECS_INVALID_PARAMETER, NULL);

return result;
}

const T* get() {
if (m_entity) {
ecs_get_ref_w_id(
m_world, &m_ref, m_entity, _::cpp_type<T>::id(m_world));
}

return static_cast<T*>(m_ref.ptr);
}

const T* get() const{
if (m_entity) {
return static_cast<const T*>(ecs_get_const_ref_w_id(
m_world, &m_ref, m_entity, _::cpp_type<T>::id(m_world)));
}
return NULL;
}

flecs::entity entity() const;
Expand Down
17 changes: 17 additions & 0 deletions include/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1980,6 +1980,23 @@ const void* ecs_get_ref_w_id(
ecs_ref_t *ref,
ecs_entity_t entity,
ecs_id_t id);

/** Get an immutable reference to a component.
* Same as ecs_get_ref_w_id, but with const ref and thus
* won't update the ref if stale.
*
* @param world The world.
* @param ref Pointer to a ecs_ref_t value. Must be initialized.
* @param entity The entity.
* @param component The entity id of the component to obtain.
* @return The component pointer, NULL if the entity does not have the component.
*/
FLECS_API
const void* ecs_get_const_ref_w_id (
const ecs_world_t *world,
const ecs_ref_t *ref,
ecs_entity_t entity,
ecs_id_t id);

/** Get an immutable reference to a component.
* Same as ecs_get_ref_w_id, but accepts the typename of a component.
Expand Down
17 changes: 17 additions & 0 deletions include/flecs/cpp/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1208,13 +1208,30 @@ class ref {
return result;
}

const T* operator->() const {
const T* result = static_cast<const T*>(ecs_get_const_ref_w_id(
m_world, &m_ref, m_entity, _::cpp_type<T>::id(m_world)));

ecs_assert(result != NULL, ECS_INVALID_PARAMETER, NULL);

return result;
}

const T* get() {
if (m_entity) {
ecs_get_ref_w_id(
m_world, &m_ref, m_entity, _::cpp_type<T>::id(m_world));
}

return static_cast<T*>(m_ref.ptr);
}

const T* get() const{
if (m_entity) {
return static_cast<const T*>(ecs_get_const_ref_w_id(
m_world, &m_ref, m_entity, _::cpp_type<T>::id(m_world)));
}
return NULL;
}

flecs::entity entity() const;
Expand Down
35 changes: 35 additions & 0 deletions src/entity.c
Original file line number Diff line number Diff line change
Expand Up @@ -2987,6 +2987,41 @@ const void* ecs_get_ref_w_id(
return ref->ptr;
}

const void* ecs_get_const_ref_w_id(
const ecs_world_t * world,
const ecs_ref_t * ref,
ecs_entity_t entity,
ecs_id_t id)
{
ecs_assert(world != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(ref != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(!entity || !ref->entity || entity == ref->entity, ECS_INVALID_PARAMETER, NULL);
ecs_assert(!id || !ref->component || id == ref->component, ECS_INVALID_PARAMETER, NULL);
ecs_record_t *record = ref->record;
/* Make sure we're not working with a stage */
world = ecs_get_world(world);
entity |= ref->entity;
if (!record) {
record = ecs_eis_get(world, entity);
}
if (!record || !record->table) {
return NULL;
}
ecs_table_t *table = record->table;
if (ref->record == record &&
ref->table == table &&
ref->row == record->row &&
ref->alloc_count == table->alloc_count)
{
return ref->ptr;
}
id |= ref->component;
int32_t row = record->row;
bool is_monitored;
row = flecs_record_to_row(row, &is_monitored);
return get_component(world, table, row, id);
}

void* ecs_get_mut_id(
ecs_world_t *world,
ecs_entity_t entity,
Expand Down

0 comments on commit c9823f3

Please sign in to comment.