Flecs: include/flecs/addons/cpp/mixins/meta/opaque.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| Flecs v4.0 A fast entity component system (ECS) for C & C++

Loading...
Searching...
No Matches
opaque.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8namespace flecs {
9
17
19 using serializer = ecs_serializer_t;
20
22 using serialize_t = ecs_meta_serialize_t;
23
25template <typename T>
26 using serialize = int(*)(const serializer *, const T*);
27
29template <typename T>
30 using serialize_member = int(*)(const serializer *, const T*, const char* name);
31
33template <typename T>
34 using serialize_element = int(*)(const serializer *, const T*, size_t element);
35
36
38template <typename T, typename ElemType = void>
39 struct opaque {
40 opaque(flecs::world_t *w = nullptr) : world(w) {
41 if (world) {
42 desc.entity = _::type<T>::id(world);
43 }
44 }
45
47 opaque& as_type(flecs::id_t func) {
48 this->desc.type.as_type = func;
49 return *this;
50 }
51
53 opaque& serialize(flecs::serialize<T> func) {
54 this->desc.type.serialize =
55 reinterpret_cast<decltype(
56 this->desc.type.serialize)>(func);
57 return *this;
58 }
59
61 opaque& serialize_member(flecs::serialize_member<T> func) {
62 this->desc.type.serialize_member =
63 reinterpret_cast<decltype(
64 this->desc.type.serialize_member)>(func);
65 return *this;
66 }
67
69 opaque& serialize_element(flecs::serialize_element<T> func) {
70 this->desc.type.serialize_element =
71 reinterpret_cast<decltype(
72 this->desc.type.serialize_element)>(func);
73 return *this;
74 }
75
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
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
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
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
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
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
125 opaque& assign_entity(
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
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
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
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
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
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
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
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
199 flecs::world_t *world = nullptr;
200 ecs_opaque_desc_t desc = {};
201};
202
204
205}
ecs_opaque_init
FLECS_API ecs_entity_t ecs_opaque_init(ecs_world_t *world, const ecs_opaque_desc_t *desc)
Create a new opaque type.
ecs_meta_serialize_t
int(* ecs_meta_serialize_t)(const ecs_serializer_t *ser, const void *src)
Callback invoked serializing an opaque type.
Definition meta.h:384
ecs_entity_t
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:380
ecs_world_t
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:424
ecs_id_t
uint64_t ecs_id_t
Ids are the things that can be added to an entity.
Definition flecs.h:373
flecs::component::member
untyped_component & member(flecs::entity_t type_id, flecs::entity_t unit, const char *name, int32_t count=0)
Add member with unit.
Definition untyped_component.inl:46
flecs::serializer
ecs_serializer_t serializer
Serializer object, used for serializing opaque types.
Definition opaque.hpp:19
flecs::serialize_element
int(*)(const serializer *, const T *, size_t element) serialize_element
Type safe variant of serialize_element function.
Definition opaque.hpp:34
flecs::serialize
int(*)(const serializer *, const T *) serialize
Type safe variant of serializer function.
Definition opaque.hpp:26
flecs::serialize_member
int(*)(const serializer *, const T *, const char *name) serialize_member
Type safe variant of serialize_member function.
Definition opaque.hpp:30
flecs::serialize_t
ecs_meta_serialize_t serialize_t
Serializer function, used to serialize opaque types.
Definition opaque.hpp:22
ecs_opaque_desc_t
Used with ecs_opaque_init().
Definition meta.h:1077
ecs_serializer_t
Serializer interface.
Definition meta.h:333
flecs::_::type
Definition flecs.hpp:39
flecs::entity
Entity.
Definition entity.hpp:30
flecs::opaque
Type safe interface for opaque types.
Definition opaque.hpp:39
flecs::opaque::assign_char
opaque & assign_char(void(*func)(T *dst, char value))
Assign char value.
Definition opaque.hpp:85
flecs::opaque::assign_uint
opaque & assign_uint(void(*func)(T *dst, uint64_t value))
Assign unsigned int value.
Definition opaque.hpp:101
flecs::opaque::assign_string
opaque & assign_string(void(*func)(T *dst, const char *value))
Assign string value.
Definition opaque.hpp:117
flecs::opaque::assign_bool
opaque & assign_bool(void(*func)(T *dst, bool value))
Assign bool value.
Definition opaque.hpp:77
flecs::opaque::as_type
opaque & as_type(flecs::id_t func)
Type that describes the type kind/structure of the opaque type.
Definition opaque.hpp:47
flecs::opaque::assign_float
opaque & assign_float(void(*func)(T *dst, double value))
Assign float value.
Definition opaque.hpp:109
flecs::opaque::assign_entity
opaque & assign_entity(void(*func)(T *dst, ecs_world_t *world, ecs_entity_t entity))
Assign entity value.
Definition opaque.hpp:125
flecs::opaque::world
flecs::world_t * world
Opaque type descriptor.
Definition opaque.hpp:199
flecs::opaque::resize
opaque & resize(void(*func)(T *dst, size_t count))
Resize to number of elements.
Definition opaque.hpp:185
flecs::opaque::assign_null
opaque & assign_null(void(*func)(T *dst))
Assign null value.
Definition opaque.hpp:145
flecs::opaque::assign_id
opaque & assign_id(void(*func)(T *dst, ecs_world_t *world, ecs_id_t id))
Assign (component) id value.
Definition opaque.hpp:135
flecs::opaque::serialize_element
opaque & serialize_element(flecs::serialize_element< T > func)
Serialize element function.
Definition opaque.hpp:69
flecs::opaque::ensure_member
opaque & ensure_member(void *(*func)(T *dst, const char *member))
Ensure & get element.
Definition opaque.hpp:169
flecs::opaque::clear
opaque & clear(void(*func)(T *dst))
Clear collection elements.
Definition opaque.hpp:153
flecs::opaque::serialize_member
opaque & serialize_member(flecs::serialize_member< T > func)
Serialize member function.
Definition opaque.hpp:61
flecs::opaque::ensure_element
opaque & ensure_element(ElemType *(*func)(T *dst, size_t elem))
Ensure & get collection element.
Definition opaque.hpp:161
flecs::opaque::serialize
opaque & serialize(flecs::serialize< T > func)
Serialize function.
Definition opaque.hpp:53
flecs::opaque::count
opaque & count(size_t(*func)(const T *dst))
Return number of elements.
Definition opaque.hpp:177
flecs::opaque::assign_int
opaque & assign_int(void(*func)(T *dst, int64_t value))
Assign int value.
Definition opaque.hpp:93
flecs::world
The world.
Definition world.hpp:172