Debian Codegraph @

include/imessagebus.h

raw
1 #pragma once
2
3 #include <string>
4 #include <functional>
5
6 namespace radiant
7 {
8
9 class IMessage
10 {
11 private:
12 bool _handled;
13
14 protected:
15 IMessage() :
16 _handled(false)
17 {}
18
19 public:
20 virtual ~IMessage() {}
21
22 // Returns a unique ID of this message, see IMessage::Type enum
23 // for well-known types
24 virtual std::size_t getId() const = 0;
25
26 // Returns true if this message has been handled
27 bool isHandled() const
28 {
29 return _handled;
30 }
31
32 void setHandled(bool handled)
33 {
34 _handled = handled;
35 }
36
37 public:
38 // Pre-defined message type IDs.
39 // Plugin code can define their own IDs in the range of 1000+
40 enum Type : std::size_t
41 {
42 ApplicationShutdownRequest,
43 AutomaticMapSaveRequest,
44 ClearConsole,
45 CommandExecutionFailed,
46 GameConfigNeeded,
47 LongRunningOperation,
48 MapFileOperation,
49 MapOperationFinished,
50 FileSelectionRequest,
51 Notification,
52 TextureChanged,
53 ApplicationIsActiveQuery,
54 FileOverwriteConfirmation,
55 FileSaveConfirmation,
56 UnselectSelectionRequest,
57 ManipulatorModeToggleRequest,
58 ComponentSelectionModeToggleRequest,
59 GridSnapRequest,
60 FocusMaterialRequest,
61 TextureToolRequest,
62
63 UserDefinedMessagesGoHigherThanThis = 999,
64 };
65 };
66
67 /**
68 * General-purpose handler used to send and process message to registered listeners.
69 * Client code can send arbitrary message objects as long as they are deriving from
70 * IMessage.
71 * Listeners need to subscribe to each message type they're interested in.
72 * To receive the message, client code needs to add a listener, which can either
73 * listen for a specific type of messages or all of them.
74 */
75 class IMessageBus
76 {
77 public:
78 virtual ~IMessageBus() {}
79
80 typedef std::function<void(IMessage&)> Listener;
81
82 /**
83 * Registers a listener that is only called when the give message type
84 * is sent across the wire. Use the returned ID to unsubscribe the listener.
85 */
86 virtual std::size_t addListener(std::size_t messageType, const Listener& listener) = 0;
87
88 /**
89 * Unsubscribe the given listener.
90 */
91 virtual void removeListener(std::size_t listenerId) = 0;
92
93 /**
94 * Send the given message to the given channel. The channel ID refers to
95 * a given message type and must have been acquired using getChannelId() beforehand.
96 */
97 virtual void sendMessage(IMessage& message) = 0;
98 };
99
100 /**
101 * Small adaptor to allow for less client setup code
102 * when only listening for a certain IMessage subtype
103 */
104 template<typename T>
105 class TypeListener :
106 private std::function<void(T&)>
107 {
108 public:
109 TypeListener(void (*specialisedFunc)(T&)) :
110 std::function<void(T&)>(specialisedFunc)
111 {
112 static_assert(std::is_base_of<IMessage, T>::value, "Listener must accept IMessage types");
113 }
114
115 TypeListener(const std::function<void(T&)>& specialisedFunc) :
116 std::function<void(T&)>(specialisedFunc)
117 {
118 static_assert(std::is_base_of<IMessage, T>::value, "Listener must accept IMessage types");
119 }
120
121 // Fulfills the Listener function signature
122 void operator()(IMessage& message)
123 {
124 // Perform a type cast and dispatch to our base class' call operator
125 std::function<void(T&)>::operator()(static_cast<T&>(message));
126 }
127 };
128
129 }