VR-Engage  2.2
Loading...
Searching...
No Matches
makVre::DtVreMessageFactory Class Reference

Detailed Description

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 DtVreMessagecreate (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
 

Member Typedef Documentation

◆ DecoderMap

using makVre::DtVreMessageFactory::DecoderMap = std::map<unsigned int, Decoder>
protected

Type definition for the map of message type hashes to decoder functions.

Constructor & Destructor Documentation

◆ DtVreMessageFactory()

makVre::DtVreMessageFactory::DtVreMessageFactory ( )

Constructor.

Initializes a new message factory and registers default system messages by calling installDefaultMessages().

◆ ~DtVreMessageFactory()

virtual makVre::DtVreMessageFactory::~DtVreMessageFactory ( )
virtual

Virtual destructor.

Cleans up resources used by the message factory.

Member Function Documentation

◆ create()

virtual DtVreMessage * makVre::DtVreMessageFactory::create ( char * bytes,
int length )
virtual

Creates a message object from serialized data.

Parameters
bytesPointer to the buffer containing the serialized message
lengthLength of the buffer in bytes
Returns
Pointer to a newly created message object, or nullptr if creation fails

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.

◆ registerMessage()

template<typename MSG>
void makVre::DtVreMessageFactory::registerMessage ( )
inline

Registers a message type with the factory.

Template Parameters
MSGMessage 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:

factory.registerMessage<MyCustomMessage>();

References registerCreator().

◆ clear()

virtual void makVre::DtVreMessageFactory::clear ( )
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.

◆ registerCreator()

virtual void makVre::DtVreMessageFactory::registerCreator ( const DtVreMessageId & id,
Decoder decoder )
protectedvirtual

Registers a message decoder with the factory.

Parameters
idMessage ID object identifying the message type
decoderFunction 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().

◆ installDefaultMessages()

virtual void makVre::DtVreMessageFactory::installDefaultMessages ( )
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.

Member Data Documentation

◆ myCreatorMap

DecoderMap makVre::DtVreMessageFactory::myCreatorMap
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.


The documentation for this class was generated from the following file: