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

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

Loading...
Searching...
No Matches
delegate.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8#include <utility> // std::declval
9
10namespace flecs
11{
12
13namespace _
14{
15
16// Binding ctx for component hooks
17 struct component_binding_ctx {
18 void *on_add = nullptr;
19 void *on_remove = nullptr;
20 void *on_set = nullptr;
21 ecs_ctx_free_t free_on_add = nullptr;
22 ecs_ctx_free_t free_on_remove = nullptr;
23 ecs_ctx_free_t free_on_set = nullptr;
24
25 ~component_binding_ctx() {
26 if (on_add && free_on_add) {
27 free_on_add(on_add);
28 }
29 if (on_remove && free_on_remove) {
30 free_on_remove(on_remove);
31 }
32 if (on_set && free_on_set) {
33 free_on_set(on_set);
34 }
35 }
36};
37
38// Utility to convert template argument pack to array of term ptrs
39 struct field_ptr {
40 void *ptr = nullptr;
41 int8_t index = 0;
42 bool is_ref = false;
43 bool is_row = false;
44};
45
46template <typename ... Components>
47 struct field_ptrs {
48 using array = flecs::array<_::field_ptr, sizeof...(Components)>;
49
50 void populate(const ecs_iter_t *iter) {
51 populate(iter, 0, static_cast<
52 remove_reference_t<
53 remove_pointer_t<Components>>
54 *>(nullptr)...);
55 }
56
57 void populate_self(const ecs_iter_t *iter) {
58 populate_self(iter, 0, static_cast<
59 remove_reference_t<
60 remove_pointer_t<Components>>
61 *>(nullptr)...);
62 }
63
64 array fields_;
65
66private:
67 void populate(const ecs_iter_t*, size_t) { }
68
69 template <typename T, typename... Targs,
70 typename A = remove_pointer_t<actual_type_t<T>>,
71 if_not_t< is_empty<A>::value > = 0>
72 void populate(const ecs_iter_t *iter, size_t index, T, Targs... comps) {
73 if (iter->row_fields & (1llu << index)) {
74 /* Need to fetch the value with ecs_field_at() */
75 fields_[index].is_row = true;
76 fields_[index].is_ref = true;
77 fields_[index].index = static_cast<int8_t>(index);
78 } else {
79 fields_[index].ptr = ecs_field_w_size(iter, sizeof(A),
80 static_cast<int8_t>(index));
81 fields_[index].is_ref = iter->sources[index] != 0;
82 }
83
84 populate(iter, index + 1, comps ...);
85 }
86
87 template <typename T, typename... Targs,
88 typename A = remove_pointer_t<actual_type_t<T>>,
89 if_t< is_empty<A>::value > = 0>
90 void populate(const ecs_iter_t *iter, size_t index, T, Targs... comps) {
91 populate(iter, index + 1, comps ...);
92 }
93
94 void populate_self(const ecs_iter_t*, size_t) { }
95
96 template <typename T, typename... Targs,
97 typename A = remove_pointer_t<actual_type_t<T>>,
98 if_not_t< is_empty<A>::value > = 0>
99 void populate_self(const ecs_iter_t *iter, size_t index, T, Targs... comps) {
100 fields_[index].ptr = ecs_field_w_size(iter, sizeof(A),
101 static_cast<int8_t>(index));
102 fields_[index].is_ref = false;
103 ecs_assert(iter->sources[index] == 0, ECS_INTERNAL_ERROR, NULL);
104 populate_self(iter, index + 1, comps ...);
105 }
106
107 template <typename T, typename... Targs,
108 typename A = remove_pointer_t<actual_type_t<T>>,
109 if_t< is_empty<A>::value > = 0>
110 void populate_self(const ecs_iter_t *iter, size_t index, T, Targs... comps) {
111 populate(iter, index + 1, comps ...);
112 }
113};
114
115 struct delegate { };
116
117// Template that figures out from the template parameters of a query/system
118// how to pass the value to the each callback
119template <typename T, typename = int>
120 struct each_field { };
121
122// Base class
123 struct each_column_base {
124 each_column_base(const _::field_ptr& field, size_t row)
125 : field_(field), row_(row) {
126 }
127
128protected:
129 const _::field_ptr& field_;
130 size_t row_;
131};
132
133// If type is not a pointer, return a reference to the type (default case)
134template <typename T>
135 struct each_field<T, if_t< !is_pointer<T>::value &&
136 !is_empty<actual_type_t<T>>::value && is_actual<T>::value > >
137 : each_column_base
138{
139 each_field(const flecs::iter_t*, _::field_ptr& field, size_t row)
140 : each_column_base(field, row) { }
141
142 T& get_row() {
143 return static_cast<T*>(this->field_.ptr)[this->row_];
144 }
145};
146
147// If argument type is not the same as actual component type, return by value.
148// This requires that the actual type can be converted to the type.
149// A typical scenario where this happens is when using flecs::pair types.
150template <typename T>
151 struct each_field<T, if_t< !is_pointer<T>::value &&
152 !is_empty<actual_type_t<T>>::value && !is_actual<T>::value> >
153 : each_column_base
154{
155 each_field(const flecs::iter_t*, _::field_ptr& field, size_t row)
156 : each_column_base(field, row) { }
157
158 T get_row() {
159 return static_cast<actual_type_t<T>*>(this->field_.ptr)[this->row_];
160 }
161};
162
163// If type is empty (indicating a tag) the query will pass a nullptr. To avoid
164// returning nullptr to reference arguments, return a temporary value.
165template <typename T>
166 struct each_field<T, if_t< is_empty<actual_type_t<T>>::value &&
167 !is_pointer<T>::value > >
168 : each_column_base
169{
170 each_field(const flecs::iter_t*, _::field_ptr& field, size_t row)
171 : each_column_base(field, row) { }
172
173 T get_row() {
174 return actual_type_t<T>();
175 }
176};
177
178// If type is a pointer (indicating an optional value) don't index with row if
179// the field is not set.
180template <typename T>
181 struct each_field<T, if_t< is_pointer<T>::value &&
182 !is_empty<actual_type_t<T>>::value > >
183 : each_column_base
184{
185 each_field(const flecs::iter_t*, _::field_ptr& field, size_t row)
186 : each_column_base(field, row) { }
187
188 actual_type_t<T> get_row() {
189 if (this->field_.ptr) {
190 return &static_cast<actual_type_t<T>>(this->field_.ptr)[this->row_];
191 } else {
192 // optional argument doesn't have a value
193 return nullptr;
194 }
195 }
196};
197
198// If the query contains component references to other entities, check if the
199// current argument is one.
200template <typename T, typename = int>
201 struct each_ref_field : public each_field<T> {
202 each_ref_field(const flecs::iter_t *iter, _::field_ptr& field, size_t row)
203 : each_field<T>(iter, field, row) {
204
205 if (field.is_ref) {
206 // If this is a reference, set the row to 0 as a ref always is a
207 // single value, not an array. This prevents the application from
208 // having to do an if-check on whether the column is owned.
209 //
210 // This check only happens when the current table being iterated
211 // over caused the query to match a reference. The check is
212 // performed once per iterated table.
213 this->row_ = 0;
214 }
215
216 if (field.is_row) {
217 field.ptr = ecs_field_at_w_size(iter, sizeof(T), field.index,
218 static_cast<int32_t>(row));
219 }
220 }
221};
222
223// Type that handles passing components to each callbacks
224template <typename Func, typename ... Components>
225 struct each_delegate : public delegate {
226 using Terms = typename field_ptrs<Components ...>::array;
227
228 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
229 explicit each_delegate(Func&& func) noexcept
230 : func_(FLECS_MOV(func)) { }
231
232 explicit each_delegate(const Func& func) noexcept
233 : func_(func) { }
234
235 // Invoke object directly. This operation is useful when the calling
236 // function has just constructed the delegate, such as what happens when
237 // iterating a query.
238 void invoke(ecs_iter_t *iter) const {
239 field_ptrs<Components...> terms;
240
241 iter->flags |= EcsIterCppEach;
242
243 if (iter->ref_fields | iter->up_fields) {
244 terms.populate(iter);
245 invoke_unpack< each_ref_field >(iter, func_, 0, terms.fields_);
246 } else {
247 terms.populate_self(iter);
248 invoke_unpack< each_field >(iter, func_, 0, terms.fields_);
249 }
250 }
251
252 // Static function that can be used as callback for systems/triggers
253 static void run(ecs_iter_t *iter) {
254 auto self = static_cast<const each_delegate*>(iter->callback_ctx);
255 ecs_assert(self != nullptr, ECS_INTERNAL_ERROR, NULL);
256 self->invoke(iter);
257 }
258
259 // Create instance of delegate
260 static each_delegate* make(const Func& func) {
261 return FLECS_NEW(each_delegate)(func);
262 }
263
264 // Function that can be used as callback to free delegate
265 static void destruct(void *obj) {
266 _::free_obj<each_delegate>(obj);
267 }
268
269 // Static function to call for component on_add hook
270 static void run_add(ecs_iter_t *iter) {
271 component_binding_ctx *ctx = reinterpret_cast<component_binding_ctx*>(
272 iter->callback_ctx);
273 iter->callback_ctx = ctx->on_add;
274 run(iter);
275 }
276
277 // Static function to call for component on_remove hook
278 static void run_remove(ecs_iter_t *iter) {
279 component_binding_ctx *ctx = reinterpret_cast<component_binding_ctx*>(
280 iter->callback_ctx);
281 iter->callback_ctx = ctx->on_remove;
282 run(iter);
283 }
284
285 // Static function to call for component on_set hook
286 static void run_set(ecs_iter_t *iter) {
287 component_binding_ctx *ctx = reinterpret_cast<component_binding_ctx*>(
288 iter->callback_ctx);
289 iter->callback_ctx = ctx->on_set;
290 run(iter);
291 }
292
293private:
294 // func(flecs::entity, Components...)
295 template <template<typename X, typename = int> class ColumnType,
296 typename... Args,
297 typename Fn = Func,
298 decltype(std::declval<const Fn&>()(
299 std::declval<flecs::entity>(),
300 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...), 0) = 0>
301 static void invoke_callback(
302 ecs_iter_t *iter, const Func& func, size_t i, Args... comps)
303 {
304 ecs_assert(iter->entities != nullptr, ECS_INVALID_PARAMETER,
305 "query does not return entities ($this variable is not populated)");
306 func(flecs::entity(iter->world, iter->entities[i]),
307 (ColumnType< remove_reference_t<Components> >(iter, comps, i)
308 .get_row())...);
309 }
310
311 // func(flecs::iter&, size_t row, Components...)
312 template <template<typename X, typename = int> class ColumnType,
313 typename... Args,
314 typename Fn = Func,
315 decltype(std::declval<const Fn&>()(
316 std::declval<flecs::iter&>(),
317 std::declval<size_t&>(),
318 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...), 0) = 0>
319 static void invoke_callback(
320 ecs_iter_t *iter, const Func& func, size_t i, Args... comps)
321 {
322 flecs::iter it(iter);
323 func(it, i, (ColumnType< remove_reference_t<Components> >(iter, comps, i)
324 .get_row())...);
325 }
326
327 // func(Components...)
328 template <template<typename X, typename = int> class ColumnType,
329 typename... Args,
330 typename Fn = Func,
331 decltype(std::declval<const Fn&>()(
332 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...), 0) = 0>
333 static void invoke_callback(
334 ecs_iter_t *iter, const Func& func, size_t i, Args... comps)
335 {
336 func((ColumnType< remove_reference_t<Components> >(iter, comps, i)
337 .get_row())...);
338 }
339
340 template <template<typename X, typename = int> class ColumnType,
341 typename... Args, if_t<
342 sizeof...(Components) == sizeof...(Args)> = 0>
343 static void invoke_unpack(
344 ecs_iter_t *iter, const Func& func, size_t, Terms&, Args... comps)
345 {
346 ECS_TABLE_LOCK(iter->world, iter->table);
347
348 size_t count = static_cast<size_t>(iter->count);
349 if (count == 0 && !iter->table) {
350 // If query has no This terms, count can be 0. Since each does not
351 // have an entity parameter, just pass through components
352 count = 1;
353 }
354
355 for (size_t i = 0; i < count; i ++) {
356 invoke_callback<ColumnType>(iter, func, i, comps...);
357 }
358
359 ECS_TABLE_UNLOCK(iter->world, iter->table);
360 }
361
362 template <template<typename X, typename = int> class ColumnType,
363 typename... Args, if_t< sizeof...(Components) != sizeof...(Args) > = 0>
364 static void invoke_unpack(ecs_iter_t *iter, const Func& func,
365 size_t index, Terms& columns, Args... comps)
366 {
367 invoke_unpack<ColumnType>(
368 iter, func, index + 1, columns, comps..., columns[index]);
369 }
370
371public:
372 Func func_;
373};
374
375template <typename Func, typename ... Components>
376 struct find_delegate : public delegate {
377 using Terms = typename field_ptrs<Components ...>::array;
378
379 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
380 explicit find_delegate(Func&& func) noexcept
381 : func_(FLECS_MOV(func)) { }
382
383 explicit find_delegate(const Func& func) noexcept
384 : func_(func) { }
385
386 // Invoke object directly. This operation is useful when the calling
387 // function has just constructed the delegate, such as what happens when
388 // iterating a query.
389 flecs::entity invoke(ecs_iter_t *iter) const {
390 field_ptrs<Components...> terms;
391
392 iter->flags |= EcsIterCppEach;
393
394 if (iter->ref_fields | iter->up_fields) {
395 terms.populate(iter);
396 return invoke_callback< each_ref_field >(iter, func_, 0, terms.fields_);
397 } else {
398 terms.populate_self(iter);
399 return invoke_callback< each_field >(iter, func_, 0, terms.fields_);
400 }
401 }
402
403private:
404 // Number of function arguments is one more than number of components, pass
405 // entity as argument.
406 template <template<typename X, typename = int> class ColumnType,
407 typename... Args,
408 typename Fn = Func,
409 if_t<sizeof...(Components) == sizeof...(Args)> = 0,
410 decltype(bool(std::declval<const Fn&>()(
411 std::declval<flecs::entity>(),
412 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) = true>
413 static flecs::entity invoke_callback(
414 ecs_iter_t *iter, const Func& func, size_t, Terms&, Args... comps)
415 {
416 ECS_TABLE_LOCK(iter->world, iter->table);
417
418 ecs_world_t *world = iter->world;
419 size_t count = static_cast<size_t>(iter->count);
420 flecs::entity result;
421
422 for (size_t i = 0; i < count; i ++) {
423 if (func(flecs::entity(world, iter->entities[i]),
424 (ColumnType< remove_reference_t<Components> >(iter, comps, i)
425 .get_row())...))
426 {
427 result = flecs::entity(world, iter->entities[i]);
428 break;
429 }
430 }
431
432 ECS_TABLE_UNLOCK(iter->world, iter->table);
433
434 return result;
435 }
436
437 // Number of function arguments is two more than number of components, pass
438 // iter + index as argument.
439 template <template<typename X, typename = int> class ColumnType,
440 typename... Args,
441 typename Fn = Func,
442 if_t<sizeof...(Components) == sizeof...(Args)> = 0,
443 decltype(bool(std::declval<const Fn&>()(
444 std::declval<flecs::iter&>(),
445 std::declval<size_t&>(),
446 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) = true>
447 static flecs::entity invoke_callback(
448 ecs_iter_t *iter, const Func& func, size_t, Terms&, Args... comps)
449 {
450 size_t count = static_cast<size_t>(iter->count);
451 if (count == 0) {
452 // If query has no This terms, count can be 0. Since each does not
453 // have an entity parameter, just pass through components
454 count = 1;
455 }
456
457 flecs::iter it(iter);
458 flecs::entity result;
459
460 ECS_TABLE_LOCK(iter->world, iter->table);
461
462 for (size_t i = 0; i < count; i ++) {
463 if (func(it, i,
464 (ColumnType< remove_reference_t<Components> >(iter, comps, i)
465 .get_row())...))
466 {
467 result = flecs::entity(iter->world, iter->entities[i]);
468 break;
469 }
470 }
471
472 ECS_TABLE_UNLOCK(iter->world, iter->table);
473
474 return result;
475 }
476
477 // Number of function arguments is equal to number of components, no entity
478 template <template<typename X, typename = int> class ColumnType,
479 typename... Args,
480 typename Fn = Func,
481 if_t<sizeof...(Components) == sizeof...(Args)> = 0,
482 decltype(bool(std::declval<const Fn&>()(
483 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) = true>
484 static flecs::entity invoke_callback(
485 ecs_iter_t *iter, const Func& func, size_t, Terms&, Args... comps)
486 {
487 size_t count = static_cast<size_t>(iter->count);
488 if (count == 0) {
489 // If query has no This terms, count can be 0. Since each does not
490 // have an entity parameter, just pass through components
491 count = 1;
492 }
493
494 flecs::iter it(iter);
495 flecs::entity result;
496
497 ECS_TABLE_LOCK(iter->world, iter->table);
498
499 for (size_t i = 0; i < count; i ++) {
500 if (func(
501 (ColumnType< remove_reference_t<Components> >(iter, comps, i)
502 .get_row())...))
503 {
504 result = flecs::entity(iter->world, iter->entities[i]);
505 break;
506 }
507 }
508
509 ECS_TABLE_UNLOCK(iter->world, iter->table);
510
511 return result;
512 }
513
514 template <template<typename X, typename = int> class ColumnType,
515 typename... Args, if_t< sizeof...(Components) != sizeof...(Args) > = 0>
516 static flecs::entity invoke_callback(ecs_iter_t *iter, const Func& func,
517 size_t index, Terms& columns, Args... comps)
518 {
519 return invoke_callback<ColumnType>(
520 iter, func, index + 1, columns, comps..., columns[index]);
521 }
522
523 Func func_;
524};
525
529
530template <typename Func>
531 struct run_delegate : delegate {
532 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
533 explicit run_delegate(Func&& func) noexcept
534 : func_(FLECS_MOV(func)) { }
535
536 explicit run_delegate(const Func& func) noexcept
537 : func_(func) { }
538
539 // Invoke object directly. This operation is useful when the calling
540 // function has just constructed the delegate, such as what happens when
541 // iterating a query.
542 void invoke(ecs_iter_t *iter) const {
543 flecs::iter it(iter);
544 iter->flags &= ~EcsIterIsValid;
545 func_(it);
546 }
547
548 // Static function that can be used as callback for systems/triggers
549 static void run(ecs_iter_t *iter) {
550 auto self = static_cast<const run_delegate*>(iter->run_ctx);
551 ecs_assert(self != nullptr, ECS_INTERNAL_ERROR, NULL);
552 self->invoke(iter);
553 }
554
555 Func func_;
556};
557
558
562
563template <typename Func>
564 struct entity_observer_delegate : delegate {
565 explicit entity_observer_delegate(Func&& func) noexcept
566 : func_(FLECS_MOV(func)) { }
567
568 // Static function that can be used as callback for systems/triggers
569 static void run(ecs_iter_t *iter) {
570 invoke<Func>(iter);
571 }
572
573private:
574 template <typename F,
575 decltype(std::declval<const F&>()(std::declval<flecs::entity>()), 0) = 0>
576 static void invoke(ecs_iter_t *iter) {
577 auto self = static_cast<const entity_observer_delegate*>(iter->callback_ctx);
578 ecs_assert(self != nullptr, ECS_INTERNAL_ERROR, NULL);
579 self->func_(flecs::entity(iter->world, ecs_field_src(iter, 0)));
580 }
581
582 template <typename F,
583 decltype(std::declval<const F&>()(), 0) = 0>
584 static void invoke(ecs_iter_t *iter) {
585 auto self = static_cast<const entity_observer_delegate*>(iter->callback_ctx);
586 ecs_assert(self != nullptr, ECS_INTERNAL_ERROR, NULL);
587 self->func_();
588 }
589
590 Func func_;
591};
592
593template <typename Func, typename Event>
594 struct entity_payload_observer_delegate : delegate {
595 explicit entity_payload_observer_delegate(Func&& func) noexcept
596 : func_(FLECS_MOV(func)) { }
597
598 // Static function that can be used as callback for systems/triggers
599 static void run(ecs_iter_t *iter) {
600 invoke<Func>(iter);
601 }
602
603private:
604 template <typename F,
605 decltype(std::declval<const F&>()(
606 std::declval<Event&>()), 0) = 0>
607 static void invoke(ecs_iter_t *iter) {
608 auto self = static_cast<const entity_payload_observer_delegate*>(
609 iter->callback_ctx);
610 ecs_assert(self != nullptr, ECS_INTERNAL_ERROR, NULL);
611 ecs_assert(iter->param != nullptr, ECS_INVALID_OPERATION,
612 "entity observer invoked without payload");
613
614 Event *data = static_cast<Event*>(iter->param);
615 self->func_(*data);
616 }
617
618 template <typename F,
619 decltype(std::declval<const F&>()(
620 std::declval<flecs::entity>(),
621 std::declval<Event&>()), 0) = 0>
622 static void invoke(ecs_iter_t *iter) {
623 auto self = static_cast<const entity_payload_observer_delegate*>(
624 iter->callback_ctx);
625 ecs_assert(self != nullptr, ECS_INTERNAL_ERROR, NULL);
626 ecs_assert(iter->param != nullptr, ECS_INVALID_OPERATION,
627 "entity observer invoked without payload");
628
629 Event *data = static_cast<Event*>(iter->param);
630 self->func_(flecs::entity(iter->world, ecs_field_src(iter, 0)), *data);
631 }
632
633 Func func_;
634};
635
636
640
641template<typename ... Args>
642 struct entity_with_delegate_impl;
643
644template<typename ... Args>
645 struct entity_with_delegate_impl<arg_list<Args ...>> {
646 using ColumnArray = flecs::array<int32_t, sizeof...(Args)>;
647 using ArrayType = flecs::array<void*, sizeof...(Args)>;
648 using DummyArray = flecs::array<int, sizeof...(Args)>;
649 using IdArray = flecs::array<id_t, sizeof...(Args)>;
650
651 static bool const_args() {
652 static flecs::array<bool, sizeof...(Args)> is_const_args ({
653 flecs::is_const<flecs::remove_reference_t<Args>>::value...
654 });
655
656 for (auto is_const : is_const_args) {
657 if (!is_const) {
658 return false;
659 }
660 }
661 return true;
662 }
663
664 static
665 bool get_ptrs(world_t *world, flecs::entity_t e, const ecs_record_t *r, ecs_table_t *table,
666 ArrayType& ptrs)
667 {
668 ecs_assert(table != NULL, ECS_INTERNAL_ERROR, NULL);
669 if (!ecs_table_column_count(table) &&
670 !ecs_table_has_flags(table, EcsTableHasSparse))
671 {
672 return false;
673 }
674
675 /* table_index_of needs real world */
676 const flecs::world_t *real_world = ecs_get_world(world);
677
678 IdArray ids ({
679 _::type<Args>().id(world)...
680 });
681
682 /* Get column indices for components */
683 ColumnArray columns ({
684 ecs_table_get_column_index(real_world, table,
685 _::type<Args>().id(world))...
686 });
687
688 /* Get pointers for columns for entity */
689 size_t i = 0;
690 for (int32_t column : columns) {
691 if (column == -1) {
692 /* Component could be sparse */
693 void *ptr = ecs_get_mut_id(world, e, ids[i]);
694 if (!ptr) {
695 return false;
696 }
697
698 ptrs[i ++] = ptr;
699 continue;
700 }
701
702 ptrs[i ++] = ecs_record_get_by_column(r, column, 0);
703 }
704
705 return true;
706 }
707
708 static bool ensure_ptrs(world_t *world, ecs_entity_t e, ArrayType& ptrs) {
709 /* Get pointers w/ensure */
710 size_t i = 0;
711 DummyArray dummy ({
712 (ptrs[i ++] = ecs_ensure_id(world, e,
713 _::type<Args>().id(world)), 0)...
714 });
715
716 return true;
717 }
718
719 template <typename Func>
720 static bool invoke_read(world_t *world, entity_t e, const Func& func) {
721 const ecs_record_t *r = ecs_read_begin(world, e);
722 if (!r) {
723 return false;
724 }
725
726 ecs_table_t *table = r->table;
727 if (!table) {
728 return false;
729 }
730
731 ArrayType ptrs;
732 bool has_components = get_ptrs(world, e, r, table, ptrs);
733 if (has_components) {
734 invoke_callback(func, 0, ptrs);
735 }
736
737 ecs_read_end(r);
738
739 return has_components;
740 }
741
742 template <typename Func>
743 static bool invoke_write(world_t *world, entity_t e, const Func& func) {
744 ecs_record_t *r = ecs_write_begin(world, e);
745 if (!r) {
746 return false;
747 }
748
749 ecs_table_t *table = r->table;
750 if (!table) {
751 return false;
752 }
753
754 ArrayType ptrs;
755 bool has_components = get_ptrs(world, e, r, table, ptrs);
756 if (has_components) {
757 invoke_callback(func, 0, ptrs);
758 }
759
760 ecs_write_end(r);
761
762 return has_components;
763 }
764
765 template <typename Func>
766 static bool invoke_get(world_t *world, entity_t e, const Func& func) {
767 if (const_args()) {
768 return invoke_read(world, e, func);
769 } else {
770 return invoke_write(world, e, func);
771 }
772 }
773
774 // Utility for storing id in array in pack expansion
775 static size_t store_added(IdArray& added, size_t elem, ecs_table_t *prev,
776 ecs_table_t *next, id_t id)
777 {
778 // Array should only contain ids for components that are actually added,
779 // so check if the prev and next tables are different.
780 if (prev != next) {
781 added[elem] = id;
782 elem ++;
783 }
784 return elem;
785 }
786
787 template <typename Func>
788 static bool invoke_ensure(world_t *world, entity_t id, const Func& func) {
789 flecs::world w(world);
790
791 ArrayType ptrs;
792 ecs_table_t *table = NULL;
793
794 // When not deferred take the fast path.
795 if (!w.is_deferred()) {
796 // Bit of low level code so we only do at most one table move & one
797 // entity lookup for the entire operation.
798
799 // Make sure the object is not a stage. Operations on a stage are
800 // only allowed when the stage is in deferred mode, which is when
801 // the world is in readonly mode.
802 ecs_assert(!w.is_stage(), ECS_INVALID_PARAMETER, NULL);
803
804 // Find table for entity
805 ecs_record_t *r = ecs_record_find(world, id);
806 if (r) {
807 table = r->table;
808 }
809
810 // Find destination table that has all components
811 ecs_table_t *prev = table, *next;
812 size_t elem = 0;
813 IdArray added;
814
815 // Iterate components, only store added component ids in added array
816 DummyArray dummy_before ({ (
817 next = ecs_table_add_id(world, prev, w.id<Args>()),
818 elem = store_added(added, elem, prev, next, w.id<Args>()),
819 prev = next, 0
820 )... });
821
822 (void)dummy_before;
823
824 // If table is different, move entity straight to it
825 if (table != next) {
826 ecs_type_t ids;
827 ids.array = added.ptr();
828 ids.count = static_cast<ecs_size_t>(elem);
829 ecs_commit(world, id, r, next, &ids, NULL);
830 table = next;
831 }
832
833 if (!get_ptrs(w, id, r, table, ptrs)) {
834 ecs_abort(ECS_INTERNAL_ERROR, NULL);
835 }
836
837 ECS_TABLE_LOCK(world, table);
838
839 // When deferred, obtain pointers with regular ensure
840 } else {
841 ensure_ptrs(world, id, ptrs);
842 }
843
844 invoke_callback(func, 0, ptrs);
845
846 if (!w.is_deferred()) {
847 ECS_TABLE_UNLOCK(world, table);
848 }
849
850 // Call modified on each component
851 DummyArray dummy_after ({
852 ( ecs_modified_id(world, id, w.id<Args>()), 0)...
853 });
854 (void)dummy_after;
855
856 return true;
857 }
858
859private:
860 template <typename Func, typename ... TArgs,
861 if_t<sizeof...(TArgs) == sizeof...(Args)> = 0>
862 static void invoke_callback(
863 const Func& f, size_t, ArrayType&, TArgs&& ... comps)
864 {
865 f(*static_cast<typename base_arg_type<Args>::type*>(comps)...);
866 }
867
868 template <typename Func, typename ... TArgs,
869 if_t<sizeof...(TArgs) != sizeof...(Args)> = 0>
870 static void invoke_callback(const Func& f, size_t arg, ArrayType& ptrs,
871 TArgs&& ... comps)
872 {
873 invoke_callback(f, arg + 1, ptrs, comps..., ptrs[arg]);
874 }
875};
876
877template <typename Func, typename U = int>
878 struct entity_with_delegate {
879 static_assert(function_traits<Func>::value, "type is not callable");
880};
881
882template <typename Func>
883 struct entity_with_delegate<Func, if_t< is_callable<Func>::value > >
884 : entity_with_delegate_impl< arg_list_t<Func> >
885{
886 static_assert(function_traits<Func>::arity > 0,
887 "function must have at least one argument");
888};
889
890} // namespace _
891
892// Experimental: allows using the each delegate for use cases outside of flecs
893template <typename Func, typename ... Args>
894using delegate = _::each_delegate<typename std::decay<Func>::type, Args...>;
895
896} // namespace flecs
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:368
ecs_abort
#define ecs_abort(error_code,...)
Abort.
Definition log.h:359
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_record_t
struct ecs_record_t ecs_record_t
Information about an entity, like its table and row.
Definition flecs.h:490
ecs_table_t
struct ecs_table_t ecs_table_t
A table stores entities and components for a specific type.
Definition flecs.h:431
ecs_ctx_free_t
void(* ecs_ctx_free_t)(void *ctx)
Function to cleanup context data.
Definition flecs.h:625
ecs_get_mut_id
void * ecs_get_mut_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Get a mutable pointer to a component.
ecs_ensure_id
void * ecs_ensure_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Get a mutable pointer to a component.
ecs_modified_id
void ecs_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Signal that a component has been modified.
ecs_field_src
ecs_entity_t ecs_field_src(const ecs_iter_t *it, int8_t index)
Return field source.
ecs_field_at_w_size
void * ecs_field_at_w_size(const ecs_iter_t *it, size_t size, int8_t index, int32_t row)
Get data for field at specified row.
ecs_field_w_size
void * ecs_field_w_size(const ecs_iter_t *it, size_t size, int8_t index)
Get data for field.
ecs_table_add_id
ecs_table_t * ecs_table_add_id(ecs_world_t *world, ecs_table_t *table, ecs_id_t id)
Get table that has all components of current table plus the specified id.
ecs_table_has_flags
bool ecs_table_has_flags(ecs_table_t *table, ecs_flags32_t flags)
Test table for flags.
ecs_table_column_count
int32_t ecs_table_column_count(const ecs_table_t *table)
Return number of columns in table.
ecs_commit
bool ecs_commit(ecs_world_t *world, ecs_entity_t entity, ecs_record_t *record, ecs_table_t *table, const ecs_type_t *added, const ecs_type_t *removed)
Commit (move) entity to a table.
ecs_table_get_column_index
int32_t ecs_table_get_column_index(const ecs_world_t *world, const ecs_table_t *table, ecs_id_t id)
Get column index for id.
ecs_get_world
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
flecs::_
Int to enum.
Definition component.hpp:21
ecs_iter_t
Iterator.
Definition flecs.h:1136
ecs_type_t
A type is a list of (component) ids.
Definition flecs.h:398
ecs_type_t::array
ecs_id_t * array
Array with ids.
Definition flecs.h:399
ecs_type_t::count
int32_t count
Number of elements in array.
Definition flecs.h:400
flecs::_::arg_list
Definition function_traits.hpp:12
flecs::_::component_binding_ctx
Definition delegate.hpp:17
flecs::_::delegate
Definition delegate.hpp:115
flecs::_::each_delegate
Definition delegate.hpp:225
flecs::_::each_field
Definition delegate.hpp:120
flecs::_::entity_with_delegate_impl
Definition delegate.hpp:642
flecs::_::entity_with_delegate
Definition delegate.hpp:878
flecs::_::field_ptr
Definition delegate.hpp:39
flecs::_::field_ptrs
Definition delegate.hpp:47
flecs::_::function_traits
Definition function_traits.hpp:94
flecs::_::type
Definition flecs.hpp:39
flecs::array
Definition array.hpp:41
flecs::entity
Entity.
Definition entity.hpp:30
flecs::field
Wrapper class around a field.
Definition field.hpp:61
flecs::id
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::is_actual
Definition pair.hpp:138
flecs::is_callable
Definition function_traits.hpp:100
flecs::iter
Class for iterating over query results.
Definition iter.hpp:68
flecs::iter::param
void * param()
Access param.
Definition iter.hpp:142
flecs::iter::entities
flecs::field< const flecs::entity_t > entities() const
Get readonly access to entity ids.
Definition iter.hpp:333
flecs::table
Definition table.hpp:18
flecs::world
The world.
Definition world.hpp:137
flecs::world::is_stage
bool is_stage() const
Test if is a stage.
Definition world.hpp:421
flecs::world::id
flecs::id id() const
Get id from a type.
Definition impl.hpp:70
flecs::world::is_deferred
bool is_deferred() const
Test whether deferring is enabled.
Definition world.hpp:370