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