VR-Engage  2.2
Loading...
Searching...
No Matches
vreMessageFactory.h
Go to the documentation of this file.
1/*********************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4*********************************************************************************/
5
6#pragma once
7
8//! \file vreMessageFactory.h
9//! \ingroup makEvent
10//! \brief Defines a message factory for creating VREngage messages from serialized data
11//!
12//! This file contains the DtVreMessageFactory class which implements the factory pattern
13//! for creating DtVreMessage objects from serialized byte arrays. It maintains a registry
14//! of message types and their corresponding decoder functions, enabling dynamic message
15//! creation based on the message type encoded in the data.
16
18
19#include <map>
20
21namespace makVre
22{
23
24class DtVreMessage;
25class DtVreMessageId;
26
27//! \brief Function pointer type for message decoder functions
28//! \param bytes Pointer to the buffer containing the serialized message
29//! \param length Length of the buffer in bytes
30//! \return Pointer to a newly created message object, or nullptr if decoding fails
31//!
32//! This typedef defines the signature for message decoder functions. Each message
33//! class must provide a static decode function with this signature to be used
34//! by the message factory for creating messages from serialized data.
35using Decoder = DtVreMessage* (*)(char* bytes, int length);
36
37//! \brief Factory for creating VREngage messages from serialized data
38//!
39//! This class implements the factory pattern for creating DtVreMessage objects
40//! from serialized byte arrays. It maintains a registry of message types and their
41//! corresponding decoder functions, enabling dynamic message creation based on the
42//! message type encoded in the data.
43//!
44//! The factory comes pre-registered with decoders for system messages like
45//! ForwardMessage and StopForwardMessage. Additional message types can be
46//! registered using the registerMessage() template method.
48{
49public:
50 //! \brief Constructor
51 //!
52 //! Initializes a new message factory and registers default system messages
53 //! by calling installDefaultMessages().
55
56 //! \brief Virtual destructor
57 //!
58 //! Cleans up resources used by the message factory.
60
61 //! \brief Creates a message object from serialized data
62 //! \param bytes Pointer to the buffer containing the serialized message
63 //! \param length Length of the buffer in bytes
64 //! \return Pointer to a newly created message object, or nullptr if creation fails
65 //!
66 //! This method deserializes the message header to determine the message type,
67 //! looks up the appropriate decoder function in the registry, and calls that
68 //! function to create the message object. It returns nullptr if no decoder is
69 //! found for the message type or if an error occurs during decoding.
70 virtual DtVreMessage* create(char* bytes, int length);
71
72 //! \brief Registers a message type with the factory
73 //! \tparam MSG Message class type to register
74 //!
75 //! This template method registers a message class with the factory by
76 //! retrieving its ID and decode function. The message class must provide:
77 //! - A static theId() method that returns the message's DtVreMessageId
78 //! - A static decode() method with the Decoder signature
79 //!
80 //! Example usage:
81 //! \code
82 //! factory.registerMessage<MyCustomMessage>();
83 //! \endcode
84 template <typename MSG>
86 {
87 registerCreator(MSG::theId(), &MSG::decode);
88 }
89
90 //! \brief Clears all registered message decoders
91 //!
92 //! This method removes all registered message types and their decoders
93 //! from the factory. After calling this method, the factory will not be able
94 //! to create any messages until new ones are registered.
95 virtual void clear();
96
97protected:
98 //! \brief Registers a message decoder with the factory
99 //! \param id Message ID object identifying the message type
100 //! \param decoder Function pointer to the message's decode function
101 //!
102 //! This method registers a decoder function for a specific message type
103 //! identified by its ID. If a decoder is already registered for the same
104 //! message type hash, it will log a warning if the new decoder is different
105 //! from the existing one, and then replace the existing decoder.
106 virtual void registerCreator(const DtVreMessageId& id, Decoder decoder);
107
108 //! \brief Installs default system message decoders
109 //!
110 //! This method registers decoders for the built-in system messages like
111 //! ForwardMessage, StopForwardMessage, RequestForwardMessage, and
112 //! RequestStopForwardMessage. It is called by the constructor.
114
115 //! \brief Type definition for the map of message type hashes to decoder functions
116 using DecoderMap = std::map<unsigned int, Decoder>;
117
118 //! \brief Map of message type hashes to their corresponding decoder functions
119 //!
120 //! This map stores the registered decoder functions indexed by the hash value
121 //! of the message type. It is used by the create() method to look up the
122 //! appropriate decoder function for a given message type.
124};
125
126} // namespace makVre
DtVreMessageFactory()
Constructor.
virtual ~DtVreMessageFactory()
Virtual destructor.
DecoderMap myCreatorMap
Map of message type hashes to their corresponding decoder functions.
Definition vreMessageFactory.h:123
virtual void clear()
Clears all registered message decoders.
virtual void registerCreator(const DtVreMessageId &id, Decoder decoder)
Registers a message decoder with the factory.
virtual void installDefaultMessages()
Installs default system message decoders.
virtual DtVreMessage * create(char *bytes, int length)
Creates a message object from serialized data.
void registerMessage()
Registers a message type with the factory.
Definition vreMessageFactory.h:85
std::map< unsigned int, Decoder > DecoderMap
Type definition for the map of message type hashes to decoder functions.
Definition vreMessageFactory.h:116
Abstract base class for all VREngage messages.
Definition vreMessage.h:50
Message identifier class for the VREngage messaging system.
Definition vreMessageId.h:35
Defines export macros for the VREngage Message Manager library.
#define MESSAGEMANAGER_DLL
Export/import macro for non-Windows platforms.
Definition export.h:39
Include export definitions for this library.
Definition glsVreMessageUtil.h:49
DtVreMessage *(*)(char *bytes, int length) Decoder
Function pointer type for message decoder functions.
Definition vreMessageFactory.h:35