Skip to content

Commit

Permalink
Add compilation flag to disable automatic component registration
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Dec 22, 2023
1 parent 8c8ec42 commit d44f1b3
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 0 deletions.
20 changes: 20 additions & 0 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@
*/
// #define FLECS_CUSTOM_BUILD

/** \def FLECS_CPP_NO_AUTO_REGISTRATION
* When set, the C++ API will require that components are registered before they
* are used. This is useful in multithreaded applications, where components need
* to be registered beforehand, and to catch issues in projects where component
* registration is mandatory. Disabling automatic component registration also
* slightly improves performance.
* The C API is not affected by this feature.
*/
// #define FLECS_CPP_NO_AUTO_REGISTRATION

#ifndef FLECS_CUSTOM_BUILD
// #define FLECS_C /**< C API convenience macros, always enabled */
#define FLECS_CPP /**< C++ API */
Expand Down Expand Up @@ -25680,6 +25690,7 @@ struct cpp_type_impl {
bool allow_tag = true)
{
// If no id has been registered yet, do it now.
#ifndef FLECS_CPP_NO_AUTO_REGISTRATION
if (!registered(world)) {
ecs_entity_t prev_scope = 0;
ecs_id_t prev_with = 0;
Expand Down Expand Up @@ -25708,6 +25719,15 @@ struct cpp_type_impl {
ecs_set_scope(world, prev_scope);
}
}
#else
(void)world;
(void)name;
(void)allow_tag;

ecs_assert(registered(world), ECS_INVALID_OPERATION,
"component '%s' was not registered before use",
type_name<T>());
#endif

// By now we should have a valid identifier
ecs_assert(s_id != 0, ECS_INTERNAL_ERROR, NULL);
Expand Down
10 changes: 10 additions & 0 deletions include/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@
*/
// #define FLECS_CUSTOM_BUILD

/** \def FLECS_CPP_NO_AUTO_REGISTRATION
* When set, the C++ API will require that components are registered before they
* are used. This is useful in multithreaded applications, where components need
* to be registered beforehand, and to catch issues in projects where component
* registration is mandatory. Disabling automatic component registration also
* slightly improves performance.
* The C API is not affected by this feature.
*/
// #define FLECS_CPP_NO_AUTO_REGISTRATION

#ifndef FLECS_CUSTOM_BUILD
// #define FLECS_C /**< C API convenience macros, always enabled */
#define FLECS_CPP /**< C++ API */
Expand Down
10 changes: 10 additions & 0 deletions include/flecs/addons/cpp/component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ struct cpp_type_impl {
bool allow_tag = true)
{
// If no id has been registered yet, do it now.
#ifndef FLECS_CPP_NO_AUTO_REGISTRATION
if (!registered(world)) {
ecs_entity_t prev_scope = 0;
ecs_id_t prev_with = 0;
Expand Down Expand Up @@ -245,6 +246,15 @@ struct cpp_type_impl {
ecs_set_scope(world, prev_scope);
}
}
#else
(void)world;
(void)name;
(void)allow_tag;

ecs_assert(registered(world), ECS_INVALID_OPERATION,
"component '%s' was not registered before use",
type_name<T>());
#endif

// By now we should have a valid identifier
ecs_assert(s_id != 0, ECS_INTERNAL_ERROR, NULL);
Expand Down
5 changes: 5 additions & 0 deletions test/custom_builds/cpp/no_auto_registration/.gitignore
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,16 @@
#ifndef NO_AUTO_REGISTRATION_H
#define NO_AUTO_REGISTRATION_H

/* This generated file contains includes for project dependencies */
#include "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 NO_AUTO_REGISTRATION_BAKE_CONFIG_H
#define NO_AUTO_REGISTRATION_BAKE_CONFIG_H

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

#endif

15 changes: 15 additions & 0 deletions test/custom_builds/cpp/no_auto_registration/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id": "no_auto_registration",
"type": "application",
"value": {
"use": [
"flecs"
],
"public": false,
"standalone": true,
"language": "c++"
},
"lang.cpp": {
"defines": ["FLECS_CPP_NO_AUTO_REGISTRATION"]
}
}
11 changes: 11 additions & 0 deletions test/custom_builds/cpp/no_auto_registration/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <no_auto_registration.h>
#include <iostream>

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

world.import<flecs::units>();
world.import<flecs::monitor>();

return 0;
}

0 comments on commit d44f1b3

Please sign in to comment.