ezEngine  Milestone 7
Message.h
1 #pragma once
2 
3 #include <Foundation/Algorithm/Hashing.h>
4 #include <Foundation/IO/Stream.h>
5 
6 typedef ezUInt16 ezMessageId;
7 
14 class EZ_FOUNDATION_DLL ezMessage
15 {
16 public:
17  virtual ~ezMessage() {}
18 
20  virtual ezMessage* Clone(ezAllocatorBase* pAllocator) const = 0;
21 
22  //virtual void Serialize(ezStreamWriterBase& stream) const;
23  //virtual ezResult Deserialize(ezStreamReaderBase& stream);
24 
26  EZ_FORCE_INLINE ezMessageId GetId() const
27  {
28  return m_Id;
29  }
30 
32  EZ_FORCE_INLINE ezUInt16 GetSize() const
33  {
34  return m_uiSize;
35  }
36 
38  EZ_FORCE_INLINE ezUInt32 GetHash() const
39  {
40  return ezHashing::MurmurHash(this, m_uiSize);
41  }
42 
43 protected:
44  EZ_FORCE_INLINE static ezMessageId GetNextMsgId()
45  {
46  return s_uiNextMsgId++;
47  }
48 
49  ezMessageId m_Id;
50  ezUInt16 m_uiSize;
51 
52  static ezMessageId s_uiNextMsgId;
53 };
54 
56 #define EZ_DECLARE_MESSAGE_TYPE(messageType) \
57  private: \
58  static ezMessageId MSG_ID; \
59  public: \
60  static ezMessageId GetMsgId() \
61  { \
62  static ezMessageId id = ezMessage::GetNextMsgId(); \
63  return id; \
64  } \
65  EZ_FORCE_INLINE messageType() \
66  { \
67  m_Id = messageType::MSG_ID; \
68  m_uiSize = sizeof(messageType); \
69  } \
70  \
71  virtual ezMessage* Clone(ezAllocatorBase* pAllocator) const override \
72  { \
73  return EZ_NEW(pAllocator, messageType, *static_cast<const messageType*>(this)); \
74  }
75 
77 #define EZ_IMPLEMENT_MESSAGE_TYPE(messageType) \
78  ezMessageId messageType::MSG_ID = messageType::GetMsgId();
79