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
6#pragma once
7
8#include <stdio.h>
9
10namespace flecs {
11
22
25
27template <typename T>
28using serialize = int(*)(const serializer *, const T*);
29
31template <typename T, typename ElemType = void>
32struct opaque {
33 opaque(flecs::world_t *w = nullptr) : world(w) {
34 if (world) {
36 }
37 }
38
40 opaque& as_type(flecs::id_t func) {
41 this->desc.type.as_type = func;
42 return *this;
43 }
44
47 this->desc.type.serialize =
48 reinterpret_cast<decltype(
49 this->desc.type.serialize)>(func);
50 return *this;
51 }
52
54 opaque& assign_bool(void (*func)(T *dst, bool value)) {
55 this->desc.type.assign_bool =
56 reinterpret_cast<decltype(
57 this->desc.type.assign_bool)>(func);
58 return *this;
59 }
60
62 opaque& assign_char(void (*func)(T *dst, char value)) {
63 this->desc.type.assign_char =
64 reinterpret_cast<decltype(
65 this->desc.type.assign_char)>(func);
66 return *this;
67 }
68
70 opaque& assign_int(void (*func)(T *dst, int64_t value)) {
71 this->desc.type.assign_int =
72 reinterpret_cast<decltype(
73 this->desc.type.assign_int)>(func);
74 return *this;
75 }
76
78 opaque& assign_uint(void (*func)(T *dst, uint64_t value)) {
79 this->desc.type.assign_uint =
80 reinterpret_cast<decltype(
81 this->desc.type.assign_uint)>(func);
82 return *this;
83 }
84
86 opaque& assign_float(void (*func)(T *dst, double value)) {
87 this->desc.type.assign_float =
88 reinterpret_cast<decltype(
89 this->desc.type.assign_float)>(func);
90 return *this;
91 }
92
94 opaque& assign_string(void (*func)(T *dst, const char *value)) {
95 this->desc.type.assign_string =
96 reinterpret_cast<decltype(
97 this->desc.type.assign_string)>(func);
98 return *this;
99 }
100
103 void (*func)(T *dst, ecs_world_t *world, ecs_entity_t entity))
104 {
105 this->desc.type.assign_entity =
106 reinterpret_cast<decltype(
107 this->desc.type.assign_entity)>(func);
108 return *this;
109 }
110
113 void (*func)(T *dst, ecs_world_t *world, ecs_id_t id))
114 {
115 this->desc.type.assign_id =
116 reinterpret_cast<decltype(
117 this->desc.type.assign_id)>(func);
118 return *this;
119 }
120
122 opaque& assign_null(void (*func)(T *dst)) {
123 this->desc.type.assign_null =
124 reinterpret_cast<decltype(
125 this->desc.type.assign_null)>(func);
126 return *this;
127 }
128
130 opaque& clear(void (*func)(T *dst)) {
131 this->desc.type.clear =
132 reinterpret_cast<decltype(
133 this->desc.type.clear)>(func);
134 return *this;
135 }
136
138 opaque& ensure_element(ElemType* (*func)(T *dst, size_t elem)) {
139 this->desc.type.ensure_element =
140 reinterpret_cast<decltype(
141 this->desc.type.ensure_element)>(func);
142 return *this;
143 }
144
146 opaque& ensure_member(void* (*func)(T *dst, const char *member)) {
147 this->desc.type.ensure_member =
148 reinterpret_cast<decltype(
149 this->desc.type.ensure_member)>(func);
150 return *this;
151 }
152
154 opaque& count(size_t (*func)(const T *dst)) {
155 this->desc.type.count =
156 reinterpret_cast<decltype(
157 this->desc.type.count)>(func);
158 return *this;
159 }
160
162 opaque& resize(void (*func)(T *dst, size_t count)) {
163 this->desc.type.resize =
164 reinterpret_cast<decltype(
165 this->desc.type.resize)>(func);
166 return *this;
167 }
168
169 ~opaque() {
170 if (world) {
171 ecs_opaque_init(world, &desc);
172 }
173 }
174
176 flecs::world_t *world = nullptr;
177 ecs_opaque_desc_t desc = {};
178};
179
182}
FLECS_API ecs_entity_t ecs_opaque_init(ecs_world_t *world, const ecs_opaque_desc_t *desc)
Create a new opaque type.
struct ecs_serializer_t ecs_serializer_t
Serializer interface.
int(* ecs_meta_serialize_t)(const ecs_serializer_t *ser, const void *src)
Callback invoked serializing an opaque type.
Definition meta.h:374
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:347
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:391
uint64_t ecs_id_t
Ids are the things that can be added to an entity.
Definition flecs.h:340
int(*)(const serializer *, const T *) serialize
Type safe variant of serializer function.
Definition opaque.hpp:28
ecs_meta_serialize_t serialize_t
Serializer function, used to serialize opaque types.
Definition opaque.hpp:24
size_t(* count)(const void *dst)
Return number of elements.
Definition meta.h:453
void(* assign_null)(void *dst)
Assign null value.
Definition meta.h:435
void(* clear)(void *dst)
Clear collection elements.
Definition meta.h:439
void(* assign_uint)(void *dst, uint64_t value)
Assign unsigned int value.
Definition meta.h:408
void *(* ensure_member)(void *dst, const char *member)
Ensure & get element.
Definition meta.h:448
ecs_meta_serialize_t serialize
Serialize action.
Definition meta.h:384
void(* assign_char)(void *dst, char value)
Assign char value.
Definition meta.h:398
ecs_entity_t as_type
Type that describes the serialized output.
Definition meta.h:383
void(* assign_bool)(void *dst, bool value)
Assign bool value.
Definition meta.h:393
void(* assign_string)(void *dst, const char *value)
Assign string value.
Definition meta.h:418
void(* resize)(void *dst, size_t count)
Resize to number of elements.
Definition meta.h:457
void(* assign_int)(void *dst, int64_t value)
Assign int value.
Definition meta.h:403
void(* assign_float)(void *dst, double value)
Assign float value.
Definition meta.h:413
void(* assign_entity)(void *dst, ecs_world_t *world, ecs_entity_t entity)
Assign entity value.
Definition meta.h:423
void(* assign_id)(void *dst, ecs_world_t *world, ecs_id_t id)
Assign (component) id value.
Definition meta.h:429
void *(* ensure_element)(void *dst, size_t elem)
Ensure & get collection element.
Definition meta.h:443
Used with ecs_opaque_init().
Definition meta.h:1051
ecs_entity_t entity
Existing entity to use for type (optional).
Definition meta.h:1052
EcsOpaque type
Type that the opaque type maps to.
Definition meta.h:1053
Serializer interface.
Definition meta.h:323
Entity.
Definition entity.hpp:30
Type safe interface for opaque types.
Definition opaque.hpp:32
opaque & assign_char(void(*func)(T *dst, char value))
Assign char value.
Definition opaque.hpp:62
opaque & assign_uint(void(*func)(T *dst, uint64_t value))
Assign unsigned int value.
Definition opaque.hpp:78
opaque & assign_string(void(*func)(T *dst, const char *value))
Assign string value.
Definition opaque.hpp:94
opaque & assign_bool(void(*func)(T *dst, bool value))
Assign bool value.
Definition opaque.hpp:54
opaque & as_type(flecs::id_t func)
Type that describes the type kind/structure of the opaque type.
Definition opaque.hpp:40
opaque & assign_float(void(*func)(T *dst, double value))
Assign float value.
Definition opaque.hpp:86
opaque & assign_entity(void(*func)(T *dst, ecs_world_t *world, ecs_entity_t entity))
Assign entity value.
Definition opaque.hpp:102
flecs::world_t * world
Opaque type descriptor.
Definition opaque.hpp:176
opaque & resize(void(*func)(T *dst, size_t count))
Resize to number of elements.
Definition opaque.hpp:162
opaque & assign_null(void(*func)(T *dst))
Assign null value.
Definition opaque.hpp:122
opaque & assign_id(void(*func)(T *dst, ecs_world_t *world, ecs_id_t id))
Assign (component) id value.
Definition opaque.hpp:112
opaque & ensure_member(void *(*func)(T *dst, const char *member))
Ensure & get element.
Definition opaque.hpp:146
opaque & clear(void(*func)(T *dst))
Clear collection elements.
Definition opaque.hpp:130
opaque & ensure_element(ElemType *(*func)(T *dst, size_t elem))
Ensure & get collection element.
Definition opaque.hpp:138
opaque & serialize(flecs::serialize< T > func)
Serialize function.
Definition opaque.hpp:46
opaque & count(size_t(*func)(const T *dst))
Return number of elements.
Definition opaque.hpp:154
opaque & assign_int(void(*func)(T *dst, int64_t value))
Assign int value.
Definition opaque.hpp:70
The world.
Definition world.hpp:137