Flecs: include/flecs/addons/cpp/mixins/observer/impl.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
impl.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8#include "builder.hpp"
9
10namespace flecs
11{
12
17 struct observer final : entity
18{
19 using entity::entity;
20
22 explicit observer() : entity() { }
23
25 observer(flecs::world_t *world, ecs_observer_desc_t *desc) {
26 world_ = world;
27 id_ = ecs_observer_init(world, desc);
28 }
29
31 observer& ctx(void *ctx) {
32 ecs_observer_desc_t desc = {};
33 desc.ctx = ctx;
34 ecs_observer_update(world_, id_, &desc);
35 return *this;
36 }
37
39 void* ctx() const {
40 return ecs_observer_get(world_, id_)->ctx;
41 }
42
44 template <typename Func>
45 observer& run(Func&& func) {
46 using Delegate = typename _::run_delegate<
47 typename std::decay<Func>::type>;
48 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
49 ecs_observer_desc_t desc = {};
50 desc.run = Delegate::run;
51 desc.run_ctx = ctx;
52 desc.run_ctx_free = _::free_obj<Delegate>;
53 ecs_observer_update(world_, id_, &desc);
54 return *this;
55 }
56
58 template <typename Func>
59 observer& each(Func&& func) {
60 using CallbackComponents =
61 typename _::each_callback_args<arg_list_t<Func>>::type;
62 return each_callback(CallbackComponents{}, FLECS_FWD(func));
63 }
64
67 template <typename Func>
68 observer& run_each(Func&& func) {
69 using CallbackComponents =
70 typename _::each_callback_args<arg_list_t<Func>>::type;
71 return run_each_callback(CallbackComponents{}, FLECS_FWD(func));
72 }
73
75 flecs::query<> query() const {
76 return flecs::query<>(ecs_observer_get(world_, id_)->query);
77 }
78
79private:
80 template <typename ... CallbackComponents, typename Func>
81 observer& each_callback(_::arg_list<CallbackComponents...>, Func&& func) {
82 using Delegate = typename _::each_delegate<
83 typename std::decay<Func>::type, CallbackComponents...>;
84 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
85 ecs_observer_desc_t desc = {};
86 desc.callback = Delegate::run;
87 desc.callback_ctx = ctx;
88 desc.callback_ctx_free = _::free_obj<Delegate>;
89 ecs_observer_update(world_, id_, &desc);
90 return *this;
91 }
92
93 template <typename ... CallbackComponents, typename Func>
94 observer& run_each_callback(_::arg_list<CallbackComponents...>, Func&& func) {
95 using Delegate = typename _::each_delegate<
96 typename std::decay<Func>::type, CallbackComponents...>;
97 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
98 ecs_observer_desc_t desc = {};
99 desc.run = Delegate::run_each;
100 desc.run_ctx = ctx;
101 desc.run_ctx_free = _::free_obj<Delegate>;
102 ecs_observer_update(world_, id_, &desc);
103 return *this;
104 }
105};
106
108 inline observer world::observer(flecs::entity e) const {
109 return flecs::observer(world_, e);
110}
111
112template <typename... Comps, typename... Args>
113inline observer_builder<Comps...> world::observer(Args &&... args) const {
114 return flecs::observer_builder<Comps...>(world_, FLECS_FWD(args)...);
115}
116
117} // namespace flecs
flecs::world_t
ecs_world_t world_t
World type.
Definition c_types.hpp:18
flecs::world::observer
flecs::observer observer(flecs::entity e) const
Observer world mixin.
Definition impl.hpp:108
ecs_observer_update
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_observer_init
ecs_entity_t ecs_observer_init(ecs_world_t *world, const ecs_observer_desc_t *desc)
Create an observer.
ecs_observer_get
const ecs_observer_t * ecs_observer_get(const ecs_world_t *world, ecs_entity_t observer)
Get the observer object.
ecs_observer_desc_t
struct ecs_observer_desc_t ecs_observer_desc_t
Used with ecs_observer_init().
builder.hpp
Observer builder.
ecs_observer_desc_t
Used with ecs_observer_init().
Definition flecs.h:1375
ecs_observer_desc_t::run_ctx_free
ecs_ctx_free_t run_ctx_free
Callback to free run ctx.
Definition flecs.h:1424
ecs_observer_desc_t::run_ctx
void * run_ctx
Context associated with run (for language bindings).
Definition flecs.h:1421
ecs_observer_desc_t::callback_ctx
void * callback_ctx
Context associated with callback (for language bindings).
Definition flecs.h:1415
ecs_observer_desc_t::ctx
void * ctx
User context to pass to callback.
Definition flecs.h:1409
ecs_observer_desc_t::callback_ctx_free
ecs_ctx_free_t callback_ctx_free
Callback to free callback ctx.
Definition flecs.h:1418
ecs_observer_desc_t::callback
ecs_iter_action_t callback
Callback to invoke on an event, invoked when the observer matches.
Definition flecs.h:1398
ecs_observer_desc_t::run
ecs_run_action_t run
Callback invoked on an event.
Definition flecs.h:1406
ecs_observer_t::ctx
void * ctx
Observer context.
Definition flecs.h:891
flecs::_::each_callback_args
Extract the component argument list from an each-callback signature.
Definition delegate.hpp:932
flecs::_::each_delegate
Definition delegate.hpp:220
flecs::_::run_delegate
Definition delegate.hpp:544
flecs::entity
Entity.
Definition entity.hpp:30
flecs::entity::entity
entity()
Default constructor.
Definition entity.hpp:32
flecs::id::id_
flecs::id_t id_
The raw ID value.
Definition decl.hpp:149
flecs::id::world_
flecs::world_t * world_
World is optional, but guarantees that entity identifiers extracted from the ID are valid.
Definition decl.hpp:147
flecs::observer_builder
Observer builder.
Definition builder.hpp:24
flecs::observer
Observer.
Definition impl.hpp:18
flecs::observer::ctx
void * ctx() const
Get the observer context.
Definition impl.hpp:39
flecs::observer::observer
observer()
Default constructor.
Definition impl.hpp:22
flecs::observer::run_each
observer & run_each(Func &&func)
Replace the observer's run callback and use an each callback for iteration.
Definition impl.hpp:68
flecs::observer::observer
observer(flecs::world_t *world, ecs_observer_desc_t *desc)
Construct from a world and an observer descriptor.
Definition impl.hpp:25
flecs::observer::ctx
observer & ctx(void *ctx)
Set the observer context.
Definition impl.hpp:31
flecs::observer::run
observer & run(Func &&func)
Replace the observer's run callback.
Definition impl.hpp:45
flecs::observer::entity
entity()
Default constructor.
Definition entity.hpp:32
flecs::observer::each
observer & each(Func &&func)
Replace the observer's each callback.
Definition impl.hpp:59
flecs::observer::query
flecs::query query() const
Get the query for this observer.
Definition impl.hpp:75
flecs::query
Typed query.
Definition impl.hpp:262
flecs::type
Type class.
Definition type.hpp:21
flecs::world
The world.
Definition world.hpp:246
flecs::world::world_
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1540