VR-Engage  2.3
Loading...
Searching...
No Matches
localMessage.h
Go to the documentation of this file.
1/*********************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4*********************************************************************************/
5
6//! \file command.h
7//! \brief Defines generic command message class
8//!
9//! Command messages can be used to send arbitrary commands with parameters to the
10//! simulation or other components.
11
12#pragma once
13
16
17#include "vreMessages/export.h"
18
19#include <vlpi/entityIdentifier.h>
20
21class DtRwLocationReference;
22
23namespace makVre
24{
25
26//! \brief Generic message type with variable parameters
27//!
28//! Resuable generic message type supports variable parameters using template methods.
29//!
30//! Does not support network replication. Use only within the application instance.
32{
33public:
34 //! \brief Used to define the message type
35 //!
36 //! Given type string is used to define the (non-static) messageId and messageType,
37 //! as "local.message.{type}"
38 virtual void setType(const std::string& type);
39
40 //! \brief Base structure for script attributes
41 //!
42 //! This structure serves as the base for all Parameter types, containing
43 //! the Parameter name and type information. Specific Parameter types
44 //! derive from this base structure and add a typed value field.
46 {
47 };
48
49 //! \brief Templated structure for typed script attributes
50 //! \tparam T Data type of the Parameter value
51 //!
52 //! This templated structure derives from BaseParameter and adds a typed value
53 //! field. It is used to store attributes of different data types in a type-safe
54 //! manner.
55 template <typename T>
56 struct Parameter : public ParameterBase
57 {
58 //! \brief Type hash for runtime type verification
59 size_t type;
60
61 //! \brief Value of the Parameter
63 };
64
65 //! \brief Type definition for parameter map
66 using ParameterMap = std::map<std::string, std::shared_ptr<ParameterBase>>;
67
68 //! \brief Virtual destructor
69 //!
70 //! Cleans up resources used by the message, including allocated Parameter objects.
71 virtual ~DtLocalMessage() override;
72
73 //! \brief Creates a new instance of this message type
74 //! \return Pointer to a newly created message of this specific type
75 //!
76 //! This static method creates a new instance of this message type and
77 //! returns a pointer to the specific message type, avoiding the need for
78 //! casting by the caller.
80
81 static const std::string& theType();
83
84 //! \brief Get the name of the message type
85 virtual const std::string& type() const override;
86 virtual const makVre::DtVreMessageId& messageId() const override;
87
88 //! \brief Return const ref to parameter map
89 virtual const ParameterMap& parameterMap() const;
90
91 //! \brief Return value of parameter with given name
92 //! \tparam T Type of parameter value
93 //! \param name Name of parameter to find
94 //! \return Value of parameter
95 template <typename T>
96 const T& getParameter(const std::string& name);
97
98 //! \brief Adds an parameter to this message
99 //! \tparam T Type of the parameter value
100 //! \param name Name of the parameter
101 //! \param data Value of the parameter
102 template <typename T>
103 void addParameter(const std::string& name, T data);
104
105protected:
106 //! \brief Static message ID for this message type
107 //!
108 //! This static member holds the unique identifier for this message type.
110
111 //! \brief Protected constructor
112 //!
113 //! Initializes a new message with default values. This constructor is protected
114 //! to enforce the use of the static create() method for creating new instances.
116
117 //! \brief Holds a type ID for this message
119
120 //! \brief List of parameters for this message
122};
123
124//===================================================================================
125
126template <typename T>
127const T& DtLocalMessage::getParameter(const std::string& name)
128{
129 auto findIt = myParameters.find(name);
130 if (findIt != myParameters.end())
131 {
132 DtASSERT(typeid(T).hash_code() == (*findIt)->type, "Incorrect template type used for getParameter");
133 return (*findIt)->value;
134 }
135 static T zeroValue();
136 return zeroValue();
137}
138
139template <typename T>
140void DtLocalMessage::addParameter(const std::string& name, T data)
141{
142 auto param = std::make_shared<Parameter<T>>();
143 param->type = typeid(T).hash_code();
144 param->value = data;
145 myParameters[name] = param;
146};
147
148} // namespace makVre
#define DtASSERT(exp, description)
Primary assertion macro for runtime validation.
Definition assert.h:58
static makVre::DtVreMessageId theMessageId
Static message ID for this message type.
Definition localMessage.h:109
static const makVre::DtVreMessageId & theId()
virtual const std::string & type() const override
Get the name of the message type.
std::map< std::string, std::shared_ptr< ParameterBase > > ParameterMap
Type definition for parameter map.
Definition localMessage.h:66
static DtLocalMessage * create()
Creates a new instance of this message type.
static const std::string & theType()
virtual void setType(const std::string &type)
Used to define the message type.
DtVreMessageId myMessageId
Holds a type ID for this message.
Definition localMessage.h:118
ParameterMap myParameters
List of parameters for this message.
Definition localMessage.h:121
DtLocalMessage()
Protected constructor.
virtual const makVre::DtVreMessageId & messageId() const override
Gets the message ID object.
const T & getParameter(const std::string &name)
Return value of parameter with given name.
Definition localMessage.h:127
virtual ~DtLocalMessage() override
Virtual destructor.
void addParameter(const std::string &name, T data)
Adds an parameter to this message.
Definition localMessage.h:140
virtual const ParameterMap & parameterMap() const
Return const ref to parameter map.
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 Messages library.
#define VREMESSAGES_DLL
Export/import macro for non-Windows platforms.
Definition export.h:39
!
Definition glsVreMessageUtil.h:50
Base structure for script attributes.
Definition localMessage.h:46
Templated structure for typed script attributes.
Definition localMessage.h:57
T value
Value of the Parameter.
Definition localMessage.h:62
size_t type
Type hash for runtime type verification.
Definition localMessage.h:59
Defines the base class for all VREngage messages.
Defines message identifier class for the VREngage messaging system.