Plasma Engine  2.0
Loading...
Searching...
No Matches
MessageHandler.h
1#pragma once
2
4
5#include <Foundation/Basics.h>
6#include <Foundation/Reflection/Implementation/RTTI.h>
7
8class plMessage;
9
11class PL_FOUNDATION_DLL plAbstractMessageHandler
12{
13public:
14 virtual ~plAbstractMessageHandler() = default;
15
16 PL_ALWAYS_INLINE void operator()(void* pInstance, plMessage& ref_msg) { (*m_DispatchFunc)(this, pInstance, ref_msg); }
17
18 PL_FORCE_INLINE void operator()(const void* pInstance, plMessage& ref_msg)
19 {
20 PL_ASSERT_DEV(m_bIsConst, "Calling a non const message handler with a const instance.");
21 (*m_ConstDispatchFunc)(this, pInstance, ref_msg);
22 }
23
24 PL_ALWAYS_INLINE plMessageId GetMessageId() const { return m_Id; }
25
26 PL_ALWAYS_INLINE bool IsConst() const { return m_bIsConst; }
27
28protected:
29 using DispatchFunc = void (*)(plAbstractMessageHandler* pSelf, void* pInstance, plMessage&);
30 using ConstDispatchFunc = void (*)(plAbstractMessageHandler* pSelf, const void* pInstance, plMessage&);
31
32 union
33 {
34 DispatchFunc m_DispatchFunc = nullptr;
35 ConstDispatchFunc m_ConstDispatchFunc;
36 };
37 plMessageId m_Id = plSmallInvalidIndex;
38 bool m_bIsConst = false;
39};
40
42{
43 const char* m_szName;
44 const plRTTI* m_pMessageType;
45};
46
47namespace plInternal
48{
49 template <typename Class, typename MessageType>
51 {
52 static plCompileTimeTrueType IsConst(void (Class::*)(MessageType&) const);
53 static plCompileTimeFalseType IsConst(...);
54 };
55
56 template <bool bIsConst>
58 {
59 template <typename Class, typename MessageType, void (Class::*Method)(MessageType&)>
61 {
62 public:
63 Impl()
64 {
65 m_DispatchFunc = &Dispatch;
66 m_Id = MessageType::GetTypeMsgId();
67 m_bIsConst = false;
68 }
69
70 static void Dispatch(plAbstractMessageHandler* pSelf, void* pInstance, plMessage& ref_msg)
71 {
72 Class* pTargetInstance = static_cast<Class*>(pInstance);
73 (pTargetInstance->*Method)(static_cast<MessageType&>(ref_msg));
74 }
75 };
76 };
77
78 template <>
79 struct MessageHandler<true>
80 {
81 template <typename Class, typename MessageType, void (Class::*Method)(MessageType&) const>
83 {
84 public:
85 Impl()
86 {
87 m_ConstDispatchFunc = &Dispatch;
88 m_Id = MessageType::GetTypeMsgId();
89 m_bIsConst = true;
90 }
91
93 static void Dispatch(plAbstractMessageHandler* pSelf, const void* pInstance, plMessage& ref_msg)
94 {
95 const Class* pTargetInstance = static_cast<const Class*>(pInstance);
96 (pTargetInstance->*Method)(static_cast<MessageType&>(ref_msg));
97 }
98 };
99 };
100} // namespace plInternal
101
102#define PL_IS_CONST_MESSAGE_HANDLER(Class, MessageType, Method) \
103 (sizeof(plInternal::MessageHandlerTraits<Class, MessageType>::IsConst(Method)) == sizeof(plCompileTimeTrueType))
The base class for all message handlers that a type provides.
Definition MessageHandler.h:12
Definition MessageHandler.h:61
Definition MessageHandler.h:83
static void Dispatch(plAbstractMessageHandler *pSelf, const void *pInstance, plMessage &ref_msg)
Casts the given message to the type of this message handler, then passes that to the class instance.
Definition MessageHandler.h:93
Base class for all message types. Each message type has it's own id which is used to dispatch message...
Definition Message.h:22
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
Definition MessageHandler.h:58
Definition MessageHandler.h:51
Definition MessageHandler.h:42