xyginext  0.2.0
Second generation of xygine game creation framework
Public Member Functions | List of all members
xy::MessageBus Class Referencefinal

System wide message bus for custom event messaging. More...

#include <MessageBus.hpp>

Public Member Functions

 MessageBus (const MessageBus &)=delete
 
 MessageBus (MessageBus &&)=delete
 
MessageBusoperator= (const MessageBus &)=delete
 
MessageBusoperator= (MessageBus &&)=delete
 
const Messagepoll ()
 Read and despatch all messages on the message stack. More...
 
template<typename T >
T * post (Message::ID id)
 Places a message on the message stack, and returns a pointer to the data. More...
 
bool empty ()
 Returns true if there are no messages left on the message bus.
 
std::size_t pendingMessageCount () const
 Returns the number of messages currently sitting on the message bus. More...
 
void disable ()
 Disables the message bus. Used internally by xygine.
 

Detailed Description

System wide message bus for custom event messaging.

The app class contains an instance of the MessageBus which allows events and messages to be broadcast system wide, upon which objects entities and components can decide whether or not to act. Custom message type IDs can be added by extending the ID set starting at Message::Type::Count. EG

enum MyMessageTypes { AlienEvent = Message::Type::Count, GhostEvent, BadgerEvent //etc... };

Member Function Documentation

◆ pendingMessageCount()

std::size_t xy::MessageBus::pendingMessageCount ( ) const

Returns the number of messages currently sitting on the message bus.

Useful for stat logging and debugging.

◆ poll()

const Message& xy::MessageBus::poll ( )

Read and despatch all messages on the message stack.

Used internally by xygine

◆ post()

template<typename T >
T* xy::MessageBus::post ( Message::ID  id)
inline

Places a message on the message stack, and returns a pointer to the data.

The message data can then be filled in via the pointer. Custom message types can be defined via structs, which are then created on the message bus. Structs should contain only trivial data such as PODs and pointers to other objects. ATTEMPING TO PLACE LARGE OBJECTS DIRECTLY ON THE MESSAGE BUS IS ASKING FOR TROUBLE Custom message types should have a unique 32 bit integer ID which can be used to identify the message type when reading messages. Message data has a maximum size of 128 bytes.

Parameters
idUnique ID for this message type
Returns
Pointer to an empty message of given type.