Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#5231: Add infrastructure to dispatch messages from (lower level) mod…
…ules to any listeners. First client is the GameManager dispatching the request to open the game setup UI in case it can't find any valid configuration.
- Loading branch information
1 parent
3dafa39
commit 536a76d
Showing
19 changed files
with
292 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <functional> | ||
|
||
namespace radiant | ||
{ | ||
|
||
class IMessage | ||
{ | ||
private: | ||
bool _handled; | ||
|
||
protected: | ||
IMessage() : | ||
_handled(false) | ||
{} | ||
|
||
public: | ||
virtual ~IMessage() {} | ||
|
||
// Returns true if this message has been handled | ||
bool isHandled() const | ||
{ | ||
return _handled; | ||
} | ||
|
||
void setHandled(bool handled) | ||
{ | ||
_handled = handled; | ||
} | ||
}; | ||
|
||
/** | ||
* General-purpose handler used to send and process message to registered listeners. | ||
* Client code can send arbitrary message objects as long as they are deriving from | ||
* IMessage. | ||
* To receive the message, client code needs to add a listener. | ||
*/ | ||
class IMessageBus | ||
{ | ||
public: | ||
virtual ~IMessageBus() {} | ||
|
||
typedef std::function<void(IMessage&)> Listener; | ||
|
||
/** | ||
* Register a listener which gets called with the message as argument. | ||
* An ID of the listener will be returned which can be used for unsubscription. | ||
*/ | ||
virtual std::size_t addListener(const Listener& listener) = 0; | ||
|
||
/** | ||
* Unsubscribe the given listener. | ||
*/ | ||
virtual void removeListener(std::size_t listenerId) = 0; | ||
|
||
/** | ||
* Send the given message along the wire. Clients need to construct | ||
* the message themselves, and check the possible result. | ||
*/ | ||
virtual void sendMessage(IMessage& message) = 0; | ||
}; | ||
|
||
/** | ||
* Small adaptor to allow for less client setup code | ||
* when only listening for a certain IMessage subtype | ||
*/ | ||
template<typename T> | ||
class TypeListener : | ||
private std::function<void(T&)> | ||
{ | ||
public: | ||
TypeListener(void (*specialisedFunc)(T&)) : | ||
std::function<void(T&)>(specialisedFunc) | ||
{} | ||
|
||
TypeListener(const std::function<void(T&)>& specialisedFunc) : | ||
std::function<void(T&)>(specialisedFunc) | ||
{} | ||
|
||
// Fulfills the Listener function signature | ||
void operator()(IMessage& message) | ||
{ | ||
// Perform a type cast and dispatch to our base class' call operator | ||
try | ||
{ | ||
std::function<void(T&)>::operator()(dynamic_cast<T&>(message)); | ||
} | ||
catch (std::bad_cast&) | ||
{} | ||
} | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
|
||
#include "igame.h" | ||
#include "imessagebus.h" | ||
|
||
namespace game | ||
{ | ||
|
||
/** | ||
* Message object sent to the MessageBus when the application | ||
* is lacking a game configuration. | ||
* | ||
* Use the setConfig() method to assign a configuration to | ||
* return to the sender. | ||
* | ||
* When this message stays unhandled after it went through the | ||
* MessageBus, the application is unlikely to be able to continue | ||
* and will probably terminate. | ||
*/ | ||
class ConfigurationNeeded : | ||
public radiant::IMessage | ||
{ | ||
private: | ||
GameConfiguration _config; | ||
|
||
public: | ||
ConfigurationNeeded() | ||
{} | ||
|
||
const GameConfiguration& getConfig() const | ||
{ | ||
return _config; | ||
} | ||
|
||
void setConfig(const GameConfiguration& config) | ||
{ | ||
_config = config; | ||
} | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#pragma once | ||
|
||
#include <mutex> | ||
#include "imessagebus.h" | ||
|
||
namespace radiant | ||
{ | ||
|
||
class MessageBus : | ||
public IMessageBus | ||
{ | ||
private: | ||
std::recursive_mutex _lock; | ||
std::map<std::size_t, Listener> _listeners; | ||
|
||
bool _processingMessage; | ||
std::size_t _nextId; | ||
|
||
public: | ||
MessageBus() : | ||
_nextId(1) | ||
{} | ||
|
||
std::size_t addListener(const Listener & listener) override | ||
{ | ||
std::lock_guard<std::recursive_mutex> guard(_lock); | ||
|
||
auto id = _nextId++; | ||
_listeners.emplace(id, listener); | ||
|
||
return id; | ||
} | ||
|
||
void removeListener(std::size_t listenerId) override | ||
{ | ||
std::lock_guard<std::recursive_mutex> guard(_lock); | ||
|
||
assert(_listeners.find(listenerId) != _listeners.end()); | ||
|
||
_listeners.erase(listenerId); | ||
} | ||
|
||
void sendMessage(IMessage& message) override | ||
{ | ||
std::lock_guard<std::recursive_mutex> guard(_lock); | ||
|
||
for (auto it = _listeners.begin(); it != _listeners.end(); /* in-loop */) | ||
{ | ||
(*it++).second(message); | ||
} | ||
} | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.