Plaquette
 
Loading...
Searching...
No Matches
PqEvents.h
1/*
2 * PqEvents.h
3 *
4 * (c) 2025 Sofian Audry :: info(@)sofianaudry(.)com
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "PqCore.h"
21
22#ifndef PQ_EVENT_MANAGER_H_
23#define PQ_EVENT_MANAGER_H_
24
25namespace pq {
26
27class Unit;
28
30typedef void (*EventCallback)();
31
33enum EventType {
34 EVENT_NONE, // no event
35 EVENT_CHANGE, // value changed
36 EVENT_RISE, // value rose
37 EVENT_FALL, // value fell
38 EVENT_BANG, // single trigger
39 EVENT_FINISH, // process/timer finished
40 EVENT_UPDATE, // unit updated and ready to be read/used
41 EVENT_CUSTOM_1, // custom event 1
42 EVENT_CUSTOM_2, // custom event 2
43 EVENT_CUSTOM_3, // custom event 3
44 EVENT_CUSTOM_4, // custom event 4
45};
46
49
50private:
51 // A listener is a tuple (unit, callback, eventType)
52 struct Listener {
53 Unit* unit;
54 EventCallback callback;
55 EventType eventType;
56
57 Listener() : unit(NULL), callback(NULL), eventType(EVENT_NONE) {}
58
59 Listener(Unit* unit, EventCallback callback, EventType eventType) :
60 unit(unit), callback(callback), eventType(eventType) {
61 }
62
63 bool operator==(const Listener& other) const {
64 return unit == other.unit &&
65 callback == other.callback &&
66 eventType == other.eventType;
67 }
68 };
69public:
71 void addListener(Unit* unit, EventCallback callback, EventType eventType);
72 // void removeListeners(Unit* unit, EventCallback callback, uint8_t eventType);
73
75 void clearListeners(Unit* unit);
76
78 void step();
79
80private:
81 // Contains all listeners.
82 HybridArrayList<Listener, 4> _listeners;
83};
84}
85
86#endif
Manages event listeners for Plaquette units.
Definition PqEvents.h:48
void clearListeners(Unit *unit)
Clears all listeners for a given unit.
Definition PqEvents.cpp:32
void addListener(Unit *unit, EventCallback callback, EventType eventType)
Adds a listener to the event manager.
Definition PqEvents.cpp:23
void step()
Performs a single step of the event manager.
Definition PqEvents.cpp:40
A generic class representing a unit in the system.
Definition PqCore.h:373