Skip to content
Flecs v4.1
opaque.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/mixins/meta/opaque.hpp
3 * @brief Helpers for opaque type registration.
4 */
5
6#pragma once
7
8namespace flecs {
9
10/**
11 * @defgroup cpp_addons_meta Meta
12 * @ingroup cpp_addons
13 * Flecs reflection framework.
14 *
15 * @{
16 */
17
18/** Serializer object, used for serializing opaque types. */
20
21/** Serializer function, used to serialize opaque types. */
23
24/** Type-safe variant of the serializer function. */
25template <typename T>
26using serialize = int(*)(const serializer *, const T*);
27
28/** Type-safe variant of the serialize_member() function. */
29template <typename T>
30using serialize_member = int(*)(const serializer *, const T*, const char* name);
31
32/** Type-safe variant of the serialize_element() function. */
33template <typename T>
34using serialize_element = int(*)(const serializer *, const T*, size_t element);
35
36
37/** Type-safe interface for opaque types. */
38template <typename T, typename ElemType = void>
39struct opaque {
40 opaque(flecs::world_t *w = nullptr) : world(w) {
41 if (world) {
42 desc.entity = _::type<T>::id(world);
43 }
44 }
45
46 /** Set the type that describes the kind/structure of the opaque type. */
47 opaque& as_type(flecs::id_t func) {
48 this->desc.type.as_type = func;
49 return *this;
50 }
51
52 /** Set the serialize function. */
54 this->desc.type.serialize =
55 reinterpret_cast<decltype(
56 this->desc.type.serialize)>(func);
57 return *this;
58 }
59
60 /** Set the serialize member function. */
62 this->desc.type.serialize_member =
63 reinterpret_cast<decltype(
64 this->desc.type.serialize_member)>(func);
65 return *this;
66 }
67
68 /** Set the serialize element function. */
70 this->desc.type.serialize_element =
71 reinterpret_cast<decltype(
72 this->desc.type.serialize_element)>(func);
73 return *this;
74 }
75
76 /** Assign bool value. */
77 opaque& assign_bool(void (*func)(T *dst, bool value)) {
78 this->desc.type.assign_bool =
79 reinterpret_cast<decltype(
80 this->desc.type.assign_bool)>(func);
81 return *this;
82 }
83
84 /** Assign char value. */
85 opaque& assign_char(void (*func)(T *dst, char value)) {
86 this->desc.type.assign_char =
87 reinterpret_cast<decltype(
88 this->desc.type.assign_char)>(func);
89 return *this;
90 }
91
92 /** Assign int value. */
93 opaque& assign_int(void (*func)(T *dst, int64_t value)) {
94 this->desc.type.assign_int =
95 reinterpret_cast<decltype(
96 this->desc.type.assign_int)>(func);
97 return *this;
98 }
99
100 /** Assign unsigned int value. */
101 opaque& assign_uint(void (*func)(T *dst, uint64_t value)) {
102 this->desc.type.assign_uint =
103 reinterpret_cast<decltype(
104 this->desc.type.assign_uint)>(func);
105 return *this;
106 }
107
108 /** Assign float value. */
109 opaque& assign_float(void (*func)(T *dst, double value)) {
110 this->desc.type.assign_float =
111 reinterpret_cast<decltype(
112 this->desc.type.assign_float)>(func);
113 return *this;
114 }
115
116 /** Assign string value. */
117 opaque& assign_string(void (*func)(T *dst, const char *value)) {
118 this->desc.type.assign_string =
119 reinterpret_cast<decltype(
120 this->desc.type.assign_string)>(func);
121 return *this;
122 }
123
124 /** Assign entity value. */
126 void (*func)(T *dst, ecs_world_t *world, ecs_entity_t entity))
127 {
128 this->desc.type.assign_entity =
129 reinterpret_cast<decltype(
130 this->desc.type.assign_entity)>(func);
131 return *this;
132 }
133
134 /** Assign (component) ID value. */
135 opaque& assign_id(
136 void (*func)(T *dst, ecs_world_t *world, ecs_id_t id))
137 {
138 this->desc.type.assign_id =
139 reinterpret_cast<decltype(
140 this->desc.type.assign_id)>(func);
141 return *this;
142 }
143
144 /** Assign null value. */
145 opaque& assign_null(void (*func)(T *dst)) {
146 this->desc.type.assign_null =
147 reinterpret_cast<decltype(
148 this->desc.type.assign_null)>(func);
149 return *this;
150 }
151
152 /** Clear collection elements. */
153 opaque& clear(void (*func)(T *dst)) {
154 this->desc.type.clear =
155 reinterpret_cast<decltype(
156 this->desc.type.clear)>(func);
157 return *this;
158 }
159
160 /** Ensure and get a collection element. */
161 opaque& ensure_element(ElemType* (*func)(T *dst, size_t elem)) {
162 this->desc.type.ensure_element =
163 reinterpret_cast<decltype(
164 this->desc.type.ensure_element)>(func);
165 return *this;
166 }
167
168 /** Ensure and get a member. */
169 opaque& ensure_member(void* (*func)(T *dst, const char *member)) {
170 this->desc.type.ensure_member =
171 reinterpret_cast<decltype(
172 this->desc.type.ensure_member)>(func);
173 return *this;
174 }
175
176 /** Return the number of elements. */
177 opaque& count(size_t (*func)(const T *dst)) {
178 this->desc.type.count =
179 reinterpret_cast<decltype(
180 this->desc.type.count)>(func);
181 return *this;
182 }
183
184 /** Resize to the given number of elements. */
185 opaque& resize(void (*func)(T *dst, size_t count)) {
186 this->desc.type.resize =
187 reinterpret_cast<decltype(
188 this->desc.type.resize)>(func);
189 return *this;
190 }
191
192 ~opaque() {
193 if (world) {
194 ecs_opaque_init(world, &desc);
195 }
196 }
197
198 /** Opaque type descriptor. */
200 ecs_opaque_desc_t desc = {};
201};
202
203/** @} */
204
205}
FLECS_API ecs_entity_t ecs_opaque_init(ecs_world_t *world, const ecs_opaque_desc_t *desc)
Create a new opaque type.
int(*) ecs_meta_serialize_t(const ecs_serializer_t *ser, const void *src)
Callback invoked serializing an opaque type.
Definition meta.h:399
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:395
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:439
uint64_t ecs_id_t
IDs are the things that can be added to an entity.
Definition flecs.h:388
ecs_serializer_t serializer
Serializer object, used for serializing opaque types.
Definition opaque.hpp:19
int(*)(const serializer *, const T *, size_t element) serialize_element
Type-safe variant of the serialize_element() function.
Definition opaque.hpp:34
int(*)(const serializer *, const T *) serialize
Type-safe variant of the serializer function.
Definition opaque.hpp:26
int(*)(const serializer *, const T *, const char *name) serialize_member
Type-safe variant of the serialize_member() function.
Definition opaque.hpp:30
ecs_meta_serialize_t serialize_t
Serializer function, used to serialize opaque types.
Definition opaque.hpp:22
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_world_t world_t
World type.
Definition c_types.hpp:18
Used with ecs_opaque_init().
Definition meta.h:1637
Serializer interface.
Definition meta.h:348
Entity.
Definition entity.hpp:30
Type-safe interface for opaque types.
Definition opaque.hpp:39
opaque & assign_char(void(*func)(T *dst, char value))
Assign char value.
Definition opaque.hpp:85
opaque & assign_uint(void(*func)(T *dst, uint64_t value))
Assign unsigned int value.
Definition opaque.hpp:101
opaque & assign_string(void(*func)(T *dst, const char *value))
Assign string value.
Definition opaque.hpp:117
opaque & assign_bool(void(*func)(T *dst, bool value))
Assign bool value.
Definition opaque.hpp:77
opaque & as_type(flecs::id_t func)
Set the type that describes the kind/structure of the opaque type.
Definition opaque.hpp:47
opaque & assign_float(void(*func)(T *dst, double value))
Assign float value.
Definition opaque.hpp:109
opaque & assign_entity(void(*func)(T *dst, ecs_world_t *world, ecs_entity_t entity))
Assign entity value.
Definition opaque.hpp:125
flecs::world_t * world
Opaque type descriptor.
Definition opaque.hpp:199
opaque & resize(void(*func)(T *dst, size_t count))
Resize to the given number of elements.
Definition opaque.hpp:185
opaque & assign_null(void(*func)(T *dst))
Assign null value.
Definition opaque.hpp:145
opaque & assign_id(void(*func)(T *dst, ecs_world_t *world, ecs_id_t id))
Assign (component) ID value.
Definition opaque.hpp:135
opaque & serialize_element(flecs::serialize_element< T > func)
Set the serialize element function.
Definition opaque.hpp:69
opaque & ensure_member(void *(*func)(T *dst, const char *member))
Ensure and get a member.
Definition opaque.hpp:169
opaque & clear(void(*func)(T *dst))
Clear collection elements.
Definition opaque.hpp:153
opaque & serialize_member(flecs::serialize_member< T > func)
Set the serialize member function.
Definition opaque.hpp:61
opaque & ensure_element(ElemType *(*func)(T *dst, size_t elem))
Ensure and get a collection element.
Definition opaque.hpp:161
opaque & serialize(flecs::serialize< T > func)
Set the serialize function.
Definition opaque.hpp:53
opaque & count(size_t(*func)(const T *dst))
Return the number of elements.
Definition opaque.hpp:177
opaque & assign_int(void(*func)(T *dst, int64_t value))
Assign int value.
Definition opaque.hpp:93
The world.
Definition world.hpp:129
world()
Create a world.
Definition world.hpp:132