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
6#pragma once
7
8#include "builder.hpp"
9
10namespace flecs
11{
12
17struct observer final : entity
18{
19 using entity::entity;
20
22 explicit observer() : entity() { }
23
29
31 observer& ctx(void *ctx) {
32 ecs_observer_desc_t desc = {};
33 desc.ctx = ctx;
35 return *this;
36 }
37
39 void* ctx() const {
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>;
54 return *this;
55 }
56
58 template <typename Func>
59 observer& each(Func&& func) {
60 using CallbackComponents =
62 return each_callback(CallbackComponents{}, FLECS_FWD(func));
63 }
64
67 template <typename Func>
68 observer& run_each(Func&& func) {
69 using CallbackComponents =
71 return run_each_callback(CallbackComponents{}, FLECS_FWD(func));
72 }
73
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>;
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>;
103 return *this;
104 }
105};
106
108inline 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
ecs_world_t world_t
World type.
Definition c_types.hpp:18
flecs::observer observer(flecs::entity e) const
Observer world mixin.
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:1374
ecs_ctx_free_t run_ctx_free
Callback to free run ctx.
Definition flecs.h:1423
void * run_ctx
Context associated with run (for language bindings).
Definition flecs.h:1420
void * callback_ctx
Context associated with callback (for language bindings).
Definition flecs.h:1414
void * ctx
User context to pass to callback.
Definition flecs.h:1408
ecs_ctx_free_t callback_ctx_free
Callback to free callback ctx.
Definition flecs.h:1417
ecs_iter_action_t callback
Callback to invoke on an event, invoked when the observer matches.
Definition flecs.h:1397
ecs_run_action_t run
Callback invoked on an event.
Definition flecs.h:1405
void * ctx
Observer context.
Definition flecs.h:891
Extract the component argument list from an each-callback signature.
Definition delegate.hpp:932
flecs::type type() const
Get the entity's type.
Definition impl.hpp:94
Entity.
Definition entity.hpp:30
entity()
Default constructor.
Definition entity.hpp:32
flecs::world world() const
Get the world.
Definition impl.hpp:58
flecs::id_t id_
The raw ID value.
Definition decl.hpp:149
flecs::world_t * world_
World is optional, but guarantees that entity identifiers extracted from the ID are valid.
Definition decl.hpp:147
Observer builder.
Definition builder.hpp:24
Observer.
Definition impl.hpp:18
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(flecs::world_t *world, ecs_observer_desc_t *desc)
Construct from a world and an observer descriptor.
Definition impl.hpp:25
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
observer & each(Func &&func)
Replace the observer's each callback.
Definition impl.hpp:59
flecs::query query() const
Get the query for this observer.
Definition impl.hpp:75
Typed query.
Definition impl.hpp:262
The world.
Definition world.hpp:246
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1540