Flecs: include/flecs/addons/cpp/mixins/meta/untyped_component.inl 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
untyped_component.inl
Go to the documentation of this file.
1
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_entity_desc_t desc = {};
25 desc.name = name;
26 desc.parent = id_;
27 ecs_entity_t eid = ecs_entity_init(world_, &desc);
28 ecs_assert(eid != 0, ECS_INTERNAL_ERROR, NULL);
29
30 flecs::entity e(world_, eid);
31
32 Member m = {};
33 m.type = type_id;
34 m.unit = unit;
35 m.count = count;
36 m.offset = static_cast<int32_t>(offset);
37 m.use_offset = use_offset;
38 e.set<Member>(m);
39
40 return *this;
41}
42
43public:
44
46 untyped_component& member(
47 flecs::entity_t type_id,
48 flecs::entity_t unit,
49 const char *name,
50 int32_t count = 0)
51{
52 return internal_member(type_id, unit, name, count, 0, false);
53}
54
56 untyped_component& member(
57 flecs::entity_t type_id,
58 flecs::entity_t unit,
59 const char *name,
60 int32_t count,
61 size_t offset)
62{
63 return internal_member(type_id, unit, name, count, offset, true);
64}
65
67 untyped_component& member(
68 flecs::entity_t type_id,
69 const char* name,
70 int32_t count = 0)
71{
72 return member(type_id, 0, name, count);
73}
74
76 untyped_component& member(
77 flecs::entity_t type_id,
78 const char* name,
79 int32_t count,
80 size_t offset)
81{
82 return member(type_id, 0, name, count, offset);
83}
84
86template <typename MemberType>
87 untyped_component& member(
88 const char *name,
89 int32_t count = 0)
90{
91 flecs::entity_t type_id = _::type<MemberType>::id(world_);
92 return member(type_id, name, count);
93}
94
96template <typename MemberType>
97 untyped_component& member(
98 const char *name,
99 int32_t count,
100 size_t offset)
101{
102 flecs::entity_t type_id = _::type<MemberType>::id(world_);
103 return member(type_id, name, count, offset);
104}
105
107template <typename MemberType>
108 untyped_component& member(
109 flecs::entity_t unit,
110 const char *name,
111 int32_t count = 0)
112{
113 flecs::entity_t type_id = _::type<MemberType>::id(world_);
114 return member(type_id, unit, name, count);
115}
116
118template <typename MemberType>
119 untyped_component& member(
120 flecs::entity_t unit,
121 const char *name,
122 int32_t count,
123 size_t offset)
124{
125 flecs::entity_t type_id = _::type<MemberType>::id(world_);
126 return member(type_id, unit, name, count, offset);
127}
128
130template <typename MemberType, typename UnitType>
131 untyped_component& member(
132 const char *name,
133 int32_t count = 0)
134{
135 flecs::entity_t type_id = _::type<MemberType>::id(world_);
136 flecs::entity_t unit_id = _::type<UnitType>::id(world_);
137 return member(type_id, unit_id, name, count);
138}
139
141template <typename MemberType, typename UnitType>
142 untyped_component& member(
143 const char *name,
144 int32_t count,
145 size_t offset)
146{
147 flecs::entity_t type_id = _::type<MemberType>::id(world_);
148 flecs::entity_t unit_id = _::type<UnitType>::id(world_);
149 return member(type_id, unit_id, name, count, offset);
150}
151
153template <typename MemberType, typename ComponentType,
154 typename RealType = typename std::remove_extent<MemberType>::type>
155 untyped_component& member(
156 const char* name,
157 const MemberType ComponentType::* ptr)
158{
159 flecs::entity_t type_id = _::type<RealType>::id(world_);
160 size_t offset = reinterpret_cast<size_t>(&(static_cast<ComponentType*>(nullptr)->*ptr));
161 return member(type_id, name, std::extent<MemberType>::value, offset);
162}
163
165template <typename MemberType, typename ComponentType,
166 typename RealType = typename std::remove_extent<MemberType>::type>
167 untyped_component& member(
168 flecs::entity_t unit,
169 const char* name,
170 const MemberType ComponentType::* ptr)
171{
172 flecs::entity_t type_id = _::type<RealType>::id(world_);
173 size_t offset = reinterpret_cast<size_t>(&(static_cast<ComponentType*>(nullptr)->*ptr));
174 return member(type_id, unit, name, std::extent<MemberType>::value, offset);
175}
176
178template <typename UnitType, typename MemberType, typename ComponentType,
179 typename RealType = typename std::remove_extent<MemberType>::type>
180 untyped_component& member(
181 const char* name,
182 const MemberType ComponentType::* ptr)
183{
184 flecs::entity_t type_id = _::type<RealType>::id(world_);
185 flecs::entity_t unit_id = _::type<UnitType>::id(world_);
186 size_t offset = reinterpret_cast<size_t>(&(static_cast<ComponentType*>(nullptr)->*ptr));
187 return member(type_id, unit_id, name, std::extent<MemberType>::value, offset);
188}
189
191 untyped_component& constant(
192 const char *name,
193 int32_t value)
194{
195 ecs_add_id(world_, id_, _::type<flecs::Enum>::id(world_));
196
197 ecs_entity_desc_t desc = {};
198 desc.name = name;
199 desc.parent = id_;
200 ecs_entity_t eid = ecs_entity_init(world_, &desc);
201 ecs_assert(eid != 0, ECS_INTERNAL_ERROR, NULL);
202
203 ecs_set_id(world_, eid,
204 ecs_pair(flecs::Constant, flecs::I32), sizeof(int32_t),
205 &value);
206
207 return *this;
208}
209
211 untyped_component& bit(
212 const char *name,
213 uint32_t value)
214{
215 ecs_add_id(world_, id_, _::type<flecs::Bitmask>::id(world_));
216
217 ecs_entity_desc_t desc = {};
218 desc.name = name;
219 desc.parent = id_;
220 ecs_entity_t eid = ecs_entity_init(world_, &desc);
221 ecs_assert(eid != 0, ECS_INTERNAL_ERROR, NULL);
222
223 ecs_set_id(world_, eid,
224 ecs_pair(flecs::Constant, flecs::U32), sizeof(uint32_t),
225 &value);
226
227 return *this;
228}
229
231template <typename Elem>
232 untyped_component& array(
233 int32_t elem_count)
234{
235 ecs_array_desc_t desc = {};
236 desc.entity = id_;
237 desc.type = _::type<Elem>::id(world_);
238 desc.count = elem_count;
239 ecs_array_init(world_, &desc);
240 return *this;
241}
242
244 untyped_component& range(
245 double min,
246 double max)
247{
248 const flecs::member_t *m = ecs_cpp_last_member(world_, id_);
249 if (!m) {
250 return *this;
251 }
252
253 flecs::world w(world_);
254 flecs::entity me = w.entity(m->member);
255
256 // Don't use C++ ensure because Unreal defines a macro called ensure
257 flecs::MemberRanges *mr = static_cast<flecs::MemberRanges*>(
258 ecs_ensure_id(w, me, w.id<flecs::MemberRanges>()));
259 mr->value.min = min;
260 mr->value.max = max;
261 me.modified<flecs::MemberRanges>();
262 return *this;
263}
264
266 untyped_component& warning_range(
267 double min,
268 double max)
269{
270 const flecs::member_t *m = ecs_cpp_last_member(world_, id_);
271 if (!m) {
272 return *this;
273 }
274
275 flecs::world w(world_);
276 flecs::entity me = w.entity(m->member);
277
278 // Don't use C++ ensure because Unreal defines a macro called ensure
279 flecs::MemberRanges *mr = static_cast<flecs::MemberRanges*>(
280 ecs_ensure_id(w, me, w.id<flecs::MemberRanges>()));
281 mr->warning.min = min;
282 mr->warning.max = max;
283 me.modified<flecs::MemberRanges>();
284 return *this;
285}
286
288 untyped_component& error_range(
289 double min,
290 double max)
291{
292 const flecs::member_t *m = ecs_cpp_last_member(world_, id_);
293 if (!m) {
294 return *this;
295 }
296
297 flecs::world w(world_);
298 flecs::entity me = w.entity(m->member);
299
300 // Don't use C++ ensure because Unreal defines a macro called ensure
301 flecs::MemberRanges *mr = static_cast<flecs::MemberRanges*>(ecs_ensure_id(
302 w, me, w.id<flecs::MemberRanges>()));
303 mr->error.min = min;
304 mr->error.max = max;
305 me.modified<flecs::MemberRanges>();
306 return *this;
307}
308
flecs::type_id
flecs::entity_t type_id()
Get id currently assigned to component.
Definition component.hpp:527
ecs_add_id
void ecs_add_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Add a (component) id to an entity.
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:352
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:347
flecs::component::member
untyped_component & member(const char *name, int32_t count, size_t offset)
Add member with unit.
Definition untyped_component.inl:142
flecs::component::member
untyped_component & member(const char *name, int32_t count=0)
Add member.
Definition untyped_component.inl:87
flecs::component::member
untyped_component & member(flecs::entity_t unit, const char *name, int32_t count, size_t offset)
Add member with unit.
Definition untyped_component.inl:119
flecs::component::constant
untyped_component & constant(const char *name, int32_t value)
Add constant.
Definition untyped_component.inl:191
flecs::component::member
untyped_component & member(const char *name, int32_t count=0)
Add member with unit.
Definition untyped_component.inl:131
flecs::entity_view::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 entity a unit.
Definition entity_builder.inl:14
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::component::member
untyped_component & member(flecs::entity_t unit, const char *name, const MemberType ComponentType::*ptr)
Add member with unit using pointer-to-member.
Definition untyped_component.inl:167
flecs::component::warning_range
untyped_component & warning_range(double min, double max)
Add member warning range.
Definition untyped_component.inl:266
flecs::component::member
untyped_component & member(flecs::entity_t unit, const char *name, int32_t count=0)
Add member with unit.
Definition untyped_component.inl:108
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 member with unit, count and offset.
Definition untyped_component.inl:56
flecs::component::member
untyped_component & member(flecs::entity_t type_id, const char *name, int32_t count, size_t offset)
Add member with count and offset.
Definition untyped_component.inl:76
flecs::component::array
untyped_component & array(int32_t elem_count)
Register array metadata for component.
Definition untyped_component.inl:232
flecs::component::range
untyped_component & range(double min, double max)
Add member value range.
Definition untyped_component.inl:244
flecs::component::member
untyped_component & member(const char *name, const MemberType ComponentType::*ptr)
Add member using pointer-to-member.
Definition untyped_component.inl:155
flecs::component::member
untyped_component & member(flecs::entity_t type_id, const char *name, int32_t count=0)
Add member.
Definition untyped_component.inl:67
flecs::component::member
untyped_component & member(const char *name, const MemberType ComponentType::*ptr)
Add member with unit using pointer-to-member.
Definition untyped_component.inl:180
flecs::component::bit
untyped_component & bit(const char *name, uint32_t value)
Add bitmask constant.
Definition untyped_component.inl:211
flecs::component::member
untyped_component & member(const char *name, int32_t count, size_t offset)
Add member.
Definition untyped_component.inl:97
flecs::component::error_range
untyped_component & error_range(double min, double max)
Add member error range.
Definition untyped_component.inl:288
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:195
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 id)
Get a mutable pointer to a component.
ecs_set_id
void ecs_set_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id, size_t size, const void *ptr)
Set the value of a component.
EcsMemberRanges
Component added to member entities to express valid value ranges.
Definition meta.h:215
EcsMemberRanges::warning
ecs_member_value_range_t warning
Member value warning range.
Definition meta.h:217
EcsMemberRanges::value
ecs_member_value_range_t value
Member value range.
Definition meta.h:216
EcsMemberRanges::error
ecs_member_value_range_t error
Member value error range.
Definition meta.h:218
EcsMember::type
ecs_entity_t type
Member type.
Definition meta.h:201
ecs_array_desc_t
Used with ecs_array_init().
Definition meta.h:996
ecs_array_desc_t::entity
ecs_entity_t entity
Existing entity to use for type (optional).
Definition meta.h:997
ecs_array_desc_t::type
ecs_entity_t type
Element type.
Definition meta.h:998
ecs_array_desc_t::count
int32_t count
Number of elements.
Definition meta.h:999
ecs_entity_desc_t
Used with ecs_entity_init().
Definition flecs.h:938
ecs_entity_desc_t::name
const char * name
Name of the entity.
Definition flecs.h:945
ecs_entity_desc_t::parent
ecs_entity_t parent
Parent entity.
Definition flecs.h:943
ecs_member_t
Element type of members vector in EcsStruct.
Definition meta.h:222
ecs_member_t::member
ecs_entity_t member
Should not be set by ecs_struct_desc_t.
Definition meta.h:261
ecs_member_value_range_t::min
double min
Min member value.
Definition meta.h:210
ecs_member_value_range_t::max
double max
Max member value.
Definition meta.h:211
flecs::_::type
Definition flecs.hpp:38
flecs::entity
Entity.
Definition entity.hpp:30
flecs::entity::modified
void modified() const
Signal that component was modified.
Definition entity.hpp:186
flecs::untyped_component
Untyped component class.
Definition component.hpp:350
flecs::world
The world.
Definition world.hpp:137
flecs::world::id
flecs::id id() const
Get id from a type.
Definition impl.hpp:70