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

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

Loading...
Searching...
No Matches
untyped_component.inl
Go to the documentation of this file.
1
5
12
13private:
14
16untyped_component& internal_member(
17 flecs::entity_t type_id,
18 flecs::entity_t unit,
19 const char *name,
20 int32_t count = 0,
21 size_t offset = 0,
22 bool use_offset = false)
23{
24 ecs_member_t m = {};
25 m.name = name;
26 m.type = type_id;
27 m.unit = unit;
28 m.count = count;
29 m.offset = static_cast<int32_t>(offset);
30 m.use_offset = use_offset;
31
32 ecs_struct_add_member(world_, id_, &m);
33
34 return *this;
35}
36
37public:
38
40 untyped_component& member(
41 flecs::entity_t type_id,
42 flecs::entity_t unit,
43 const char *name,
44 int32_t count = 0)
45{
46 return internal_member(type_id, unit, name, count, 0, false);
47}
48
50 untyped_component& member(
51 flecs::entity_t type_id,
52 flecs::entity_t unit,
53 const char *name,
54 int32_t count,
55 size_t offset)
56{
57 return internal_member(type_id, unit, name, count, offset, true);
58}
59
61 untyped_component& member(
62 flecs::entity_t type_id,
63 const char* name,
64 int32_t count = 0)
65{
66 return member(type_id, 0, name, count);
67}
68
70 untyped_component& member(
71 flecs::entity_t type_id,
72 const char* name,
73 int32_t count,
74 size_t offset)
75{
76 return member(type_id, 0, name, count, offset);
77}
78
80template <typename MemberType>
81 untyped_component& member(
82 const char *name,
83 int32_t count = 0)
84{
85 flecs::entity_t type_id = _::type<MemberType>::id(world_);
86 return member(type_id, name, count);
87}
88
90template <typename MemberType>
91 untyped_component& member(
92 const char *name,
93 int32_t count,
94 size_t offset)
95{
96 flecs::entity_t type_id = _::type<MemberType>::id(world_);
97 return member(type_id, name, count, offset);
98}
99
101template <typename MemberType>
102 untyped_component& member(
103 flecs::entity_t unit,
104 const char *name,
105 int32_t count = 0)
106{
107 flecs::entity_t type_id = _::type<MemberType>::id(world_);
108 return member(type_id, unit, name, count);
109}
110
112template <typename MemberType>
113 untyped_component& member(
114 flecs::entity_t unit,
115 const char *name,
116 int32_t count,
117 size_t offset)
118{
119 flecs::entity_t type_id = _::type<MemberType>::id(world_);
120 return member(type_id, unit, name, count, offset);
121}
122
124template <typename MemberType, typename UnitType>
125 untyped_component& member(
126 const char *name,
127 int32_t count = 0)
128{
129 flecs::entity_t type_id = _::type<MemberType>::id(world_);
130 flecs::entity_t unit_id = _::type<UnitType>::id(world_);
131 return member(type_id, unit_id, name, count);
132}
133
135template <typename MemberType, typename UnitType>
136 untyped_component& member(
137 const char *name,
138 int32_t count,
139 size_t offset)
140{
141 flecs::entity_t type_id = _::type<MemberType>::id(world_);
142 flecs::entity_t unit_id = _::type<UnitType>::id(world_);
143 return member(type_id, unit_id, name, count, offset);
144}
145
147template <typename MemberType, typename ComponentType,
148 typename RealType = typename std::remove_extent<MemberType>::type>
149 untyped_component& member(
150 const char* name,
151 const MemberType ComponentType::* ptr)
152{
153 flecs::entity_t type_id = _::type<RealType>::id(world_);
154 size_t offset = reinterpret_cast<size_t>(&(static_cast<ComponentType*>(nullptr)->*ptr));
155 return member(type_id, name, std::extent<MemberType>::value, offset);
156}
157
159template <typename MemberType, typename ComponentType,
160 typename RealType = typename std::remove_extent<MemberType>::type>
161 untyped_component& member(
162 flecs::entity_t unit,
163 const char* name,
164 const MemberType ComponentType::* ptr)
165{
166 flecs::entity_t type_id = _::type<RealType>::id(world_);
167 size_t offset = reinterpret_cast<size_t>(&(static_cast<ComponentType*>(nullptr)->*ptr));
168 return member(type_id, unit, name, std::extent<MemberType>::value, offset);
169}
170
172template <typename UnitType, typename MemberType, typename ComponentType,
173 typename RealType = typename std::remove_extent<MemberType>::type>
174 untyped_component& member(
175 const char* name,
176 const MemberType ComponentType::* ptr)
177{
178 flecs::entity_t type_id = _::type<RealType>::id(world_);
179 flecs::entity_t unit_id = _::type<UnitType>::id(world_);
180 size_t offset = reinterpret_cast<size_t>(&(static_cast<ComponentType*>(nullptr)->*ptr));
181 return member(type_id, unit_id, name, std::extent<MemberType>::value, offset);
182}
183
185template <typename T = int32_t>
186 untyped_component& constant(
187 const char *name,
188 T value)
189{
190 ecs_add_id(world_, id_, _::type<flecs::Enum>::id(world_));
191
192 ecs_entity_desc_t desc = {};
193 desc.name = name;
194 desc.parent = id_;
195 ecs_entity_t eid = ecs_entity_init(world_, &desc);
196 ecs_assert(eid != 0, ECS_INTERNAL_ERROR, nullptr);
197
198 ecs_set_id(world_, eid,
199 ecs_pair(flecs::Constant, _::type<T>::id(world_)), sizeof(T),
200 &value);
201
202 return *this;
203}
204
206template <typename T = uint32_t>
207 untyped_component& bit(
208 const char *name,
209 T value)
210{
211 ecs_add_id(world_, id_, _::type<flecs::Bitmask>::id(world_));
212
213 ecs_entity_desc_t desc = {};
214 desc.name = name;
215 desc.parent = id_;
216 ecs_entity_t eid = ecs_entity_init(world_, &desc);
217 ecs_assert(eid != 0, ECS_INTERNAL_ERROR, nullptr);
218
219 ecs_set_id(world_, eid,
220 ecs_pair(flecs::Constant, _::type<T>::id(world_)), sizeof(T),
221 &value);
222
223 return *this;
224}
225
227template <typename Elem>
228 untyped_component& array(
229 int32_t elem_count)
230{
231 ecs_array_desc_t desc = {};
232 desc.entity = id_;
233 desc.type = _::type<Elem>::id(world_);
234 desc.count = elem_count;
235 ecs_array_init(world_, &desc);
236 return *this;
237}
238
240 untyped_component& range(
241 double min,
242 double max)
243{
244 flecs::member_t *m = ecs_cpp_last_member(world_, id_);
245 if (!m) {
246 return *this;
247 }
248
249 m->range.min = min;
250 m->range.max = max;
251
252 if (m->member) {
253 flecs::world w(world_);
254 flecs::entity me = w.entity(m->member);
255 flecs::MemberRanges *mr = static_cast<flecs::MemberRanges*>(
256 ecs_ensure_id(w, me, w.id<flecs::MemberRanges>(),
257 sizeof(flecs::MemberRanges)));
258 mr->value.min = min;
259 mr->value.max = max;
260 me.modified<flecs::MemberRanges>();
261 }
262
263 return *this;
264}
265
267 untyped_component& warning_range(
268 double min,
269 double max)
270{
271 flecs::member_t *m = ecs_cpp_last_member(world_, id_);
272 if (!m) {
273 return *this;
274 }
275
276 m->warning_range.min = min;
277 m->warning_range.max = max;
278
279 if (m->member) {
280 flecs::world w(world_);
281 flecs::entity me = w.entity(m->member);
282 flecs::MemberRanges *mr = static_cast<flecs::MemberRanges*>(
283 ecs_ensure_id(w, me, w.id<flecs::MemberRanges>(),
284 sizeof(flecs::MemberRanges)));
285 mr->warning.min = min;
286 mr->warning.max = max;
287 me.modified<flecs::MemberRanges>();
288 }
289
290 return *this;
291}
292
294 untyped_component& error_range(
295 double min,
296 double max)
297{
298 flecs::member_t *m = ecs_cpp_last_member(world_, id_);
299 if (!m) {
300 return *this;
301 }
302
303 m->error_range.min = min;
304 m->error_range.max = max;
305
306 if (m->member) {
307 flecs::world w(world_);
308 flecs::entity me = w.entity(m->member);
309 flecs::MemberRanges *mr = static_cast<flecs::MemberRanges*>(
310 ecs_ensure_id(w, me, w.id<flecs::MemberRanges>(),
311 sizeof(flecs::MemberRanges)));
312 mr->error.min = min;
313 mr->error.max = max;
314 me.modified<flecs::MemberRanges>();
315 }
316
317 return *this;
318}
319
ecs_add_id
void ecs_add_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Add a (component) ID to an entity.
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
ECS_INTERNAL_ERROR
#define ECS_INTERNAL_ERROR
Internal error code.
Definition log.h:681
ecs_struct_add_member
FLECS_API int ecs_struct_add_member(ecs_world_t *world, ecs_entity_t type, const ecs_member_t *member)
Add member to struct.
ecs_array_init
FLECS_API ecs_entity_t ecs_array_init(ecs_world_t *world, const ecs_array_desc_t *desc)
Create a new array type.
ecs_entity_t
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:385
flecs::component::member
untyped_component & member(const char *name, int32_t count, size_t offset)
Add a member with unit.
Definition untyped_component.inl:136
flecs::component::member
untyped_component & member(const char *name, int32_t count=0)
Add a member.
Definition untyped_component.inl:81
flecs::member_t
ecs_member_t member_t
Embedded type aliases.
Definition decl.hpp:35
flecs::component::member
untyped_component & member(flecs::entity_t unit, const char *name, int32_t count, size_t offset)
Add a member with unit.
Definition untyped_component.inl:113
flecs::component::member
untyped_component & member(const char *name, int32_t count=0)
Add a member with unit.
Definition untyped_component.inl:125
flecs::component::member
untyped_component & member(flecs::entity_t type_id, flecs::entity_t unit, const char *name, int32_t count=0)
Add a member with unit.
Definition untyped_component.inl:40
flecs::component::member
untyped_component & member(flecs::entity_t unit, const char *name, const MemberType ComponentType::*ptr)
Add a member with unit using pointer-to-member.
Definition untyped_component.inl:161
flecs::component::warning_range
untyped_component & warning_range(double min, double max)
Add a member warning range.
Definition untyped_component.inl:267
flecs::component::member
untyped_component & member(flecs::entity_t unit, const char *name, int32_t count=0)
Add a member with unit.
Definition untyped_component.inl:102
flecs::component::constant
untyped_component & constant(const char *name, T value)
Add a constant.
Definition untyped_component.inl:186
flecs::component::member
untyped_component & member(flecs::entity_t type_id, flecs::entity_t unit, const char *name, int32_t count, size_t offset)
Add a member with unit, count, and offset.
Definition untyped_component.inl:50
flecs::component::member
untyped_component & member(flecs::entity_t type_id, const char *name, int32_t count, size_t offset)
Add a member with count and offset.
Definition untyped_component.inl:70
flecs::component::array
untyped_component & array(int32_t elem_count)
Register array metadata for a component.
Definition untyped_component.inl:228
flecs::component::bit
untyped_component & bit(const char *name, T value)
Add a bitmask constant.
Definition untyped_component.inl:207
flecs::component::range
untyped_component & range(double min, double max)
Add a member value range.
Definition untyped_component.inl:240
flecs::component::member
untyped_component & member(const char *name, const MemberType ComponentType::*ptr)
Add a member using pointer-to-member.
Definition untyped_component.inl:149
flecs::component::member
untyped_component & member(flecs::entity_t type_id, const char *name, int32_t count=0)
Add a member.
Definition untyped_component.inl:61
flecs::component::member
untyped_component & member(const char *name, const MemberType ComponentType::*ptr)
Add a member with unit using pointer-to-member.
Definition untyped_component.inl:174
flecs::component::member
untyped_component & member(const char *name, int32_t count, size_t offset)
Add a member.
Definition untyped_component.inl:91
flecs::component::error_range
untyped_component & error_range(double min, double max)
Add a member error range.
Definition untyped_component.inl:294
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:190
flecs::entity_t
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
ecs_entity_init
ecs_entity_t ecs_entity_init(ecs_world_t *world, const ecs_entity_desc_t *desc)
Find or create an entity.
ecs_ensure_id
void * ecs_ensure_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component, size_t size)
Ensure an entity has a component and return a pointer.
ecs_set_id
void ecs_set_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component, size_t size, const void *ptr)
Set the value of a component.
EcsMemberRanges::warning
ecs_member_value_range_t warning
Member value warning range.
Definition meta.h:215
EcsMemberRanges::value
ecs_member_value_range_t value
Member value range.
Definition meta.h:214
EcsMemberRanges::error
ecs_member_value_range_t error
Member value error range.
Definition meta.h:216
ecs_array_desc_t
Used with ecs_array_init().
Definition meta.h:1083
ecs_array_desc_t::entity
ecs_entity_t entity
Existing entity to use for type (optional).
Definition meta.h:1084
ecs_array_desc_t::type
ecs_entity_t type
Element type.
Definition meta.h:1085
ecs_array_desc_t::count
int32_t count
Number of elements.
Definition meta.h:1086
ecs_entity_desc_t
Used with ecs_entity_init().
Definition flecs.h:1046
ecs_entity_desc_t::name
const char * name
Name of the entity.
Definition flecs.h:1053
ecs_entity_desc_t::parent
ecs_entity_t parent
Parent entity.
Definition flecs.h:1051
ecs_member_t
Element type of members vector in EcsStruct.
Definition meta.h:220
ecs_member_t::warning_range
ecs_member_value_range_t warning_range
Numerical range outside of which the value represents a warning.
Definition meta.h:254
ecs_member_t::name
const char * name
Must be set when used with ecs_struct_desc_t.
Definition meta.h:222
ecs_member_t::use_offset
bool use_offset
Set to true to prevent automatic offset computation.
Definition meta.h:241
ecs_member_t::type
ecs_entity_t type
Member type.
Definition meta.h:225
ecs_member_t::member
ecs_entity_t member
Should not be set by ecs_struct_desc_t.
Definition meta.h:260
ecs_member_t::offset
int32_t offset
May be set when used with ecs_struct_desc_t.
Definition meta.h:232
ecs_member_t::unit
ecs_entity_t unit
May be set when used with ecs_struct_desc_t.
Definition meta.h:236
ecs_member_t::range
ecs_member_value_range_t range
Numerical range that specifies which values member can assume.
Definition meta.h:246
ecs_member_t::count
int32_t count
Element count (for inline arrays).
Definition meta.h:229
ecs_member_t::error_range
ecs_member_value_range_t error_range
Numerical range outside of which the value represents an error.
Definition meta.h:250
ecs_member_value_range_t::min
double min
Min member value.
Definition meta.h:208
ecs_member_value_range_t::max
double max
Max member value.
Definition meta.h:209
flecs::_::type
Definition flecs.hpp:39
flecs::entity_builder::unit
const Self & unit(const char *symbol, flecs::entity_t prefix=0, flecs::entity_t base=0, flecs::entity_t over=0, int32_t factor=0, int32_t power=0) const
Make an entity a unit.
Definition entity_builder.inl:14
flecs::entity_view::name
flecs::string_view name() const
Return the entity name.
Definition entity_view.hpp:88
flecs::entity
Entity.
Definition entity.hpp:30
flecs::entity::modified
void modified() const
Signal that component was modified.
Definition entity.hpp:233
flecs::id::type_id
flecs::entity type_id() const
Return component type of ID.
Definition impl.hpp:62
flecs::id::id_
flecs::id_t id_
The raw ID value.
Definition decl.hpp:149
flecs::id::world_
flecs::world_t * world_
World is optional, but guarantees that entity identifiers extracted from the ID are valid.
Definition decl.hpp:147
flecs::untyped_component::untyped_component
untyped_component()
Default constructor.
Definition component.hpp:317
flecs::world
The world.
Definition world.hpp:246
flecs::world::id
flecs::id id() const
Get ID from a type.
Definition impl.hpp:70