Flecs: include/flecs/addons/cpp/mixins/event/impl.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| 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
5
6#pragma once
7
8#include "builder.hpp"
9
10namespace flecs
11{
12
13// Mixin implementation
14
15 inline flecs::event_builder world::event(flecs::entity_t evt) const {
16 return flecs::event_builder(world_, evt);
17}
18
19template <typename E>
20 inline flecs::event_builder_typed<E> world::event() const {
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>
46 struct entity_observer_factory {
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 {
64 using Delegate = _::entity_payload_observer_delegate<Func, Evt>;
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>
73 inline 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>
84 inline const Self& entity_builder<Self>::observe(Func&& f) const {
85 _::entity_observer_factory<Func>::template create<Evt>(
86 world_, id_, FLECS_FWD(f));
87 return to_base();
88}
89
90template <typename Self>
91template <typename Func>
92 inline const Self& entity_builder<Self>::observe(Func&& f) const {
93 return this->observe<_::event_from_func_t<Func>>(FLECS_FWD(f));
94}
95
96 inline void entity_view::emit(flecs::entity evt) const {
97 this->emit(evt.id());
98}
99
100 inline void entity_view::enqueue(flecs::entity evt) const {
101 this->enqueue(evt.id());
102}
103
104} // namespace flecs
EcsChildOf
const ecs_entity_t EcsChildOf
Used to express parent-child relationships.
EcsAny
const ecs_entity_t EcsAny
Any entity ("_").
flecs::world::event
flecs::event_builder_typed< E > event() const
Create a new event.
Definition impl.hpp:20
ecs_iter_action_t
void(* ecs_iter_action_t)(ecs_iter_t *it)
Function prototype for iterables.
Definition flecs.h:545
ecs_ctx_free_t
void(* ecs_ctx_free_t)(void *ctx)
Function to cleanup context data.
Definition flecs.h:614
ecs_observer_init
ecs_entity_t ecs_observer_init(ecs_world_t *world, const ecs_observer_desc_t *desc)
Create observer.
builder.hpp
Event builder.
flecs::_
Int to enum.
Definition component.hpp:21
ecs_observer_desc_t
Used with ecs_observer_init().
Definition flecs.h:1273
ecs_observer_desc_t::events
ecs_entity_t events[(8)]
Events to observe (OnAdd, OnRemove, OnSet)
Definition flecs.h:1284
ecs_observer_desc_t::callback_ctx
void * callback_ctx
Context associated with callback (for language bindings).
Definition flecs.h:1308
ecs_observer_desc_t::query
ecs_query_desc_t query
Query for observer.
Definition flecs.h:1281
ecs_observer_desc_t::callback_ctx_free
ecs_ctx_free_t callback_ctx_free
Callback to free callback ctx.
Definition flecs.h:1311
ecs_observer_desc_t::callback
ecs_iter_action_t callback
Callback to invoke on an event, invoked when the observer matches.
Definition flecs.h:1291
ecs_query_desc_t::terms
ecs_term_t terms[32]
Query terms.
Definition flecs.h:1204
ecs_term_ref_t::id
ecs_entity_t id
Entity id.
Definition flecs.h:752
ecs_term_t::src
ecs_term_ref_t src
Source of term.
Definition flecs.h:772
ecs_term_t::id
ecs_id_t id
Component id to be matched by term.
Definition flecs.h:767
flecs::_::entity_observer_delegate
Definition delegate.hpp:562
flecs::_::entity_observer_factory
Definition impl.hpp:46
flecs::_::entity_payload_observer_delegate
Definition delegate.hpp:592
flecs::_::type
Definition flecs.hpp:38
flecs::entity_builder::observe
const Self & observe(flecs::entity_t evt, Func &&callback) const
Observe event on entity.
Definition impl.hpp:73
flecs::entity_view::enqueue
void enqueue() const
Enqueue event for entity.
Definition entity_view.hpp:83
flecs::entity_view::emit
void emit() const
Emit event for entity.
Definition entity_view.hpp:35
flecs::entity_view::id
entity_t id() const
Get entity id.
Definition entity_view.hpp:50
flecs::entity
Entity.
Definition entity.hpp:30
flecs::event_builder_typed
Definition builder.hpp:141
flecs::event_builder
Definition builder.hpp:136
flecs::world
The world.
Definition world.hpp:137
flecs::world::event
flecs::event_builder event(flecs::entity_t evt) const
Create a new event.