|
VR-Engage
2.2
|
This class implements the factory pattern for creating DtVreMessage objects from serialized byte arrays. It maintains a registry of message types and their corresponding decoder functions, enabling dynamic message creation based on the message type encoded in the data.
The factory comes pre-registered with decoders for system messages like ForwardMessage and StopForwardMessage. Additional message types can be registered using the registerMessage() template method.
#include <vreMessageFactory.h>
Public Member Functions | |
| DtVreMessageFactory () | |
| virtual | ~DtVreMessageFactory () |
| virtual DtVreMessage * | create (char *bytes, int length) |
| template<typename MSG> | |
| void | registerMessage () |
| virtual void | clear () |
Protected Types | |
| using | DecoderMap = std::map<unsigned int, Decoder> |
Protected Member Functions | |
| virtual void | registerCreator (const DtVreMessageId &id, Decoder decoder) |
| virtual void | installDefaultMessages () |
Protected Attributes | |
| DecoderMap | myCreatorMap |
|
protected |
Type definition for the map of message type hashes to decoder functions.
| makVre::DtVreMessageFactory::DtVreMessageFactory | ( | ) |
Constructor.
Initializes a new message factory and registers default system messages by calling installDefaultMessages().
|
virtual |
Virtual destructor.
Cleans up resources used by the message factory.
|
virtual |
Creates a message object from serialized data.
| bytes | Pointer to the buffer containing the serialized message |
| length | Length of the buffer in bytes |
This method deserializes the message header to determine the message type, looks up the appropriate decoder function in the registry, and calls that function to create the message object. It returns nullptr if no decoder is found for the message type or if an error occurs during decoding.
|
inline |
Registers a message type with the factory.
| MSG | Message class type to register |
This template method registers a message class with the factory by retrieving its ID and decode function. The message class must provide:
Example usage:
References registerCreator().
|
virtual |
Clears all registered message decoders.
This method removes all registered message types and their decoders from the factory. After calling this method, the factory will not be able to create any messages until new ones are registered.
|
protectedvirtual |
Registers a message decoder with the factory.
| id | Message ID object identifying the message type |
| decoder | Function pointer to the message's decode function |
This method registers a decoder function for a specific message type identified by its ID. If a decoder is already registered for the same message type hash, it will log a warning if the new decoder is different from the existing one, and then replace the existing decoder.
Referenced by registerMessage().
|
protectedvirtual |
Installs default system message decoders.
This method registers decoders for the built-in system messages like ForwardMessage, StopForwardMessage, RequestForwardMessage, and RequestStopForwardMessage. It is called by the constructor.
|
protected |
Map of message type hashes to their corresponding decoder functions.
This map stores the registered decoder functions indexed by the hash value of the message type. It is used by the create() method to look up the appropriate decoder function for a given message type.