VR-Engage  2.3
Loading...
Searching...
No Matches
attribute_data.h
Go to the documentation of this file.
1/******************************************************************************
2 ** Copyright (c) 2026 MAK Technologies
3 ** All rights reserved.
4 ******************************************************************************/
5
6//! \file attribute_data.h
7//! \ingroup vreUtil
8//! \brief Defines data types for the attribute system in VREngage
9//!
10//! This file provides the base classes for attribute data storage and
11//! type-specific implementations. It supports the attribute system by providing
12//! type-safe data access with history tracking for change detection.
13
14#pragma once
15
16#include "vreUtil/export.h"
17
18namespace makVre
19{
20
21template <typename T>
23
24//! \brief Base class for attribute data storage
25//!
26//! This abstract class provides the interface for type-independent attribute
27//! data storage. It allows type-safe access to attribute values through dynamic
28//! casting and defines operations common to all attribute types.
30{
31public:
32 //! \brief Default constructor
34
35 //! \brief Virtual destructor
36 virtual ~DtAttributeData() {}
37
38 //! \brief Gets the type name of this attribute data
39 //! \return String representation of the data type
40 virtual const std::string& type() const = 0;
41
42 //! \brief Gets a hash of the type for faster comparisons
43 //! \return Hash code for the data type
44 virtual std::size_t typeHash() const = 0;
45
46 //! @brief String representation for undefined type ("undefined")
47 //! @return A constant reference to the string "undefined".
48 static const std::string& UndefinedType();
49
50 //! \brief Casts this attribute data to a specific type
51 //! \tparam T The type to cast to
52 //! \return Reference to this object as the requested type
53 //! \note No type checking is performed; use verifyType() first to ensure safety
54 template <typename T>
56 {
57 return *static_cast<DtAttributeDataType<T>*>(this);
58 }
59
60 //! \brief Casts this attribute data to a specific type (const version)
61 //! \tparam T The type to cast to
62 //! \return Const reference to this object as the requested type
63 //! \note No type checking is performed; use verifyType() first to ensure safety
64 template <typename T>
66 {
67 return *static_cast<const DtAttributeDataType<T>*>(this);
68 }
69
70 //! \brief Gets the attribute value as a string
71 //! \return String representation of the current value
72 virtual std::string getAsString() const = 0;
73
74 //! \brief Sets the attribute value from a string
75 //! \param newValue String representation of the value to set
76 //! \return True if conversion and setting succeeded, false otherwise
77 virtual bool setFromString(const std::string& newValue) = 0;
78
79 //! \brief Gets a raw pointer to the attribute data
80 //! \return Void pointer to the raw data
81 virtual const void* raw() const = 0;
82};
83
84//! \brief Template for type-specific attribute data storage
85//! \tparam T The data type stored by this attribute
86//!
87//! This template class provides type-safe storage for attribute values of a specific type.
88//! It maintains the current value and the previous value to support change detection.
89template <typename T>
91{
92public:
93 //! \brief Default constructor
94 //!
95 //! Initializes both current and previous values to default-constructed instances
97 : myVi(0)
98 , myValue{T(), T()}
99 {
100 }
101
102 //! \brief Virtual destructor
103 virtual ~DtAttributeDataType() override {}
104
105 //! \brief Gets the type name of this attribute data
106 //! \return typeid(T).name()
107 virtual const std::string& type() const override;
108
109 //! \brief Gets a hash of the type for faster comparisons
110 //! \return typeid(T).hash_code()
111 virtual std::size_t typeHash() const override;
112
113 //! \brief Verifies that this attribute is of the expected type
114 //! \return True if the type matches the template parameter, false otherwise
115 //! \note Use this method before as<T>() to ensure type safety
116 bool verifyType() const;
117
118 //! \brief Gets the current value of the attribute
119 //! \return Const reference to the current value
120 const T& get() const;
121
122 //! \brief Sets a new value for the attribute
123 //! \param newValue The new value to set
124 //! \return True if the new value differs from the current value
125 bool set(const T& newValue);
126
127 //! \brief Gets the attribute value as a string
128 //! \return String representation of the current value
129 //! \note Uses toString() for the templated type
130 virtual std::string getAsString() const override;
131
132 //! \brief Sets the attribute value from a string
133 //! \param newValue String representation of the value to set
134 //! \return True if conversion and setting succeeded, false otherwise
135 //! \note Uses fromString() for the templated type
136 virtual bool setFromString(const std::string& newValue) override;
137
138 //! \brief Gets the previous value of the attribute
139 //! \return Const reference to the value before the last change
140 //! \note Useful for detecting and responding to changes
141 const T& previousValue() const;
142
143 //! \brief Gets the static type name for this attribute type
144 //! \return String representation of the data type
145 static const std::string& Type();
146
147 //! \brief Gets the static type hash for this attribute type
148 //! \return Hash code for the data type
149 static std::size_t TypeHash();
150
151 //! \brief Gets a raw pointer to the attribute data
152 //! \return Void pointer to the raw data
153 virtual const void* raw() const override;
154
155protected:
156 //! Allow DtAttribute direct access to internals
157 friend class DtAttribute;
158
159 //! \brief Gets a reference to the current value for modification
160 //! \return Reference to the current value
161 T& data();
162
163 //! \brief Gets a reference to the previous value for modification
164 //! \return Reference to the previous value
166
167 //! \brief Array storing current and previous values
168 //!
169 //! Two values are stored, one is current, one is previous
171
172 //! \brief Index indicating which value is current (0 or 1)
173 //!
174 //! Toggles between 0 and 1 to swap current and previous values efficiently
175 unsigned char myVi;
176};
177
178} // namespace makVre
virtual ~DtAttributeData()
Virtual destructor.
Definition attribute_data.h:36
virtual const void * raw() const =0
Gets a raw pointer to the attribute data.
virtual const std::string & type() const =0
Gets the type name of this attribute data.
const DtAttributeDataType< T > & as() const
Casts this attribute data to a specific type (const version)
Definition attribute_data.h:65
virtual bool setFromString(const std::string &newValue)=0
Sets the attribute value from a string.
DtAttributeData()
Default constructor.
Definition attribute_data.h:33
static const std::string & UndefinedType()
String representation for undefined type ("undefined")
DtAttributeDataType< T > & as()
Casts this attribute data to a specific type.
Definition attribute_data.h:55
virtual std::string getAsString() const =0
Gets the attribute value as a string.
virtual std::size_t typeHash() const =0
Gets a hash of the type for faster comparisons.
Template for type-specific attribute data storage.
Definition attribute_data.h:91
T & prevData()
Gets a reference to the previous value for modification.
friend class DtAttribute
Allow DtAttribute direct access to internals.
Definition attribute_data.h:157
virtual std::size_t typeHash() const override
Gets a hash of the type for faster comparisons.
static const std::string & Type()
Gets the static type name for this attribute type.
DtAttributeDataType()
Default constructor.
Definition attribute_data.h:96
virtual bool setFromString(const std::string &newValue) override
Sets the attribute value from a string.
virtual ~DtAttributeDataType() override
Virtual destructor.
Definition attribute_data.h:103
static std::size_t TypeHash()
Gets the static type hash for this attribute type.
virtual const std::string & type() const override
Gets the type name of this attribute data.
bool verifyType() const
Verifies that this attribute is of the expected type.
unsigned char myVi
Index indicating which value is current (0 or 1)
Definition attribute_data.h:175
T & data()
Gets a reference to the current value for modification.
const T & get() const
Gets the current value of the attribute.
T myValue[2]
Array storing current and previous values.
Definition attribute_data.h:170
bool set(const T &newValue)
Sets a new value for the attribute.
virtual std::string getAsString() const override
Gets the attribute value as a string.
const T & previousValue() const
Gets the previous value of the attribute.
virtual const void * raw() const override
Gets a raw pointer to the attribute data.
Defines export macros for the VREngage Utility library.
#define UTIL_DLL
Export/import macro for non-Windows platforms.
Definition export.h:39
!
Definition glsVreMessageUtil.h:50