Skip to content
Flecs v4.1
world.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/world.hpp
3 * @brief World class.
4 */
5
6#pragma once
7
8namespace flecs
9{
10
11namespace _ {
13template <auto Write, typename T>
14void write_component(world_t *world, flecs::entity_t entity, T&& value, flecs::id_t id) {
15 using A = std::remove_const_t<remove_reference_t<T>>;
17 "operation invalid for empty type");
18 auto res = Write(world, entity, id, &value, sizeof(A));
19 A& dst = *static_cast<A*>(res.ptr);
20 if constexpr (std::is_copy_assignable_v<T> || std::is_const_v<remove_reference_t<T>>) {
21 dst = FLECS_FWD(value);
22 } else {
23 dst = FLECS_MOV(value);
24 }
25 if (res.stage) {
26 flecs_commands_end(res.world, res.stage);
27 }
28 if (res.call_modified) {
30 }
31}
34
35template <typename T>
36inline void set(world_t *world, flecs::entity_t entity, T&& value, flecs::id_t id) {
37 _::write_component<ecs_cpp_set>(world, entity, FLECS_FWD(value), id);
38}
39
40template <typename T>
41inline void set(world_t *world, flecs::entity_t entity, const T& value, flecs::id_t id) {
42 _::write_component<ecs_cpp_set>(world, entity, value, id);
43}
44
45template <typename T, typename A>
46inline void set(world_t *world, entity_t entity, A&& value) {
47 flecs::set(world, entity, FLECS_FWD(value), _::type<T>::id(world));
48}
49
50template <typename T, typename A>
51inline void set(world_t *world, entity_t entity, const A& value) {
52 flecs::set(world, entity, value, _::type<T>::id(world));
53}
54
55template <typename T>
56inline void assign(world_t *world, flecs::entity_t entity, T&& value, flecs::id_t id) {
57 _::write_component<ecs_cpp_assign>(world, entity, FLECS_FWD(value), id);
58}
59
60template <typename T>
61inline void assign(world_t *world, flecs::entity_t entity, const T& value, flecs::id_t id) {
62 _::write_component<ecs_cpp_assign>(world, entity, value, id);
63}
64
65template <typename T, typename A>
66inline void assign(world_t *world, entity_t entity, A&& value) {
67 flecs::assign(world, entity, FLECS_FWD(value), _::type<T>::id(world));
68}
69
70template <typename T, typename A>
71inline void assign(world_t *world, entity_t entity, const A& value) {
72 flecs::assign(world, entity, value, _::type<T>::id(world));
73}
74
75/** Emplace a component value, constructing it in place.
76 *
77 * @tparam T The component type.
78 * @tparam Args Constructor argument types.
79 * @param world The world.
80 * @param entity The entity.
81 * @param id The component ID.
82 * @param args Constructor arguments.
83 */
84template <typename T, typename ... Args, if_t<
85 std::is_constructible<actual_type_t<T>, Args...>::value ||
86 std::is_default_constructible<actual_type_t<T>>::value > = 0>
87inline void emplace(world_t *world, flecs::entity_t entity, flecs::id_t id, Args&&... args) {
89 "operation invalid for empty type");
90 T& dst = *static_cast<T*>(ecs_emplace_id(world, entity, id, sizeof(T), nullptr));
91
92 FLECS_PLACEMENT_NEW(&dst, T{FLECS_FWD(args)...});
93
95}
96
97/** Return the ID without generation.
98 *
99 * @param e The entity ID.
100 * @return The entity ID without generation.
101 *
102 * @see ecs_strip_generation()
103 */
105 return ecs_strip_generation(e);
106}
107
108/** Return the entity generation.
109 *
110 * @param e The entity ID.
111 * @return The generation of the entity.
112 */
113inline uint32_t get_generation(flecs::entity_t e) {
114 return ECS_GENERATION(e);
115}
116
117/**
118 * @defgroup cpp_world World
119 * @ingroup cpp_core
120 * World operations.
121 *
122 * @{
123 */
124
125/** The world.
126 * The world is the container of all ECS data and systems. If the world is
127 * deleted, all data in the world will be deleted as well.
128 */
129struct world {
130 /** Create a world.
131 */
132 explicit world()
133 : world_( ecs_init() ) {
135 }
136
137 /** Create a world with command-line arguments.
138 * Currently, command-line arguments are not interpreted, but they may be
139 * used in the future to configure Flecs parameters.
140 */
141 explicit world(int argc, char *argv[])
142 : world_( ecs_init_w_args(argc, argv) ) {
144 }
145
146 /** Create a world from a C world.
147 */
148 explicit world(world_t *w)
149 : world_( w ) {
150 if (w) {
151 flecs_poly_claim(w);
152 }
153 }
154
155 /** Copy constructor. Increases reference count on the world.
156 */
157 world(const world& obj) {
158 this->world_ = obj.world_;
159 flecs_poly_claim(this->world_);
160 }
161
162 /** Copy assignment operator. Increases reference count on the world. */
163 world& operator=(const world& obj) noexcept {
164 release();
165 this->world_ = obj.world_;
166 flecs_poly_claim(this->world_);
167 return *this;
168 }
169
170 /** Move constructor. Transfers world ownership. */
171 world(world&& obj) noexcept {
172 world_ = obj.world_;
173 obj.world_ = nullptr;
174 }
175
176 /** Move assignment operator. Transfers world ownership. */
177 world& operator=(world&& obj) noexcept {
178 release();
179 world_ = obj.world_;
180 obj.world_ = nullptr;
181 return *this;
182 }
183
184 /** Release the underlying world object.
185 * If this is the last handle, the world will be finalized.
186 */
187 void release() {
188 if (world_) {
189 if (!flecs_poly_release(world_)) {
190 if (ecs_stage_get_id(world_) == -1) {
192 } else {
193 // Before we call ecs_fini(), we increment the reference count back to 1.
194 // Otherwise, copies of this object created during ecs_fini() (e.g., a component on_remove hook)
195 // would again call this destructor and ecs_fini().
196 flecs_poly_claim(world_);
198 }
199 }
200 world_ = nullptr;
201 }
202 }
203
204 /** Destructor. Releases the world reference. */
206 release();
207 }
208
209 /** Implicit conversion to world_t*. */
210 operator world_t*() const { return world_; }
211
212 /** Make the current world object the owner of the world. This may only be called on
213 * one flecs::world object, and may only be called once. Failing to do so
214 * will result in undefined behavior.
215 *
216 * This operation allows a custom (C) world to be wrapped by a C++ object,
217 * and transfer ownership so that the world is automatically cleaned up.
218 */
219 void make_owner() {
220 flecs_poly_release(world_);
221 }
222
223 /** Delete and recreate the world. */
224 void reset() {
225 /* Make sure there's only one reference to the world. */
226 ecs_assert(flecs_poly_refcount(world_) == 1, ECS_INVALID_OPERATION,
227 "reset would invalidate other handles");
229 world_ = ecs_init();
231 }
232
233 /** Obtain a pointer to the C world object.
234 */
235 world_t* c_ptr() const {
236 return world_;
237 }
238
239 /** Register action to be executed when world is destroyed.
240 */
241 void atfini(ecs_fini_action_t action, void *ctx = nullptr) const {
242 ecs_atfini(world_, action, ctx);
243 }
244
245 /** Begin readonly mode.
246 *
247 * @param multi_threaded Whether to enable readonly/multi-threaded mode.
248 *
249 * @return Whether world is currently readonly.
250 *
251 * @see ecs_readonly_begin()
252 * @see flecs::world::is_readonly()
253 * @see flecs::world::readonly_end()
254 */
255 bool readonly_begin(bool multi_threaded = false) const {
256 return ecs_readonly_begin(world_, multi_threaded);
257 }
258
259 /** End readonly mode.
260 *
261 * @see ecs_readonly_end()
262 * @see flecs::world::is_readonly()
263 * @see flecs::world::readonly_begin()
264 */
265 void readonly_end() const {
267 }
268
269 /** Configure world to have N stages.
270 * This initializes N stages, which allows applications to defer operations to
271 * multiple isolated defer queues. This is typically used for applications with
272 * multiple threads, where each thread gets its own queue, and commands are
273 * merged when threads are synchronized.
274 *
275 * Note that set_threads() already creates the appropriate number of stages.
276 * The set_stage_count() operation is useful for applications that want to manage
277 * their own stages and/or threads.
278 *
279 * @param stages The number of stages.
280 *
281 * @see ecs_set_stage_count()
282 * @see flecs::world::get_stage_count()
283 */
284 void set_stage_count(int32_t stages) const {
286 }
287
288 /** Get the number of configured stages.
289 * Return the number of stages set by set_stage_count().
290 *
291 * @return The number of stages used for threading.
292 *
293 * @see ecs_get_stage_count()
294 * @see flecs::world::set_stage_count()
295 */
296 int32_t get_stage_count() const {
298 }
299
300 /** Get current stage ID.
301 * The stage ID can be used by an application to learn about which stage it
302 * is using, which typically corresponds with the worker thread ID.
303 *
304 * @return The stage ID.
305 */
306 int32_t get_stage_id() const {
307 return ecs_stage_get_id(world_);
308 }
309
310 /** Test if this is a stage.
311 * If this function returns false, it is guaranteed that this is a valid
312 * world object.
313 *
314 * @return True if the world is a stage, false if not.
315 */
316 bool is_stage() const {
321 "flecs::world instance contains invalid reference to world or stage");
323 }
324
325 /** Merge world or stage.
326 * When automatic merging is disabled, an application can call this
327 * operation on either an individual stage, or on the world which will merge
328 * all stages. This operation may only be called when staging is not enabled
329 * (either after progress() or after readonly_end()).
330 *
331 * This operation may be called on an already merged stage or world.
332 *
333 * @see ecs_merge()
334 */
335 void merge() const {
337 }
338
339 /** Get stage-specific world pointer.
340 * Flecs threads can safely invoke the API as long as they have a private
341 * context to write to, also referred to as the stage. This function returns a
342 * pointer to a stage, disguised as a world pointer.
343 *
344 * Note that this function does not(!) create a new world. It simply wraps the
345 * existing world in a thread-specific context, which the API knows how to
346 * unwrap. The reason the stage is returned as an ecs_world_t is so that it
347 * can be passed transparently to the existing API functions, vs. having to
348 * create a dedicated API for threading.
349 *
350 * @param stage_id The index of the stage to retrieve.
351 * @return A thread-specific pointer to the world.
352 */
353 flecs::world get_stage(int32_t stage_id) const {
354 return flecs::world(ecs_get_stage(world_, stage_id));
355 }
356
357 /** Create an asynchronous stage.
358 * An asynchronous stage can be used to asynchronously queue operations for
359 * later merging with the world. An asynchronous stage is similar to a regular
360 * stage, except that it does not allow reading from the world.
361 *
362 * The application must ensure that no commands are added to the stage while the
363 * stage is being merged.
364 *
365 * @return The stage.
366 */
369 flecs_poly_release(as); // World object will claim.
370 return flecs::world(as);
371 }
372
373 /** Get actual world.
374 * If the current object points to a stage, this operation will return the
375 * actual world.
376 *
377 * @return The actual world.
378 */
380 /* Safe cast, mutability is checked. */
381 return flecs::world(
382 world_ ? const_cast<flecs::world_t*>(ecs_get_world(world_)) : nullptr);
383 }
384
385 /** Test whether the current world object is readonly.
386 * This function allows the code to test whether the currently used world
387 * object is readonly or whether it allows for writing.
388 *
389 * @return True if the world or stage is readonly.
390 *
391 * @see ecs_stage_is_readonly()
392 * @see flecs::world::readonly_begin()
393 * @see flecs::world::readonly_end()
394 */
395 bool is_readonly() const {
397 }
398
399 /** Set world context.
400 * Set a context value that can be accessed by anyone that has a reference
401 * to the world.
402 *
403 * @param ctx A pointer to a user-defined structure.
404 * @param ctx_free A function that is invoked with ctx when the world is freed.
405 *
406 * @see ecs_set_ctx()
407 * @see flecs::world::get_ctx()
408 */
409 void set_ctx(void* ctx, ecs_ctx_free_t ctx_free = nullptr) const {
410 ecs_set_ctx(world_, ctx, ctx_free);
411 }
412
413 /** Get world context.
414 * This operation retrieves a previously set world context.
415 *
416 * @return The context set with set_ctx(). If no context was set, the
417 * function returns nullptr.
418 *
419 * @see ecs_get_ctx()
420 * @see flecs::world::set_ctx()
421 */
422 void* get_ctx() const {
423 return ecs_get_ctx(world_);
424 }
425
426 /** Set world binding context.
427 *
428 * Same as set_ctx() but for binding context. A binding context is intended
429 * specifically for language bindings to store binding-specific data.
430 *
431 * @param ctx A pointer to a user-defined structure.
432 * @param ctx_free A function that is invoked with ctx when the world is freed.
433 *
434 * @see ecs_set_binding_ctx()
435 * @see flecs::world::get_binding_ctx()
436 */
437 void set_binding_ctx(void* ctx, ecs_ctx_free_t ctx_free = nullptr) const {
438 ecs_set_binding_ctx(world_, ctx, ctx_free);
439 }
440
441 /** Get world binding context.
442 * This operation retrieves a previously set world binding context.
443 *
444 * @return The context set with set_binding_ctx(). If no context was set, the
445 * function returns nullptr.
446 *
447 * @see ecs_get_binding_ctx()
448 * @see flecs::world::set_binding_ctx()
449 */
450 void* get_binding_ctx() const {
452 }
453
454 /** Preallocate memory for a number of entities.
455 * This function preallocates memory for the entity index.
456 *
457 * @param entity_count Number of entities to preallocate memory for.
458 *
459 * @see ecs_dim()
460 */
461 void dim(int32_t entity_count) const {
462 ecs_dim(world_, entity_count);
463 }
464
465 /** Set current scope.
466 *
467 * @param scope The scope to set.
468 * @return The previous scope.
469 *
470 * @see ecs_set_scope()
471 * @see flecs::world::get_scope()
472 */
474
475 /** Get current scope.
476 *
477 * @return The current scope.
478 *
479 * @see ecs_get_scope()
480 * @see flecs::world::set_scope()
481 */
482 flecs::entity get_scope() const;
483
484 /** Same as set_scope(), but with type.
485 *
486 * @see ecs_set_scope()
487 * @see flecs::world::get_scope()
488 */
489 template <typename T>
490 flecs::entity set_scope() const;
491
492 /** Set search path.
493 *
494 * @see ecs_set_lookup_path()
495 * @see flecs::world::lookup()
496 */
498 return ecs_set_lookup_path(world_, search_path);
499 }
500
501 /** Lookup entity by name.
502 *
503 * @param name Entity name.
504 * @param sep The scope separator.
505 * @param root_sep The root scope separator.
506 * @param recursive When false, only the current scope is searched.
507 * @return The entity if found, or 0 if not found.
508 */
509 flecs::entity lookup(const char *name, const char *sep = "::", const char *root_sep = "::", bool recursive = true) const;
510
511 template <typename... T, typename A = _::value_type_t<T...>, if_not_t<is_callable<A>::value> = 0>
512 void set(A&& value) const {
513 auto id = _::value_id<T...>(world_, value);
514 flecs::set(world_, id.owner(world_), FLECS_FWD(value), id.id);
515 }
516
517 template <typename... T, typename A = _::value_type_t<T...>, if_not_t<is_callable<A>::value> = 0>
518 void set(const A& value) const {
519 auto id = _::value_id<T...>(world_, value);
520 flecs::set(world_, id.owner(world_), value, id.id);
521 }
522
523 template <typename First, typename Second>
524 void set(Second second, First&& value) const {
525 auto id = _::make_id<First>(world_, second);
526 flecs::set(world_, id.owner(world_), value, id.id);
527 }
528
529 template <typename First, typename Second>
530 void set(Second second, const First& value) const {
531 auto id = _::make_id<First>(world_, second);
532 flecs::set(world_, id.owner(world_), value, id.id);
533 }
534
535 /** Set singleton component inside a callback.
536 */
537 template <typename Func, if_t< is_callable<Func>::value > = 0 >
538 void set(const Func& func) const;
539
540 template <typename T, typename ... Args>
541 void emplace(Args&&... args) const {
542 flecs::id_t component_id = _::type<T>::id(world_);
543 flecs::emplace<T>(world_, component_id, component_id, FLECS_FWD(args)...);
544 }
545
546 #ifndef ensure
547 template <typename... T, typename... Args>
548 decltype(auto) ensure(Args... args) const {
549 auto id = _::make_id<T...>(world_, args...);
550 return _::get_component<true, true, true>(world_, id.owner(world_), id);
551 }
552 #endif
553
554 template <typename T>
555 void modified() const {
556 auto id = _::make_id<T>(world_);
557 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
558 "operation invalid for empty type");
559 ecs_modified_id(world_, id.owner(world_), id.id);
560 }
561
562 /** Get ref singleton component.
563 */
564 template <typename T>
565 ref<T> get_ref() const;
566
567
568 template <typename Func, if_t<is_callable<Func>::value> = 0>
569 void get(const Func& func) const;
570
571 template <typename... T, typename... Args>
572 decltype(auto) try_get(Args... args) const {
573 auto id = _::make_id<T...>(world_, args...);
574 return _::get_component<false, false>(world_, id.owner(world_), id);
575 }
576
577 template <typename... T, typename... Args>
578 decltype(auto) get(Args... args) const {
579 auto id = _::make_id<T...>(world_, args...);
580 return _::get_component<false, true>(world_, id.owner(world_), id);
581 }
582
583 template <typename... T, typename... Args>
584 decltype(auto) try_get_mut(Args... args) const {
585 auto id = _::make_id<T...>(world_, args...);
586 return _::get_component<true, false>(world_, id.owner(world_), id);
587 }
588
589 template <typename... T, typename... Args>
590 decltype(auto) get_mut(Args... args) const {
591 auto id = _::make_id<T...>(world_, args...);
592 return _::get_component<true, true>(world_, id.owner(world_), id);
593 }
594
595 template <typename... T, typename... Args>
596 bool has(Args... args) const {
597 auto id = _::make_id<T...>(world_, args...);
598 return _::has_component(world_, id.owner(world_), id);
599 }
600
601 template <typename... T, typename... Args>
602 void add(Args... args) const {
603 auto id = _::make_id<T...>(world_, args...);
604 _::add_component(world_, id.owner(world_), id);
605 }
606
607 template <typename... T, typename... Args>
608 void remove(Args... args) const {
609 auto id = _::make_id<T...>(world_, args...);
610 ecs_remove_id(world_, id.owner(world_), id.id);
611 }
612
613 /** Iterate entities in root of world.
614 * Accepts a callback with the following signature:
615 *
616 * @code
617 * void(*)(flecs::entity e);
618 * @endcode
619 */
620 template <typename Func>
621 void children(Func&& f) const;
622
623 /** Get singleton entity for type.
624 */
625 template <typename T>
626 flecs::entity singleton() const;
627
628 /** Get target for a given pair from a singleton entity.
629 * This operation returns the target for a given pair. The optional
630 * index can be used to iterate through targets, in case the entity has
631 * multiple instances for the same relationship.
632 *
633 * @tparam First The first element of the pair.
634 * @param index The index (0 for the first instance of the relationship).
635 * @return The target entity.
636 */
637 template<typename First>
638 flecs::entity target(int32_t index = 0) const;
639
640 /** Get target for a given pair from a singleton entity.
641 * This operation returns the target for a given pair. The optional
642 * index can be used to iterate through targets, in case the entity has
643 * multiple instances for the same relationship.
644 *
645 * @tparam T The singleton type.
646 * @param first The first element of the pair for which to retrieve the target.
647 * @param index The index (0 for the first instance of the relationship).
648 * @return The target entity.
649 */
650 template<typename T>
651 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
652
653 /** Get target for a given pair from a singleton entity.
654 * This operation returns the target for a given pair. The optional
655 * index can be used to iterate through targets, in case the entity has
656 * multiple instances for the same relationship.
657 *
658 * @param first The first element of the pair for which to retrieve the target.
659 * @param index The index (0 for the first instance of the relationship).
660 * @return The target entity.
661 */
662 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
663
664 /** Create an alias for a component.
665 *
666 * @tparam T The type to create an alias for.
667 * @param alias Alias for the component.
668 * @return Entity representing the component.
669 */
670 template <typename T>
671 flecs::entity use(const char *alias = nullptr) const;
672
673 /** Create an alias for an entity.
674 *
675 * @param name Name of the entity.
676 * @param alias Alias for the entity.
677 */
678 flecs::entity use(const char *name, const char *alias = nullptr) const;
679
680 /** Create an alias for an entity.
681 *
682 * @param entity Entity for which to create the alias.
683 * @param alias Alias for the entity.
684 */
685 void use(flecs::entity entity, const char *alias = nullptr) const;
686
687 template <typename... T, typename... Args>
688 int count(Args... args) const {
689 return ecs_count_id(world_, _::make_id<T...>(world_, args...).id);
690 }
691
692 /** All entities created in the function are created in the scope. All operations
693 * called in the function (such as lookup()) are relative to the scope.
694 */
695 template <typename Func>
696 void scope(id_t parent, const Func& func) const {
697 ecs_entity_t prev = ecs_set_scope(world_, parent);
698 func();
699 ecs_set_scope(world_, prev);
700 }
701
702 /** Same as scope(parent, func), but with T as parent.
703 */
704 template <typename T, typename Func>
705 void scope(const Func& func) const {
707 scope(parent, func);
708 }
709
710 template <typename... T, typename... Args>
711 void delete_with(Args... args) const {
712 ecs_delete_with(world_, _::make_id<T...>(world_, args...).id);
713 }
714
715 template <typename... T, typename... Args>
716 void remove_all(Args... args) const {
717 ecs_remove_all(world_, _::make_id<T...>(world_, args...).id);
718 }
719
720 /** Check if entity ID exists in the world.
721 *
722 * @see ecs_exists()
723 * @see flecs::world::is_alive()
724 * @see flecs::world::is_valid()
725 */
726 bool exists(flecs::entity_t e) const {
727 return ecs_exists(world_, e);
728 }
729
730 /** Check if entity is alive.
731 *
732 * @see ecs_is_alive()
733 * @see flecs::world::exists()
734 * @see flecs::world::is_valid()
735 */
736 bool is_alive(flecs::entity_t e) const {
737 return ecs_is_alive(world_, e);
738 }
739
740 /** Check if entity ID is valid.
741 * Invalid entities cannot be used with API functions.
742 *
743 * @see ecs_is_valid()
744 * @see flecs::world::exists()
745 * @see flecs::world::is_alive()
746 */
747 bool is_valid(flecs::entity_t e) const {
748 return ecs_is_valid(world_, e);
749 }
750
751 /** Get alive entity for ID.
752 * Return the entity with the current generation.
753 *
754 * @see ecs_get_alive()
755 */
757
758 /**
759 * @see ecs_make_alive()
760 */
762
763 /** Set the version of an entity to the provided value.
764 *
765 * @see ecs_set_version()
766 */
769 }
770
771 /** Get the version of the provided entity.
772 *
773 * @see ecs_get_version()
774 */
775 uint32_t get_version(flecs::entity_t e) const {
776 return ecs_get_version(e);
777 }
778
779 /** Get the world info.
780 *
781 * @see ecs_get_world_info()
782 */
785 }
786
787 /** Get delta_time. */
789 return get_info()->delta_time;
790 }
791
792 /** Free unused memory.
793 *
794 * @see ecs_shrink()
795 */
796 void shrink() const {
798 }
799
800 /** Begin exclusive access.
801 *
802 * @param thread_name Optional thread name for improved debug messages.
803 * @see ecs_exclusive_access_begin()
804 */
805 void exclusive_access_begin(const char *thread_name = nullptr) {
807 }
808
809 /** End exclusive access.
810 *
811 * @param lock_world Lock world for all threads, allow readonly operations.
812 * @see ecs_exclusive_access_end()
813 */
814 void exclusive_access_end(bool lock_world = false) {
815 ecs_exclusive_access_end(world_, lock_world);
816 }
817
818 /** Return the component ID if it has been registered.
819 * This operation is similar to world::id(), but will never automatically
820 * register the component.
821 *
822 * @tparam T The type for which to obtain the ID.
823 */
824 template <typename T>
827 return _::type<T>::id(world_);
828 }
829 else {
830 return 0;
831 }
832 }
833
834 template <typename... T, typename... Args>
835 const flecs::type_info_t* type_info(Args... args) {
836 return ecs_get_type_info(world_, _::make_id<T...>(world_, args...).id);
837 }
838
839# include "mixins/id/mixin.inl"
841# include "mixins/entity/mixin.inl"
842# include "mixins/event/mixin.inl"
843# include "mixins/term/mixin.inl"
845# include "mixins/query/mixin.inl"
846# include "mixins/enum/mixin.inl"
847
848# ifdef FLECS_PREFAB
849# include "mixins/prefab/mixin.inl"
850# endif
851# ifdef FLECS_ENTITY_RANGES
853# endif
854# ifdef FLECS_FRAME
855# include "mixins/frame/mixin.inl"
856# endif
857# ifdef FLECS_MODULE
858# include "mixins/module/mixin.inl"
859# endif
860# ifdef FLECS_PIPELINE
862# endif
863# ifdef FLECS_SYSTEM
864# include "mixins/system/mixin.inl"
865# endif
866# ifdef FLECS_TIMER
867# include "mixins/timer/mixin.inl"
868# endif
869# ifdef FLECS_SCRIPT
870# include "mixins/script/mixin.inl"
871# endif
872# ifdef FLECS_META
873# include "mixins/meta/world.inl"
874# endif
875# ifdef FLECS_JSON
876# include "mixins/json/world.inl"
877# endif
878# ifdef FLECS_APP
879# include "mixins/app/mixin.inl"
880# endif
881# ifdef FLECS_METRICS
882# include "mixins/metrics/mixin.inl"
883# endif
884# ifdef FLECS_ALERTS
885# include "mixins/alerts/mixin.inl"
886# endif
887
888public:
889 /** Initialize built-in components. */
891
892 world_t *world_; /**< Pointer to the underlying C world. */
893};
894
895/** @} */
896
897} // namespace flecs
Alert world mixin.
App world addon mixin.
Component mixin.
Entity world mixin.
Entity ranges world mixin.
Enum world mixin.
Event world mixin.
Frame world mixin.
void ecs_remove_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Remove a component from an entity.
void ecs_remove_all(ecs_world_t *world, ecs_id_t component)
Remove all instances of the specified component.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_INVALID_OPERATION
Invalid operation error code.
Definition log.h:669
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:671
ecs_world_t * ecs_stage_new(ecs_world_t *world)
Create an unmanaged stage.
bool ecs_readonly_begin(ecs_world_t *world, bool multi_threaded)
Begin readonly mode.
void ecs_stage_free(ecs_world_t *stage)
Free an unmanaged stage.
void ecs_merge(ecs_world_t *stage)
Merge a stage.
int32_t ecs_stage_get_id(const ecs_world_t *world)
Get the stage ID.
bool ecs_stage_is_readonly(const ecs_world_t *world)
Test whether the current world is readonly.
int32_t ecs_get_stage_count(const ecs_world_t *world)
Get the number of configured stages.
ecs_world_t * ecs_get_stage(const ecs_world_t *world, int32_t stage_id)
Get stage-specific world pointer.
void ecs_set_stage_count(ecs_world_t *world, int32_t stages)
Configure the world to have N stages.
void ecs_readonly_end(ecs_world_t *world)
End readonly mode.
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.
struct ecs_stage_t ecs_stage_t
A stage enables modification while iterating and from multiple threads.
Definition flecs.h:442
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:395
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:439
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:195
ecs_world_info_t world_info_t
World info type.
Definition c_types.hpp:19
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
ecs_world_t world_t
World type.
Definition c_types.hpp:18
ecs_type_info_t type_info_t
Type info type.
Definition c_types.hpp:33
void ecs_delete_with(ecs_world_t *world, ecs_id_t component)
Delete all entities with the specified component.
int32_t ecs_count_id(const ecs_world_t *world, ecs_id_t entity)
Count entities that have the specified ID.
void(*) ecs_ctx_free_t(void *ctx)
Function to clean up context data.
Definition flecs.h:660
void(*) ecs_fini_action_t(ecs_world_t *world, void *ctx)
Action callback on world exit.
Definition flecs.h:655
void * ecs_emplace_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component, size_t size, bool *is_new)
Emplace a component.
void ecs_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Signal that a component has been modified.
ecs_id_t ecs_strip_generation(ecs_entity_t e)
Remove the generation from an entity ID.
bool ecs_is_valid(const ecs_world_t *world, ecs_entity_t e)
Test whether an entity is valid.
uint32_t ecs_get_version(ecs_entity_t entity)
Get the generation of an entity.
bool ecs_exists(const ecs_world_t *world, ecs_entity_t entity)
Test whether an entity exists.
bool ecs_is_alive(const ecs_world_t *world, ecs_entity_t e)
Test whether an entity is alive.
void ecs_set_version(ecs_world_t *world, ecs_entity_t entity)
Override the generation of an entity.
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
ecs_entity_t * ecs_set_lookup_path(ecs_world_t *world, const ecs_entity_t *lookup_path)
Set the search path for lookup operations.
ecs_entity_t ecs_set_scope(ecs_world_t *world, ecs_entity_t scope)
Set the current scope.
void ecs_atfini(ecs_world_t *world, ecs_fini_action_t action, void *ctx)
Register an action to be executed when the world is destroyed.
int ecs_fini(ecs_world_t *world)
Delete a world.
ecs_world_t * ecs_init(void)
Create a new world.
ecs_world_t * ecs_init_w_args(int argc, char *argv[])
Create a new world with arguments.
void * ecs_get_binding_ctx(const ecs_world_t *world)
Get the world binding context.
void ecs_shrink(ecs_world_t *world)
Free unused memory.
void ecs_dim(ecs_world_t *world, int32_t entity_count)
Dimension the world for a specified number of entities.
const ecs_world_info_t * ecs_get_world_info(const ecs_world_t *world)
Get the world info.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get the world from a poly.
void ecs_set_ctx(ecs_world_t *world, void *ctx, ecs_ctx_free_t ctx_free)
Set a world context.
void ecs_exclusive_access_begin(ecs_world_t *world, const char *thread_name)
Begin exclusive thread access.
void ecs_set_binding_ctx(ecs_world_t *world, void *ctx, ecs_ctx_free_t ctx_free)
Set a world binding context.
#define flecs_poly_is(object, type)
Test if a pointer is of the specified type.
Definition flecs.h:2511
void ecs_exclusive_access_end(ecs_world_t *world, bool lock_world)
End exclusive thread access.
void * ecs_get_ctx(const ecs_world_t *world)
Get the world context.
ID world mixin.
JSON world mixin.
Meta world mixin.
Metrics world mixin.
Module world mixin.
Int to enum.
Definition component.hpp:18
Observer world mixin.
Pipeline world mixin.
Query world mixin.
Script world mixin.
float delta_time
Time passed to or computed by ecs_progress().
Definition flecs.h:1528
Entity.
Definition entity.hpp:30
The world.
Definition world.hpp:129
bool is_stage() const
Test if this is a stage.
Definition world.hpp:316
void shrink() const
Free unused memory.
Definition world.hpp:796
uint32_t get_version(flecs::entity_t e) const
Get the version of the provided entity.
Definition world.hpp:775
void merge() const
Merge world or stage.
Definition world.hpp:335
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:892
const flecs::world_info_t * get_info() const
Get the world info.
Definition world.hpp:783
flecs::entity get_scope() const
Get current scope.
Definition world.hpp:91
flecs::entity lookup(const char *name, const char *sep="::", const char *root_sep="::", bool recursive=true) const
Lookup entity by name.
Definition world.hpp:102
ecs_ftime_t delta_time() const
Get delta_time.
Definition world.hpp:788
flecs::entity get_alive(flecs::entity_t e) const
Get alive entity for ID.
Definition world.hpp:170
void readonly_end() const
End readonly mode.
Definition world.hpp:265
flecs::entity make_alive(flecs::entity_t e) const
Ensure an entity ID is alive.
Definition world.hpp:176
void exclusive_access_begin(const char *thread_name=nullptr)
Begin exclusive access.
Definition world.hpp:805
flecs::entity target(int32_t index=0) const
Get target for a given pair from a singleton entity.
Definition world.hpp:128
bool is_alive(flecs::entity_t e) const
Check if entity is alive.
Definition world.hpp:736
world_t * c_ptr() const
Obtain a pointer to the C world object.
Definition world.hpp:235
~world()
Destructor.
Definition world.hpp:205
void reset()
Delete and recreate the world.
Definition world.hpp:224
flecs::entity_t * set_lookup_path(const flecs::entity_t *search_path) const
Set search path.
Definition world.hpp:497
void make_owner()
Make the current world object the owner of the world.
Definition world.hpp:219
bool is_valid(flecs::entity_t e) const
Check if entity ID is valid.
Definition world.hpp:747
flecs::world async_stage() const
Create an asynchronous stage.
Definition world.hpp:367
void release()
Release the underlying world object.
Definition world.hpp:187
void * get_binding_ctx() const
Get world binding context.
Definition world.hpp:450
int32_t get_stage_id() const
Get current stage ID.
Definition world.hpp:306
void init_builtin_components()
Initialize built-in components.
Definition world.hpp:12
world(const world &obj)
Copy constructor.
Definition world.hpp:157
void scope(const Func &func) const
Same as scope(parent, func), but with T as parent.
Definition world.hpp:705
flecs::entity set_scope() const
Same as set_scope(), but with type.
Definition world.hpp:97
void dim(int32_t entity_count) const
Preallocate memory for a number of entities.
Definition world.hpp:461
void set_version(flecs::entity_t e) const
Set the version of an entity to the provided value.
Definition world.hpp:767
void set_stage_count(int32_t stages) const
Configure world to have N stages.
Definition world.hpp:284
void * get_ctx() const
Get world context.
Definition world.hpp:422
world(world_t *w)
Create a world from a C world.
Definition world.hpp:148
void children(Func &&f) const
Iterate entities in root of world.
Definition world.hpp:116
flecs::world get_world() const
Get actual world.
Definition world.hpp:379
void scope(id_t parent, const Func &func) const
All entities created in the function are created in the scope.
Definition world.hpp:696
world(world &&obj) noexcept
Move constructor.
Definition world.hpp:171
world & operator=(const world &obj) noexcept
Copy assignment operator.
Definition world.hpp:163
flecs::id_t id_if_registered()
Return the component ID if it has been registered.
Definition world.hpp:825
flecs::entity use(const char *alias=nullptr) const
Create an alias for a component.
Definition world.hpp:54
bool is_readonly() const
Test whether the current world object is readonly.
Definition world.hpp:395
bool readonly_begin(bool multi_threaded=false) const
Begin readonly mode.
Definition world.hpp:255
void get(const Func &func) const
Get a singleton component using a callback.
Definition world.hpp:155
flecs::world get_stage(int32_t stage_id) const
Get stage-specific world pointer.
Definition world.hpp:353
bool exists(flecs::entity_t e) const
Check if entity ID exists in the world.
Definition world.hpp:726
world()
Create a world.
Definition world.hpp:132
void set_binding_ctx(void *ctx, ecs_ctx_free_t ctx_free=nullptr) const
Set world binding context.
Definition world.hpp:437
void atfini(ecs_fini_action_t action, void *ctx=nullptr) const
Register action to be executed when world is destroyed.
Definition world.hpp:241
int32_t get_stage_count() const
Get the number of configured stages.
Definition world.hpp:296
flecs::entity singleton() const
Get singleton entity for type.
Definition world.hpp:122
void exclusive_access_end(bool lock_world=false)
End exclusive access.
Definition world.hpp:814
void set_ctx(void *ctx, ecs_ctx_free_t ctx_free=nullptr) const
Set world context.
Definition world.hpp:409
world(int argc, char *argv[])
Create a world with command-line arguments.
Definition world.hpp:141
world & operator=(world &&obj) noexcept
Move assignment operator.
Definition world.hpp:177
ref< T > get_ref() const
Get ref singleton component.
Definition world.hpp:109
flecs::entity entity(Args &&... args) const
Create an entity.
System module world mixin.
Term world mixin.
Timer module mixin.
enable_if_t< false==V, int > if_not_t
Convenience enable_if alias for negated conditions.
Definition utils.hpp:172
enable_if_t< V, int > if_t
Convenience enable_if alias using int as default type.
Definition utils.hpp:168
flecs::id_t strip_generation(flecs::entity_t e)
Return the ID without generation.
Definition world.hpp:104
uint32_t get_generation(flecs::entity_t e)
Return the entity generation.
Definition world.hpp:113
void emplace(world_t *world, flecs::entity_t entity, flecs::id_t id, Args &&... args)
Emplace a component value, constructing it in place.
Definition world.hpp:87