-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObesever.cpp
More file actions
55 lines (47 loc) · 894 Bytes
/
Obesever.cpp
File metadata and controls
55 lines (47 loc) · 894 Bytes
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
#include<functional>
#include<unordered_map>
#include<string>
using namespace std;
class NonCopyable {
NonCopyable() = default;
~NonCopyable() = default;
NonCopyable(const NonCopyable& rhs) = delete;
NonCopyable& operator=(const NonCopyable& rhs) = delete;
};
template<typename Func>
class Observer :NonCopyable
{
public:
Observer() {}
~Observer() {}
int add(Func &f)
{
return assign(f);
}
int add(Func &&f)
{
return assign(f);
}
void remove(int &&f)
{
ObeseverMap.erase(f);
}
template<typename... Args>
void Notify(Args&&... args)
{
for (auto it : ObeseverMap)
{
it.second(forward<Args>(args)...);
}
}
private:
template<typename F>
int assign(F &&f)
{
int k = ObeserverId++;
ObeseverMap.emplace(k, f);
return k;
}
static int ObeserverId = 0;
unordered_map<int, Func> ObeseverMap;
};