| e8555c89 Andy McCall |
1 |
#ifndef EVENTDISTRIBUTOR_HH |
| 2 |
#define EVENTDISTRIBUTOR_HH |
| 3 |
|
| 4 |
#include "Event.hh" |
| 5 |
|
| 6 |
#include <array> |
| 7 |
#include <cstdint> |
| 8 |
#include <mutex> |
| 9 |
#include <optional> |
| 10 |
#include <utility> |
| 11 |
#include <vector> |
| 12 |
|
| 13 |
namespace openmsx { |
| 14 |
|
| 15 |
class Reactor; |
| 16 |
class EventListener; |
| 17 |
|
| 18 |
class EventDistributor |
| 19 |
{ |
| 20 |
public: |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
enum class Priority : uint8_t { |
| 25 |
OTHER, |
| 26 |
HOTKEY_HIGH, |
| 27 |
IMGUI, |
| 28 |
HOTKEY_LOW, |
| 29 |
MSX, |
| 30 |
LOWEST, |
| 31 |
}; |
| 32 |
friend auto operator<=>(Priority x, Priority y) { return std::to_underlying(x) <=> std::to_underlying(y); } |
| 33 |
|
| 34 |
explicit EventDistributor(Reactor& reactor); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
void registerEventListener(EventType type, EventListener& listener, |
| 44 |
Priority priority = Priority::OTHER); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
void unregisterEventListener(EventType type, EventListener& listener); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
void distributeEvent(Event&& event); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
void deliverEvents(std::optional<int> timeoutMs = std::nullopt); |
| 67 |
|
| 68 |
[[nodiscard]] Reactor& getReactor() { return reactor; } |
| 69 |
|
| 70 |
private: |
| 71 |
[[nodiscard]] bool isRegistered(EventType type, EventListener* listener) const; |
| 72 |
|
| 73 |
private: |
| 74 |
Reactor& reactor; |
| 75 |
|
| 76 |
struct Entry { |
| 77 |
Priority priority; |
| 78 |
EventListener* listener; |
| 79 |
}; |
| 80 |
using PriorityMap = std::vector<Entry>; |
| 81 |
std::array<PriorityMap, size_t(EventType::NUM_EVENT_TYPES)> listeners; |
| 82 |
using EventQueue = std::vector<Event>; |
| 83 |
EventQueue scheduledEvents; |
| 84 |
std::mutex mutex; |
| 85 |
}; |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
#endif |