forked from rayofhope-dev/XRay-Shared
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxrEvent.h
56 lines (49 loc) · 1.43 KB
/
xrEvent.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
#pragma once
#include "xrDelegate/xrDelegate.h"
#include "xrArrayHelpers.h"
template<typename ... Args>
class xrEvent
{
public:
xrEvent() = default;
xrEvent(const xrEvent& other) = delete;
xrEvent(xrEvent&& other) = delete;
xrEvent& operator=(const xrEvent& other) = delete;
xrEvent& operator=(xrEvent&& other) = delete;
xrEvent operator*() = delete;
template<typename TFunction>
void subscribe(TFunction fx) const
{
d_subscribers.push_back(BindDelegate(fx));
}
template<typename TClass, typename TFunction>
void subscribe(TClass tx, TFunction fx) const
{
d_subscribers.push_back(BindDelegate(tx, fx));
}
template<typename TFunction>
void unsubscribe(TFunction fx) const
{
auto self = BindDelegate(fx);
array_eraseif(d_subscribers, [self](const xrDelegate<void(Args...)>& delegate)
{
return self == delegate;
});
}
template<typename TClass, typename TFunction>
void unsubscribe(TClass tx, TFunction fx) const
{
auto self = BindDelegate(tx, fx);
array_eraseif(d_subscribers, [self](const xrDelegate<void(Args...)>& delegate)
{
return self == delegate;
});
}
void operator()(Args...args)
{
for (auto it : d_subscribers)
it(args...);
}
private:
mutable std::vector<xrDelegate<void(Args...)>> d_subscribers;
};