VR-Engage  2.2
Loading...
Searching...
No Matches
vreMessageManager.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file vreMessageManager.h
7//! \brief Defines the central message manager for the VREngage messaging system
8//!
9//! This file contains the DtVreMessageManager abstract base class which serves as the
10//! central hub for message routing and dispatching in the VREngage system. It provides
11//! a thread-safe mechanism for registering message handlers, queuing messages, and
12//! dispatching messages to appropriate handlers across multiple threads.
13
14#pragma once
15
18
19#include "vreUtil/assert.h"
20
21namespace makVre
22{
24
25//! \brief Central manager for the VREngage messaging system
26//!
27//! This abstract base class defines the interface for the message manager, which
28//! serves as the central hub for message routing and dispatching in the VREngage
29//! system. It provides thread-safe mechanisms for registering message handlers,
30//! queuing messages, and dispatching messages to appropriate handlers.
31//!
32//! The message manager uses a hierarchical message listener tree to efficiently
33//! route messages to the appropriate handlers based on their message types, and
34//! supports both immediate message dispatch and delayed message queuing.
35//!
36//! The system is implemented as a singleton, with a concrete implementation
37//! (DtTbbVreMessageManager) that uses Intel TBB concurrent containers for
38//! thread-safe message queuing across multiple threads.
40{
41public:
42 //! \brief Statistics for message dispatching
43 //!
44 //! This structure stores statistics about message dispatching activity,
45 //! including the number of messages dispatched in the current frame and
46 //! the types of messages that were dispatched.
48 {
49 //! \brief Number of messages dispatched in the current frame
51
52 //! \brief List of message types that were dispatched
53 std::vector<std::string> messageTypesDispatched;
54 };
55
56 //! \brief Position for adding a message handler in the handler list
57 //!
58 //! This enumeration defines the possible positions for adding a message
59 //! handler in the handler list for a specific message type. The position
60 //! affects the order in which handlers are called when a message is dispatched.
61 enum class HandlerPosition
62 {
63 FRONT = 0, //!< Add the handler at the front of the list (called first)
64 BACK //!< Add the handler at the back of the list (called last)
65 };
66
67 //! \brief Gets the singleton instance of the message manager
68 //! \return Reference to the singleton message manager instance
69 //!
70 //! This static method returns a reference to the singleton instance of the
71 //! message manager. If the instance doesn't exist yet, it creates a new one.
73
74 //! \brief Initializes the message manager
75 //!
76 //! This method must be called in the main thread to initialize the message
77 //! manager. It identifies the main thread, which has special responsibilities
78 //! in the messaging system, such as handling delayed messages.
79 //!
80 //! \note This function must be called from the main thread because the main
81 //! thread performs some additional work in the messaging system.
82 virtual void init() = 0;
83
84 //! \brief Registers a message handler for a specific message type
85 //! \param messageType Type of messages to handle (can include wildcards)
86 //! \param handler Delegate to the handler function
87 //! \param handlerPos Position to add the handler (FRONT or BACK)
88 //!
89 //! This method registers a handler function for a specific message type.
90 //! When a message of the specified type is dispatched, the handler function
91 //! will be called with the message as its parameter. The position parameter
92 //! determines whether the handler is added to the front or back of the
93 //! handler list, affecting its execution order relative to other handlers
94 //! for the same message type.
95 //!
96 //! The message type can include wildcards ("*") to match multiple message
97 //! types. For example, "entity.*" would match all message types that start
98 //! with "entity.".
99 virtual void addHandler(const std::string& messageType, const DtVreMessageDelegate& handler,
100 HandlerPosition handlerPos = HandlerPosition::BACK) = 0;
101
102 //! \brief Unregisters a message handler for a specific message type
103 //! \param messageType Type of messages the handler was registered for
104 //! \param handler Delegate to the handler function to remove
105 //!
106 //! This method removes a previously registered handler function for a
107 //! specific message type. After removal, the handler will no longer be
108 //! called when messages of the specified type are dispatched.
109 virtual void removeHandler(const std::string& messageType, const DtVreMessageDelegate& handler) = 0;
110
111 //! \brief Processes queued messages for the current thread
112 //! \param dt Delta time in seconds since the last tick
113 //!
114 //! This method processes all queued messages for the current thread,
115 //! dispatching them to registered handlers. It also updates the timers
116 //! for delayed messages, making them available for dispatch when their
117 //! delay time has elapsed.
118 //!
119 //! \note Each thread that participates in the messaging system must call
120 //! this method in its thread loop to process messages targeted for
121 //! that thread.
122 virtual void tick(double dt) = 0;
123
124 //! \brief Queues a message for later dispatch
125 //! \param msg Pointer to the message to queue
126 //! \param delay Delay in seconds before the message is dispatched
127 //!
128 //! This method queues a message for later dispatch. If a delay is specified,
129 //! the message will not be dispatched until the delay time has elapsed.
130 //! The message will be dispatched the next time tick() is called after the
131 //! delay has elapsed.
132 //!
133 //! \note The message manager takes ownership of the message and will delete
134 //! it when it is no longer needed.
135 virtual void queueMessage(DtVreMessage* msg, double delay = 0.0) = 0;
136
137 //! \brief Immediately dispatches a message
138 //! \param msg Pointer to the message to dispatch
139 //!
140 //! This method immediately dispatches a message to all registered handlers
141 //! that match its message type. The message is dispatched in the context of
142 //! the calling thread.
143 //!
144 //! \note The message manager takes ownership of the message and will delete
145 //! it when it is no longer needed.
146 virtual void sendMessage(DtVreMessage* msg) = 0;
147
148 //! \brief Gets the message factory associated with this message manager
149 //! \return Reference to the message factory
150 //!
151 //! This method returns a reference to the message factory associated with
152 //! this message manager. The factory is used to create messages from
153 //! serialized data and to register message types.
155
156 //! \brief Gets the event statistics for the current thread
157 //! \return Reference to the event statistics structure
158 //!
159 //! This method returns a reference to the event statistics structure for
160 //! the current thread. The statistics include the number of messages
161 //! dispatched in the current frame and the types of messages that were
162 //! dispatched.
163 virtual const EventStats& stats() = 0;
164
165protected:
166 //! \brief Protected constructor
167 //!
168 //! This constructor is protected to enforce the singleton pattern.
169 //! It creates a new message factory and initializes member variables.
171
172 //! \brief Virtual destructor
173 //!
174 //! Ensures proper cleanup of derived classes.
176
177 //! \brief Singleton instance of the message manager
178 //!
179 //! This static pointer holds the singleton instance of the message manager.
180 //! It is initialized by the constructor and accessed through the instance() method.
182
183 //! \brief Message factory for creating messages
184 //!
185 //! This static pointer holds the message factory associated with the message
186 //! manager. It is initialized by the constructor and accessed through the
187 //! factory() method.
189};
190
191} // namespace makVre
192
193//! \name Message Type Assertion Macros
194//! @{
195
196//! \brief Asserts that a message is of the expected type and casts it
197//! \param msg Pointer to the message to check and cast
198//! \param MsgType Expected message type (class name)
199//! \param msgCast Variable name for the cast message pointer
200//!
201//! This macro checks that a message is of the expected type using dynamic_cast,
202//! and then creates a properly typed pointer to the message using static_cast.
203//! It should be used when handling messages for which you have registered a
204//! handler with the exact message type (no wildcards).
205#define ASSERT_TYPE(msg, MsgType, msgCast) \
206 DtASSERT(dynamic_cast<MsgType*>(msg), "Message is incorrect type"); \
207 MsgType* msgCast = static_cast<MsgType*>(msg);
208
209//! \brief Asserts that a message is of the expected type without casting it
210//! \param msg Pointer to the message to check
211//! \param MsgType Expected message type (class name)
212//!
213//! This macro checks that a message is of the expected type using dynamic_cast,
214//! but does not create a cast pointer. It should be used when handling messages
215//! for which you have registered a handler with the exact message type (no wildcards)
216//! and you don't need to access type-specific members of the message.
217#define ASSERT_TYPE_NO_CAST(msg, MsgType) DtASSERT(dynamic_cast<MsgType*>(msg), "Message is incorrect type");
218
219//! @}
Provides assertion macros and functions for error detection and handling.
Factory for creating VREngage messages from serialized data.
Definition vreMessageFactory.h:48
Abstract base class for all VREngage messages.
Definition vreMessage.h:50
static DtVreMessageManager * theVreMessageManager
Singleton instance of the message manager.
Definition vreMessageManager.h:181
virtual void queueMessage(DtVreMessage *msg, double delay=0.0)=0
Queues a message for later dispatch.
virtual const EventStats & stats()=0
Gets the event statistics for the current thread.
static DtVreMessageManager & instance()
Gets the singleton instance of the message manager.
DtVreMessageManager()
Protected constructor.
virtual void sendMessage(DtVreMessage *msg)=0
Immediately dispatches a message.
virtual DtVreMessageFactory & factory()=0
Gets the message factory associated with this message manager.
virtual ~DtVreMessageManager()
Virtual destructor.
static DtVreMessageFactory * theMessageFactory
Message factory for creating messages.
Definition vreMessageManager.h:188
virtual void tick(double dt)=0
Processes queued messages for the current thread.
virtual void addHandler(const std::string &messageType, const DtVreMessageDelegate &handler, HandlerPosition handlerPos=HandlerPosition::BACK)=0
Registers a message handler for a specific message type.
virtual void init()=0
Initializes the message manager.
HandlerPosition
Position for adding a message handler in the handler list.
Definition vreMessageManager.h:62
@ BACK
Add the handler at the back of the list (called last)
Definition vreMessageManager.h:64
@ FRONT
Add the handler at the front of the list (called first)
Definition vreMessageManager.h:63
virtual void removeHandler(const std::string &messageType, const DtVreMessageDelegate &handler)=0
Unregisters a message handler for a specific message type.
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
DtDelegate< DtVreMessageResult, DtVreMessage * > DtVreMessageDelegate
Delegate type for message handler callbacks.
Definition vreMessage.h:212
Statistics for message dispatching.
Definition vreMessageManager.h:48
std::vector< std::string > messageTypesDispatched
List of message types that were dispatched.
Definition vreMessageManager.h:53
int messagesDispatchedThisFrame
Number of messages dispatched in the current frame.
Definition vreMessageManager.h:50
Defines the base class for all VREngage messages.