Skip to content
Merged
12 changes: 8 additions & 4 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -27859,9 +27859,11 @@ untyped_component& member(
}

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

Expand All @@ -27872,16 +27874,18 @@ 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, TId), sizeof(T),
&value);

return *this;
}

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

Expand All @@ -27892,7 +27896,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, TId), sizeof(T),
&value);

return *this;
Expand Down
12 changes: 8 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,11 @@ untyped_component& member(
}

/** Add constant. */
template <typename T = int32_t>
untyped_component& constant(
const char *name,
int32_t value)
T value,
flecs::entity_t TId = flecs::I32)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you passing in this parameter vs. using flecs::_::type<T>::id(world)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oop didnt think of that, can change that now

{
ecs_add_id(world_, id_, _::type<flecs::Enum>::id(world_));

Expand All @@ -201,16 +203,18 @@ 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, TId), sizeof(T),
&value);

return *this;
}

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

Expand All @@ -221,7 +225,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, TId), 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 @@ -427,7 +427,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 @@ -1580,3 +1580,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, flecs::U8)
.constant<uint8_t>("Second", 2, flecs::U8)
.constant<uint8_t>("Third", 3, flecs::U8);

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);
}