Gamedev Framework (gf)  0.5.0
A C++11 framework for 2D games
MessageManager.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 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 <functional>
26 #include <map>
27 #include <type_traits>
28 #include <vector>
29 
30 #include "ArrayRef.h"
31 #include "Message.h"
32 #include "Portability.h"
33 
34 namespace gf {
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 inline namespace v1 {
37 #endif
38 
45  using MessageHandlerId = uint64_t;
46 
76  class GF_API MessageManager {
77  public:
82 
107  MessageHandlerId registerHandler(Id type, MessageHandler handler);
108 
127  template<typename E>
129  static_assert(std::is_base_of<Message, E>::value, "E must be an Message");
130  static_assert(E::type != InvalidId, "E must define its type");
131  return registerHandler(E::type, handler);
132  }
133 
161  template<typename R, typename T>
162  MessageHandlerId registerHandler(Id type, R T::*pm, T *obj) {
163  return registerHandler(type, std::bind(pm, obj, std::placeholders::_1, std::placeholders::_2));
164  }
165 
192  template<typename E, typename R, typename T>
194  static_assert(std::is_base_of<Message, E>::value, "E must be an Message");
195  static_assert(E::type != InvalidId, "E must define its type");
196  return registerHandler(E::type, std::bind(pm, obj, std::placeholders::_1, std::placeholders::_2));
197  }
198 
211  void removeHandler(MessageHandlerId id);
212 
219  for (auto id : ids) {
220  removeHandler(id);
221  }
222  }
223 
254  void sendMessage(Id type, Message *message);
255 
278  template<typename E>
279  void sendMessage(E *message) {
280  static_assert(std::is_base_of<Message, E>::value, "E must be an Message");
281  static_assert(E::type != InvalidId, "E must define its type");
282  sendMessage(E::type, message);
283  }
284 
287  private:
288  struct Handler {
289  MessageHandlerId id;
290  MessageHandler handler;
291  };
292 
293  MessageHandlerId m_currentId;
294  std::map<Id, std::vector<Handler>> m_handlers;
295  };
296 
297 #ifndef DOXYGEN_SHOULD_SKIP_THIS
298 }
299 #endif
300 }
301 
302 #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:128
void sendMessage(E *message)
Send a message.
Definition: MessageManager.h:279
uint64_t Id
An identifier.
Definition: Id.h:38
A message manager.
Definition: MessageManager.h:76
uint64_t MessageHandlerId
An identifier for a message handler.
Definition: MessageManager.h:45
MessageHandlerId registerHandler(R T::*pm, T *obj)
Register a message handler for a type of message.
Definition: MessageManager.h:193
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:162
The base class for all messages.
Definition: Message.h:53
void removeHandlers(ArrayRef< MessageHandlerId > ids)
Remove a list of handlers.
Definition: MessageManager.h:218
constexpr Id InvalidId
The invalid id (which is 0)
Definition: Id.h:44