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
717 void set_entity_range(entity_t min, entity_t max) const {
718 ecs_set_entity_range(world_, min, max);
719 }
720
731 void enable_range_check(bool enabled = true) const {
732 ecs_enable_range_check(world_, enabled);
733 }
734
743 flecs::entity set_scope(const flecs::entity_t scope) const;
744
752 flecs::entity get_scope() const;
753
759 template <typename T>
760 flecs::entity set_scope() const;
761
767 flecs::entity_t* set_lookup_path(const flecs::entity_t *search_path) const {
768 return ecs_set_lookup_path(world_, search_path);
769 }
770
779 flecs::entity lookup(const char *name, const char *sep = "::", const char *root_sep = "::", bool recursive = true) const;
780
783 template <typename T, if_t< !is_callable<T>::value > = 0>
784 void set(const T& value) const {
785 flecs::set<T>(world_, _::type<T>::id(world_), value);
786 }
787
790 template <typename T, if_t< !is_callable<T>::value > = 0>
791 void set(T&& value) const {
792 flecs::set<T>(world_, _::type<T>::id(world_),
793 FLECS_FWD(value));
794 }
795
798 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
799 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
800 void set(const A& value) const {
801 flecs::set<P>(world_, _::type<First>::id(world_), value);
802 }
803
806 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
807 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
808 void set(A&& value) const {
809 flecs::set<P>(world_, _::type<First>::id(world_), FLECS_FWD(value));
810 }
811
814 template <typename First, typename Second>
815 void set(Second second, const First& value) const;
816
819 template <typename First, typename Second>
820 void set(Second second, First&& value) const;
821
824 template <typename Func, if_t< is_callable<Func>::value > = 0 >
825 void set(const Func& func) const;
826
827 template <typename T, typename ... Args>
828 void emplace(Args&&... args) const {
829 flecs::id_t component_id = _::type<T>::id(world_);
830 flecs::emplace<T>(world_, component_id, component_id, FLECS_FWD(args)...);
831 }
832
835 #ifndef ensure
836 template <typename T>
837 T& ensure() const;
838 #endif
839
842 template <typename T>
843 void modified() const;
844
847 template <typename T>
848 ref<T> get_ref() const;
849
850
851 /* try_get */
852
855 const void* try_get(flecs::id_t id) const;
856
859 const void* try_get(flecs::entity_t r, flecs::entity_t t) const;
860
863 template <typename T>
864 const T* try_get() const;
865
868 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
869 typename A = actual_type_t<P>>
870 const A* try_get() const;
871
874 template <typename First, typename Second>
875 const First* try_get(Second second) const;
876
877
878 /* get */
879
882 const void* get(flecs::id_t id) const;
883
886 const void* get(flecs::entity_t r, flecs::entity_t t) const;
887
888 template <typename T>
889 const T& get() const;
890
893 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
894 typename A = actual_type_t<P>>
895 const A& get() const;
896
899 template <typename First, typename Second>
900 const First& get(Second second) const;
901
904 template <typename Func, if_t< is_callable<Func>::value > = 0 >
905 void get(const Func& func) const;
906
907
908 /* try_get_mut */
909
912 void* try_get_mut(flecs::id_t id) const;
913
916 void* try_get_mut(flecs::entity_t r, flecs::entity_t t) const;
917
918 template <typename T>
919 T* try_get_mut() const;
920
923 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
924 typename A = actual_type_t<P>>
925 A* try_get_mut() const;
926
929 template <typename First, typename Second>
930 First* try_get_mut(Second second) const;
931
932
933 /* get_mut */
934
937 void* get_mut(flecs::id_t id) const;
938
941 void* get_mut(flecs::entity_t r, flecs::entity_t t) const;
942
943 template <typename T>
944 T& get_mut() const;
945
948 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
949 typename A = actual_type_t<P>>
950 A& get_mut() const;
951
954 template <typename First, typename Second>
955 First& get_mut(Second second) const;
956
957
963 template <typename T>
964 bool has() const;
965
972 template <typename First, typename Second>
973 bool has() const;
974
981 template <typename First>
982 bool has(flecs::id_t second) const;
983
990 bool has(flecs::id_t first, flecs::id_t second) const;
991
998 template <typename E, if_t< is_enum<E>::value > = 0>
999 bool has(E value) const;
1000
1003 template <typename T>
1004 void add() const;
1005
1011 template <typename First, typename Second>
1012 void add() const;
1013
1019 template <typename First>
1020 void add(flecs::entity_t second) const;
1021
1027 void add(flecs::entity_t first, flecs::entity_t second) const;
1028
1034 template <typename E, if_t< is_enum<E>::value > = 0>
1035 void add(E value) const;
1036
1039 template <typename T>
1040 void remove() const;
1041
1047 template <typename First, typename Second>
1048 void remove() const;
1049
1055 template <typename First>
1056 void remove(flecs::entity_t second) const;
1057
1063 void remove(flecs::entity_t first, flecs::entity_t second) const;
1064
1072 template <typename Func>
1073 void children(Func&& f) const;
1074
1077 template <typename T>
1078 flecs::entity singleton() const;
1079
1089 template<typename First>
1090 flecs::entity target(int32_t index = 0) const;
1091
1102 template<typename T>
1103 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
1104
1114 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
1115
1122 template <typename T>
1123 flecs::entity use(const char *alias = nullptr) const;
1124
1130 flecs::entity use(const char *name, const char *alias = nullptr) const;
1131
1137 void use(flecs::entity entity, const char *alias = nullptr) const;
1138
1144 int count(flecs::id_t component_id) const {
1145 return ecs_count_id(world_, component_id);
1146 }
1147
1154 int count(flecs::entity_t first, flecs::entity_t second) const {
1155 return ecs_count_id(world_, ecs_pair(first, second));
1156 }
1157
1163 template <typename T>
1164 int count() const {
1165 return count(_::type<T>::id(world_));
1166 }
1167
1174 template <typename First>
1175 int count(flecs::entity_t second) const {
1176 return count(_::type<First>::id(world_), second);
1177 }
1178
1185 template <typename First, typename Second>
1186 int count() const {
1187 return count(
1188 _::type<First>::id(world_),
1189 _::type<Second>::id(world_));
1190 }
1191
1194 template <typename Func>
1195 void with(id_t with_id, const Func& func) const {
1196 ecs_id_t prev = ecs_set_with(world_, with_id);
1197 func();
1198 ecs_set_with(world_, prev);
1199 }
1200
1203 template <typename T, typename Func>
1204 void with(const Func& func) const {
1205 with(this->id<T>(), func);
1206 }
1207
1210 template <typename First, typename Second, typename Func>
1211 void with(const Func& func) const {
1212 with(ecs_pair(this->id<First>(), this->id<Second>()), func);
1213 }
1214
1217 template <typename First, typename Func>
1218 void with(id_t second, const Func& func) const {
1219 with(ecs_pair(this->id<First>(), second), func);
1220 }
1221
1224 template <typename Func>
1225 void with(id_t first, id_t second, const Func& func) const {
1226 with(ecs_pair(first, second), func);
1227 }
1228
1232 template <typename Func>
1233 void scope(id_t parent, const Func& func) const {
1234 ecs_entity_t prev = ecs_set_scope(world_, parent);
1235 func();
1236 ecs_set_scope(world_, prev);
1237 }
1238
1241 template <typename T, typename Func>
1242 void scope(const Func& func) const {
1243 flecs::id_t parent = _::type<T>::id(world_);
1244 scope(parent, func);
1245 }
1246
1250 flecs::scoped_world scope(id_t parent) const;
1251
1252 template <typename T>
1253 flecs::scoped_world scope() const;
1254
1255 flecs::scoped_world scope(const char* name) const;
1256
1258 void delete_with(id_t the_id) const {
1259 ecs_delete_with(world_, the_id);
1260 }
1261
1263 void delete_with(entity_t first, entity_t second) const {
1264 delete_with(ecs_pair(first, second));
1265 }
1266
1268 template <typename T>
1269 void delete_with() const {
1270 delete_with(_::type<T>::id(world_));
1271 }
1272
1274 template <typename First, typename Second>
1275 void delete_with() const {
1276 delete_with(_::type<First>::id(world_), _::type<Second>::id(world_));
1277 }
1278
1280 template <typename First>
1281 void delete_with(entity_t second) const {
1282 delete_with(_::type<First>::id(world_), second);
1283 }
1284
1286 void remove_all(id_t the_id) const {
1287 ecs_remove_all(world_, the_id);
1288 }
1289
1291 void remove_all(entity_t first, entity_t second) const {
1292 remove_all(ecs_pair(first, second));
1293 }
1294
1296 template <typename T>
1297 void remove_all() const {
1298 remove_all(_::type<T>::id(world_));
1299 }
1300
1302 template <typename First, typename Second>
1303 void remove_all() const {
1304 remove_all(_::type<First>::id(world_), _::type<Second>::id(world_));
1305 }
1306
1308 template <typename First>
1309 void remove_all(entity_t second) const {
1310 remove_all(_::type<First>::id(world_), second);
1311 }
1312
1321 template <typename Func>
1322 void defer(const Func& func) const {
1323 ecs_defer_begin(world_);
1324 func();
1325 ecs_defer_end(world_);
1326 }
1327
1337 void defer_suspend() const {
1338 ecs_defer_suspend(world_);
1339 }
1340
1350 void defer_resume() const {
1351 ecs_defer_resume(world_);
1352 }
1353
1360 bool exists(flecs::entity_t e) const {
1361 return ecs_exists(world_, e);
1362 }
1363
1370 bool is_alive(flecs::entity_t e) const {
1371 return ecs_is_alive(world_, e);
1372 }
1373
1381 bool is_valid(flecs::entity_t e) const {
1382 return ecs_is_valid(world_, e);
1383 }
1384
1390 flecs::entity get_alive(flecs::entity_t e) const;
1391
1395 flecs::entity make_alive(flecs::entity_t e) const;
1396
1401 void set_version(flecs::entity_t e) const {
1402 ecs_set_version(world_, e);
1403 }
1404
1409 uint32_t get_version(flecs::entity_t e) const {
1410 return ecs_get_version(e);
1411 }
1412
1414 void run_post_frame(ecs_fini_action_t action, void *ctx) const {
1415 ecs_run_post_frame(world_, action, ctx);
1416 }
1417
1422 const flecs::world_info_t* get_info() const {
1423 return ecs_get_world_info(world_);
1424 }
1425
1427 ecs_ftime_t delta_time() const {
1428 return get_info()->delta_time;
1429 }
1430
1435 void shrink() const {
1436 ecs_shrink(world_);
1437 }
1438
1444 void exclusive_access_begin(const char *thread_name = nullptr) {
1445 ecs_exclusive_access_begin(world_, thread_name);
1446 }
1447
1453 void exclusive_access_end(bool lock_world = false) {
1454 ecs_exclusive_access_end(world_, lock_world);
1455 }
1456
1463 template <typename T>
1464 flecs::id_t id_if_registered() {
1465 if (_::type<T>::registered(world_)) {
1466 return _::type<T>::id(world_);
1467 }
1468 else {
1469 return 0;
1470 }
1471 }
1472
1474 const flecs::type_info_t* type_info(flecs::id_t component) {
1475 return ecs_get_type_info(world_, component);
1476 }
1477
1479 const flecs::type_info_t* type_info(flecs::entity_t r, flecs::entity_t t) {
1480 return ecs_get_type_info(world_, ecs_pair(r, t));
1481 }
1482
1484 template <typename T>
1485 const flecs::type_info_t* type_info() {
1486 return ecs_get_type_info(world_, _::type<T>::id(world_));
1487 }
1488
1490 template <typename R>
1491 const flecs::type_info_t* type_info(flecs::entity_t t) {
1492 return type_info(_::type<R>::id(world_), t);
1493 }
1494
1496 template <typename R, typename T>
1497 const flecs::type_info_t* type_info() {
1498 return type_info<R>(_::type<T>::id(world_));
1499 }
1500
1501# include "mixins/id/mixin.inl"
1502# include "mixins/component/mixin.inl"
1503# include "mixins/entity/mixin.inl"
1504# include "mixins/event/mixin.inl"
1505# include "mixins/term/mixin.inl"
1506# include "mixins/observer/mixin.inl"
1507# include "mixins/query/mixin.inl"
1508# include "mixins/enum/mixin.inl"
1509
1510# ifdef FLECS_MODULE
1511# include "mixins/module/mixin.inl"
1512# endif
1513# ifdef FLECS_PIPELINE
1514# include "mixins/pipeline/mixin.inl"
1515# endif
1516# ifdef FLECS_SYSTEM
1517# include "mixins/system/mixin.inl"
1518# endif
1519# ifdef FLECS_TIMER
1520# include "mixins/timer/mixin.inl"
1521# endif
1522# ifdef FLECS_SCRIPT
1523# include "mixins/script/mixin.inl"
1524# endif
1525# ifdef FLECS_META
1526# include "mixins/meta/world.inl"
1527# endif
1528# ifdef FLECS_JSON
1529# include "mixins/json/world.inl"
1530# endif
1531# ifdef FLECS_APP
1532# include "mixins/app/mixin.inl"
1533# endif
1534# ifdef FLECS_METRICS
1535# include "mixins/metrics/mixin.inl"
1536# endif
1537# ifdef FLECS_ALERTS
1538# include "mixins/alerts/mixin.inl"
1539# endif
1540
1541public:
1543 void init_builtin_components();
1544
1545 world_t *world_;
1546};
1547
1551 struct scoped_world : world {
1557 scoped_world(
1558 flecs::world_t *w,
1559 flecs::entity_t s) : world(w)
1560 {
1561 prev_scope_ = ecs_set_scope(w, s);
1562 }
1563
1565 ~scoped_world() {
1566 ecs_set_scope(world_, prev_scope_);
1567 }
1568
1570 scoped_world(const scoped_world& obj) : world(nullptr) {
1571 prev_scope_ = obj.prev_scope_;
1572 world_ = obj.world_;
1573 flecs_poly_claim(world_);
1574 }
1575
1576 flecs::entity_t prev_scope_;
1577};
1578
1580
1581} // 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:659
ECS_INVALID_PARAMETER
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:661
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:428
ecs_entity_t
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:381
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:425
ecs_id_t
uint64_t ecs_id_t
IDs are the things that can be added to an entity.
Definition flecs.h:374
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:641
ecs_ctx_free_t
void(* ecs_ctx_free_t)(void *ctx)
Function to clean up context data.
Definition flecs.h:646
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_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_set_entity_range
void ecs_set_entity_range(ecs_world_t *world, ecs_entity_t id_start, ecs_entity_t id_end)
Set a range for issuing new entity IDs.
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_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:2814
ecs_enable_range_check
bool ecs_enable_range_check(ecs_world_t *world, bool enable)
Enable or disable range limits.
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_world_info_t::delta_time
float delta_time
Time passed to or computed by ecs_progress().
Definition flecs.h:1484
flecs::_::type
Definition flecs.hpp:39
flecs::entity
Entity.
Definition entity.hpp:30
flecs::scoped_world
Scoped world.
Definition world.hpp:1551
flecs::scoped_world::~scoped_world
~scoped_world()
Destructor.
Definition world.hpp:1565
flecs::scoped_world::scoped_world
scoped_world(flecs::world_t *w, flecs::entity_t s)
Create a scoped world.
Definition world.hpp:1557
flecs::scoped_world::prev_scope_
flecs::entity_t prev_scope_
The previous scope entity.
Definition world.hpp:1576
flecs::scoped_world::scoped_world
scoped_world(const scoped_world &obj)
Copy constructor.
Definition world.hpp:1570
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:1435
flecs::world::get_version
uint32_t get_version(flecs::entity_t e) const
Get the version of the provided entity.
Definition world.hpp:1409
flecs::world::delete_with
void delete_with() const
Delete all entities with specified component.
Definition world.hpp:1269
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:1286
flecs::world::delete_with
void delete_with(entity_t second) const
Delete all entities with specified pair.
Definition world.hpp:1281
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:1545
flecs::world::delete_with
void delete_with(id_t the_id) const
Delete all entities with specified id.
Definition world.hpp:1258
flecs::world::get_info
const flecs::world_info_t * get_info() const
Get the world info.
Definition world.hpp:1422
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:808
flecs::world::delta_time
ecs_ftime_t delta_time() const
Get delta_time.
Definition world.hpp:1427
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:1485
flecs::world::set_entity_range
void set_entity_range(entity_t min, entity_t max) const
Set entity range.
Definition world.hpp:717
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:1211
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:1164
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:1444
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:1322
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:1370
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:767
flecs::world::defer_suspend
void defer_suspend() const
Suspend deferring operations.
Definition world.hpp:1337
flecs::world::set
void set(const A &value) const
Set singleton pair.
Definition world.hpp:800
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:1381
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:1242
flecs::world::defer_resume
void defer_resume() const
Resume deferring operations.
Definition world.hpp:1350
flecs::world::set_scope
flecs::entity set_scope() const
Same as set_scope(), but with type.
Definition world.hpp:97
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:1401
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:1195
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:1479
flecs::world::count
int count() const
Count entities matching a pair.
Definition world.hpp:1186
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:1303
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:1154
flecs::world::remove_all
void remove_all(entity_t second) const
Remove all instances of specified pair.
Definition world.hpp:1309
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:1233
flecs::world::with
void with(const Func &func) const
All entities created in the function are created with the type.
Definition world.hpp:1204
flecs::world::remove_all
void remove_all(entity_t first, entity_t second) const
Remove all instances of specified pair.
Definition world.hpp:1291
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::enable_range_check
void enable_range_check(bool enabled=true) const
Enforce that operations cannot modify entities outside of range.
Definition world.hpp:731
flecs::world::count
int count(flecs::entity_t second) const
Count entities matching a pair.
Definition world.hpp:1175
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:1464
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:784
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:1225
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:791
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:1360
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:1414
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:1474
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:1263
flecs::world::delete_with
void delete_with() const
Delete all entities with specified pair.
Definition world.hpp:1275
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:1453
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:1218
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:1144
flecs::world::type_info
const flecs::type_info_t * type_info()
Return the type info.
Definition world.hpp:1497
flecs::world::type_info
const flecs::type_info_t * type_info(flecs::entity_t t)
Return the type info.
Definition world.hpp:1491
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:1297
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