Flecs v4.0
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
13// Mixin implementation
14
15inline flecs::event_builder world::event(flecs::entity_t evt) const {
16 return flecs::event_builder(world_, evt);
17}
18
19template <typename E>
21 return flecs::event_builder_typed<E>(world_, _::type<E>().id(world_));
22}
23
24namespace _ {
25 inline void entity_observer_create(
26 flecs::world_t *world,
27 flecs::entity_t event,
28 flecs::entity_t entity,
29 ecs_iter_action_t callback,
30 void *callback_ctx,
31 ecs_ctx_free_t callback_ctx_free)
32 {
33 ecs_observer_desc_t desc = {};
34 desc.events[0] = event;
35 desc.query.terms[0].id = EcsAny;
36 desc.query.terms[0].src.id = entity;
37 desc.callback = callback;
38 desc.callback_ctx = callback_ctx;
39 desc.callback_ctx_free = callback_ctx_free;
40
41 flecs::entity_t o = ecs_observer_init(world, &desc);
42 ecs_add_pair(world, o, EcsChildOf, entity);
43 }
44
45 template <typename Func>
47 template <typename Evt, if_t<is_empty<Evt>::value> = 0>
48 static void create(
49 flecs::world_t *world,
50 flecs::entity_t entity,
51 Func&& f)
52 {
53 using Delegate = _::entity_observer_delegate<Func>;
54 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));
55 entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx, _::free_obj<Delegate>);
56 }
57
58 template <typename Evt, if_not_t<is_empty<Evt>::value> = 0>
59 static void create(
60 flecs::world_t *world,
61 flecs::entity_t entity,
62 Func&& f)
63 {
65 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));
66 entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx, _::free_obj<Delegate>);
67 }
68 };
69}
70
71template <typename Self>
72template <typename Func>
73inline const Self& entity_builder<Self>::observe(flecs::entity_t evt, Func&& f) const {
74 using Delegate = _::entity_observer_delegate<Func>;
75 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));
76
77 _::entity_observer_create(world_, evt, id_, Delegate::run, ctx, _::free_obj<Delegate>);
78
79 return to_base();
80}
81
82template <typename Self>
83template <typename Evt, typename Func>
84inline const Self& entity_builder<Self>::observe(Func&& f) const {
86 world_, id_, FLECS_FWD(f));
87 return to_base();
88}
89
90template <typename Self>
91template <typename Func>
92inline const Self& entity_builder<Self>::observe(Func&& f) const {
93 return this->observe<_::event_from_func_t<Func>>(FLECS_FWD(f));
94}
95
96inline void entity_view::emit(flecs::entity evt) const {
97 this->emit(evt.id());
98}
99
100inline void entity_view::enqueue(flecs::entity evt) const {
101 this->enqueue(evt.id());
102}
103
104} // namespace flecs
const ecs_entity_t EcsChildOf
Used to express parent-child relationships.
const ecs_entity_t EcsAny
Any entity ("_").
flecs::event_builder_typed< E > event() const
Create a new event.
void(* ecs_iter_action_t)(ecs_iter_t *it)
Function prototype for iterables.
Definition flecs.h:547
void(* ecs_ctx_free_t)(void *ctx)
Function to cleanup context data.
Definition flecs.h:616
ecs_entity_t ecs_observer_init(ecs_world_t *world, const ecs_observer_desc_t *desc)
Create observer.
Event builder.
Used with ecs_observer_init().
Definition flecs.h:1236
ecs_entity_t events[(8)]
Events to observe (OnAdd, OnRemove, OnSet)
Definition flecs.h:1247
void * callback_ctx
Context associated with callback (for language bindings).
Definition flecs.h:1271
ecs_query_desc_t query
Query for observer.
Definition flecs.h:1244
ecs_ctx_free_t callback_ctx_free
Callback to free callback ctx.
Definition flecs.h:1274
ecs_iter_action_t callback
Callback to invoke on an event, invoked when the observer matches.
Definition flecs.h:1254
ecs_term_t terms[32]
Query terms.
Definition flecs.h:1167
ecs_entity_t id
Entity id.
Definition flecs.h:754
ecs_term_ref_t src
Source of term.
Definition flecs.h:774
ecs_id_t id
Component id to be matched by term.
Definition flecs.h:769
const Self & observe(flecs::entity_t evt, Func &&callback) const
Observe event on entity.
Definition impl.hpp:73
void enqueue() const
Enqueue event for entity.
void emit() const
Emit event for entity.
entity_t id() const
Get entity id.
Entity.
Definition entity.hpp:30
The world.
Definition world.hpp:137