ST_engine  0.3-ALPHA
message_bus.hpp
1 /* This file is part of the "ST" project.
2  * You may use, distribute or modify this code under the terms
3  * of the GNU General Public License version 2.
4  * See LICENCE.txt in the root directory of the project.
5  *
6  * Author: Maxim Atanasov
7  * E-mail: maxim.atanasov@protonmail.com
8  */
9 
10 #ifndef MSG_BUS_DEF
11 #define MSG_BUS_DEF
12 
13 #include <cstdlib>
14 #include <iostream>
15 #include <ST_util/atomic_queue/concurrentqueue.h>
16 #include "../src/main/message.hpp"
17 #include <ST_util/bytell_hash_map.hpp>
18 #include <memory>
19 #include <vector>
20 #include <cstring>
21 #include "message_types.hpp"
22 #include "../src/main/subscriber.hpp"
23 
25 
29 class message_bus {
30 private:
31  friend class message_bus_tests;
32 
33  ska::bytell_hash_map<uint8_t, std::vector<subscriber *>> subscribers; //each message enum maps to a list of subscribers for that message
34 public:
35  message_bus();
36 
37  ~message_bus();
38 
39  void clear();
40 
41  void send_msg(message *msg);
42 
43  void subscribe(uint8_t msg, subscriber *sub);
44 };
45 
52 template<class T>
53 std::shared_ptr<void> make_data(T data) {
54  return std::make_shared<T>(data);
55 }
56 
57 #endif //MSG_BUS_DEF
The central messaging system of the engine. All subsystem make extensive use of it.
Definition: message_bus.hpp:29
void clear()
Definition: message_bus.cpp:62
void subscribe(uint8_t msg, subscriber *sub)
Definition: message_bus.cpp:71
void send_msg(message *msg)
Definition: message_bus.cpp:19
A message object passed around in the message bus. Holds anything created with make_data<>().
Definition: message.hpp:21
This class handles a small queue for messages.
Definition: subscriber.hpp:20