Skip to content

Commit

Permalink
#1558 Add template availablility for untyped component constants and …
Browse files Browse the repository at this point in the history
…bits

* Add template availablility for untyped component constants and bits

* Update flecs.h

* Update flecs.h

* Update untyped_component.inl

* Update Enum.cpp

* Update Enum.cpp

* Update project.json

* Update Enum.cpp

* Update Enum.cpp

* Update project.json

* Update Enum.cpp

* Update untyped_component.inl

* Update flecs.h

* Update flecs.h

* Update untyped_component.inl

* Update flecs.h
  • Loading branch information
Reddy-dev authored Feb 11, 2025
1 parent 9de91ef commit 8e15d0e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
10 changes: 6 additions & 4 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -27859,9 +27859,10 @@ untyped_component& member(
}

/** Add constant. */
template <typename T = int32_t>
untyped_component& constant(
const char *name,
int32_t value)
T value)
{
ecs_add_id(world_, id_, _::type<flecs::Enum>::id(world_));

Expand All @@ -27872,16 +27873,17 @@ untyped_component& constant(
ecs_assert(eid != 0, ECS_INTERNAL_ERROR, NULL);

ecs_set_id(world_, eid,
ecs_pair(flecs::Constant, flecs::I32), sizeof(int32_t),
ecs_pair(flecs::Constant, _::type<T>::id(world_)), sizeof(T),
&value);

return *this;
}

/** Add bitmask constant. */
template <typename T = uint32_t>
untyped_component& bit(
const char *name,
uint32_t value)
T value)
{
ecs_add_id(world_, id_, _::type<flecs::Bitmask>::id(world_));

Expand All @@ -27892,7 +27894,7 @@ untyped_component& bit(
ecs_assert(eid != 0, ECS_INTERNAL_ERROR, NULL);

ecs_set_id(world_, eid,
ecs_pair(flecs::Constant, flecs::U32), sizeof(uint32_t),
ecs_pair(flecs::Constant, _::type<T>::id(world_)), sizeof(T),
&value);

return *this;
Expand Down
10 changes: 6 additions & 4 deletions include/flecs/addons/cpp/mixins/meta/untyped_component.inl
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,10 @@ untyped_component& member(
}

/** Add constant. */
template <typename T = int32_t>
untyped_component& constant(
const char *name,
int32_t value)
T value)
{
ecs_add_id(world_, id_, _::type<flecs::Enum>::id(world_));

Expand All @@ -201,16 +202,17 @@ untyped_component& constant(
ecs_assert(eid != 0, ECS_INTERNAL_ERROR, NULL);

ecs_set_id(world_, eid,
ecs_pair(flecs::Constant, flecs::I32), sizeof(int32_t),
ecs_pair(flecs::Constant, _::type<T>::id(world_)), sizeof(T),
&value);

return *this;
}

/** Add bitmask constant. */
template <typename T = uint32_t>
untyped_component& bit(
const char *name,
uint32_t value)
T value)
{
ecs_add_id(world_, id_, _::type<flecs::Bitmask>::id(world_));

Expand All @@ -221,7 +223,7 @@ untyped_component& bit(
ecs_assert(eid != 0, ECS_INTERNAL_ERROR, NULL);

ecs_set_id(world_, eid,
ecs_pair(flecs::Constant, flecs::U32), sizeof(uint32_t),
ecs_pair(flecs::Constant, _::type<T>::id(world_)), sizeof(T),
&value);

return *this;
Expand Down
3 changes: 2 additions & 1 deletion test/cpp/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,8 @@
"enum_u8",
"enum_u16",
"enum_u32",
"enum_u64"
"enum_u64",
"runtime_type_constant_u8_template"
]
}, {
"id": "Union",
Expand Down
28 changes: 28 additions & 0 deletions test/cpp/src/Enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1589,3 +1589,31 @@ void Enum_enum_w_one_constant_index_of(void) {
auto one_type = flecs::enum_type<OneConstant>(ecs);
test_int(one_type.index_by_value(0), 0);
}

void Enum_runtime_type_constant_u8_template() {
flecs::world ecs;

auto comp = ecs.component("TestEnumConstant");
comp.set<flecs::Component>({ sizeof(uint8_t), alignof(uint8_t) });
comp.set<flecs::Enum>({ flecs::U8 });

comp.constant<uint8_t>("First", 1)
.constant<uint8_t>("Second", 2)
.constant<uint8_t>("Third", 3);

auto first = comp.lookup("First");
auto second = comp.lookup("Second");
auto third = comp.lookup("Third");

test_assert(first.is_valid());
test_assert(second.is_valid());
test_assert(third.is_valid());

const uint8_t *val_first = first.get_second<uint8_t>(flecs::Constant);
const uint8_t *val_second = second.get_second<uint8_t>(flecs::Constant);
const uint8_t *val_third = third.get_second<uint8_t>(flecs::Constant);

test_true(val_first != nullptr && *val_first == 1);
test_true(val_second != nullptr && *val_second == 2);
test_true(val_third != nullptr && *val_third == 3);
}

0 comments on commit 8e15d0e

Please sign in to comment.