-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_event_aggregator.h
144 lines (105 loc) · 3.28 KB
/
simple_event_aggregator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#pragma once
#include <unordered_map>
#include <functional>
#include <typeinfo>
#include <memory>
#include <vector>
#ifdef _WIN32
#include <rpc.h>
#pragma comment(lib, "Rpcrt4.lib")
#else
#endif // _WIN32
namespace simple::ea {
namespace internal {
template <typename T>
inline static constexpr size_t GetHashCode() {
return typeid(T).hash_code();
}
}
class SubscriptionToken {
public:
inline SubscriptionToken() {
UuidCreate(&uuid_);
}
inline ~SubscriptionToken() {
this->Unsubscribe();
}
inline void Unsubscribe() {
if (deleter_ != nullptr) {
deleter_();
}
}
private:
UUID uuid_;
std::function<void()> deleter_;
template <typename> friend class PubSubEvent;
};
class PubSubEventBase {
public:
virtual ~PubSubEventBase() { }
};
template <typename TPayload>
class Subscription {
public:
inline Subscription(const std::function<void(TPayload)>& callback) {
callback_ = callback;
token_ = std::make_shared<SubscriptionToken>();
}
inline Subscription(const std::function<void(TPayload)>& callback,
const std::function<bool(TPayload)>& filter)
: Subscription(callback)
{ }
private:
std::function<bool(TPayload)> filter_;
std::function<void(TPayload)> callback_;
std::shared_ptr<SubscriptionToken> token_;
template <typename> friend class PubSubEvent;
};
template <typename TPayload>
class PubSubEvent : public PubSubEventBase {
public:
inline std::shared_ptr<SubscriptionToken> Subscribe(const std::function<void(TPayload)>& callback, const std::function<bool(TPayload)>& filter = nullptr) {
Subscription<TPayload> new_subscription(callback);
new_subscription.filter_ = filter;
new_subscription.token_->deleter_ = [=]() {
/* Search for pair that contains the subscription uuid as the one we want to delete. */
auto it = std::remove_if(
subscriptions_.begin(),
subscriptions_.end(),
[=, this](Subscription<TPayload> subscription) { return new_subscription.token_->uuid_ == subscription.token_->uuid_; });
if (it != subscriptions_.end()) {
subscriptions_.erase(it);
}
};
subscriptions_.push_back(new_subscription);
return new_subscription.token_;
}
inline void Publish(TPayload payload) {
for (Subscription<TPayload>& subscription : subscriptions_) {
if (subscription.filter_ != nullptr && !subscription.filter_(payload)) {
continue;
}
subscription.callback_(payload);
}
}
private:
std::vector<Subscription<TPayload>> subscriptions_;
};
class EventAggregator
{
public:
template <typename TEventType>
inline std::shared_ptr<TEventType> GetEvent() {
const size_t hash_code = internal::GetHashCode<TEventType>();
auto found = events_.find(hash_code);
if (found == events_.end()) {
auto event = std::make_shared<TEventType>();
events_[hash_code] = event;
return event;
}
return std::dynamic_pointer_cast<TEventType>(found->second);
}
private:
std::unordered_map<size_t, std::shared_ptr<PubSubEventBase>> events_;
};
}