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
8#include <stdio.h>
9
10namespace flecs {
11
19
21 using serializer = ecs_serializer_t;
22
24 using serialize_t = ecs_meta_serialize_t;
25
27template <typename T>
28 using serialize = int(*)(const serializer *, const T*);
29
31template <typename T>
32 using serialize_member = int(*)(const serializer *, const T*, const char* name);
33
35template <typename T>
36 using serialize_element = int(*)(const serializer *, const T*, size_t element);
37
38
40template <typename T, typename ElemType = void>
41 struct opaque {
42 opaque(flecs::world_t *w = nullptr) : world(w) {
43 if (world) {
44 desc.entity = _::type<T>::id(world);
45 }
46 }
47
49 opaque& as_type(flecs::id_t func) {
50 this->desc.type.as_type = func;
51 return *this;
52 }
53
55 opaque& serialize(flecs::serialize<T> func) {
56 this->desc.type.serialize =
57 reinterpret_cast<decltype(
58 this->desc.type.serialize)>(func);
59 return *this;
60 }
61
63 opaque& serialize_member(flecs::serialize_member<T> func) {
64 this->desc.type.serialize_member =
65 reinterpret_cast<decltype(
66 this->desc.type.serialize_member)>(func);
67 return *this;
68 }
69
71 opaque& serialize_element(flecs::serialize_element<T> func) {
72 this->desc.type.serialize_element =
73 reinterpret_cast<decltype(
74 this->desc.type.serialize_element)>(func);
75 return *this;
76 }
77
79 opaque& assign_bool(void (*func)(T *dst, bool value)) {
80 this->desc.type.assign_bool =
81 reinterpret_cast<decltype(
82 this->desc.type.assign_bool)>(func);
83 return *this;
84 }
85
87 opaque& assign_char(void (*func)(T *dst, char value)) {
88 this->desc.type.assign_char =
89 reinterpret_cast<decltype(
90 this->desc.type.assign_char)>(func);
91 return *this;
92 }
93
95 opaque& assign_int(void (*func)(T *dst, int64_t value)) {
96 this->desc.type.assign_int =
97 reinterpret_cast<decltype(
98 this->desc.type.assign_int)>(func);
99 return *this;
100 }
101
103 opaque& assign_uint(void (*func)(T *dst, uint64_t value)) {
104 this->desc.type.assign_uint =
105 reinterpret_cast<decltype(
106 this->desc.type.assign_uint)>(func);
107 return *this;
108 }
109
111 opaque& assign_float(void (*func)(T *dst, double value)) {
112 this->desc.type.assign_float =
113 reinterpret_cast<decltype(
114 this->desc.type.assign_float)>(func);
115 return *this;
116 }
117
119 opaque& assign_string(void (*func)(T *dst, const char *value)) {
120 this->desc.type.assign_string =
121 reinterpret_cast<decltype(
122 this->desc.type.assign_string)>(func);
123 return *this;
124 }
125
127 opaque& assign_entity(
128 void (*func)(T *dst, ecs_world_t *world, ecs_entity_t entity))
129 {
130 this->desc.type.assign_entity =
131 reinterpret_cast<decltype(
132 this->desc.type.assign_entity)>(func);
133 return *this;
134 }
135
137 opaque& assign_id(
138 void (*func)(T *dst, ecs_world_t *world, ecs_id_t id))
139 {
140 this->desc.type.assign_id =
141 reinterpret_cast<decltype(
142 this->desc.type.assign_id)>(func);
143 return *this;
144 }
145
147 opaque& assign_null(void (*func)(T *dst)) {
148 this->desc.type.assign_null =
149 reinterpret_cast<decltype(
150 this->desc.type.assign_null)>(func);
151 return *this;
152 }
153
155 opaque& clear(void (*func)(T *dst)) {
156 this->desc.type.clear =
157 reinterpret_cast<decltype(
158 this->desc.type.clear)>(func);
159 return *this;
160 }
161
163 opaque& ensure_element(ElemType* (*func)(T *dst, size_t elem)) {
164 this->desc.type.ensure_element =
165 reinterpret_cast<decltype(
166 this->desc.type.ensure_element)>(func);
167 return *this;
168 }
169
171 opaque& ensure_member(void* (*func)(T *dst, const char *member)) {
172 this->desc.type.ensure_member =
173 reinterpret_cast<decltype(
174 this->desc.type.ensure_member)>(func);
175 return *this;
176 }
177
179 opaque& count(size_t (*func)(const T *dst)) {
180 this->desc.type.count =
181 reinterpret_cast<decltype(
182 this->desc.type.count)>(func);
183 return *this;
184 }
185
187 opaque& resize(void (*func)(T *dst, size_t count)) {
188 this->desc.type.resize =
189 reinterpret_cast<decltype(
190 this->desc.type.resize)>(func);
191 return *this;
192 }
193
194 ~opaque() {
195 if (world) {
196 ecs_opaque_init(world, &desc);
197 }
198 }
199
201 flecs::world_t *world = nullptr;
202 ecs_opaque_desc_t desc = {};
203};
204
206
207}
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:381
ecs_entity_t
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:381
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:425
ecs_id_t
uint64_t ecs_id_t
Ids are the things that can be added to an entity.
Definition flecs.h:374
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:21
flecs::serialize_element
int(*)(const serializer *, const T *, size_t element) serialize_element
Type safe variant of serialize_element function.
Definition opaque.hpp:36
flecs::serialize
int(*)(const serializer *, const T *) serialize
Type safe variant of serializer function.
Definition opaque.hpp:28
flecs::serialize_member
int(*)(const serializer *, const T *, const char *name) serialize_member
Type safe variant of serialize_member function.
Definition opaque.hpp:32
flecs::serialize_t
ecs_meta_serialize_t serialize_t
Serializer function, used to serialize opaque types.
Definition opaque.hpp:24
ecs_opaque_desc_t
Used with ecs_opaque_init().
Definition meta.h:1074
ecs_serializer_t
Serializer interface.
Definition meta.h:330
flecs::_::type
Definition flecs.hpp:39
flecs::entity
Entity.
Definition entity.hpp:30
flecs::opaque
Type safe interface for opaque types.
Definition opaque.hpp:41
flecs::opaque::assign_char
opaque & assign_char(void(*func)(T *dst, char value))
Assign char value.
Definition opaque.hpp:87
flecs::opaque::assign_uint
opaque & assign_uint(void(*func)(T *dst, uint64_t value))
Assign unsigned int value.
Definition opaque.hpp:103
flecs::opaque::assign_string
opaque & assign_string(void(*func)(T *dst, const char *value))
Assign string value.
Definition opaque.hpp:119
flecs::opaque::assign_bool
opaque & assign_bool(void(*func)(T *dst, bool value))
Assign bool value.
Definition opaque.hpp:79
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:49
flecs::opaque::assign_float
opaque & assign_float(void(*func)(T *dst, double value))
Assign float value.
Definition opaque.hpp:111
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:127
flecs::opaque::world
flecs::world_t * world
Opaque type descriptor.
Definition opaque.hpp:201
flecs::opaque::resize
opaque & resize(void(*func)(T *dst, size_t count))
Resize to number of elements.
Definition opaque.hpp:187
flecs::opaque::assign_null
opaque & assign_null(void(*func)(T *dst))
Assign null value.
Definition opaque.hpp:147
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:137
flecs::opaque::serialize_element
opaque & serialize_element(flecs::serialize_element< T > func)
Serialize element function.
Definition opaque.hpp:71
flecs::opaque::ensure_member
opaque & ensure_member(void *(*func)(T *dst, const char *member))
Ensure & get element.
Definition opaque.hpp:171
flecs::opaque::clear
opaque & clear(void(*func)(T *dst))
Clear collection elements.
Definition opaque.hpp:155
flecs::opaque::serialize_member
opaque & serialize_member(flecs::serialize_member< T > func)
Serialize member function.
Definition opaque.hpp:63
flecs::opaque::ensure_element
opaque & ensure_element(ElemType *(*func)(T *dst, size_t elem))
Ensure & get collection element.
Definition opaque.hpp:163
flecs::opaque::serialize
opaque & serialize(flecs::serialize< T > func)
Serialize function.
Definition opaque.hpp:55
flecs::opaque::count
opaque & count(size_t(*func)(const T *dst))
Return number of elements.
Definition opaque.hpp:179
flecs::opaque::assign_int
opaque & assign_int(void(*func)(T *dst, int64_t value))
Assign int value.
Definition opaque.hpp:95
flecs::world
The world.
Definition world.hpp:137