Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Classes | Public Member Functions | List of all members
gf::MessageManager Class Reference

A message manager. More...

#include <gf/MessageManager.h>

Public Member Functions

 MessageManager ()
 Constructor. More...
 
Registering an handler
MessageHandlerId registerHandler (Id type, MessageHandler handler)
 Register a message handler for a type of message. More...
 
template<typename E >
MessageHandlerId registerHandler (MessageHandler handler)
 Register a message handler for a type of message. More...
 
template<typename R , typename T >
MessageHandlerId registerHandler (Id type, R T::*pm, T *obj)
 Register a message handler for a type of message. More...
 
template<typename E , typename R , typename T >
MessageHandlerId registerHandler (R T::*pm, T *obj)
 Register a message handler for a type of message. More...
 
Removing an handler
void removeHandler (MessageHandlerId id)
 Remove a handler. More...
 
void removeHandlers (std::initializer_list< MessageHandlerId > ids)
 Remove a list of handlers. More...
 
Sending a message
void sendMessage (Id type, Message *message)
 Send a message. More...
 
template<typename E >
void sendMessage (E *message)
 Send a message. More...
 

Detailed Description

A message manager.

A message manager is responsible for passing messages synchronously between game entities. It relies on a variant of the observer pattern. Some entities send messages (subclasses of gf::Message identified by their unique message type) to the message manager while some other entities listen to messages of a defined type through a message handler (gf::MessageHandler). As a consequence, there is very low coupling between entities.

Generally, you only need one message manager in a game. It is a good candidate for being a singleton (thanks to gf::Singleton).

int main() {
gf::SingletonStorage<gf::MessageManager> storageForMessageManager(gMessageManager);
gMessageManager().sendMessage(&foo);
}
See also
gf::Message, gf::MessageHandler, gf::Id

Constructor & Destructor Documentation

gf::MessageManager::MessageManager ( )

Constructor.

Member Function Documentation

MessageHandlerId gf::MessageManager::registerHandler ( Id  type,
MessageHandler  handler 
)

Register a message handler for a type of message.

struct Foo : public gf::Message {
static const gf::Id type = "Foo"_id;
// other fields
};
gf::MessageHandler handler = ...;
messages.registerHandler(Foo::type, handler);
Parameters
typeThe type of message
handlerThe message handler
Returns
A handler id
template<typename E >
MessageHandlerId gf::MessageManager::registerHandler ( MessageHandler  handler)
inline

Register a message handler for a type of message.

struct Foo : public gf::Message {
static const gf::Id type = "Foo"_id;
// other fields
};
gf::MessageHandler handler = ...;
messages.registerHandler<Foo>(handler);
Parameters
handlerThe message handler
Returns
A handler id
template<typename R , typename T >
MessageHandlerId gf::MessageManager::registerHandler ( Id  type,
R T::*  pm,
T obj 
)
inline

Register a message handler for a type of message.

struct Foo : public gf::Message {
static const gf::Id type = "Foo"_id;
// other fields
};
class Bar {
gf::MessageStatus onFoo(gf::Id type, gf::Message *msg) {
// do something useful
}
};
Bar bar;
messages.registerHandler(Foo::type, &Bar::onFoo, &bar);
Parameters
typeThe type of message
pmA pointer to member function that represents the handler
objThe destination object that receives the message
Returns
A handler id
template<typename E , typename R , typename T >
MessageHandlerId gf::MessageManager::registerHandler ( R T::*  pm,
T obj 
)
inline

Register a message handler for a type of message.

struct Foo : public gf::Message {
static const gf::Id type = "Foo"_id;
// other fields
};
class Bar {
gf::MessageStatus onFoo(gf::Id type, gf::Message *msg) {
// do something useful
}
};
Bar bar;
messages.registerHandler<Foo>(&Bar::onFoo, &bar);
Parameters
pmA pointer to member function that represents the handler
objThe destination object that receives the message
Returns
A handler id
void gf::MessageManager::removeHandler ( MessageHandlerId  id)

Remove a handler.

Parameters
idThe handler id
void gf::MessageManager::removeHandlers ( std::initializer_list< MessageHandlerId ids)
inline

Remove a list of handlers.

Parameters
idsThe list of handler ids
void gf::MessageManager::sendMessage ( Id  type,
Message message 
)

Send a message.

The message is sent synchronously i.e. immediately when calling the function. A consequence is that the memory for the message can be allocated on the stack because it only has to live during the function call.

struct Foo : public gf::Message {
static const gf::Id type = "Foo"_id;
// other fields
};
Foo foo;
messages.sendMessage(Foo::type, &foo);
Parameters
typeThe message type
messageA pointer to the message
template<typename E >
void gf::MessageManager::sendMessage ( E message)
inline

Send a message.

The message is sent synchronously i.e. immediately when calling the function. A consequence is that the memory for the message can be allocated on the stack because it only has to live during the function call.

struct Foo : public gf::Message {
static const gf::Id type = "Foo"_id;
// other fields
};
Foo foo;
messages.sendMessage(&foo);
Parameters
messageA pointer to the message