VR-Engage  2.3
Loading...
Searching...
No Matches
attributeHandle.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 attributeHandle.h
9//! \brief Provides handle classes for safe access to attributes
10
11#include "vreUtil/export.h"
12
13#include <memory>
14
15namespace makVre
16{
17class DtAttribute;
18template <typename T>
19class DtAttributeType;
20template <typename T>
22
23//! \brief Handle class for safe access to attributes
24//!
25//! This class provides a handle-based mechanism for accessing attributes safely.
26//! It manages reference counting, type safety, and automatic attribute creation
27//! when needed. It works in conjunction with the DtAttribute class to provide
28//! a clean interface for attribute manipulation.
30{
31public:
32 //! \brief Copy constructor
33 //!
34 //! \param other The handle to copy
36
37 //! \brief Default constructor
38 //!
39 //! Creates an unassigned handle
41
42 //! \brief Constructor from attribute path
43 //! \param path Full path, relative to root
44 DtAttributeHandle(const std::string& path);
45
46 //! \brief Constructor from attribute path
47 //! \param path Full path, relative to root
48 DtAttributeHandle(const char* path);
49
50 //! \brief Virtual destructor
51 //!
52 //! Calls recursiveUnlock() to release both single and recursive locks on destruction.
54
55 //! \brief Assigns this handle to refer to the same attribute as another handle
56 //!
57 //! \param other The handle to assign from
58 //! \param attrMustBeSet (Optional) If set to true, asserts if the attribute is not set, default is false
59 virtual void assign(const DtAttributeHandle& other, bool attrMustBeSet = false) const;
60
61 //! \brief Copy assignment operator
62 //!
63 //! Assigns this handle to refer to the same attribute as another handle.
64 //! If this handle currently holds a lock, it is released before reassignment.
65 //!
66 //! \param other The handle to assign from
67 //! \return A reference to this handle
68 const DtAttributeHandle& operator=(const DtAttributeHandle& other) const;
70
71 //! \brief Checks if this handle is assigned to an attribute
72 //!
73 //! \return true if the handle is assigned, false otherwise
74 bool assigned() const;
75
76 //! \brief Checks if this handle is assigned to the same attribute as another handle
77 //!
78 //! \param other The handle to compare with
79 //! \return true if both handles refer to the same attribute, false otherwise
80 bool assignedSame(const DtAttributeHandle& other) const;
81
82 //! \brief Unassigns this handle from its attribute
83 void unassign() const;
84
85 //! \brief Returns true if the handle points to an attribute found via an alias path
86 //!
87 //! \return true if the attribute was found via an alias path, false otherwise
88 bool isAlias() const;
89
90 //! \brief Returns the alias name if the attribute was found via an alias path
91 const std::string& aliasName() const;
92
93 //! \brief Treats this handle as an iterator and advances to the next sibling attribute
94 void next() const;
95
96 //! \brief Dereference operator for accessing the attribute
97 //!
98 //! \return A pointer to the attribute
100
101 //! \brief Const dereference operator for accessing the attribute
102 //!
103 //! \return A const pointer to the attribute
104 const DtAttribute* operator->() const;
105
106 //! \brief Indexing operator for accessing child attributes by name
107 //!
108 //! \param name The name of the child attribute
109 //! \return A handle to the child attribute
110 DtAttributeHandle operator[](const std::string& name);
111
112 //! \brief Const indexing operator for accessing child attributes by name
113 //!
114 //! \param name The name of the child attribute
115 //! \return A const handle to the child attribute
116 const DtAttributeHandle operator[](const std::string& name) const;
117
118 //! \brief Indexing operator for accessing child attributes by index
119 //!
120 //! \param i The index of the child attribute
121 //! \return A handle to the child attribute
122 DtAttributeHandle operator[](const unsigned int i);
123
124 //! \brief Const indexing operator for accessing child attributes by index
125 //!
126 //! \param i The index of the child attribute
127 //! \return A const handle to the child attribute
128 const DtAttributeHandle operator[](const unsigned int i) const;
129
130 //! \brief Locks the attribute, making it read-only for other handles.
131 //!
132 //! This handle acquires an exclusive write lock so that other handles
133 //! cannot modify the attribute until unlock() is called.
134 //!
135 //! \return true if the lock was successfully acquired
136 bool lock();
137
138 //! \brief Unlocks the attribute, allowing other handles to write again.
139 //!
140 //! Releases the exclusive write lock previously acquired by lock().
141 //!
142 //! \return true if the lock was successfully released
143 bool unlock();
144
145 //! \brief Returns true if this handle currently holds the write lock.
146 //!
147 //! \return true if this handle acquired the lock via lock()
148 bool isLocked() const;
149
150 //! \brief Sets the attribute value, bypassing the exclusive write lock.
151 //!
152 //! Temporarily releases the lock, writes the value, then re-acquires the lock.
153 //! This is required because the lock blocks all writers via lockCheck(), including
154 //! the lock-holding handle itself. Safe to call before the first lock() has been
155 //! acquired (e.g. during initialisation); unlock() is a no-op in that case.
156 //!
157 //! \note There is a narrow TOCTOU window between unlock() and set() — a competing
158 //! handle could acquire the lock in that gap. Locking attributes is not a
159 //! security feature, it is intended as an error-checking mechanism to catch
160 //! accidental writes to attributes that should be read-only.
161 //!
162 //! \param data The value to set
163 //! \return true if the value changed
164 template <typename T>
165 bool exclusiveSet(const T& data);
166
167 //! \brief Locks this attribute and all descendants recursively.
168 //!
169 //! Uses best-effort semantics: all lockable nodes are locked even if some cannot be.
170 //!
171 //! \return true if all nodes were successfully locked
173
174 //! \brief Unlocks this attribute and all descendants locked with this handle's key.
175 //!
176 //! \return true if all matching nodes were successfully unlocked
178
179protected:
180 //! \brief Shared pointer type for attributes
181 using Ptr = std::shared_ptr<DtAttribute>;
182
183 //! \brief Constructor that takes a shared pointer to an attribute
184 //!
185 //! \param ptr The shared pointer to the attribute
186 //! \param aliasParent The parent attribute as found in the tree if path was an alias
187 //! \param aliasName The alias name of this attribute if found by an alias path
188 DtAttributeHandle(const Ptr& ptr, const std::string& aliasName = "", const Ptr& aliasParent = Ptr());
189
190 //! \brief Ensures that the handle is assigned to an attribute
191 //!
192 //! Checks if the handle is unassigned and asserts if it is
193 void avoidNull() const;
194
195 //! \brief Generates a new unique key for locking
196 //!
197 //! \return A new unique key
198 static unsigned int nextKey();
199
200protected:
201 //! \brief Shared pointer to the attribute
202 mutable Ptr myAttrPtr;
203
204 //! \brief Pointer to alias parent if found by an alias path
206
207 //! \brief Alias name of the attribute if found by an alias path
208 mutable std::string myAliasName;
209
210 //! \brief Flag indicating if this handle is locked
212
213 //! \brief Key used for locking and unlocking
214 unsigned int myKey;
215
216 friend class DtAttribute;
217};
218
219
220//! \brief Type-specific handle class for attributes
221//!
222//! This template class extends DtAttributeHandle to provide type-safe
223//! access to attributes. It ensures that operations on the attribute
224//! are type-compatible.
225//!
226//! \tparam T The type of the attribute value
227template <typename T>
229{
230public:
231 //! \brief Constructor from a generic attribute handle
232 //!
233 //! \param other The generic handle to convert
235
236 //! \brief Copy constructor
237 //!
238 //! \param other The typed handle to copy
240
241 //! \brief Default constructor
242 //!
243 //! Creates an unassigned typed handle
245
246 //! \brief Virtual destructor
247 virtual ~DtAttributeHandleType() override;
248
249 //! \brief Assigns this handle to refer to the same attribute as another handle
250 //!
251 //! \param other The handle to assign from
252 //! \param attrMustBeSet (Optional) If set to true, asserts if the attribute is not set, default is false
253 void assign(const DtAttributeHandle& other, bool attrMustBeSet = false) const override;
254
255 //! \brief Dereference operator for accessing the typed attribute
256 //!
257 //! \return A pointer to the attribute (base type to satisfy virtual override)
259
260 //! \brief Const dereference operator for accessing the typed attribute
261 //!
262 //! \return A const pointer to the attribute (base type to satisfy virtual override)
264
265 //! \brief Conversion operator to the attribute value type
266 //!
267 //! \return The value of the attribute
268 operator const T() const;
269
270 //! \brief Dereference operator returning the attribute value
271 //!
272 //! \return A const reference to the typed attribute value
273 const T operator*() const;
274
275 //! \brief Assignment operator for setting the attribute value
276 //!
277 //! \param newValue The new value to set
278 //! \return A reference to this handle
279 const DtAttributeHandleType<T>& operator=(const T& newValue) const;
281};
282
283//! \brief Equality comparison between a typed attribute handle and a value
284//!
285//! \tparam T The attribute value type
286//! \param a The typed attribute handle
287//! \param b The value to compare against
288//! \return true if the attribute value equals b
289template <typename T>
291
292//! \brief Equality comparison between a value and a typed attribute handle
293//!
294//! \tparam T The attribute value type
295//! \param a The value to compare against
296//! \param b The typed attribute handle
297//! \return true if a equals the attribute value
298template <typename T>
300
301//! \brief Inequality comparison between a typed attribute handle and a value
302//!
303//! \tparam T The attribute value type
304//! \param a The typed attribute handle
305//! \param b The value to compare against
306//! \return true if the attribute value does not equal b
307template <typename T>
309
310//! \brief Inequality comparison between a value and a typed attribute handle
311//!
312//! \tparam T The attribute value type
313//! \param a The value to compare against
314//! \param b The typed attribute handle
315//! \return true if a does not equal the attribute value
316template <typename T>
318
319} // namespace makVre
unsigned int myKey
Key used for locking and unlocking.
Definition attributeHandle.h:214
DtAttributeHandle(const Ptr &ptr, const std::string &aliasName="", const Ptr &aliasParent=Ptr())
Constructor that takes a shared pointer to an attribute.
static unsigned int nextKey()
Generates a new unique key for locking.
DtAttribute * operator->()
Dereference operator for accessing the attribute.
void unassign() const
Unassigns this handle from its attribute.
bool assigned() const
Checks if this handle is assigned to an attribute.
std::string myAliasName
Alias name of the attribute if found by an alias path.
Definition attributeHandle.h:208
friend class DtAttribute
Definition attributeHandle.h:216
bool unlock()
Unlocks the attribute, allowing other handles to write again.
virtual ~DtAttributeHandle()
Virtual destructor.
const DtAttributeHandle operator[](const unsigned int i) const
Const indexing operator for accessing child attributes by index.
bool lock()
Locks the attribute, making it read-only for other handles.
bool isLocked() const
Returns true if this handle currently holds the write lock.
bool myLocked
Flag indicating if this handle is locked.
Definition attributeHandle.h:211
DtAttributeHandle operator[](const std::string &name)
Indexing operator for accessing child attributes by name.
bool recursiveUnlock()
Unlocks this attribute and all descendants locked with this handle's key.
const std::string & aliasName() const
Returns the alias name if the attribute was found via an alias path.
const DtAttributeHandle operator[](const std::string &name) const
Const indexing operator for accessing child attributes by name.
DtAttributeHandle(const char *path)
Constructor from attribute path.
virtual void assign(const DtAttributeHandle &other, bool attrMustBeSet=false) const
Assigns this handle to refer to the same attribute as another handle.
const DtAttributeHandle & operator=(const DtAttributeHandle &other) const
Copy assignment operator.
bool assignedSame(const DtAttributeHandle &other) const
Checks if this handle is assigned to the same attribute as another handle.
DtAttributeHandle()
Default constructor.
bool exclusiveSet(const T &data)
Sets the attribute value, bypassing the exclusive write lock.
DtAttributeHandle(const std::string &path)
Constructor from attribute path.
Ptr myAliasParent
Pointer to alias parent if found by an alias path.
Definition attributeHandle.h:205
DtAttributeHandle(const DtAttributeHandle &other)
Copy constructor.
std::shared_ptr< DtAttribute > Ptr
Shared pointer type for attributes.
Definition attributeHandle.h:181
void next() const
Treats this handle as an iterator and advances to the next sibling attribute.
DtAttributeHandle & operator=(const DtAttributeHandle &other)
void avoidNull() const
Ensures that the handle is assigned to an attribute.
bool recursiveLock()
Locks this attribute and all descendants recursively.
const DtAttribute * operator->() const
Const dereference operator for accessing the attribute.
Ptr myAttrPtr
Shared pointer to the attribute.
Definition attributeHandle.h:202
DtAttributeHandle operator[](const unsigned int i)
Indexing operator for accessing child attributes by index.
bool isAlias() const
Returns true if the handle points to an attribute found via an alias path.
Type-specific handle class for attributes.
Definition attributeHandle.h:229
DtAttributeHandleType< T > & operator=(const T &newValue)
const T operator*() const
Dereference operator returning the attribute value.
DtAttributeType< T > * operator->()
Dereference operator for accessing the typed attribute.
DtAttributeHandleType(const DtAttributeHandle &other)
Constructor from a generic attribute handle.
DtAttributeHandleType(const DtAttributeHandleType< T > &other)
Copy constructor.
DtAttributeHandleType()
Default constructor.
void assign(const DtAttributeHandle &other, bool attrMustBeSet=false) const override
Assigns this handle to refer to the same attribute as another handle.
const DtAttributeType< T > * operator->() const
Const dereference operator for accessing the typed attribute.
virtual ~DtAttributeHandleType() override
Virtual destructor.
const DtAttributeHandleType< T > & operator=(const T &newValue) const
Assignment operator for setting the attribute value.
Core attribute class for shared data storage.
Definition attribute.h:35
Type-specific attribute class.
Definition attribute.h:937
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
bool operator!=(const makVre::DtAttributeHandleType< T > &a, const T &b)
Inequality comparison between a typed attribute handle and a value.
bool operator==(const makVre::DtAttributeHandleType< T > &a, const T &b)
Equality comparison between a typed attribute handle and a value.