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, typename ElemType = void>
32 struct opaque {
33 opaque(flecs::world_t *w = nullptr) : world(w) {
34 if (world) {
35 desc.entity = _::type<T>::id(world);
36 }
37 }
38
40 opaque& as_type(flecs::id_t func) {
41 this->desc.type.as_type = func;
42 return *this;
43 }
44
46 opaque& serialize(flecs::serialize<T> func) {
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
102 opaque& assign_entity(
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
112 opaque& assign_id(
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
181
182}
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:382
ecs_entity_t
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:346
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:390
ecs_id_t
uint64_t ecs_id_t
Ids are the things that can be added to an entity.
Definition flecs.h:339
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
int(*)(const serializer *, const T *) serialize
Type safe variant of serializer function.
Definition opaque.hpp:28
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:1060
ecs_serializer_t
Serializer interface.
Definition meta.h:331
flecs::_::type
Definition flecs.hpp:38
flecs::entity
Entity.
Definition entity.hpp:30
flecs::opaque
Type safe interface for opaque types.
Definition opaque.hpp:32
flecs::opaque::assign_char
opaque & assign_char(void(*func)(T *dst, char value))
Assign char value.
Definition opaque.hpp:62
flecs::opaque::assign_uint
opaque & assign_uint(void(*func)(T *dst, uint64_t value))
Assign unsigned int value.
Definition opaque.hpp:78
flecs::opaque::assign_string
opaque & assign_string(void(*func)(T *dst, const char *value))
Assign string value.
Definition opaque.hpp:94
flecs::opaque::assign_bool
opaque & assign_bool(void(*func)(T *dst, bool value))
Assign bool value.
Definition opaque.hpp:54
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:40
flecs::opaque::assign_float
opaque & assign_float(void(*func)(T *dst, double value))
Assign float value.
Definition opaque.hpp:86
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:102
flecs::opaque::world
flecs::world_t * world
Opaque type descriptor.
Definition opaque.hpp:176
flecs::opaque::resize
opaque & resize(void(*func)(T *dst, size_t count))
Resize to number of elements.
Definition opaque.hpp:162
flecs::opaque::assign_null
opaque & assign_null(void(*func)(T *dst))
Assign null value.
Definition opaque.hpp:122
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:112
flecs::opaque::ensure_member
opaque & ensure_member(void *(*func)(T *dst, const char *member))
Ensure & get element.
Definition opaque.hpp:146
flecs::opaque::clear
opaque & clear(void(*func)(T *dst))
Clear collection elements.
Definition opaque.hpp:130
flecs::opaque::ensure_element
opaque & ensure_element(ElemType *(*func)(T *dst, size_t elem))
Ensure & get collection element.
Definition opaque.hpp:138
flecs::opaque::serialize
opaque & serialize(flecs::serialize< T > func)
Serialize function.
Definition opaque.hpp:46
flecs::opaque::count
opaque & count(size_t(*func)(const T *dst))
Return number of elements.
Definition opaque.hpp:154
flecs::opaque::assign_int
opaque & assign_int(void(*func)(T *dst, int64_t value))
Assign int value.
Definition opaque.hpp:70
flecs::world
The world.
Definition world.hpp:137