-
Notifications
You must be signed in to change notification settings - Fork 1
/
EventHandler.hpp
58 lines (48 loc) · 1.69 KB
/
EventHandler.hpp
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
#pragma once
#include "EventSystem.hpp"
namespace lite
{
// Registers a handler with the global EventSystem upon
// construction and removes it upon destruction.
class EventHandler
{
private: // data
// Name of the event being listened for.
string eventName;
// Unique id of this handler.
size_t id = (size_t) -1;
public: // methods
// Cannot copy or move event handler because a previously
// captured 'this' pointer may be pointing to an invalid
// object after the copy.
EventHandler() = delete;
EventHandler(const EventHandler&) = delete;
EventHandler& operator=(const EventHandler&) = delete;
EventHandler(EventHandler&&) = delete;
EventHandler& operator=(EventHandler&&) = delete;
// Constructs the handler from an event name and function object.
EventHandler(string eventName_, function<void(EventData&)> fn) :
eventName(move(eventName_)),
id(EventSystem::GenerateHandlerId())
{
EventSystem::Instance().AddHandler(eventName, move(fn), id);
}
// Constructs the handler from an event name, a 'this' pointer, and a pointer to the
// member function handling the event.
template <class ThisType, class MemberFunctionPointer>
EventHandler(string eventName_, ThisType* this_, MemberFunctionPointer memfn) :
EventHandler(move(eventName_), [=](EventData& data) { (this_->*memfn)(data); })
{
}
// Unregisters the event handler from the global event system.
~EventHandler()
{
Clear();
}
// Unregisters the event handler from the global event system.
void Clear()
{
EventSystem::Instance().RemoveHandler(eventName, id);
}
};
} // namespace lite