Skip to content
Flecs v4.1
meta_c.h
Go to the documentation of this file.
1/**
2 * @file addons/meta_c.h
3 * @brief Utility macros for populating reflection data in C.
4 */
5
6#ifdef FLECS_META
7
8/**
9 * @defgroup c_addons_meta_c Meta Utilities
10 * @ingroup c_addons
11 * Macro utilities to automatically insert reflection data.
12 *
13 * @{
14 */
15
16#ifndef FLECS_META_C_H
17#define FLECS_META_C_H
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23/* Macro that controls behavior of API. Usually set in module header. When the
24 * macro is not defined, it defaults to IMPL. */
25
26/* Define variables used by reflection utilities. This should only be defined
27 * by the module itself, not by the code importing the module. */
28/* #define ECS_META_IMPL IMPL */
29
30/* Don't define variables used by reflection utilities, but still declare the
31 * variable for the component ID. This enables the reflection utilities to be
32 * used for global component variables, even if no reflection is used. */
33/* #define ECS_META_IMPL DECLARE */
34
35/* Don't define variables used by reflection utilities. This generates an extern
36 * variable for the component identifier. */
37/* #define ECS_META_IMPL EXTERN */
38
39/** Declare component with descriptor. */
40#define ECS_META_COMPONENT(world, name)\
41 ECS_COMPONENT_DEFINE(world, name);\
42 ecs_meta_from_desc(world, ecs_id(name),\
43 FLECS__##name##_kind, FLECS__##name##_desc)
44
45/** ECS_STRUCT(name, body). */
46#define ECS_STRUCT(name, ...)\
47 ECS_META_IMPL_CALL(ECS_STRUCT_, ECS_META_IMPL, name, #__VA_ARGS__);\
48 ECS_STRUCT_TYPE(name, __VA_ARGS__)
49
50/** ECS_ENUM(name, body). */
51#define ECS_ENUM(name, ...)\
52 ECS_META_IMPL_CALL(ECS_ENUM_, ECS_META_IMPL, name, #__VA_ARGS__);\
53 ECS_ENUM_TYPE(name, __VA_ARGS__)
54
55/** ECS_BITMASK(name, body). */
56#define ECS_BITMASK(name, ...)\
57 ECS_META_IMPL_CALL(ECS_BITMASK_, ECS_META_IMPL, name, #__VA_ARGS__);\
58 ECS_ENUM_TYPE(name, __VA_ARGS__)
59
60/** Macro used to mark part of type for which no reflection data is created. */
61#define ECS_PRIVATE
62
63/** Macro used to declare a vector member in types with reflection data. */
64#define ecs_vec(T) ecs_vec_t
65
66/** Macro used to declare a map member in types with reflection data. */
67#define ecs_map(K, V) ecs_map_t
68
69/** Populate meta information from type descriptor. */
70FLECS_API
72 ecs_world_t *world,
73 ecs_entity_t component,
74 ecs_type_kind_t kind,
75 const char *desc);
76
77
78/** \cond
79 * Private utilities to switch between meta IMPL, DECLARE and EXTERN variants.
80 */
81
82#define ECS_META_IMPL_CALL_INNER(base, impl, name, type_desc)\
83 base ## impl(name, type_desc)
84
85#define ECS_META_IMPL_CALL(base, impl, name, type_desc)\
86 ECS_META_IMPL_CALL_INNER(base, impl, name, type_desc)
87
88/* ECS_STRUCT implementation */
89#define ECS_STRUCT_TYPE(name, ...)\
90 typedef struct name __VA_ARGS__ name
91
92#define ECS_STRUCT_ECS_META_IMPL ECS_STRUCT_IMPL
93
94#define ECS_STRUCT_IMPL(name, type_desc)\
95 extern ECS_COMPONENT_DECLARE(name);\
96 static const char *FLECS__##name##_desc = type_desc;\
97 static ecs_type_kind_t FLECS__##name##_kind = EcsStructType;\
98 ECS_COMPONENT_DECLARE(name) = 0
99
100#define ECS_STRUCT_DECLARE(name, type_desc)\
101 extern ECS_COMPONENT_DECLARE(name);\
102 ECS_COMPONENT_DECLARE(name) = 0
103
104#define ECS_STRUCT_EXTERN(name, type_desc)\
105 extern ECS_COMPONENT_DECLARE(name)
106
107
108/* ECS_ENUM implementation */
109#define ECS_ENUM_TYPE(name, ...)\
110 typedef enum name __VA_ARGS__ name
111
112#define ECS_ENUM_ECS_META_IMPL ECS_ENUM_IMPL
113
114#define ECS_ENUM_IMPL(name, type_desc)\
115 extern ECS_COMPONENT_DECLARE(name);\
116 static const char *FLECS__##name##_desc = type_desc;\
117 static ecs_type_kind_t FLECS__##name##_kind = EcsEnumType;\
118 ECS_COMPONENT_DECLARE(name) = 0
119
120#define ECS_ENUM_DECLARE(name, type_desc)\
121 extern ECS_COMPONENT_DECLARE(name);\
122 ECS_COMPONENT_DECLARE(name) = 0
123
124#define ECS_ENUM_EXTERN(name, type_desc)\
125 extern ECS_COMPONENT_DECLARE(name)
126
127
128/* ECS_BITMASK implementation */
129#define ECS_BITMASK_TYPE(name, ...)\
130 typedef enum name __VA_ARGS__ name
131
132#define ECS_BITMASK_ECS_META_IMPL ECS_BITMASK_IMPL
133
134#define ECS_BITMASK_IMPL(name, type_desc)\
135 extern ECS_COMPONENT_DECLARE(name);\
136 static const char *FLECS__##name##_desc = type_desc;\
137 static ecs_type_kind_t FLECS__##name##_kind = EcsBitmaskType;\
138 ECS_COMPONENT_DECLARE(name) = 0
139
140#define ECS_BITMASK_DECLARE(name, type_desc)\
141 extern ECS_COMPONENT_DECLARE(name);\
142 ECS_COMPONENT_DECLARE(name) = 0
143
144#define ECS_BITMASK_EXTERN(name, type_desc)\
145 extern ECS_COMPONENT_DECLARE(name)
146
147/** \endcond */
148
149#ifdef __cplusplus
150}
151#endif
152
153#ifdef __cplusplus
154
155#undef ECS_STRUCT
156#define ECS_STRUCT(name, ...)\
157 struct name __VA_ARGS__;\
158 inline const char* flecs_meta_cpp_desc(name*) { return #__VA_ARGS__; }\
159 inline ecs_type_kind_t flecs_meta_cpp_kind(name*) { return EcsStructType; }\
160 static_assert(true, "")
161
162#undef ECS_ENUM
163#define ECS_ENUM(name, ...)\
164 enum name __VA_ARGS__;\
165 inline const char* flecs_meta_cpp_desc(name*) { return #__VA_ARGS__; }\
166 inline ecs_type_kind_t flecs_meta_cpp_kind(name*) { return EcsEnumType; }\
167 static_assert(true, "")
168
169#undef ECS_BITMASK
170#define ECS_BITMASK(name, ...)\
171 enum name __VA_ARGS__;\
172 inline const char* flecs_meta_cpp_desc(name*) { return #__VA_ARGS__; }\
173 inline ecs_type_kind_t flecs_meta_cpp_kind(name*) { return EcsBitmaskType; }\
174 static_assert(true, "")
175
176#endif
177
178#endif // FLECS_META_C_H
179
180/** @} */
181
182#endif // FLECS_META
FLECS_API int ecs_meta_from_desc(ecs_world_t *world, ecs_entity_t component, ecs_type_kind_t kind, const char *desc)
Populate meta information from type descriptor.
ecs_type_kind_t
Type kinds supported by meta addon.
Definition meta.h:156
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:394
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:438