VR-Engage  2.2
Loading...
Searching...
No Matches
attribute_data.h
Go to the documentation of this file.
1/******************************************************************************
2 ** Copyright (c) 2025 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 Casts this attribute data to a specific type
47 //! \tparam T The type to cast to
48 //! \return Reference to this object as the requested type
49 //! \note No type checking is performed; use verifyType() first to ensure safety
50 template <typename T>
52 {
53 return *static_cast<DtAttributeDataType<T>*>(this);
54 }
55
56 //! \brief Casts this attribute data to a specific type (const version)
57 //! \tparam T The type to cast to
58 //! \return Const reference to this object as the requested type
59 //! \note No type checking is performed; use verifyType() first to ensure safety
60 template <typename T>
62 {
63 return *static_cast<const DtAttributeDataType<T>*>(this);
64 }
65
66 //! \brief Gets the attribute value as a string
67 //! \return String representation of the current value
68 virtual std::string getAsString() const = 0;
69
70 //! \brief Sets the attribute value from a string
71 //! \param newValue String representation of the value to set
72 //! \return True if conversion and setting succeeded, false otherwise
73 virtual bool setFromString(const std::string& newValue) = 0;
74
75 //! \brief Gets a raw pointer to the attribute data
76 //! \return Void pointer to the raw data
77 virtual const void* raw() const = 0;
78};
79
80//! \brief Template for type-specific attribute data storage
81//! \tparam T The data type stored by this attribute
82//!
83//! This template class provides type-safe storage for attribute values of a specific type.
84//! It maintains the current value and the previous value to support change detection.
85template <typename T>
87{
88public:
89 //! \brief Default constructor
90 //!
91 //! Initializes both current and previous values to default-constructed instances
93 : myVi(0)
94 , myValue{T(), T()}
95 {
96 }
97
98 //! \brief Virtual destructor
99 virtual ~DtAttributeDataType() override {}
100
101 //! \brief Gets the type name of this attribute data
102 //! \return String representation of the data type
103 virtual const std::string& type() const override;
104
105 //! \brief Gets a hash of the type for faster comparisons
106 //! \return Hash code for the data type
107 virtual std::size_t typeHash() const override;
108
109 //! \brief Verifies that this attribute is of the expected type
110 //! \return True if the type matches the template parameter, false otherwise
111 //! \note Use this method before as<T>() to ensure type safety
112 bool verifyType() const;
113
114 //! \brief Gets the current value of the attribute
115 //! \return Const reference to the current value
116 const T& get() const;
117
118 //! \brief Sets a new value for the attribute
119 //! \param newValue The new value to set
120 //! \return True if the new value differs from the current value
121 bool set(const T& newValue);
122
123 //! \brief Gets the attribute value as a string
124 //! \return String representation of the current value
125 //! \note Uses toString() for the templated type
126 virtual std::string getAsString() const override;
127
128 //! \brief Sets the attribute value from a string
129 //! \param newValue String representation of the value to set
130 //! \return True if conversion and setting succeeded, false otherwise
131 //! \note Uses fromString() for the templated type
132 virtual bool setFromString(const std::string& newValue) override;
133
134 //! \brief Gets the previous value of the attribute
135 //! \return Const reference to the value before the last change
136 //! \note Useful for detecting and responding to changes
137 const T& previousValue() const;
138
139 //! \brief Gets the static type name for this attribute type
140 //! \return String representation of the data type
141 static const std::string& Type();
142
143 //! \brief Gets the static type hash for this attribute type
144 //! \return Hash code for the data type
145 static std::size_t TypeHash();
146
147 //! \brief Gets a raw pointer to the attribute data
148 //! \return Void pointer to the raw data
149 virtual const void* raw() const override;
150
151protected:
152 //! Allow DtAttribute direct access to internals
153 friend class DtAttribute;
154
155 //! \brief Gets a reference to the current value for modification
156 //! \return Reference to the current value
157 T& data();
158
159 //! \brief Gets a reference to the previous value for modification
160 //! \return Reference to the previous value
162
163 //! \brief Array storing current and previous values
164 //!
165 //! Two values are stored, one is current, one is previous
167
168 //! \brief Index indicating which value is current (0 or 1)
169 //!
170 //! Toggles between 0 and 1 to swap current and previous values efficiently
171 unsigned char myVi;
172};
173
174} // 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:61
virtual bool setFromString(const std::string &newValue)=0
Sets the attribute value from a string.
DtAttributeData()
Default constructor.
Definition attribute_data.h:33
DtAttributeDataType< T > & as()
Casts this attribute data to a specific type.
Definition attribute_data.h:51
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:87
T & prevData()
Gets a reference to the previous value for modification.
friend class DtAttribute
Allow DtAttribute direct access to internals.
Definition attribute_data.h:153
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:92
virtual bool setFromString(const std::string &newValue) override
Sets the attribute value from a string.
virtual ~DtAttributeDataType() override
Virtual destructor.
Definition attribute_data.h:99
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:171
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:166
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
Include export definitions for this library.
Definition glsVreMessageUtil.h:49