-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mediator.h
54 lines (46 loc) · 1.39 KB
/
Mediator.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
#pragma once
#include <unordered_map>
#include <vector>
#include <string>
namespace AcsGameEngine::Util {
template <typename keyType, typename funcType>
class Mediator {
public:
Mediator() = default;
virtual ~Mediator() = default;
Mediator(const Mediator& other) : m_events(other.m_events)
{
}
Mediator(Mediator&& other) noexcept : m_events(std::move(other.m_events))
{
}
Mediator& operator=(const Mediator& other)
{
if (this == &other)
return *this;
m_events = other.m_events;
return *this;
}
Mediator& operator=(Mediator&& other) noexcept
{
if (this == &other)
return *this;
m_events = std::move(other.m_events);
return *this;
}
void attach(keyType eventName, funcType &&func) noexcept
{
m_events[eventName] = std::move(func);
}
template <typename... Args>
void trigger(keyType eventName, Args&&... args)
{
if (m_events.find(eventName) != m_events.end()) {
m_events[eventName](std::forward<Args>(args)...);
}
}
private:
using keyFunc = std::unordered_map<keyType, funcType>;
keyFunc m_events;
};
}