Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
EventDelegate.h
Go to the documentation of this file.
1/*
2 Author : Tobias Stein
3 Date : 26th July, 2016
4 File : EventDelegate.h
5
6 A delegate to forward events to specific handler methdos.
7
8 All Rights Reserved. (c) Copyright 2016.
9*/
10
11#pragma once
12#ifndef ECS__EVENT_DELEGATE_H__
13#define ECS__EVENT_DELEGATE_H__
14
15#include "Platform.h"
16
17namespace ECS { namespace Event {
18
19 class IEvent;
20
21 namespace Internal
22 {
23 using EventDelegateId = size_t;
24
25
27 {
28 public:
29
30 virtual inline void invoke(const IEvent* const e) = 0;
31
32 virtual inline EventDelegateId GetDelegateId() const = 0;
33
34 virtual inline u64 GetStaticEventTypeId() const = 0;
35
36 virtual bool operator==(const IEventDelegate* other) const = 0;
37
38 virtual IEventDelegate* clone() = 0;
39
40 }; // class IEventDelegate
41
42 template<class Class, class EventType>
43 class EventDelegate final : public IEventDelegate
44 {
45 typedef void(Class::*Callback)(const EventType* const);
46
47 Class* m_Receiver;
49
50 public:
51
52 EventDelegate(Class* receiver, Callback& callbackFunction) :
53 m_Receiver(receiver),
54 m_Callback(callbackFunction)
55 {}
56
58 {
59 return new EventDelegate(this->m_Receiver, this->m_Callback);
60 }
61
62 inline void invoke(const IEvent* const e) override
63 {
64 (m_Receiver->*m_Callback)(reinterpret_cast<const EventType* const>(e));
65 }
66
67 inline EventDelegateId GetDelegateId() const override
68 {
69 static const EventDelegateId DELEGATE_ID { typeid(Class).hash_code() ^ typeid(Callback).hash_code() };
70 return DELEGATE_ID;
71 }
72
73
74 inline u64 GetStaticEventTypeId() const override
75 {
76 static const u64 SEID { EventType::STATIC_EVENT_TYPE_ID };
77 return SEID;
78 }
79
80 bool operator==(const IEventDelegate* other) const override
81 {
82 if (this->GetDelegateId() != other->GetDelegateId())
83 return false;
84
85 EventDelegate* delegate = (EventDelegate*)other;
86 if (other == nullptr)
87 return false;
88
89 return ((this->m_Callback == delegate->m_Callback) && (this->m_Receiver == delegate->m_Receiver));
90 }
91
92 }; // class EventDelegate
93
94 }
95}} // namespace ECS::Event::Internal
96
97#endif // ECS__EVENT_DELEGATE_H__
void(Class::* Callback)(const EventType *const)
Definition: EventDelegate.h:45
bool operator==(const IEventDelegate *other) const override
Definition: EventDelegate.h:80
EventDelegate(Class *receiver, Callback &callbackFunction)
Definition: EventDelegate.h:52
void invoke(const IEvent *const e) override
Definition: EventDelegate.h:62
u64 GetStaticEventTypeId() const override
Definition: EventDelegate.h:74
EventDelegateId GetDelegateId() const override
Definition: EventDelegate.h:67
IEventDelegate * clone() override
Definition: EventDelegate.h:57
virtual void invoke(const IEvent *const e)=0
virtual bool operator==(const IEventDelegate *other) const =0
virtual IEventDelegate * clone()=0
virtual EventDelegateId GetDelegateId() const =0
virtual u64 GetStaticEventTypeId() const =0
uint32_t u64
Definition: Platform.h:55