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,
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, _::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 const Self& entity_builder<Self>::observe(flecs::entity_t evt, Func&& f) const {
76 using Delegate = _::entity_observer_delegate<Func>;
77 auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));
78
79 _::entity_observer_create(world_, evt, 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 const Self& entity_builder<Self>::observe(Func&& f) const {
89 world_, id_, FLECS_FWD(f));
90 return to_base();
91}
92
93template <typename Self>
94template <typename Func>
95inline const Self& entity_builder<Self>::observe(Func&& f) const {
96 return this->observe<_::event_from_func_t<Func>>(FLECS_FWD(f));
97}
98
99inline void entity_view::emit(flecs::entity evt) const {
100 this->emit(evt.id());
101}
102
103inline void entity_view::enqueue(flecs::entity evt) const {
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:515
void(* ecs_ctx_free_t)(void *ctx)
Function to cleanup context data.
Definition flecs.h:584
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:1212
ecs_entity_t events[(8)]
Events to observe (OnAdd, OnRemove, OnSet)
Definition flecs.h:1223
void * callback_ctx
Context associated with callback (for language bindings).
Definition flecs.h:1247
ecs_query_desc_t query
Query for observer.
Definition flecs.h:1220
ecs_ctx_free_t callback_ctx_free
Callback to free callback ctx.
Definition flecs.h:1250
ecs_iter_action_t callback
Callback to invoke on an event, invoked when the observer matches.
Definition flecs.h:1230
ecs_term_t terms[32]
Query terms.
Definition flecs.h:1143
ecs_entity_t id
Entity id.
Definition flecs.h:722
ecs_term_ref_t src
Source of term.
Definition flecs.h:742
ecs_id_t id
Component id to be matched by term.
Definition flecs.h:737
const Self & observe(flecs::entity_t evt, Func &&callback) const
Observe event on entity.
Definition impl.hpp:75
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