-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathevent_generator.h
60 lines (43 loc) · 1.21 KB
/
event_generator.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
#pragma once
#include "util.h"
class GameEvent;
class ListenerBase {
public:
virtual void onEvent(const GameEvent&) = 0;
virtual ~ListenerBase() {}
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
}
};
template<typename T>
class ListenerTemplate : public ListenerBase {
public:
ListenerTemplate(WeakPointer<T> p) : ptr(p) {}
SERIALIZATION_CONSTRUCTOR(ListenerTemplate)
virtual void onEvent(const GameEvent& e) override {
ptr->onEvent(e);
}
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & SUBCLASS(ListenerBase);
ar(ptr);
}
private:
WeakPointer<T> SERIAL(ptr);
};
class EventGenerator : public OwnedObject<EventGenerator> {
public:
using SubscriberId = long long;
void addEvent(const GameEvent&);
template <typename T>
SubscriberId addListener(WeakPointer<T> t) {
auto id = Random.getLL();
listeners.emplace(id, unique_ptr<ListenerBase>(new ListenerTemplate<T>(t)));
return id;
}
void removeListener(SubscriberId id);
template <class Archive>
void serialize(Archive& ar, const unsigned int version);
private:
map<SubscriberId, unique_ptr<ListenerBase>> SERIAL(listeners);
};