Skip to content

Commit 8e15d0e

Browse files
authored
#1558 Add template availablility for untyped component constants and 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
1 parent 9de91ef commit 8e15d0e

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

distr/flecs.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27859,9 +27859,10 @@ untyped_component& member(
2785927859
}
2786027860

2786127861
/** Add constant. */
27862+
template <typename T = int32_t>
2786227863
untyped_component& constant(
2786327864
const char *name,
27864-
int32_t value)
27865+
T value)
2786527866
{
2786627867
ecs_add_id(world_, id_, _::type<flecs::Enum>::id(world_));
2786727868

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

2787427875
ecs_set_id(world_, eid,
27875-
ecs_pair(flecs::Constant, flecs::I32), sizeof(int32_t),
27876+
ecs_pair(flecs::Constant, _::type<T>::id(world_)), sizeof(T),
2787627877
&value);
2787727878

2787827879
return *this;
2787927880
}
2788027881

2788127882
/** Add bitmask constant. */
27883+
template <typename T = uint32_t>
2788227884
untyped_component& bit(
2788327885
const char *name,
27884-
uint32_t value)
27886+
T value)
2788527887
{
2788627888
ecs_add_id(world_, id_, _::type<flecs::Bitmask>::id(world_));
2788727889

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

2789427896
ecs_set_id(world_, eid,
27895-
ecs_pair(flecs::Constant, flecs::U32), sizeof(uint32_t),
27897+
ecs_pair(flecs::Constant, _::type<T>::id(world_)), sizeof(T),
2789627898
&value);
2789727899

2789827900
return *this;

include/flecs/addons/cpp/mixins/meta/untyped_component.inl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,10 @@ untyped_component& member(
188188
}
189189

190190
/** Add constant. */
191+
template <typename T = int32_t>
191192
untyped_component& constant(
192193
const char *name,
193-
int32_t value)
194+
T value)
194195
{
195196
ecs_add_id(world_, id_, _::type<flecs::Enum>::id(world_));
196197

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

203204
ecs_set_id(world_, eid,
204-
ecs_pair(flecs::Constant, flecs::I32), sizeof(int32_t),
205+
ecs_pair(flecs::Constant, _::type<T>::id(world_)), sizeof(T),
205206
&value);
206207

207208
return *this;
208209
}
209210

210211
/** Add bitmask constant. */
212+
template <typename T = uint32_t>
211213
untyped_component& bit(
212214
const char *name,
213-
uint32_t value)
215+
T value)
214216
{
215217
ecs_add_id(world_, id_, _::type<flecs::Bitmask>::id(world_));
216218

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

223225
ecs_set_id(world_, eid,
224-
ecs_pair(flecs::Constant, flecs::U32), sizeof(uint32_t),
226+
ecs_pair(flecs::Constant, _::type<T>::id(world_)), sizeof(T),
225227
&value);
226228

227229
return *this;

test/cpp/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,8 @@
428428
"enum_u8",
429429
"enum_u16",
430430
"enum_u32",
431-
"enum_u64"
431+
"enum_u64",
432+
"runtime_type_constant_u8_template"
432433
]
433434
}, {
434435
"id": "Union",

test/cpp/src/Enum.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,3 +1589,31 @@ void Enum_enum_w_one_constant_index_of(void) {
15891589
auto one_type = flecs::enum_type<OneConstant>(ecs);
15901590
test_int(one_type.index_by_value(0), 0);
15911591
}
1592+
1593+
void Enum_runtime_type_constant_u8_template() {
1594+
flecs::world ecs;
1595+
1596+
auto comp = ecs.component("TestEnumConstant");
1597+
comp.set<flecs::Component>({ sizeof(uint8_t), alignof(uint8_t) });
1598+
comp.set<flecs::Enum>({ flecs::U8 });
1599+
1600+
comp.constant<uint8_t>("First", 1)
1601+
.constant<uint8_t>("Second", 2)
1602+
.constant<uint8_t>("Third", 3);
1603+
1604+
auto first = comp.lookup("First");
1605+
auto second = comp.lookup("Second");
1606+
auto third = comp.lookup("Third");
1607+
1608+
test_assert(first.is_valid());
1609+
test_assert(second.is_valid());
1610+
test_assert(third.is_valid());
1611+
1612+
const uint8_t *val_first = first.get_second<uint8_t>(flecs::Constant);
1613+
const uint8_t *val_second = second.get_second<uint8_t>(flecs::Constant);
1614+
const uint8_t *val_third = third.get_second<uint8_t>(flecs::Constant);
1615+
1616+
test_true(val_first != nullptr && *val_first == 1);
1617+
test_true(val_second != nullptr && *val_second == 2);
1618+
test_true(val_third != nullptr && *val_third == 3);
1619+
}

0 commit comments

Comments
 (0)