Flecs: include/flecs/addons/cpp/world.hpp Source File
Description: A fast entity component system (ECS) for C & C++

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

Loading...
Searching...
No Matches
world.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8namespace flecs
9{
10
12
16
18 * @param value The value to set (rvalue reference).
19 * @param id The component ID.
20 */
21 template <typename T>
22 inline void set(world_t *world, flecs::entity_t entity, T&& value, flecs::id_t id) {
23 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
24 "operation invalid for empty type");
25
26 ecs_cpp_get_mut_t res = ecs_cpp_set(world, entity, id, &value, sizeof(T));
27
28 T& dst = *static_cast<remove_reference_t<T>*>(res.ptr);
29 if constexpr (std::is_copy_assignable_v<T>) {
30 dst = FLECS_FWD(value);
31 } else {
32 dst = FLECS_MOV(value);
33 }
34
35 if (res.stage) {
36 flecs_defer_end(res.world, res.stage);
37 }
38
39 if (res.call_modified) {
40 ecs_modified_id(world, entity, id);
41 }
42}
43
45
52 template <typename T>
53 inline void set(world_t *world, flecs::entity_t entity, const T& value, flecs::id_t id) {
54 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
55 "operation invalid for empty type");
56
57 ecs_cpp_get_mut_t res = ecs_cpp_set(world, entity, id, &value, sizeof(T));
58
59 T& dst = *static_cast<remove_reference_t<T>*>(res.ptr);
60 dst = value;
61
62 if (res.stage) {
63 flecs_defer_end(res.world, res.stage);
64 }
65
66 if (res.call_modified) {
67 ecs_modified_id(world, entity, id);
68 }
69}
70
71/** Set a component value using move semantics, with automatic ID lookup.
72 *
73 * @tparam T The component type.
74 * @tparam A The actual value type.
75 * @param world The world.
76 * @param entity The entity.
77 * @param value The value to set (rvalue reference).
78 */
79template <typename T, typename A>
80 inline void set(world_t *world, entity_t entity, A&& value) {
81 id_t id = _::type<T>::id(world);
82 flecs::set(world, entity, FLECS_FWD(value), id);
83}
84
93template <typename T, typename A>
94 inline void set(world_t *world, entity_t entity, const A& value) {
95 id_t id = _::type<T>::id(world);
96 flecs::set(world, entity, value, id);
97}
98
108template <typename T>
109 inline void assign(world_t *world, flecs::entity_t entity, T&& value, flecs::id_t id) {
110 ecs_assert(_::type<remove_reference_t<T>>::size() != 0,
111 ECS_INVALID_PARAMETER, "operation invalid for empty type");
112
113 ecs_cpp_get_mut_t res = ecs_cpp_assign(
114 world, entity, id, &value, sizeof(T));
115
116 T& dst = *static_cast<remove_reference_t<T>*>(res.ptr);
117 if constexpr (std::is_copy_assignable_v<T>) {
118 dst = FLECS_FWD(value);
119 } else {
120 dst = FLECS_MOV(value);
121 }
122
123 if (res.stage) {
124 flecs_defer_end(res.world, res.stage);
125 }
126
127 if (res.call_modified) {
128 ecs_modified_id(world, entity, id);
129 }
130}
131
141template <typename T>
142 inline void assign(world_t *world, flecs::entity_t entity, const T& value, flecs::id_t id) {
143 ecs_assert(_::type<remove_reference_t<T>>::size() != 0,
144 ECS_INVALID_PARAMETER, "operation invalid for empty type");
145
146 ecs_cpp_get_mut_t res = ecs_cpp_assign(
147 world, entity, id, &value, sizeof(T));
148
149 T& dst = *static_cast<remove_reference_t<T>*>(res.ptr);
150 dst = value;
151
152 if (res.stage) {
153 flecs_defer_end(res.world, res.stage);
154 }
155
156 if (res.call_modified) {
157 ecs_modified_id(world, entity, id);
158 }
159}
160
169template <typename T, typename A>
170 inline void assign(world_t *world, entity_t entity, A&& value) {
171 id_t id = _::type<T>::id(world);
172 flecs::assign(world, entity, FLECS_FWD(value), id);
173}
174
183template <typename T, typename A>
184 inline void assign(world_t *world, entity_t entity, const A& value) {
185 id_t id = _::type<T>::id(world);
186 flecs::assign(world, entity, value, id);
187}
188
189
199template <typename T, typename ... Args, if_t<
200 std::is_constructible<actual_type_t<T>, Args...>::value ||
201 std::is_default_constructible<actual_type_t<T>>::value > = 0>
202 inline void emplace(world_t *world, flecs::entity_t entity, flecs::id_t id, Args&&... args) {
203 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
204 "operation invalid for empty type");
205 T& dst = *static_cast<T*>(ecs_emplace_id(world, entity, id, sizeof(T), nullptr));
206
207 FLECS_PLACEMENT_NEW(&dst, T{FLECS_FWD(args)...});
208
209 ecs_modified_id(world, entity, id);
210}
211
219 inline flecs::id_t strip_generation(flecs::entity_t e) {
220 return ecs_strip_generation(e);
221}
222
228 inline uint32_t get_generation(flecs::entity_t e) {
229 return ECS_GENERATION(e);
230}
231
232struct scoped_world;
233
241
246 struct world {
249 explicit world()
250 : world_( ecs_init() ) {
251 init_builtin_components();
252 }
253
258 explicit world(int argc, char *argv[])
259 : world_( ecs_init_w_args(argc, argv) ) {
260 init_builtin_components();
261 }
262
265 explicit world(world_t *w)
266 : world_( w ) {
267 if (w) {
268 flecs_poly_claim(w);
269 }
270 }
271
274 world(const world& obj) {
275 this->world_ = obj.world_;
276 flecs_poly_claim(this->world_);
277 }
278
280 world& operator=(const world& obj) noexcept {
281 release();
282 this->world_ = obj.world_;
283 flecs_poly_claim(this->world_);
284 return *this;
285 }
286
288 world(world&& obj) noexcept {
289 world_ = obj.world_;
290 obj.world_ = nullptr;
291 }
292
294 world& operator=(world&& obj) noexcept {
295 release();
296 world_ = obj.world_;
297 obj.world_ = nullptr;
298 return *this;
299 }
300
304 void release() {
305 if (world_) {
306 if (!flecs_poly_release(world_)) {
307 if (ecs_stage_get_id(world_) == -1) {
308 ecs_stage_free(world_);
309 } else {
310 // Before we call ecs_fini(), we increment the reference count back to 1.
311 // Otherwise, copies of this object created during ecs_fini() (e.g., a component on_remove hook)
312 // would again call this destructor and ecs_fini().
313 flecs_poly_claim(world_);
314 ecs_fini(world_);
315 }
316 }
317 world_ = nullptr;
318 }
319 }
320
322 ~world() {
323 release();
324 }
325
327 operator world_t*() const { return world_; }
328
336 void make_owner() {
337 flecs_poly_release(world_);
338 }
339
341 void reset() {
342 /* Make sure there's only one reference to the world. */
343 ecs_assert(flecs_poly_refcount(world_) == 1, ECS_INVALID_OPERATION,
344 "reset would invalidate other handles");
345 ecs_fini(world_);
346 world_ = ecs_init();
347 init_builtin_components();
348 }
349
352 world_t* c_ptr() const {
353 return world_;
354 }
355
359 void quit() const {
360 ecs_quit(world_);
361 }
362
365 void atfini(ecs_fini_action_t action, void *ctx = nullptr) const {
366 ecs_atfini(world_, action, ctx);
367 }
368
371 bool should_quit() const {
372 return ecs_should_quit(world_);
373 }
374
396 ecs_ftime_t frame_begin(float delta_time = 0) const {
397 return ecs_frame_begin(world_, delta_time);
398 }
399
409 void frame_end() const {
410 ecs_frame_end(world_);
411 }
412
423 bool readonly_begin(bool multi_threaded = false) const {
424 return ecs_readonly_begin(world_, multi_threaded);
425 }
426
433 void readonly_end() const {
434 ecs_readonly_end(world_);
435 }
436
453 bool defer_begin() const {
454 return ecs_defer_begin(world_);
455 }
456
472 bool defer_end() const {
473 return ecs_defer_end(world_);
474 }
475
488 bool is_deferred() const {
489 return ecs_is_deferred(world_);
490 }
491
504 bool is_defer_suspended() const {
505 return ecs_is_defer_suspended(world_);
506 }
507
523 void set_stage_count(int32_t stages) const {
524 ecs_set_stage_count(world_, stages);
525 }
526
535 int32_t get_stage_count() const {
536 return ecs_get_stage_count(world_);
537 }
538
545 int32_t get_stage_id() const {
546 return ecs_stage_get_id(world_);
547 }
548
555 bool is_stage() const {
556 ecs_assert(
557 flecs_poly_is(world_, ecs_world_t) ||
558 flecs_poly_is(world_, ecs_stage_t),
559 ECS_INVALID_PARAMETER,
560 "flecs::world instance contains invalid reference to world or stage");
561 return flecs_poly_is(world_, ecs_stage_t);
562 }
563
574 void merge() const {
575 ecs_merge(world_);
576 }
577
592 flecs::world get_stage(int32_t stage_id) const {
593 return flecs::world(ecs_get_stage(world_, stage_id));
594 }
595
611 flecs::world async_stage() const {
612 ecs_world_t *as = ecs_stage_new(world_);
613 flecs_poly_release(as); // World object will claim.
614 return flecs::world(as);
615 }
616
623 flecs::world get_world() const {
624 /* Safe cast, mutability is checked. */
625 return flecs::world(
626 world_ ? const_cast<flecs::world_t*>(ecs_get_world(world_)) : nullptr);
627 }
628
639 bool is_readonly() const {
640 return ecs_stage_is_readonly(world_);
641 }
642
653 void set_ctx(void* ctx, ecs_ctx_free_t ctx_free = nullptr) const {
654 ecs_set_ctx(world_, ctx, ctx_free);
655 }
656
666 void* get_ctx() const {
667 return ecs_get_ctx(world_);
668 }
669
681 void set_binding_ctx(void* ctx, ecs_ctx_free_t ctx_free = nullptr) const {
682 ecs_set_binding_ctx(world_, ctx, ctx_free);
683 }
684
694 void* get_binding_ctx() const {
695 return ecs_get_binding_ctx(world_);
696 }
697
705 void dim(int32_t entity_count) const {
706 ecs_dim(world_, entity_count);
707 }
708
712 const ecs_entity_range_t* range_new(uint32_t min, uint32_t max) const {
713 return ecs_entity_range_new(world_, min, max);
714 }
715
719 void range_set(const ecs_entity_range_t *range) const {
720 ecs_entity_range_set(world_, range);
721 }
722
726 const ecs_entity_range_t* range_get() const {
727 return ecs_entity_range_get(world_);
728 }
729
738 flecs::entity set_scope(const flecs::entity_t scope) const;
739
747 flecs::entity get_scope() const;
748
754 template <typename T>
755 flecs::entity set_scope() const;
756
762 flecs::entity_t* set_lookup_path(const flecs::entity_t *search_path) const {
763 return ecs_set_lookup_path(world_, search_path);
764 }
765
774 flecs::entity lookup(const char *name, const char *sep = "::", const char *root_sep = "::", bool recursive = true) const;
775
778 template <typename T, if_t< !is_callable<T>::value > = 0>
779 void set(const T& value) const {
780 flecs::set<T>(world_, _::type<T>::id(world_), value);
781 }
782
785 template <typename T, if_t< !is_callable<T>::value > = 0>
786 void set(T&& value) const {
787 flecs::set<T>(world_, _::type<T>::id(world_),
788 FLECS_FWD(value));
789 }
790
793 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
794 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
795 void set(const A& value) const {
796 flecs::set<P>(world_, _::type<First>::id(world_), value);
797 }
798
801 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
802 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
803 void set(A&& value) const {
804 flecs::set<P>(world_, _::type<First>::id(world_), FLECS_FWD(value));
805 }
806
809 template <typename First, typename Second>
810 void set(Second second, const First& value) const;
811
814 template <typename First, typename Second>
815 void set(Second second, First&& value) const;
816
819 template <typename Func, if_t< is_callable<Func>::value > = 0 >
820 void set(const Func& func) const;
821
822 template <typename T, typename ... Args>
823 void emplace(Args&&... args) const {
824 flecs::id_t component_id = _::type<T>::id(world_);
825 flecs::emplace<T>(world_, component_id, component_id, FLECS_FWD(args)...);
826 }
827
830 #ifndef ensure
831 template <typename T>
832 T& ensure() const;
833 #endif
834
837 template <typename T>
838 void modified() const;
839
842 template <typename T>
843 ref<T> get_ref() const;
844
845
846 /* try_get */
847
850 const void* try_get(flecs::id_t id) const;
851
854 const void* try_get(flecs::entity_t r, flecs::entity_t t) const;
855
858 template <typename T>
859 const T* try_get() const;
860
863 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
864 typename A = actual_type_t<P>>
865 const A* try_get() const;
866
869 template <typename First, typename Second>
870 const First* try_get(Second second) const;
871
872
873 /* get */
874
877 const void* get(flecs::id_t id) const;
878
881 const void* get(flecs::entity_t r, flecs::entity_t t) const;
882
883 template <typename T>
884 const T& get() const;
885
888 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
889 typename A = actual_type_t<P>>
890 const A& get() const;
891
894 template <typename First, typename Second>
895 const First& get(Second second) const;
896
899 template <typename Func, if_t< is_callable<Func>::value > = 0 >
900 void get(const Func& func) const;
901
902
903 /* try_get_mut */
904
907 void* try_get_mut(flecs::id_t id) const;
908
911 void* try_get_mut(flecs::entity_t r, flecs::entity_t t) const;
912
913 template <typename T>
914 T* try_get_mut() const;
915
918 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
919 typename A = actual_type_t<P>>
920 A* try_get_mut() const;
921
924 template <typename First, typename Second>
925 First* try_get_mut(Second second) const;
926
927
928 /* get_mut */
929
932 void* get_mut(flecs::id_t id) const;
933
936 void* get_mut(flecs::entity_t r, flecs::entity_t t) const;
937
938 template <typename T>
939 T& get_mut() const;
940
943 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
944 typename A = actual_type_t<P>>
945 A& get_mut() const;
946
949 template <typename First, typename Second>
950 First& get_mut(Second second) const;
951
952
958 template <typename T>
959 bool has() const;
960
967 template <typename First, typename Second>
968 bool has() const;
969
976 template <typename First>
977 bool has(flecs::id_t second) const;
978
985 bool has(flecs::id_t first, flecs::id_t second) const;
986
993 template <typename E, if_t< is_enum<E>::value > = 0>
994 bool has(E value) const;
995
998 template <typename T>
999 void add() const;
1000
1006 template <typename First, typename Second>
1007 void add() const;
1008
1014 template <typename First>
1015 void add(flecs::entity_t second) const;
1016
1022 void add(flecs::entity_t first, flecs::entity_t second) const;
1023
1029 template <typename E, if_t< is_enum<E>::value > = 0>
1030 void add(E value) const;
1031
1034 template <typename T>
1035 void remove() const;
1036
1042 template <typename First, typename Second>
1043 void remove() const;
1044
1050 template <typename First>
1051 void remove(flecs::entity_t second) const;
1052
1058 void remove(flecs::entity_t first, flecs::entity_t second) const;
1059
1067 template <typename Func>
1068 void children(Func&& f) const;
1069
1072 template <typename T>
1073 flecs::entity singleton() const;
1074
1084 template<typename First>
1085 flecs::entity target(int32_t index = 0) const;
1086
1097 template<typename T>
1098 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
1099
1109 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
1110
1117 template <typename T>
1118 flecs::entity use(const char *alias = nullptr) const;
1119
1125 flecs::entity use(const char *name, const char *alias = nullptr) const;
1126
1132 void use(flecs::entity entity, const char *alias = nullptr) const;
1133
1139 int count(flecs::id_t component_id) const {
1140 return ecs_count_id(world_, component_id);
1141 }
1142
1149 int count(flecs::entity_t first, flecs::entity_t second) const {
1150 return ecs_count_id(world_, ecs_pair(first, second));
1151 }
1152
1158 template <typename T>
1159 int count() const {
1160 return count(_::type<T>::id(world_));
1161 }
1162
1169 template <typename First>
1170 int count(flecs::entity_t second) const {
1171 return count(_::type<First>::id(world_), second);
1172 }
1173
1180 template <typename First, typename Second>
1181 int count() const {
1182 return count(
1183 _::type<First>::id(world_),
1184 _::type<Second>::id(world_));
1185 }
1186
1189 template <typename Func>
1190 void with(id_t with_id, const Func& func) const {
1191 ecs_id_t prev = ecs_set_with(world_, with_id);
1192 func();
1193 ecs_set_with(world_, prev);
1194 }
1195
1198 template <typename T, typename Func>
1199 void with(const Func& func) const {
1200 with(this->id<T>(), func);
1201 }
1202
1205 template <typename First, typename Second, typename Func>
1206 void with(const Func& func) const {
1207 with(ecs_pair(this->id<First>(), this->id<Second>()), func);
1208 }
1209
1212 template <typename First, typename Func>
1213 void with(id_t second, const Func& func) const {
1214 with(ecs_pair(this->id<First>(), second), func);
1215 }
1216
1219 template <typename Func>
1220 void with(id_t first, id_t second, const Func& func) const {
1221 with(ecs_pair(first, second), func);
1222 }
1223
1227 template <typename Func>
1228 void scope(id_t parent, const Func& func) const {
1229 ecs_entity_t prev = ecs_set_scope(world_, parent);
1230 func();
1231 ecs_set_scope(world_, prev);
1232 }
1233
1236 template <typename T, typename Func>
1237 void scope(const Func& func) const {
1238 flecs::id_t parent = _::type<T>::id(world_);
1239 scope(parent, func);
1240 }
1241
1245 flecs::scoped_world scope(id_t parent) const;
1246
1247 template <typename T>
1248 flecs::scoped_world scope() const;
1249
1250 flecs::scoped_world scope(const char* name) const;
1251
1253 void delete_with(id_t the_id) const {
1254 ecs_delete_with(world_, the_id);
1255 }
1256
1258 void delete_with(entity_t first, entity_t second) const {
1259 delete_with(ecs_pair(first, second));
1260 }
1261
1263 template <typename T>
1264 void delete_with() const {
1265 delete_with(_::type<T>::id(world_));
1266 }
1267
1269 template <typename First, typename Second>
1270 void delete_with() const {
1271 delete_with(_::type<First>::id(world_), _::type<Second>::id(world_));
1272 }
1273
1275 template <typename First>
1276 void delete_with(entity_t second) const {
1277 delete_with(_::type<First>::id(world_), second);
1278 }
1279
1281 void remove_all(id_t the_id) const {
1282 ecs_remove_all(world_, the_id);
1283 }
1284
1286 void remove_all(entity_t first, entity_t second) const {
1287 remove_all(ecs_pair(first, second));
1288 }
1289
1291 template <typename T>
1292 void remove_all() const {
1293 remove_all(_::type<T>::id(world_));
1294 }
1295
1297 template <typename First, typename Second>
1298 void remove_all() const {
1299 remove_all(_::type<First>::id(world_), _::type<Second>::id(world_));
1300 }
1301
1303 template <typename First>
1304 void remove_all(entity_t second) const {
1305 remove_all(_::type<First>::id(world_), second);
1306 }
1307
1316 template <typename Func>
1317 void defer(const Func& func) const {
1318 ecs_defer_begin(world_);
1319 func();
1320 ecs_defer_end(world_);
1321 }
1322
1332 void defer_suspend() const {
1333 ecs_defer_suspend(world_);
1334 }
1335
1345 void defer_resume() const {
1346 ecs_defer_resume(world_);
1347 }
1348
1355 bool exists(flecs::entity_t e) const {
1356 return ecs_exists(world_, e);
1357 }
1358
1365 bool is_alive(flecs::entity_t e) const {
1366 return ecs_is_alive(world_, e);
1367 }
1368
1376 bool is_valid(flecs::entity_t e) const {
1377 return ecs_is_valid(world_, e);
1378 }
1379
1385 flecs::entity get_alive(flecs::entity_t e) const;
1386
1390 flecs::entity make_alive(flecs::entity_t e) const;
1391
1396 void set_version(flecs::entity_t e) const {
1397 ecs_set_version(world_, e);
1398 }
1399
1404 uint32_t get_version(flecs::entity_t e) const {
1405 return ecs_get_version(e);
1406 }
1407
1409 void run_post_frame(ecs_fini_action_t action, void *ctx) const {
1410 ecs_run_post_frame(world_, action, ctx);
1411 }
1412
1417 const flecs::world_info_t* get_info() const {
1418 return ecs_get_world_info(world_);
1419 }
1420
1422 ecs_ftime_t delta_time() const {
1423 return get_info()->delta_time;
1424 }
1425
1430 void shrink() const {
1431 ecs_shrink(world_);
1432 }
1433
1439 void exclusive_access_begin(const char *thread_name = nullptr) {
1440 ecs_exclusive_access_begin(world_, thread_name);
1441 }
1442
1448 void exclusive_access_end(bool lock_world = false) {
1449 ecs_exclusive_access_end(world_, lock_world);
1450 }
1451
1458 template <typename T>
1459 flecs::id_t id_if_registered() {
1460 if (_::type<T>::registered(world_)) {
1461 return _::type<T>::id(world_);
1462 }
1463 else {
1464 return 0;
1465 }
1466 }
1467
1469 const flecs::type_info_t* type_info(flecs::id_t component) {
1470 return ecs_get_type_info(world_, component);
1471 }
1472
1474 const flecs::type_info_t* type_info(flecs::entity_t r, flecs::entity_t t) {
1475 return ecs_get_type_info(world_, ecs_pair(r, t));
1476 }
1477
1479 template <typename T>
1480 const flecs::type_info_t* type_info() {
1481 return ecs_get_type_info(world_, _::type<T>::id(world_));
1482 }
1483
1485 template <typename R>
1486 const flecs::type_info_t* type_info(flecs::entity_t t) {
1487 return type_info(_::type<R>::id(world_), t);
1488 }
1489
1491 template <typename R, typename T>
1492 const flecs::type_info_t* type_info() {
1493 return type_info<R>(_::type<T>::id(world_));
1494 }
1495
1496# include "mixins/id/mixin.inl"
1497# include "mixins/component/mixin.inl"
1498# include "mixins/entity/mixin.inl"
1499# include "mixins/event/mixin.inl"
1500# include "mixins/term/mixin.inl"
1501# include "mixins/observer/mixin.inl"
1502# include "mixins/query/mixin.inl"
1503# include "mixins/enum/mixin.inl"
1504
1505# ifdef FLECS_MODULE
1506# include "mixins/module/mixin.inl"
1507# endif
1508# ifdef FLECS_PIPELINE
1509# include "mixins/pipeline/mixin.inl"
1510# endif
1511# ifdef FLECS_SYSTEM
1512# include "mixins/system/mixin.inl"
1513# endif
1514# ifdef FLECS_TIMER
1515# include "mixins/timer/mixin.inl"
1516# endif
1517# ifdef FLECS_SCRIPT
1518# include "mixins/script/mixin.inl"
1519# endif
1520# ifdef FLECS_META
1521# include "mixins/meta/world.inl"
1522# endif
1523# ifdef FLECS_JSON
1524# include "mixins/json/world.inl"
1525# endif
1526# ifdef FLECS_APP
1527# include "mixins/app/mixin.inl"
1528# endif
1529# ifdef FLECS_METRICS
1530# include "mixins/metrics/mixin.inl"
1531# endif
1532# ifdef FLECS_ALERTS
1533# include "mixins/alerts/mixin.inl"
1534# endif
1535
1536public:
1538 void init_builtin_components();
1539
1540 world_t *world_;
1541};
1542
1546 struct scoped_world : world {
1552 scoped_world(
1553 flecs::world_t *w,
1554 flecs::entity_t s) : world(w)
1555 {
1556 prev_scope_ = ecs_set_scope(w, s);
1557 }
1558
1560 ~scoped_world() {
1561 ecs_set_scope(world_, prev_scope_);
1562 }
1563
1565 scoped_world(const scoped_world& obj) : world(nullptr) {
1566 prev_scope_ = obj.prev_scope_;
1567 world_ = obj.world_;
1568 flecs_poly_claim(world_);
1569 }
1570
1571 flecs::entity_t prev_scope_;
1572};
1573
1575
1576} // namespace flecs
mixin.inl
Alert world mixin.
mixin.inl
App world addon mixin.
mixin.inl
Component mixin.
mixin.inl
Entity world mixin.
mixin.inl
Enum world mixin.
mixin.inl
Event world mixin.
ecs_remove_all
void ecs_remove_all(ecs_world_t *world, ecs_id_t component)
Remove all instances of the specified component.
ecs_set_with
ecs_entity_t ecs_set_with(ecs_world_t *world, ecs_id_t component)
Create new entities with a specified component.
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
ECS_INVALID_OPERATION
#define ECS_INVALID_OPERATION
Invalid operation error code.
Definition log.h:669
ECS_INVALID_PARAMETER
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:671
ecs_stage_new
ecs_world_t * ecs_stage_new(ecs_world_t *world)
Create an unmanaged stage.
ecs_defer_end
bool ecs_defer_end(ecs_world_t *world)
End a block of operations to defer.
ecs_readonly_begin
bool ecs_readonly_begin(ecs_world_t *world, bool multi_threaded)
Begin readonly mode.
ecs_defer_resume
void ecs_defer_resume(ecs_world_t *world)
Resume deferring.
ecs_defer_begin
bool ecs_defer_begin(ecs_world_t *world)
Defer operations until the end of the frame.
ecs_defer_suspend
void ecs_defer_suspend(ecs_world_t *world)
Suspend deferring but do not flush queue.
ecs_is_deferred
bool ecs_is_deferred(const ecs_world_t *world)
Test if deferring is enabled for the current stage.
ecs_stage_free
void ecs_stage_free(ecs_world_t *stage)
Free an unmanaged stage.
ecs_merge
void ecs_merge(ecs_world_t *stage)
Merge a stage.
ecs_stage_get_id
int32_t ecs_stage_get_id(const ecs_world_t *world)
Get the stage ID.
ecs_stage_is_readonly
bool ecs_stage_is_readonly(const ecs_world_t *world)
Test whether the current world is readonly.
ecs_get_stage_count
int32_t ecs_get_stage_count(const ecs_world_t *world)
Get the number of configured stages.
ecs_get_stage
ecs_world_t * ecs_get_stage(const ecs_world_t *world, int32_t stage_id)
Get stage-specific world pointer.
ecs_set_stage_count
void ecs_set_stage_count(ecs_world_t *world, int32_t stages)
Configure the world to have N stages.
ecs_readonly_end
void ecs_readonly_end(ecs_world_t *world)
End readonly mode.
ecs_is_defer_suspended
bool ecs_is_defer_suspended(const ecs_world_t *world)
Test if deferring is suspended for the current stage.
ecs_get_type_info
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_stage_t
struct ecs_stage_t ecs_stage_t
A stage enables modification while iterating and from multiple threads.
Definition flecs.h:432
ecs_entity_t
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:385
ecs_world_t
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:429
ecs_id_t
uint64_t ecs_id_t
IDs are the things that can be added to an entity.
Definition flecs.h:378
flecs::component::range
untyped_component & range(double min, double max)
Add a member value range.
Definition untyped_component.inl:240
flecs::world::component
flecs::component< T > component(Args &&... args) const
Find or register a component.
Definition impl.hpp:11
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:190
flecs::world_info_t
ecs_world_info_t world_info_t
World info type.
Definition c_types.hpp:19
flecs::id_t
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
flecs::entity_t
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
flecs::world_t
ecs_world_t world_t
World type.
Definition c_types.hpp:18
flecs::type_info_t
ecs_type_info_t type_info_t
Type info type.
Definition c_types.hpp:33
ecs_delete_with
void ecs_delete_with(ecs_world_t *world, ecs_id_t component)
Delete all entities with the specified component.
ecs_count_id
int32_t ecs_count_id(const ecs_world_t *world, ecs_id_t entity)
Count entities that have the specified ID.
ecs_fini_action_t
void(* ecs_fini_action_t)(ecs_world_t *world, void *ctx)
Action callback on world exit.
Definition flecs.h:645
ecs_ctx_free_t
void(* ecs_ctx_free_t)(void *ctx)
Function to clean up context data.
Definition flecs.h:650
ecs_emplace_id
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.
ecs_modified_id
void ecs_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Signal that a component has been modified.
ecs_strip_generation
ecs_id_t ecs_strip_generation(ecs_entity_t e)
Remove the generation from an entity ID.
ecs_is_valid
bool ecs_is_valid(const ecs_world_t *world, ecs_entity_t e)
Test whether an entity is valid.
ecs_get_version
uint32_t ecs_get_version(ecs_entity_t entity)
Get the generation of an entity.
ecs_exists
bool ecs_exists(const ecs_world_t *world, ecs_entity_t entity)
Test whether an entity exists.
ecs_is_alive
bool ecs_is_alive(const ecs_world_t *world, ecs_entity_t e)
Test whether an entity is alive.
ecs_set_version
void ecs_set_version(ecs_world_t *world, ecs_entity_t entity)
Override the generation of an entity.
ecs_ftime_t
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
ecs_set_lookup_path
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_set_scope
ecs_entity_t ecs_set_scope(ecs_world_t *world, ecs_entity_t scope)
Set the current scope.
ecs_atfini
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.
ecs_fini
int ecs_fini(ecs_world_t *world)
Delete a world.
ecs_init
ecs_world_t * ecs_init(void)
Create a new world.
ecs_init_w_args
ecs_world_t * ecs_init_w_args(int argc, char *argv[])
Create a new world with arguments.
ecs_frame_begin
float ecs_frame_begin(ecs_world_t *world, float delta_time)
Begin frame.
ecs_run_post_frame
void ecs_run_post_frame(ecs_world_t *world, ecs_fini_action_t action, void *ctx)
Register an action to be executed once after the frame.
ecs_should_quit
bool ecs_should_quit(const ecs_world_t *world)
Return whether a quit has been requested.
ecs_quit
void ecs_quit(ecs_world_t *world)
Signal exit.
ecs_frame_end
void ecs_frame_end(ecs_world_t *world)
End frame.
ecs_entity_range_set
void ecs_entity_range_set(ecs_world_t *world, const ecs_entity_range_t *range)
Set the active entity range.
ecs_entity_range_new
const ecs_entity_range_t * ecs_entity_range_new(ecs_world_t *world, uint32_t min, uint32_t max)
Create a new entity range.
ecs_get_binding_ctx
void * ecs_get_binding_ctx(const ecs_world_t *world)
Get the world binding context.
ecs_shrink
void ecs_shrink(ecs_world_t *world)
Free unused memory.
ecs_dim
void ecs_dim(ecs_world_t *world, int32_t entity_count)
Dimension the world for a specified number of entities.
ecs_get_world_info
const ecs_world_info_t * ecs_get_world_info(const ecs_world_t *world)
Get the world info.
ecs_get_world
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get the world from a poly.
ecs_set_ctx
void ecs_set_ctx(ecs_world_t *world, void *ctx, ecs_ctx_free_t ctx_free)
Set a world context.
ecs_exclusive_access_begin
void ecs_exclusive_access_begin(ecs_world_t *world, const char *thread_name)
Begin exclusive thread access.
ecs_entity_range_get
const ecs_entity_range_t * ecs_entity_range_get(const ecs_world_t *world)
Get the currently active entity id range.
ecs_set_binding_ctx
void ecs_set_binding_ctx(ecs_world_t *world, void *ctx, ecs_ctx_free_t ctx_free)
Set a world binding context.
flecs_poly_is
#define flecs_poly_is(object, type)
Test if a pointer is of the specified type.
Definition flecs.h:2872
ecs_exclusive_access_end
void ecs_exclusive_access_end(ecs_world_t *world, bool lock_world)
End exclusive thread access.
ecs_get_ctx
void * ecs_get_ctx(const ecs_world_t *world)
Get the world context.
mixin.inl
ID world mixin.
world.inl
JSON world mixin.
world.inl
Meta world mixin.
mixin.inl
Metrics world mixin.
mixin.inl
Module world mixin.
mixin.inl
Observer world mixin.
mixin.inl
Pipeline world mixin.
mixin.inl
Query world mixin.
mixin.inl
Script world mixin.
ecs_entity_range_t
Type that stores an entity id range.
Definition flecs.h:1574
ecs_world_info_t::delta_time
float delta_time
Time passed to or computed by ecs_progress().
Definition flecs.h:1508
flecs::_::type
Definition flecs.hpp:39
flecs::entity
Entity.
Definition entity.hpp:30
flecs::scoped_world
Scoped world.
Definition world.hpp:1546
flecs::scoped_world::~scoped_world
~scoped_world()
Destructor.
Definition world.hpp:1560
flecs::scoped_world::scoped_world
scoped_world(flecs::world_t *w, flecs::entity_t s)
Create a scoped world.
Definition world.hpp:1552
flecs::scoped_world::prev_scope_
flecs::entity_t prev_scope_
The previous scope entity.
Definition world.hpp:1571
flecs::scoped_world::scoped_world
scoped_world(const scoped_world &obj)
Copy constructor.
Definition world.hpp:1565
flecs::world
The world.
Definition world.hpp:246
flecs::world::is_stage
bool is_stage() const
Test if this is a stage.
Definition world.hpp:555
flecs::world::shrink
void shrink() const
Free unused memory.
Definition world.hpp:1430
flecs::world::get_version
uint32_t get_version(flecs::entity_t e) const
Get the version of the provided entity.
Definition world.hpp:1404
flecs::world::delete_with
void delete_with() const
Delete all entities with specified component.
Definition world.hpp:1264
flecs::world::get
const T & get() const
Get a singleton component.
Definition world.hpp:191
flecs::world::remove_all
void remove_all(id_t the_id) const
Remove all instances of specified id.
Definition world.hpp:1281
flecs::world::delete_with
void delete_with(entity_t second) const
Delete all entities with specified pair.
Definition world.hpp:1276
flecs::world::merge
void merge() const
Merge world or stage.
Definition world.hpp:574
flecs::world::world_
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1540
flecs::world::delete_with
void delete_with(id_t the_id) const
Delete all entities with specified id.
Definition world.hpp:1253
flecs::world::get_info
const flecs::world_info_t * get_info() const
Get the world info.
Definition world.hpp:1417
flecs::world::get_scope
flecs::entity get_scope() const
Get current scope.
Definition world.hpp:91
flecs::world::get_mut
T & get_mut() const
Get a mutable singleton component.
Definition world.hpp:257
flecs::world::remove
void remove() const
Remove singleton component.
Definition world.hpp:346
flecs::world::lookup
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
flecs::world::set
void set(A &&value) const
Set singleton pair.
Definition world.hpp:803
flecs::world::delta_time
ecs_ftime_t delta_time() const
Get delta_time.
Definition world.hpp:1422
flecs::world::quit
void quit() const
Signal application should quit.
Definition world.hpp:359
flecs::world::get_alive
flecs::entity get_alive(flecs::entity_t e) const
Get alive entity for ID.
Definition world.hpp:427
flecs::world::type_info
const flecs::type_info_t * type_info()
Return the type info.
Definition world.hpp:1480
flecs::world::readonly_end
void readonly_end() const
End readonly mode.
Definition world.hpp:433
flecs::world::with
void with(const Func &func) const
All entities created in the function are created with the pair.
Definition world.hpp:1206
flecs::world::make_alive
flecs::entity make_alive(flecs::entity_t e) const
Ensure an entity ID is alive.
Definition world.hpp:433
flecs::world::count
int count() const
Count entities matching a component.
Definition world.hpp:1159
flecs::world::id
flecs::id id() const
Get ID from a type.
Definition impl.hpp:70
flecs::world::exclusive_access_begin
void exclusive_access_begin(const char *thread_name=nullptr)
Begin exclusive access.
Definition world.hpp:1439
flecs::world::target
flecs::entity target(int32_t index=0) const
Get target for a given pair from a singleton entity.
Definition world.hpp:385
flecs::world::defer
void defer(const Func &func) const
Defer all operations called in function.
Definition world.hpp:1317
flecs::world::should_quit
bool should_quit() const
Test if quit() has been called.
Definition world.hpp:371
flecs::world::is_alive
bool is_alive(flecs::entity_t e) const
Check if entity is alive.
Definition world.hpp:1365
flecs::world::c_ptr
world_t * c_ptr() const
Obtain a pointer to the C world object.
Definition world.hpp:352
flecs::world::try_get
const T * try_get() const
Get singleton component.
Definition world.hpp:158
flecs::world::~world
~world()
Destructor.
Definition world.hpp:322
flecs::world::defer_begin
bool defer_begin() const
Defer operations until end of frame.
Definition world.hpp:453
flecs::world::reset
void reset()
Delete and recreate the world.
Definition world.hpp:341
flecs::world::set_lookup_path
flecs::entity_t * set_lookup_path(const flecs::entity_t *search_path) const
Set search path.
Definition world.hpp:762
flecs::world::defer_suspend
void defer_suspend() const
Suspend deferring operations.
Definition world.hpp:1332
flecs::world::set
void set(const A &value) const
Set singleton pair.
Definition world.hpp:795
flecs::world::make_owner
void make_owner()
Make the current world object the owner of the world.
Definition world.hpp:336
flecs::world::is_valid
bool is_valid(flecs::entity_t e) const
Check if entity ID is valid.
Definition world.hpp:1376
flecs::world::async_stage
flecs::world async_stage() const
Create an asynchronous stage.
Definition world.hpp:611
flecs::world::is_deferred
bool is_deferred() const
Test whether deferring is enabled.
Definition world.hpp:488
flecs::world::release
void release()
Release the underlying world object.
Definition world.hpp:304
flecs::world::get_binding_ctx
void * get_binding_ctx() const
Get world binding context.
Definition world.hpp:694
flecs::world::get_stage_id
int32_t get_stage_id() const
Get current stage ID.
Definition world.hpp:545
flecs::world::init_builtin_components
void init_builtin_components()
Initialize built-in components.
Definition world.hpp:12
flecs::world::world
world(const world &obj)
Copy constructor.
Definition world.hpp:274
flecs::world::scope
void scope(const Func &func) const
Same as scope(parent, func), but with T as parent.
Definition world.hpp:1237
flecs::world::defer_resume
void defer_resume() const
Resume deferring operations.
Definition world.hpp:1345
flecs::world::set_scope
flecs::entity set_scope() const
Same as set_scope(), but with type.
Definition world.hpp:97
flecs::world::range_set
void range_set(const ecs_entity_range_t *range) const
Set the active entity id range.
Definition world.hpp:719
flecs::world::dim
void dim(int32_t entity_count) const
Preallocate memory for a number of entities.
Definition world.hpp:705
flecs::world::set_version
void set_version(flecs::entity_t e) const
Set the version of an entity to the provided value.
Definition world.hpp:1396
flecs::world::set_stage_count
void set_stage_count(int32_t stages) const
Configure world to have N stages.
Definition world.hpp:523
flecs::world::with
void with(id_t with_id, const Func &func) const
All entities created in the function are created with the ID.
Definition world.hpp:1190
flecs::world::get_ctx
void * get_ctx() const
Get world context.
Definition world.hpp:666
flecs::world::type_info
const flecs::type_info_t * type_info(flecs::entity_t r, flecs::entity_t t)
Return the type info.
Definition world.hpp:1474
flecs::world::count
int count() const
Count entities matching a pair.
Definition world.hpp:1181
flecs::world::scope
flecs::scoped_world scope() const
Use the provided scope (by type) for operations run on the returned world.
Definition world.hpp:484
flecs::world::remove_all
void remove_all() const
Remove all instances of specified pair.
Definition world.hpp:1298
flecs::world::range_get
const ecs_entity_range_t * range_get() const
Get the currently active entity id range.
Definition world.hpp:726
flecs::world::defer_end
bool defer_end() const
End block of operations to defer.
Definition world.hpp:472
flecs::world::count
int count(flecs::entity_t first, flecs::entity_t second) const
Count entities matching a pair.
Definition world.hpp:1149
flecs::world::remove_all
void remove_all(entity_t second) const
Remove all instances of specified pair.
Definition world.hpp:1304
flecs::world::world
world(world_t *w)
Create a world from a C world.
Definition world.hpp:265
flecs::world::children
void children(Func &&f) const
Iterate entities in root of world.
Definition world.hpp:373
flecs::world::get_world
flecs::world get_world() const
Get actual world.
Definition world.hpp:623
flecs::world::try_get_mut
T * try_get_mut() const
Try to get a mutable singleton component (returns nullptr if not found).
Definition world.hpp:224
flecs::world::scope
void scope(id_t parent, const Func &func) const
All entities created in the function are created in the scope.
Definition world.hpp:1228
flecs::world::with
void with(const Func &func) const
All entities created in the function are created with the type.
Definition world.hpp:1199
flecs::world::remove_all
void remove_all(entity_t first, entity_t second) const
Remove all instances of specified pair.
Definition world.hpp:1286
flecs::world::modified
void modified() const
Mark singleton component as modified.
Definition world.hpp:118
flecs::world::world
world(world &&obj) noexcept
Move constructor.
Definition world.hpp:288
flecs::world::count
int count(flecs::entity_t second) const
Count entities matching a pair.
Definition world.hpp:1170
flecs::world::operator=
world & operator=(const world &obj) noexcept
Copy assignment operator.
Definition world.hpp:280
flecs::world::id_if_registered
flecs::id_t id_if_registered()
Return the component ID if it has been registered.
Definition world.hpp:1459
flecs::world::frame_begin
ecs_ftime_t frame_begin(float delta_time=0) const
Begin frame.
Definition world.hpp:396
flecs::world::use
flecs::entity use(const char *alias=nullptr) const
Create an alias for a component.
Definition world.hpp:54
flecs::world::is_readonly
bool is_readonly() const
Test whether the current world object is readonly.
Definition world.hpp:639
flecs::world::add
void add() const
Add singleton component.
Definition world.hpp:312
flecs::world::ensure
T & ensure() const
Ensure singleton component.
Definition world.hpp:110
flecs::world::readonly_begin
bool readonly_begin(bool multi_threaded=false) const
Begin readonly mode.
Definition world.hpp:423
flecs::world::set
void set(const T &value) const
Set singleton component.
Definition world.hpp:779
flecs::world::with
void with(id_t first, id_t second, const Func &func) const
All entities created in the function are created with the pair.
Definition world.hpp:1220
flecs::world::get_stage
flecs::world get_stage(int32_t stage_id) const
Get stage-specific world pointer.
Definition world.hpp:592
flecs::world::set
void set(T &&value) const
Set singleton component.
Definition world.hpp:786
flecs::world::has
bool has() const
Test if world has singleton component.
Definition world.hpp:278
flecs::world::exists
bool exists(flecs::entity_t e) const
Check if entity ID exists in the world.
Definition world.hpp:1355
flecs::world::world
world()
Create a world.
Definition world.hpp:249
flecs::world::frame_end
void frame_end() const
End frame.
Definition world.hpp:409
flecs::world::run_post_frame
void run_post_frame(ecs_fini_action_t action, void *ctx) const
Run callback after completing the frame.
Definition world.hpp:1409
flecs::world::range_new
const ecs_entity_range_t * range_new(uint32_t min, uint32_t max) const
Create a new entity id range.
Definition world.hpp:712
flecs::world::set_binding_ctx
void set_binding_ctx(void *ctx, ecs_ctx_free_t ctx_free=nullptr) const
Set world binding context.
Definition world.hpp:681
flecs::world::atfini
void atfini(ecs_fini_action_t action, void *ctx=nullptr) const
Register action to be executed when world is destroyed.
Definition world.hpp:365
flecs::world::type_info
const flecs::type_info_t * type_info(flecs::id_t component)
Return the type info.
Definition world.hpp:1469
flecs::world::get_stage_count
int32_t get_stage_count() const
Get the number of configured stages.
Definition world.hpp:535
flecs::world::delete_with
void delete_with(entity_t first, entity_t second) const
Delete all entities with specified pair.
Definition world.hpp:1258
flecs::world::delete_with
void delete_with() const
Delete all entities with specified pair.
Definition world.hpp:1270
flecs::world::singleton
flecs::entity singleton() const
Get singleton entity for type.
Definition world.hpp:379
flecs::world::exclusive_access_end
void exclusive_access_end(bool lock_world=false)
End exclusive access.
Definition world.hpp:1448
flecs::world::with
void with(id_t second, const Func &func) const
All entities created in the function are created with the pair.
Definition world.hpp:1213
flecs::world::set_ctx
void set_ctx(void *ctx, ecs_ctx_free_t ctx_free=nullptr) const
Set world context.
Definition world.hpp:653
flecs::world::is_defer_suspended
bool is_defer_suspended() const
Test whether deferring is suspended.
Definition world.hpp:504
flecs::world::world
world(int argc, char *argv[])
Create a world with command-line arguments.
Definition world.hpp:258
flecs::world::count
int count(flecs::id_t component_id) const
Count entities matching a component.
Definition world.hpp:1139
flecs::world::type_info
const flecs::type_info_t * type_info()
Return the type info.
Definition world.hpp:1492
flecs::world::type_info
const flecs::type_info_t * type_info(flecs::entity_t t)
Return the type info.
Definition world.hpp:1486
flecs::world::operator=
world & operator=(world &&obj) noexcept
Move assignment operator.
Definition world.hpp:294
flecs::world::remove_all
void remove_all() const
Remove all instances of specified component.
Definition world.hpp:1292
flecs::world::get_ref
ref< T > get_ref() const
Get ref singleton component.
Definition world.hpp:139
mixin.inl
System module world mixin.
mixin.inl
Term world mixin.
mixin.inl
Timer module mixin.
flecs::strip_generation
flecs::id_t strip_generation(flecs::entity_t e)
Return the ID without generation.
Definition world.hpp:219
flecs::get_generation
uint32_t get_generation(flecs::entity_t e)
Return the entity generation.
Definition world.hpp:228
flecs::assign
void assign(world_t *world, flecs::entity_t entity, T &&value, flecs::id_t id)
Assign a component value using move semantics.
Definition world.hpp:109
flecs::set
void set(world_t *world, flecs::entity_t entity, T &&value, flecs::id_t id)
Static helper functions to assign a component value.
Definition world.hpp:22
flecs::emplace
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:202