Skip to content
Flecs v4.1
builder_i.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/mixins/observer/builder_i.hpp
3 * @brief Observer builder interface.
4 */
5
6#pragma once
7
9
10namespace flecs {
11
12/** Observer builder interface.
13 *
14 * @ingroup cpp_observers
15 */
16template<typename Base, typename ... Components>
17struct observer_builder_i : query_builder_i<Base, Components ...> {
18 using BaseClass = query_builder_i<Base, Components ...>;
19 /** Default constructor. */
21 : BaseClass(nullptr)
22 , desc_(nullptr)
23 , event_count_(0) { }
24
25 /** Construct from an observer descriptor. */
27 : BaseClass(&desc->query, term_index)
28 , desc_(desc)
29 , event_count_(0) { }
30
31 /** Specify the event(s) for when the observer should run.
32 * @param evt The event.
33 */
34 Base& event(entity_t evt) {
35 desc_->events[event_count_ ++] = evt;
36 return *this;
37 }
38
39 /** Specify the event(s) for when the observer should run.
40 * @tparam E The event.
41 */
42 template <typename E>
43 Base& event() {
44 desc_->events[event_count_ ++] = _::type<E>().id(world_v());
45 return *this;
46 }
47
48 /** Invoke the observer for anything that matches its query on creation. */
49 Base& yield_existing(bool value = true) {
50 desc_->yield_existing = value;
51 return *this;
52 }
53
54 /** Set the observer flags. */
55 Base& observer_flags(ecs_flags32_t flags) {
56 desc_->flags_ |= flags;
57 return *this;
58 }
59
60 /** Set the observer context. */
61 Base& ctx(void *ptr) {
62 desc_->ctx = ptr;
63 return *this;
64 }
65
66 /** Set the observer run callback. */
67 Base& run(ecs_iter_action_t action) {
68 desc_->run = action;
69 return *this;
70 }
71
72protected:
73 virtual flecs::world_t* world_v() override = 0;
74
75private:
76 operator Base&() {
77 return *static_cast<Base*>(this);
78 }
79
81 int32_t event_count_;
82};
83
84}
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
ecs_world_t world_t
World type.
Definition c_types.hpp:18
void(*) ecs_iter_action_t(ecs_iter_t *it)
Function prototype for iterables.
Definition flecs.h:591
Query builder interface.
Used with ecs_observer_init().
Definition flecs.h:1399
Base & event()
Specify the event(s) for when the observer should run.
Definition builder_i.hpp:43
observer_builder_i()
Default constructor.
Definition builder_i.hpp:20
Base & ctx(void *ptr)
Set the observer context.
Definition builder_i.hpp:61
Base & yield_existing(bool value=true)
Invoke the observer for anything that matches its query on creation.
Definition builder_i.hpp:49
observer_builder_i(ecs_observer_desc_t *desc, int32_t term_index=0)
Construct from an observer descriptor.
Definition builder_i.hpp:26
Base & run(ecs_iter_action_t action)
Set the observer run callback.
Definition builder_i.hpp:67
Base & event(entity_t evt)
Specify the event(s) for when the observer should run.
Definition builder_i.hpp:34
Base & observer_flags(ecs_flags32_t flags)
Set the observer flags.
Definition builder_i.hpp:55
query_builder_i(ecs_query_desc_t *desc, int32_t term_index=0)
Definition builder_i.hpp:20
Typed query.
Definition impl.hpp:266
Base & desc()
Use with cascade() to iterate results in descending (bottom-to-top) order.
Base & flags(flecs::flags64_t flags)
Override the term ID flags.
Definition builder_i.hpp:74