Skip to content
Flecs v4.1
impl.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/mixins/observer/impl.hpp
3 * @brief Observer implementation.
4 */
5
6#pragma once
7
8#include "builder.hpp"
9
10namespace flecs
11{
12
13/** Observer.
14 *
15 * @ingroup cpp_observers
16 */
17struct observer final : entity
18{
19 using entity::entity;
20
21 /** Default constructor. */
22 explicit observer() : entity() { }
23
24 /** Construct from a world and an observer descriptor. */
26 world_ = world;
28 }
29
30 /** Set the observer context. */
31 observer& ctx(void *ctx) {
32 ecs_observer_desc_t desc = {};
33 desc.ctx = ctx;
35 return *this;
36 }
37
38 /** Get the observer context. */
39 void* ctx() const {
41 }
42
43 /** Replace the observer's run callback. */
44 template <typename Func>
45 observer& run(Func&& func) {
46 using Delegate = typename _::run_delegate<
47 typename std::decay<Func>::type>;
48 ecs_observer_desc_t desc = {};
49 _::set_callback<Delegate, Delegate::run, true>(desc, FLECS_FWD(func));
51 return *this;
52 }
53
54 /** Replace the observer's each callback. */
55 template <typename Func>
56 observer& each(Func&& func) {
57 using CallbackComponents =
59 ecs_observer_desc_t desc = {};
60 _::set_each_callback<false>(desc, FLECS_FWD(func), CallbackComponents{});
62 return *this;
63 }
64
65 /** Replace the observer's run callback and use an each callback for
66 * iteration. */
67 template <typename Func>
68 observer& run_each(Func&& func) {
69 using CallbackComponents =
71 ecs_observer_desc_t desc = {};
72 _::set_each_callback<true>(desc, FLECS_FWD(func), CallbackComponents{});
74 return *this;
75 }
76
77 /** Get the query for this observer. */
80 }
81
82};
83
84/** Mixin implementation. */
86 return flecs::observer(world_, e);
87}
88
89template <typename... Comps, typename... Args>
90inline observer_builder<Comps...> world::observer(Args &&... args) const {
91 return flecs::observer_builder<Comps...>(world_, FLECS_FWD(args)...);
92}
93
94} // namespace flecs
ecs_world_t world_t
World type.
Definition c_types.hpp:18
flecs::observer observer(flecs::entity e) const
Observer world mixin.
Definition impl.hpp:85
ecs_entity_t ecs_observer_update(ecs_world_t *world, ecs_entity_t observer, const ecs_observer_desc_t *desc)
Update an existing observer.
ecs_entity_t ecs_observer_init(ecs_world_t *world, const ecs_observer_desc_t *desc)
Create an observer.
const ecs_observer_t * ecs_observer_get(const ecs_world_t *world, ecs_entity_t observer)
Get the observer object.
Observer builder.
Used with ecs_observer_init().
Definition flecs.h:1399
void * ctx
User context to pass to callback.
Definition flecs.h:1433
void * ctx
Observer context.
Definition flecs.h:915
Extract the component argument list from an each-callback signature.
Definition delegate.hpp:470
Entity.
Definition entity.hpp:30
entity()
Default constructor.
Definition entity.hpp:32
flecs::id_t id_
The raw ID value.
Definition decl.hpp:154
flecs::world_t * world_
World is optional, but guarantees that entity identifiers extracted from the ID are valid.
Definition decl.hpp:152
Observer builder.
Definition builder.hpp:24
Observer.
Definition impl.hpp:18
observer(flecs::world_t *world, const ecs_observer_desc_t *desc)
Construct from a world and an observer descriptor.
Definition impl.hpp:25
void * ctx() const
Get the observer context.
Definition impl.hpp:39
observer()
Default constructor.
Definition impl.hpp:22
observer & run_each(Func &&func)
Replace the observer's run callback and use an each callback for iteration.
Definition impl.hpp:68
observer & ctx(void *ctx)
Set the observer context.
Definition impl.hpp:31
observer & run(Func &&func)
Replace the observer's run callback.
Definition impl.hpp:45
entity()
Default constructor.
Definition entity.hpp:32
observer & each(Func &&func)
Replace the observer's each callback.
Definition impl.hpp:56
flecs::query query() const
Get the query for this observer.
Definition impl.hpp:78
Typed query.
Definition impl.hpp:266
Type class.
Definition type.hpp:21
The world.
Definition world.hpp:129
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1009