Skip to content

Commit 30f8d96

Browse files
committed
Fix issues with C++ enum reflection without meta addon
1 parent 02012cb commit 30f8d96

File tree

11 files changed

+242
-0
lines changed

11 files changed

+242
-0
lines changed

distr/flecs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21287,6 +21287,10 @@ void ecs_cpp_enum_init(
2128721287
world = flecs_suspend_readonly(world, &readonly_state);
2128821288
ecs_set(world, id, EcsEnum, { .underlying_type = underlying_type });
2128921289
flecs_resume_readonly(world, &readonly_state);
21290+
#else
21291+
/* Make sure that enums still behave the same even without meta */
21292+
ecs_add_id(world, id, EcsExclusive);
21293+
ecs_add_id(world, id, EcsOneOf);
2129021294
#endif
2129121295
}
2129221296

distr/flecs.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33312,6 +33312,20 @@ inline void world::init_builtin_components() {
3331233312
this->component<Identifier>();
3331333313
this->component<Poly>();
3331433314

33315+
/* If meta is not defined and we're using enum reflection, make sure that
33316+
* primitive types are registered. This makes sure we can set components of
33317+
* underlying_type_t<E> when registering constants. */
33318+
# if !defined(FLECS_META) && !defined(FLECS_CPP_NO_ENUM_REFLECTION)
33319+
this->component<uint8_t>("flecs::meta::u8");
33320+
this->component<uint16_t>("flecs::meta::u16");
33321+
this->component<uint32_t>("flecs::meta::u32");
33322+
this->component<uint64_t>("flecs::meta::u64");
33323+
this->component<int8_t>("flecs::meta::i8");
33324+
this->component<int16_t>("flecs::meta::i16");
33325+
this->component<int32_t>("flecs::meta::i32");
33326+
this->component<int64_t>("flecs::meta::i64");
33327+
# endif
33328+
3331533329
# ifdef FLECS_SYSTEM
3331633330
_::system_init(*this);
3331733331
# endif

include/flecs/addons/cpp/impl/world.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ inline void world::init_builtin_components() {
1313
this->component<Identifier>();
1414
this->component<Poly>();
1515

16+
/* If meta is not defined and we're using enum reflection, make sure that
17+
* primitive types are registered. This makes sure we can set components of
18+
* underlying_type_t<E> when registering constants. */
19+
# if !defined(FLECS_META) && !defined(FLECS_CPP_NO_ENUM_REFLECTION)
20+
this->component<uint8_t>("flecs::meta::u8");
21+
this->component<uint16_t>("flecs::meta::u16");
22+
this->component<uint32_t>("flecs::meta::u32");
23+
this->component<uint64_t>("flecs::meta::u64");
24+
this->component<int8_t>("flecs::meta::i8");
25+
this->component<int16_t>("flecs::meta::i16");
26+
this->component<int32_t>("flecs::meta::i32");
27+
this->component<int64_t>("flecs::meta::i64");
28+
# endif
29+
1630
# ifdef FLECS_SYSTEM
1731
_::system_init(*this);
1832
# endif

src/addons/flecs_cpp.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,10 @@ void ecs_cpp_enum_init(
382382
world = flecs_suspend_readonly(world, &readonly_state);
383383
ecs_set(world, id, EcsEnum, { .underlying_type = underlying_type });
384384
flecs_resume_readonly(world, &readonly_state);
385+
#else
386+
/* Make sure that enums still behave the same even without meta */
387+
ecs_add_id(world, id, EcsExclusive);
388+
ecs_add_id(world, id, EcsOneOf);
385389
#endif
386390
}
387391

test/custom_builds/cpp/enum_reflection_no_meta/src/main.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,24 @@ int main(int argc, char *argv[]) {
8080
}
8181
}
8282

83+
flecs::entity e = world.entity();
84+
85+
e.add(Color::Red);
86+
if (!e.has(Color::Red)) {
87+
std::cout << "Entity doesn't have Color::Red\n";
88+
return -1;
89+
}
90+
91+
e.add(Color::Green);
92+
if (!e.has(Color::Green)) {
93+
std::cout << "Entity doesn't have Color::Green\n";
94+
return -1;
95+
}
96+
97+
if (e.has(Color::Red)) {
98+
std::cout << "Entity has Color::Red\n";
99+
return -1;
100+
}
101+
83102
return 0;
84103
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.bake_cache
2+
.DS_Store
3+
.vscode
4+
gcov
5+
bin
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
)
3+
(.)
4+
.|.
5+
| |
6+
_.--| |--._
7+
.-'; ;`-'& ; `&.
8+
\ & ; & &_/
9+
|"""---...---"""|
10+
\ | | | | | | | /
11+
`---.|.|.|.---'
12+
13+
* This file is generated by bake.lang.c for your convenience. Headers of
14+
* dependencies will automatically show up in this file. Include bake_config.h
15+
* in your main project file. Do not edit! */
16+
17+
#ifndef ENUM_REFLECTION_NO_META_BAKE_CONFIG_H
18+
#define ENUM_REFLECTION_NO_META_BAKE_CONFIG_H
19+
20+
/* Headers of public dependencies */
21+
#include "../../deps/flecs.h"
22+
23+
#endif
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef ENUM_REFLECTION_NO_META_NO_AUTO_REGISTRATION_H
2+
#define ENUM_REFLECTION_NO_META_NO_AUTO_REGISTRATION_H
3+
4+
/* This generated file contains includes for project dependencies */
5+
#include "enum_reflection_no_meta_no_auto_registration/bake_config.h"
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
#ifdef __cplusplus
12+
}
13+
#endif
14+
15+
#endif
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
)
3+
(.)
4+
.|.
5+
| |
6+
_.--| |--._
7+
.-'; ;`-'& ; `&.
8+
\ & ; & &_/
9+
|"""---...---"""|
10+
\ | | | | | | | /
11+
`---.|.|.|.---'
12+
13+
* This file is generated by bake.lang.c for your convenience. Headers of
14+
* dependencies will automatically show up in this file. Include bake_config.h
15+
* in your main project file. Do not edit! */
16+
17+
#ifndef ENUM_REFLECTION_NO_META_NO_AUTO_REGISTRATION_BAKE_CONFIG_H
18+
#define ENUM_REFLECTION_NO_META_NO_AUTO_REGISTRATION_BAKE_CONFIG_H
19+
20+
/* Headers of public dependencies */
21+
#include "../../deps/flecs.h"
22+
23+
#endif
24+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"id": "enum_reflection_no_meta_no_auto_registration",
3+
"type": "application",
4+
"value": {
5+
"use": [
6+
"flecs"
7+
],
8+
"language": "c++",
9+
"public": false,
10+
"standalone": true
11+
},
12+
"lang.cpp": {
13+
"defines": ["FLECS_CUSTOM_BUILD", "FLECS_CPP", "FLECS_CPP_NO_AUTO_REGISTRATION"]
14+
}
15+
}

0 commit comments

Comments
 (0)