Skip to content
Flecs v4.2
builder.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/mixins/entity/builder.hpp
3 * @brief Entity builder.
4 */
5
6#pragma once
7
8namespace flecs
9{
10
11/** Entity builder.
12 * @ingroup cpp_entities
13 */
14template <typename Self>
16
18
19 template <typename... T, typename... Args>
20 const Self& add(Args... args) const {
21 _::add_component(this->world_, this->id_,
22 _::make_id<T...>(this->world_, args...));
23 return to_base();
24 }
25
26 template <typename Second>
27 const Self& add_second(flecs::entity_t first) const {
28 return add(first, _::type<Second>::id(this->world_));
29 }
30
31 /** Conditional add.
32 * This operation adds if condition is true, removes if condition is false.
33 *
34 * @param cond The condition to evaluate.
35 * @param component The component to add.
36 */
37 const Self& add_if(bool cond, flecs::id_t component) const {
38 if (cond) {
39 return this->add(component);
40 } else {
41 return this->remove(component);
42 }
43 }
44
45 /** Conditional add.
46 * This operation adds if condition is true, removes if condition is false.
47 *
48 * @tparam T The component to add.
49 * @param cond The condition to evaluate.
50 */
51 template <typename T>
52 const Self& add_if(bool cond) const {
53 if (cond) {
54 return this->add<T>();
55 } else {
56 return this->remove<T>();
57 }
58 }
59
60 /** Conditional add.
61 * This operation adds if condition is true, removes if condition is false.
62 *
63 * @param cond The condition to evaluate.
64 * @param first The first element of the pair.
65 * @param second The second element of the pair.
66 */
67 const Self& add_if(bool cond, flecs::entity_t first, flecs::entity_t second) const {
68 if (cond) {
69 return this->add(first, second);
70 } else {
71 /* If second is 0 or if relationship is exclusive, use wildcard for
72 * second which will remove all instances of the relationship.
73 * Replacing 0 with Wildcard will make it possible to use the second
74 * as the condition. */
75 if (!second || ecs_has_id(this->world_, first, flecs::Exclusive)) {
76 second = flecs::Wildcard;
77 }
78 return this->remove(first, second);
79 }
80 }
81
82 /** Conditional add.
83 * This operation adds if condition is true, removes if condition is false.
84 *
85 * @tparam First The first element of the pair.
86 * @param cond The condition to evaluate.
87 * @param second The second element of the pair.
88 */
89 template <typename First>
90 const Self& add_if(bool cond, flecs::entity_t second) const {
91 return this->add_if(cond, _::type<First>::id(this->world_), second);
92 }
93
94 /** Conditional add.
95 * This operation adds if condition is true, removes if condition is false.
96 *
97 * @tparam First The first element of the pair.
98 * @tparam Second The second element of the pair.
99 * @param cond The condition to evaluate.
100 */
101 template <typename First, typename Second>
102 const Self& add_if(bool cond) const {
103 return this->add_if<First>(cond, _::type<Second>::id(this->world_));
104 }
105
106 /** Conditional add.
107 * This operation adds if condition is true, removes if condition is false.
108 *
109 * @param cond The condition to evaluate.
110 * @param constant The enumeration constant.
111 */
112 template <typename E, if_t< is_enum<E>::value > = 0>
113 const Self& add_if(bool cond, E constant) const {
114 const auto& et = enum_type<E>(this->world_);
115 return this->add_if<E>(cond, et.entity(constant));
116 }
118 /** Shortcut for `add(IsA, entity)`.
119 *
120 * @param second The second element of the pair.
121 */
122 const Self& is_a(entity_t second) const {
123 return this->add(flecs::IsA, second);
124 }
125
126 /** Shortcut for `add(IsA, entity)`.
127 *
128 * @tparam T The type associated with the entity.
129 */
130 template <typename T>
131 const Self& is_a() const {
132 return this->add(flecs::IsA, _::type<T>::id(this->world_));
133 }
134
135 /** Shortcut for `add(ChildOf, entity)`.
136 *
137 * @param second The second element of the pair.
138 */
139 const Self& child_of(entity_t second) const {
140 return this->add(flecs::ChildOf, second);
141 }
142
143 /** Shortcut for `add(DependsOn, entity)`.
144 *
145 * @param second The second element of the pair.
146 */
147 const Self& depends_on(entity_t second) const {
148 return this->add(flecs::DependsOn, second);
149 }
150
151 /** Shortcut for `add(DependsOn, entity)`.
152 *
153 * @param second The second element of the pair.
154 */
155 template <typename E, if_t<is_enum<E>::value> = 0>
156 const Self& depends_on(E second) const {
157 const auto& et = enum_type<E>(this->world_);
159 return depends_on(target);
160 }
161
162 /** Shortcut for `add(ChildOf, entity)`.
163 *
164 * @tparam T The type associated with the entity.
165 */
166 template <typename T>
167 const Self& child_of() const {
168 return this->child_of(_::type<T>::id(this->world_));
169 }
170
171 /** Shortcut for `add(DependsOn, entity)`.
172 *
173 * @tparam T The type associated with the entity.
174 */
175 template <typename T>
176 const Self& depends_on() const {
177 return this->depends_on(_::type<T>::id(this->world_));
178 }
179
180 template <typename... T, typename... Args>
181 const Self& remove(Args... args) const {
182 ecs_remove_id(this->world_, this->id_,
183 _::make_id<T...>(this->world_, args...).id);
184 return to_base();
185 }
186
187 template <typename Second>
188 const Self& remove_second(flecs::entity_t first) const {
189 return remove(first, _::type<Second>::id(this->world_));
190 }
191
192 template <typename... T, typename... Args>
193 const Self& auto_override(Args... args) const {
194 return add(ECS_AUTO_OVERRIDE | _::make_id<T...>(this->world_, args...).id);
195 }
196
197 template <typename Second>
198 const Self& auto_override_second(flecs::entity_t first) const {
199 return auto_override(first, _::type<Second>::id(this->world_));
200 }
201
202 template <typename... T, typename A = _::value_type_t<T...>>
203 const Self& set_auto_override(A&& value) const {
204 auto id = _::value_id<T...>(this->world_, value);
205 this->auto_override(id.id);
206 flecs::set(this->world_, this->id_, _::value_cast<T...>(FLECS_FWD(value)), id.id);
207 return to_base();
208 }
209
210 template <typename First>
211 const Self& set_auto_override(flecs::entity_t second, First&& value) const {
212 auto id = _::make_id<First>(this->world_, second);
213 this->auto_override(id.id);
214 flecs::set(this->world_, this->id_, FLECS_FWD(value), id.id);
215 return to_base();
216 }
217
218 template <typename First>
219 const Self& set_auto_override(flecs::entity_t second, const First& value) const {
220 auto id = _::make_id<First>(this->world_, second);
221 this->auto_override(id.id);
222 flecs::set(this->world_, this->id_, value, id.id);
223 return to_base();
224 }
225
226 template <typename... T, typename... Args>
227 const Self& emplace_auto_override(Args&&... args) const {
228 auto id = _::make_id<T...>(this->world_);
229 this->auto_override(id.id);
230 flecs::emplace<typename decltype(id)::type>(this->world_, this->id_,
231 id.id, FLECS_FWD(args)...);
232 return to_base();
233 }
234
235 /** Enable an entity.
236 * Enabled entities are matched with systems and can be searched with
237 * queries.
238 */
239 const Self& enable() const {
240 ecs_enable(this->world_, this->id_, true);
241 return to_base();
242 }
243
244 /** Disable an entity.
245 * Disabled entities are not matched with systems and cannot be searched
246 * with queries, unless explicitly specified in the query expression.
247 */
248 const Self& disable() const {
249 ecs_enable(this->world_, this->id_, false);
250 return to_base();
251 }
252
253 /** Enable an ID.
254 * This sets the enabled bit for this component. If this is the first time
255 * the component is enabled or disabled, the bitset is added.
256 *
257 * @param id The ID to enable.
258 * @param toggle True to enable, false to disable (default = true).
259 *
260 * @see ecs_enable_id()
261 */
262 const Self& enable(flecs::id_t id, bool toggle = true) const {
263 ecs_enable_id(this->world_, this->id_, id, toggle);
264 return to_base();
265 }
266
267 /** Enable a component.
268 * @see enable(flecs::id_t) const
269 *
270 * @tparam T The component to enable.
271 */
272 template<typename T>
273 const Self& enable() const {
274 return this->enable(_::type<T>::id(this->world_));
275 }
276
277 /** Enable a pair.
278 * @see enable(flecs::id_t) const
279 *
280 * @param first The first element of the pair.
281 * @param second The second element of the pair.
282 */
284 return this->enable(ecs_pair(first, second));
285 }
286
287 /** Enable a pair.
288 * @see enable(flecs::id_t) const
289 *
290 * @tparam First The first element of the pair.
291 * @param second The second element of the pair.
292 */
293 template<typename First>
294 const Self& enable(flecs::id_t second) const {
295 return this->enable(_::type<First>::id(world_), second);
296 }
297
298 /** Enable a pair.
299 * @see enable(flecs::id_t) const
300 *
301 * @tparam First The first element of the pair.
302 * @tparam Second The second element of the pair.
303 */
304 template<typename First, typename Second>
305 const Self& enable() const {
307 }
308
309 /** Disable an ID.
310 * This sets the enabled bit for this ID. If this is the first time
311 * the ID is enabled or disabled, the bitset is added.
312 *
313 * @param id The ID to disable.
314 *
315 * @see ecs_enable_id()
316 * @see enable(flecs::id_t) const
317 */
318 const Self& disable(flecs::id_t id) const {
319 return this->enable(id, false);
320 }
321
322 /** Disable a component.
323 * @see disable(flecs::id_t) const
324 *
325 * @tparam T The component to disable.
326 */
327 template<typename T>
328 const Self& disable() const {
329 return this->disable(_::type<T>::id(world_));
330 }
331
332 /** Disable a pair.
333 * @see disable(flecs::id_t) const
334 *
335 * @param first The first element of the pair.
336 * @param second The second element of the pair.
337 */
339 return this->disable(ecs_pair(first, second));
340 }
341
342 /** Disable a pair.
343 * @see disable(flecs::id_t) const
344 *
345 * @tparam First The first element of the pair.
346 * @param second The second element of the pair.
347 */
348 template<typename First>
349 const Self& disable(flecs::id_t second) const {
350 return this->disable(_::type<First>::id(world_), second);
351 }
352
353 /** Disable a pair.
354 * @see disable(flecs::id_t) const
355 *
356 * @tparam First The first element of the pair.
357 * @tparam Second The second element of the pair.
358 */
359 template<typename First, typename Second>
360 const Self& disable() const {
362 }
363
364 const Self& set_ptr(entity_t comp, size_t size, const void *ptr) const {
365 ecs_set_id(this->world_, this->id_, comp, size, ptr);
366 return to_base();
367 }
368
369 const Self& set_ptr(entity_t comp, const void *ptr) const {
370
371 const ecs_type_info_t *type_info = ecs_get_type_info(this->world_, comp);
372
373 /* Can't set if it's not a component */
374 ecs_assert(type_info != nullptr, ECS_INVALID_PARAMETER, nullptr);
375
376 return set_ptr(comp, type_info->size, ptr);
377 }
378
379 template <typename... T, typename A = _::value_type_t<T...>>
380 const Self& set(A&& value) const {
381 auto id = _::value_id<T...>(this->world_, value);
382 flecs::set(this->world_, this->id_, _::value_cast<T...>(FLECS_FWD(value)), id.id);
383 return to_base();
384 }
385
386 template <typename First, typename Second>
387 const Self& set(Second second, First&& value) const {
388 auto id = _::make_id<First>(this->world_, second);
389 flecs::set(this->world_, this->id_, FLECS_FWD(value), id.id);
390 return to_base();
391 }
392
393 template <typename First, typename Second>
394 const Self& set(Second second, const First& value) const {
395 auto id = _::make_id<First>(this->world_, second);
396 flecs::set(this->world_, this->id_, value, id.id);
397 return to_base();
398 }
399
400 template <typename Second>
401 const Self& set_second(entity_t first, Second&& value) const {
402 auto id = _::second_id<Second>(this->world_, first);
403 flecs::set(this->world_, this->id_, FLECS_FWD(value), id.id);
404 return to_base();
405 }
406
407 template <typename Second>
408 const Self& set_second(entity_t first, const Second& value) const {
409 auto id = _::second_id<Second>(this->world_, first);
410 flecs::set(this->world_, this->id_, value, id.id);
411 return to_base();
412 }
413
414 template <typename First, typename Second>
415 const Self& set_second(const Second& value) const {
416 flecs::set<pair_object<First, Second>>(this->world_, this->id_, value);
417 return to_base();
418 }
419
420 template <typename... T, typename A = _::value_type_t<T...>>
421 const Self& assign(A&& value) const {
422 auto id = _::value_id<T...>(this->world_, value);
423 flecs::assign(this->world_, this->id_, _::value_cast<T...>(FLECS_FWD(value)), id.id);
424 return to_base();
425 }
426
427 template <typename First, typename Second>
428 const Self& assign(Second second, First&& value) const {
429 auto id = _::make_id<First>(this->world_, second);
430 flecs::assign(this->world_, this->id_, FLECS_FWD(value), id.id);
431 return to_base();
432 }
433
434 template <typename First, typename Second>
435 const Self& assign(Second second, const First& value) const {
436 auto id = _::make_id<First>(this->world_, second);
437 flecs::assign(this->world_, this->id_, value, id.id);
438 return to_base();
439 }
440
441 template <typename Second>
442 const Self& assign_second(entity_t first, Second&& value) const {
443 auto id = _::second_id<Second>(this->world_, first);
444 flecs::assign(this->world_, this->id_, FLECS_FWD(value), id.id);
445 return to_base();
446 }
447
448 template <typename Second>
449 const Self& assign_second(entity_t first, const Second& value) const {
450 auto id = _::second_id<Second>(this->world_, first);
451 flecs::assign(this->world_, this->id_, value, id.id);
452 return to_base();
453 }
454
455 template <typename First, typename Second>
456 const Self& assign_second(const Second& value) const {
457 flecs::assign<pair_object<First, Second>>(this->world_, this->id_, value);
458 return to_base();
459 }
460
461 /** Set 1..N components.
462 * This operation accepts a callback with as arguments the components to
463 * set. If the entity does not have all of the provided components, they
464 * will be added.
465 *
466 * This operation is faster than individually calling ensure() for each component
467 * as it only obtains entity metadata once. When this operation is called
468 * while deferred, its performance is equivalent to that of calling ensure()
469 * for each component separately.
470 *
471 * The operation will invoke modified() for each component after the callback
472 * has been invoked.
473 *
474 * @param func The callback to invoke.
475 */
476 template <typename Func>
477 const Self& insert(const Func& func) const;
478
479 template <typename... T, typename... Args>
480 const Self& emplace(Args&&... args) const {
481 auto id = _::make_id<T...>(this->world_);
482 flecs::emplace<typename decltype(id)::type>(this->world_, this->id_,
483 id.id, FLECS_FWD(args)...);
484 return to_base();
485 }
486
487 template <typename First, typename ... Args>
488 const Self& emplace_first(flecs::entity_t second, Args&&... args) const {
489 auto first = _::type<First>::id(this->world_);
490 flecs::emplace<First>(this->world_, this->id_,
491 ecs_pair(first, second),
492 FLECS_FWD(args)...);
493 return to_base();
494 }
495
496 template <typename Second, typename ... Args>
497 const Self& emplace_second(flecs::entity_t first, Args&&... args) const {
498 auto second = _::type<Second>::id(this->world_);
499 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != nullptr,
500 ECS_INVALID_PARAMETER, "pair is not a component");
501 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second))->component == second,
502 ECS_INVALID_PARAMETER, "type of pair is not Second");
503 flecs::emplace<Second>(this->world_, this->id_,
504 ecs_pair(first, second),
505 FLECS_FWD(args)...);
506 return to_base();
507 }
508
509 /** The function will be run with the scope set to the current entity. */
510 template <typename Func>
511 const Self& scope(const Func& func) const {
512 ecs_entity_t prev = ecs_set_scope(this->world_, this->id_);
513 func();
514 ecs_set_scope(this->world_, prev);
515 return to_base();
516 }
517
518 /** Set the entity name. */
519 const Self& set_name(const char *name) const {
520 ecs_set_name(this->world_, this->id_, name);
521 return to_base();
522 }
523
524 /** Set the entity alias. */
525 const Self& set_alias(const char *name) const {
526 ecs_set_alias(this->world_, this->id_, name);
527 return to_base();
528 }
529
530# ifdef FLECS_DOC
532# endif
533
534# ifdef FLECS_META
536# endif
537
538# ifdef FLECS_JSON
540# endif
541
543
544protected:
545 const Self& to_base() const {
546 return *static_cast<const Self*>(this);
547 }
548};
549
550}
Doc entity builder mixin.
enum_data< E > enum_type(flecs::world_t *world)
Convenience function for getting enum reflection data.
Definition enum.hpp:457
Event entity mixin.
void ecs_remove_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Remove a component from an entity.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:479
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:704
const ecs_type_info_t * ecs_get_type_info(const ecs_world_t *world, ecs_id_t component)
Get the type info for a component.
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:396
untyped_component & constant(const char *name, T value)
Add a constant.
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
void ecs_enable_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component, bool enable)
Enable or disable a component.
void ecs_enable(ecs_world_t *world, ecs_entity_t entity, bool enabled)
Enable or disable an entity.
bool ecs_has_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Test if an entity has a component.
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.
const ecs_id_t ECS_AUTO_OVERRIDE
Automatically override component when it is inherited.
void ecs_set_alias(ecs_world_t *world, ecs_entity_t entity, const char *alias)
Set an alias for an entity.
ecs_entity_t ecs_set_name(ecs_world_t *world, ecs_entity_t entity, const char *name)
Set the name of an entity.
ecs_entity_t ecs_set_scope(ecs_world_t *world, ecs_entity_t scope)
Set the current scope.
JSON entity mixin.
Meta entity builder mixin.
Type that contains component information (passed to ctors/dtors/...).
Definition flecs.h:1053
ecs_size_t size
Size of the type.
Definition flecs.h:1054
Component class.
Entity builder.
Definition builder.hpp:15
const Self & disable() const
Disable an entity.
Definition builder.hpp:248
const Self & enable(flecs::id_t second) const
Enable a pair.
Definition builder.hpp:294
const Self & add_if(bool cond, E constant) const
Conditional add.
Definition builder.hpp:113
const Self & disable(flecs::id_t first, flecs::id_t second) const
Disable a pair.
Definition builder.hpp:338
const Self & is_a() const
Shortcut for add(IsA, entity).
Definition builder.hpp:131
const Self & enable() const
Enable an entity.
Definition builder.hpp:239
const Self & enable() const
Enable a component.
Definition builder.hpp:273
const Self & child_of() const
Shortcut for add(ChildOf, entity).
Definition builder.hpp:167
const Self & enable() const
Enable a pair.
Definition builder.hpp:305
const Self & enable(flecs::id_t first, flecs::id_t second) const
Enable a pair.
Definition builder.hpp:283
const Self & disable(flecs::id_t id) const
Disable an ID.
Definition builder.hpp:318
const Self & add_if(bool cond) const
Conditional add.
Definition builder.hpp:102
const Self & is_a(entity_t second) const
Shortcut for add(IsA, entity).
Definition builder.hpp:122
const Self & depends_on(E second) const
Shortcut for add(DependsOn, entity).
Definition builder.hpp:156
const Self & add_if(bool cond, flecs::entity_t first, flecs::entity_t second) const
Conditional add.
Definition builder.hpp:67
const Self & add_if(bool cond) const
Conditional add.
Definition builder.hpp:52
const Self & disable() const
Disable a component.
Definition builder.hpp:328
entity_view()
Default constructor.
const Self & set_name(const char *name) const
Set the entity name.
Definition builder.hpp:519
const Self & insert(const Func &func) const
Set 1..N components.
Definition impl.hpp:23
const Self & scope(const Func &func) const
The function will be run with the scope set to the current entity.
Definition builder.hpp:511
const Self & add_if(bool cond, flecs::entity_t second) const
Conditional add.
Definition builder.hpp:90
const Self & add_if(bool cond, flecs::id_t component) const
Conditional add.
Definition builder.hpp:37
const Self & set_alias(const char *name) const
Set the entity alias.
Definition builder.hpp:525
const Self & disable(flecs::id_t second) const
Disable a pair.
Definition builder.hpp:349
const Self & enable(flecs::id_t id, bool toggle=true) const
Enable an ID.
Definition builder.hpp:262
const Self & child_of(entity_t second) const
Shortcut for add(ChildOf, entity).
Definition builder.hpp:139
const Self & depends_on(entity_t second) const
Shortcut for add(DependsOn, entity).
Definition builder.hpp:147
const Self & depends_on() const
Shortcut for add(DependsOn, entity).
Definition builder.hpp:176
const Self & disable() const
Disable a pair.
Definition builder.hpp:360
flecs::string_view name() const
Return the entity name.
entity_view()
Default constructor.
flecs::entity target(int32_t index=0) const
Get target for a given pair.
Definition impl.hpp:36
entity()
Default constructor.
Definition entity.hpp:32
flecs::id_t id_
The raw ID value.
Definition decl.hpp:154
flecs::entity second() const
Get second element from a pair.
Definition impl.hpp:31
flecs::world_t * world_
World is optional, but guarantees that entity identifiers extracted from the ID are valid.
Definition decl.hpp:152
flecs::entity first() const
Get first element from a pair.
Definition impl.hpp:20