Flecs v3.2
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(m_world, evt);
17}
18
19template <typename E>
21 return flecs::event_builder_typed<E>(m_world, _::cpp_type<E>().id(m_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 *binding_ctx,
31 ecs_ctx_free_t binding_ctx_free)
32 {
33 ecs_observer_desc_t desc = {};
34 desc.events[0] = event;
35 desc.filter.terms[0].id = EcsAny;
37 desc.callback = callback;
38 desc.binding_ctx = binding_ctx;
39 desc.binding_ctx_free = binding_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, _::cpp_type<Evt>::id(world), entity, Delegate::run, ctx,
56 reinterpret_cast<ecs_ctx_free_t>(_::free_obj<Delegate>));
57 }
58
59 template <typename Evt, if_not_t<is_empty<Evt>::value> = 0>
60 static void create(
61 flecs::world_t *world,
62 flecs::entity_t entity,
63 Func&& f)
64 {
66 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));
67 entity_observer_create(world, _::cpp_type<Evt>::id(world), entity, Delegate::run, ctx,
68 reinterpret_cast<ecs_ctx_free_t>(_::free_obj<Delegate>));
69 }
70 };
71}
72
73template <typename Self>
74template <typename Func>
75inline Self& entity_builder<Self>::observe(flecs::entity_t evt, Func&& f) {
76 using Delegate = _::entity_observer_delegate<Func>;
77 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));
78
79 _::entity_observer_create(m_world, evt, m_id, Delegate::run, ctx,
80 reinterpret_cast<ecs_ctx_free_t>(_::free_obj<Delegate>));
81
82 return to_base();
83}
84
85template <typename Self>
86template <typename Evt, typename Func>
87inline Self& entity_builder<Self>::observe(Func&& f) {
89 m_world, m_id, FLECS_FWD(f));
90 return to_base();
91}
92
93template <typename Self>
94template <typename Func>
95inline Self& entity_builder<Self>::observe(Func&& f) {
96 return this->observe<_::event_from_func_t<Func>>(FLECS_FWD(f));
97}
98
100 this->emit(evt.id());
101}
102
104 this->enqueue(evt.id());
105}
106
107} // 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:540
void(* ecs_ctx_free_t)(void *ctx)
Function to cleanup context data.
Definition flecs.h:626
ecs_entity_t ecs_observer_init(ecs_world_t *world, const ecs_observer_desc_t *desc)
Create observer.
Event builder.
ecs_term_t terms[(16)]
Terms of the filter.
Definition flecs.h:1003
Used with ecs_observer_init().
Definition flecs.h:1104
void * binding_ctx
Context to be used for language bindings.
Definition flecs.h:1136
ecs_filter_desc_t filter
Filter for observer.
Definition flecs.h:1111
ecs_entity_t events[(8)]
Events to observe (OnAdd, OnRemove, OnSet, UnSet)
Definition flecs.h:1114
ecs_ctx_free_t binding_ctx_free
Callback to free binding_ctx.
Definition flecs.h:1142
ecs_iter_action_t callback
Callback to invoke on an event, invoked when the observer matches.
Definition flecs.h:1122
ecs_entity_t id
Entity id.
Definition flecs.h:738
ecs_id_t id
Component id to be matched by term.
Definition flecs.h:759
ecs_term_id_t src
Source of term.
Definition flecs.h:764
Self & observe(flecs::entity_t evt, Func &&callback)
Observe event on entity.
Definition impl.hpp:75
void emit()
Emit event for entity.
entity_t id() const
Get entity id.
void enqueue()
Enqueue event for entity.
Entity.
Definition entity.hpp:30
The world.
Definition world.hpp:132