Maki
Formally verified low-code orchestration
Loading...
Searching...
No Matches
event_dispatcher.h
1#pragma once
2
3#include <QMetaObject>
4#include <QObject>
5#include <cstdint>
6#include <functional>
7#include <memory>
8#include <unordered_map>
9#include <utility>
10#include <vector>
11
12#include "test_and_set.h"
13
14template <typename Event>
15class EventDispatcher : public QObject
16{
17public:
18 using Callback = std::function<void(const Event&)>;
19 using Id = std::uint64_t;
20
21private:
22 struct State
23 {
24 Id nextId = 0;
25 std::unordered_map<Id, Callback> listeners;
26 };
27
28public:
29 class Subscription
30 {
31 public:
32 Subscription() = default;
33
34 Subscription(std::weak_ptr<State> state, Id id)
35 : mState(std::move(state))
36 , mId(id)
37 {
38 }
39
40 ~Subscription()
41 {
42 reset();
43 }
44
45 Subscription(const Subscription&) = delete;
46 Subscription& operator=(const Subscription&) = delete;
47
48 Subscription(Subscription&& other) noexcept
49 : mState(std::move(other.mState))
50 , mId(std::exchange(other.mId, 0))
51 {
52 }
53
54 Subscription& operator=(Subscription&& other) noexcept
55 {
56 if (this != &other)
57 {
58 reset();
59 mState = std::move(other.mState);
60 mId = std::exchange(other.mId, 0);
61 }
62 return *this;
63 }
64
65 void reset()
66 {
67 if (mId == 0)
68 return;
69
70 if (auto state = mState.lock())
71 state->listeners.erase(mId);
72
73 mState.reset();
74 mId = 0;
75 }
76
77 private:
78 std::weak_ptr<State> mState;
79 Id mId = 0;
80 };
81
82 explicit EventDispatcher(QObject* parent = nullptr)
83 : QObject(parent)
84 , mState(std::make_shared<State>())
85 {
86 }
87
88 Subscription subscribe(Callback callback)
89 {
90 const Id id = ++mState->nextId;
91 mState->listeners.emplace(id, std::move(callback));
92
93 return Subscription(mState, id);
94 }
95
96 void publish(const Event& event)
97 {
98 // Do not publish anything if the dispatcher is blocked
99 if (mBlocked.Value())
100 return;
101
102 mPending.push_back(std::move(event));
103 if (!mScheduled.SetAndFailOnDifferent(false, true))
104 return;
105
106 QMetaObject::invokeMethod(this, [this]() { flush(); }, Qt::QueuedConnection);
107 }
108
109 void blockEvents()
110 {
111 mBlocked.SetUnconditionally(true);
112 mPending.clear();
113 }
114
115 void unblockEvents()
116 {
117 mBlocked.SetUnconditionally(false);
118 }
119
120 void flush()
121 {
122 mScheduled.SetUnconditionally(false);
123
124 auto events = std::move(mPending);
125 mPending.clear();
126
127 for (const auto& event : events)
128 {
129 // Snapshot IDs so callbacks may unsubscribe safely.
130 std::vector<Id> ids;
131 ids.reserve(mState->listeners.size());
132
133 for (const auto& [id, callback] : mState->listeners)
134 ids.push_back(id);
135
136 for (const auto id : ids)
137 {
138 auto it = mState->listeners.find(id);
139 if (it == mState->listeners.end())
140 continue;
141
142 // Copy callback: invoking it may modify the listener map.
143 auto callback = it->second;
144 callback(event);
145 }
146 }
147 }
148
149private:
150 std::shared_ptr<State> mState;
151
152 std::vector<Event> mPending;
153 TestAndSet<bool> mScheduled = false;
154 TestAndSet<bool> mBlocked = false;
155};
Definition event_dispatcher.h:23