Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -21287,6 +21287,10 @@ void ecs_cpp_enum_init(
world = flecs_suspend_readonly(world, &readonly_state);
ecs_set(world, id, EcsEnum, { .underlying_type = underlying_type });
flecs_resume_readonly(world, &readonly_state);
#else
/* Make sure that enums still behave the same even without meta */
ecs_add_id(world, id, EcsExclusive);
ecs_add_id(world, id, EcsOneOf);
#endif
}

Expand Down
14 changes: 14 additions & 0 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -33312,6 +33312,20 @@ inline void world::init_builtin_components() {
this->component<Identifier>();
this->component<Poly>();

/* If meta is not defined and we're using enum reflection, make sure that
* primitive types are registered. This makes sure we can set components of
* underlying_type_t<E> when registering constants. */
# if !defined(FLECS_META) && !defined(FLECS_CPP_NO_ENUM_REFLECTION)
this->component<uint8_t>("flecs::meta::u8");
this->component<uint16_t>("flecs::meta::u16");
this->component<uint32_t>("flecs::meta::u32");
this->component<uint64_t>("flecs::meta::u64");
this->component<int8_t>("flecs::meta::i8");
this->component<int16_t>("flecs::meta::i16");
this->component<int32_t>("flecs::meta::i32");
this->component<int64_t>("flecs::meta::i64");
# endif

# ifdef FLECS_SYSTEM
_::system_init(*this);
# endif
Expand Down
14 changes: 14 additions & 0 deletions include/flecs/addons/cpp/impl/world.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ inline void world::init_builtin_components() {
this->component<Identifier>();
this->component<Poly>();

/* If meta is not defined and we're using enum reflection, make sure that
* primitive types are registered. This makes sure we can set components of
* underlying_type_t<E> when registering constants. */
# if !defined(FLECS_META) && !defined(FLECS_CPP_NO_ENUM_REFLECTION)
this->component<uint8_t>("flecs::meta::u8");
this->component<uint16_t>("flecs::meta::u16");
this->component<uint32_t>("flecs::meta::u32");
this->component<uint64_t>("flecs::meta::u64");
this->component<int8_t>("flecs::meta::i8");
this->component<int16_t>("flecs::meta::i16");
this->component<int32_t>("flecs::meta::i32");
this->component<int64_t>("flecs::meta::i64");
# endif

# ifdef FLECS_SYSTEM
_::system_init(*this);
# endif
Expand Down
4 changes: 4 additions & 0 deletions src/addons/flecs_cpp.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ void ecs_cpp_enum_init(
world = flecs_suspend_readonly(world, &readonly_state);
ecs_set(world, id, EcsEnum, { .underlying_type = underlying_type });
flecs_resume_readonly(world, &readonly_state);
#else
/* Make sure that enums still behave the same even without meta */
ecs_add_id(world, id, EcsExclusive);
ecs_add_id(world, id, EcsOneOf);
#endif
}

Expand Down
19 changes: 19 additions & 0 deletions test/custom_builds/cpp/enum_reflection_no_meta/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,24 @@ int main(int argc, char *argv[]) {
}
}

flecs::entity e = world.entity();

e.add(Color::Red);
if (!e.has(Color::Red)) {
std::cout << "Entity doesn't have Color::Red\n";
return -1;
}

e.add(Color::Green);
if (!e.has(Color::Green)) {
std::cout << "Entity doesn't have Color::Green\n";
return -1;
}

if (e.has(Color::Red)) {
std::cout << "Entity has Color::Red\n";
return -1;
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.bake_cache
.DS_Store
.vscode
gcov
bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'

* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */

#ifndef ENUM_REFLECTION_NO_META_BAKE_CONFIG_H
#define ENUM_REFLECTION_NO_META_BAKE_CONFIG_H

/* Headers of public dependencies */
#include "../../deps/flecs.h"

#endif

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef ENUM_REFLECTION_NO_META_NO_AUTO_REGISTRATION_H
#define ENUM_REFLECTION_NO_META_NO_AUTO_REGISTRATION_H

/* This generated file contains includes for project dependencies */
#include "enum_reflection_no_meta_no_auto_registration/bake_config.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

#endif

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
)
(.)
.|.
| |
_.--| |--._
.-'; ;`-'& ; `&.
\ & ; & &_/
|"""---...---"""|
\ | | | | | | | /
`---.|.|.|.---'

* This file is generated by bake.lang.c for your convenience. Headers of
* dependencies will automatically show up in this file. Include bake_config.h
* in your main project file. Do not edit! */

#ifndef ENUM_REFLECTION_NO_META_NO_AUTO_REGISTRATION_BAKE_CONFIG_H
#define ENUM_REFLECTION_NO_META_NO_AUTO_REGISTRATION_BAKE_CONFIG_H

/* Headers of public dependencies */
#include "../../deps/flecs.h"

#endif

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id": "enum_reflection_no_meta_no_auto_registration",
"type": "application",
"value": {
"use": [
"flecs"
],
"language": "c++",
"public": false,
"standalone": true
},
"lang.cpp": {
"defines": ["FLECS_CUSTOM_BUILD", "FLECS_CPP", "FLECS_CPP_NO_AUTO_REGISTRATION"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include <enum_reflection_no_meta_no_auto_registration.h>
#include <iostream>

// Test if enum reflection works without the meta addon

enum Color {
Red, Green, Blue
};

int main(int argc, char *argv[]) {
flecs::world world(argc, argv);

flecs::entity c = world.component<Color>();

if (!world.is_alive(flecs::Constant)) {
std::cout << "Constant tag is not alive\n";
return -1;
}

{
flecs::entity constant = c.lookup("Red");
if (!constant) {
std::cout << "Constant not found\n";
return -1;
}

using U = std::underlying_type<Color>::type;

if (!constant.has(flecs::Constant, world.id<U>())) {
std::cout << "Constant pair not found on constant\n";
return -1;
}

const U *v = constant.get_second<U>(flecs::Constant);
if (*v != Red) {
std::cout << "Constant has wrong value\n";
return -1;
}
}

{
flecs::entity constant = c.lookup("Green");
if (!constant) {
std::cout << "Constant not found\n";
return -1;
}

using U = std::underlying_type<Color>::type;

if (!constant.has(flecs::Constant, world.id<U>())) {
std::cout << "Constant pair not found on constant\n";
return -1;
}

const U *v = constant.get_second<U>(flecs::Constant);
if (*v != Green) {
std::cout << "Constant has wrong value\n";
return -1;
}
}

{
flecs::entity constant = c.lookup("Blue");
if (!constant) {
std::cout << "Constant not found\n";
return -1;
}

using U = std::underlying_type<Color>::type;

if (!constant.has(flecs::Constant, world.id<U>())) {
std::cout << "Constant pair not found on constant\n";
return -1;
}

const U *v = constant.get_second<U>(flecs::Constant);
if (*v != Blue) {
std::cout << "Constant has wrong value\n";
return -1;
}
}

flecs::entity e = world.entity();

e.add(Color::Red);
if (!e.has(Color::Red)) {
std::cout << "Entity doesn't have Color::Red\n";
return -1;
}

e.add(Color::Green);
if (!e.has(Color::Green)) {
std::cout << "Entity doesn't have Color::Green\n";
return -1;
}

if (e.has(Color::Red)) {
std::cout << "Entity has Color::Red\n";
return -1;
}

return 0;
}