Gamedev Framework (gf)  0.10.0
A C++14 framework for 2D games
MessageManager.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_MESSAGE_MANAGER_H
22 #define GF_MESSAGE_MANAGER_H
23 
24 #include <cstdint>
25 #include <map>
26 #include <type_traits>
27 #include <vector>
28 
29 #include "ArrayRef.h"
30 #include "Message.h"
31 #include "Portability.h"
32 
33 namespace gf {
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 inline namespace v1 {
36 #endif
37 
44  using MessageHandlerId = uint64_t;
45 
75  class GF_API MessageManager {
76  public:
81 
106  MessageHandlerId registerHandler(Id type, MessageHandler handler);
107 
126  template<typename E>
128  static_assert(std::is_base_of<Message, E>::value, "E must be an Message");
129  static_assert(E::type != InvalidId, "E must define its type");
130  return registerHandler(E::type, handler);
131  }
132 
160  template<typename R, typename T>
161  MessageHandlerId registerHandler(Id type, R T::*pm, T *obj) {
162  return registerHandler(type, [pm, obj](Id id, Message *msg) {
163  return (obj->*pm)(id, msg);
164  });
165  }
166 
193  template<typename E, typename R, typename T>
195  static_assert(std::is_base_of<Message, E>::value, "E must be an Message");
196  static_assert(E::type != InvalidId, "E must define its type");
197  return registerHandler(E::type, [pm, obj](Id id, Message *msg) {
198  return (obj->*pm)(id, msg);
199  });
200  }
201 
214  void removeHandler(MessageHandlerId id);
215 
222  for (auto id : ids) {
223  removeHandler(id);
224  }
225  }
226 
257  void sendMessage(Id type, Message *message);
258 
281  template<typename E>
282  void sendMessage(E *message) {
283  static_assert(std::is_base_of<Message, E>::value, "E must be an Message");
284  static_assert(E::type != InvalidId, "E must define its type");
285  sendMessage(E::type, message);
286  }
287 
290  private:
291  struct Handler {
292  MessageHandlerId id;
293  MessageHandler handler;
294  };
295 
296  MessageHandlerId m_currentId;
297  std::map<Id, std::vector<Handler>> m_handlers;
298  };
299 
300 #ifndef DOXYGEN_SHOULD_SKIP_THIS
301 }
302 #endif
303 }
304 
305 #endif // GF_MESSAGE_MANAGER_H
std::function< MessageStatus(Id, Message *)> MessageHandler
A message handler.
Definition: Message.h:121
MessageHandlerId registerHandler(MessageHandler handler)
Register a message handler for a type of message.
Definition: MessageManager.h:127
void sendMessage(E *message)
Send a message.
Definition: MessageManager.h:282
uint64_t Id
An identifier.
Definition: Id.h:39
A message manager.
Definition: MessageManager.h:75
uint64_t MessageHandlerId
An identifier for a message handler.
Definition: MessageManager.h:44
MessageHandlerId registerHandler(R T::*pm, T *obj)
Register a message handler for a type of message.
Definition: MessageManager.h:194
The namespace for gf classes.
Definition: Action.h:34
A constant reference to an array and its size.
Definition: ArrayRef.h:42
MessageHandlerId registerHandler(Id type, R T::*pm, T *obj)
Register a message handler for a type of message.
Definition: MessageManager.h:161
The base class for all messages.
Definition: Message.h:53
void removeHandlers(ArrayRef< MessageHandlerId > ids)
Remove a list of handlers.
Definition: MessageManager.h:221
constexpr Id InvalidId
The invalid id (which is 0)
Definition: Id.h:45