DSC Engine
Loading...
Searching...
No Matches
event.hpp
Go to the documentation of this file.
1
5#pragma once
6
8#include "DSCEngine/debug/assert.hpp"
9
10namespace DSC
11{
16 typedef void (*StaticEventHandler)(void* sender, void* args);
17
18
23 template <class C>
24 using NonStaticEventHandler = void (C::*)(void* sender, void* args);
25
26
28 {
29 protected:
30 int type_id = 0;
31 public:
32 bool has_same_type(const EventHandlerContainer& other) const;
33 EventHandlerContainer() = default;
34 virtual void execute(void* sender, void* args) const;
35
36 virtual bool operator == (const EventHandlerContainer& other) const;
37
38 virtual ~EventHandlerContainer() = default;
39 };
40
42 {
43 private:
44 StaticEventHandler __handler;
45 public:
47 virtual void execute(void* sender, void* args) const override;
48
49 virtual bool operator == (const EventHandlerContainer& other) const;
50
52 };
53
54 template<class C>
56 {
57 protected:
59 C* __instance;
60 public:
62 virtual void execute(void* sender, void* args) const override;
63
64 virtual bool operator == (const EventHandlerContainer& other) const;
65
67 };
68
69
75 class Event
76 {
77 private:
79
80 public:
85
91 Event& operator += (const StaticEventHandler& e);
92
98 Event& operator -= (const StaticEventHandler& e);
99
100 template<class C>
101 void add_event(const NonStaticEventHandler<C> e, C* instance);
102
103 template<class C>
104 void remove_event(const NonStaticEventHandler<C> e, C* instance);
105
111 void trigger(void* sender, void* args) const;
112
113 ~Event();
114 };
115
116
117 template<class C>
119 : __handler(handler), __instance(instance)
120 {
121 type_id = 2;
122 }
123
124 template<class C>
125 void NonStaticEventHandlerContainer<C>::execute(void* sender, void* args) const
126 {
127 nds_assert(__handler!=nullptr); // if this is raised, something's really messed up
128 //Debug::log("Executing event...");
129
130 (__instance->*__handler)(sender, args);
131
132 //Debug::log("Event executed");
133 }
134
135 template<class C>
136 bool NonStaticEventHandlerContainer<C>::operator == (const EventHandlerContainer& other) const
137 {
138 if(!this->has_same_type(other))
139 return false;
140 const NonStaticEventHandlerContainer<C>* p_other
141 = (const NonStaticEventHandlerContainer<C>*)(&other);
142 return this->__handler == p_other->__handler && this->__instance == p_other->__instance;
143 }
144
145
146
147 template<class C>
148 void Event::add_event(const NonStaticEventHandler<C> e, C* instance)
149 {
150 nds_assert(e != nullptr);
151 actions.push_back(new NonStaticEventHandlerContainer<C>(e, instance));
152 }
153
154 template<class C>
155 void Event::remove_event(const NonStaticEventHandler<C> e, C* instance)
156 {
157 NonStaticEventHandlerContainer<C> eh(e, instance);
158 for(int i=0;i<actions.size();i++)
159 {
160 if(*actions[i] == eh)
161 {
162 EventHandlerContainer* target = actions[i];
163 actions.remove_at(i);
164 delete target;
165 }
166 }
167 }
168}
Definition: event.hpp:28
Class responsible with event registration and execution.
Definition: event.hpp:76
Event()
Creates a new Event instance.
void trigger(void *sender, void *args) const
Fires the event.
Event & operator+=(const StaticEventHandler &e)
Adds a new event handler to this event.
Event & operator-=(const StaticEventHandler &e)
Removes an event handler from this event.
Definition: event.hpp:56
Definition: event.hpp:42
Generic dynamic vector.
Definition: vector.hpp:23
void(* StaticEventHandler)(void *sender, void *args)
Function type to be executed when event fires
Definition: event.hpp:16
void(C::*)(void *sender, void *args) NonStaticEventHandler
Class method type to be executed when event fires.
Definition: event.hpp:24
Generic dynamic vector definition.