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};
42
45
46private:
47 // A listener is a tuple (unit, callback, eventType)
48 struct Listener {
49 Unit* unit;
50 EventCallback callback;
51 EventType eventType;
52
53 Listener() : unit(NULL), callback(NULL), eventType(EVENT_NONE) {}
54
55 Listener(Unit* unit, EventCallback callback, EventType eventType) :
56 unit(unit), callback(callback), eventType(eventType) {
57 }
58
59 bool operator==(const Listener& other) const {
60 return unit == other.unit &&
61 callback == other.callback &&
62 eventType == other.eventType;
63 }
64 };
65public:
67 void addListener(Unit* unit, EventCallback callback, EventType eventType);
68 // void removeListeners(Unit* unit, EventCallback callback, uint8_t eventType);
69
71 void clearListeners(Unit* unit);
72
74 void step();
75
76private:
77 // Contains all listeners.
78 HybridArrayList<Listener, 4> _listeners;
79};
80}
81
82#endif
Manages event listeners for Plaquette units.
Definition PqEvents.h:44
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:335