VR-Engage  2.2
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 Virtual destructor
44
45 //! \brief Assigns this handle to refer to the same attribute as another handle
46 //!
47 //! \param other The handle to assign from
48 void assign(const DtAttributeHandle& other);
49
51
52 //! \brief Checks if this handle is assigned to an attribute
53 //!
54 //! \return true if the handle is assigned, false otherwise
55 bool assigned() const;
56
57 //! \brief Checks if this handle is assigned to the same attribute as another handle
58 //!
59 //! \param other The handle to compare with
60 //! \return true if both handles refer to the same attribute, false otherwise
61 bool assignedSame(const DtAttributeHandle& other) const;
62
63 //! \brief Unassigns this handle from its attribute
64 void unassign();
65
66 //! \brief Converts this handle to a typed handle
67 //!
68 //! \return A typed handle to the same attribute
69 template <typename T>
71
72 //! \brief Converts this handle to a const typed handle
73 //!
74 //! \return A const typed handle to the same attribute
75 template <typename T>
77
78 // template <typename T>
79 // operator DtAttributeHandleType<T>();
80 //
81 // template <typename T>
82 // operator const DtAttributeHandleType<T>() const;
83
84 //! \brief Dereference operator for accessing the attribute
85 //!
86 //! \return A pointer to the attribute
88
89 //! \brief Const dereference operator for accessing the attribute
90 //!
91 //! \return A const pointer to the attribute
92 virtual const DtAttribute* operator->() const;
93
94 //! \brief Indexing operator for accessing child attributes by name
95 //!
96 //! \param name The name of the child attribute
97 //! \return A handle to the child attribute
98 DtAttributeHandle operator[](const std::string& name);
99
100 //! \brief Const indexing operator for accessing child attributes by name
101 //!
102 //! \param name The name of the child attribute
103 //! \return A const handle to the child attribute
104 const DtAttributeHandle operator[](const std::string& name) const;
105
106 //! \brief Indexing operator for accessing child attributes by index
107 //!
108 //! \param i The index of the child attribute
109 //! \return A handle to the child attribute
110 DtAttributeHandle operator[](const unsigned int i);
111
112 //! \brief Const indexing operator for accessing child attributes by index
113 //!
114 //! \param i The index of the child attribute
115 //! \return A const handle to the child attribute
116 const DtAttributeHandle operator[](const unsigned int i) const;
117
118 // template <typename T>
119 // operator T() const;
120 //
121 // template <typename T>
122 // DtAttributeHandle& operator = (const T& newValue);
123
124 //! \brief Locks the attribute
125 //!
126 //! \return true if the attribute was successfully locked, false otherwise
127 bool lock();
128
129 //! \brief Unlocks the attribute
130 //!
131 //! \return true if the attribute was successfully unlocked, false otherwise
132 bool unlock();
133
134 //! \brief Checks if this handle has locking enabled
135 //!
136 //! \return true if locking is enabled for this handle, false otherwise
137 bool locking() const;
138
139protected:
140 //! \brief Shared pointer type for attributes
141 using Ptr = std::shared_ptr<DtAttribute>;
142
143 //! \brief Constructor that takes a shared pointer to an attribute
144 //!
145 //! \param ptr The shared pointer to the attribute
147
148 //! \brief Ensures that the handle is assigned to an attribute
149 //!
150 //! Checks if the handle is unassigned and asserts if it is
151 void avoidNull() const;
152
153 //! \brief Generates a new unique key for locking
154 //!
155 //! \return A new unique key
156 static unsigned int nextKey();
157
158protected:
159 //! \brief Shared pointer to the attribute
160 mutable Ptr myPtr;
161
162 //! \brief Flag indicating if this handle uses locking
164
165 //! \brief Key used for locking and unlocking
166 unsigned int myKey;
167
168 friend class DtAttribute;
169};
170
171
172//! \brief Type-specific handle class for attributes
173//!
174//! This template class extends DtAttributeHandle to provide type-safe
175//! access to attributes. It ensures that operations on the attribute
176//! are type-compatible.
177//!
178//! \tparam T The type of the attribute value
179template <typename T>
181{
182public:
183 //! \brief Constructor from a generic attribute handle
184 //!
185 //! \param other The generic handle to convert
187
188 //! \brief Copy constructor
189 //!
190 //! \param other The typed handle to copy
192
193 //! \brief Default constructor
194 //!
195 //! Creates an unassigned typed handle
197
198 //! \brief Virtual destructor
199 virtual ~DtAttributeHandleType() override;
200
201 //! \brief Dereference operator for accessing the typed attribute
202 //!
203 //! \return A pointer to the typed attribute
204 virtual DtAttributeType<T>* operator->() override;
205
206 //! \brief Const dereference operator for accessing the typed attribute
207 //!
208 //! \return A const pointer to the typed attribute
209 virtual const DtAttributeType<T>* operator->() const override;
210
211 //! \brief Conversion operator to the attribute value type
212 //!
213 //! \return The value of the attribute
214 operator const T() const;
215
216 const T& operator*() const;
217
218 //! \brief Assignment operator for setting the attribute value
219 //!
220 //! \param newValue The new value to set
221 //! \return A reference to this handle
223};
224
225template <typename T>
227
228template <typename T>
230
231template <typename T>
233
234template <typename T>
236
237} // namespace makVre
unsigned int myKey
Key used for locking and unlocking.
Definition attributeHandle.h:166
static unsigned int nextKey()
Generates a new unique key for locking.
bool assigned() const
Checks if this handle is assigned to an attribute.
friend class DtAttribute
Definition attributeHandle.h:168
virtual DtAttribute * operator->()
Dereference operator for accessing the attribute.
bool unlock()
Unlocks the attribute.
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.
DtAttributeHandle operator[](const std::string &name)
Indexing operator for accessing child attributes by name.
DtAttributeHandle(Ptr ptr)
Constructor that takes a shared pointer to an attribute.
void assign(const DtAttributeHandle &other)
Assigns this handle to refer to the same attribute as another handle.
const DtAttributeHandle operator[](const std::string &name) const
Const indexing operator for accessing child attributes by name.
Ptr myPtr
Shared pointer to the attribute.
Definition attributeHandle.h:160
bool assignedSame(const DtAttributeHandle &other) const
Checks if this handle is assigned to the same attribute as another handle.
DtAttributeHandle()
Default constructor.
DtAttributeHandle(const DtAttributeHandle &other)
Copy constructor.
void unassign()
Unassigns this handle from its attribute.
bool locking() const
Checks if this handle has locking enabled.
std::shared_ptr< DtAttribute > Ptr
Shared pointer type for attributes.
Definition attributeHandle.h:141
const DtAttributeHandleType< T > as() const
Converts this handle to a const typed handle.
DtAttributeHandleType< T > as()
Converts this handle to a typed handle.
DtAttributeHandle & operator=(const DtAttributeHandle &other)
void avoidNull() const
Ensures that the handle is assigned to an attribute.
virtual const DtAttribute * operator->() const
Const dereference operator for accessing the attribute.
DtAttributeHandle operator[](const unsigned int i)
Indexing operator for accessing child attributes by index.
bool myLocking
Flag indicating if this handle uses locking.
Definition attributeHandle.h:163
Type-specific handle class for attributes.
Definition attributeHandle.h:181
DtAttributeHandleType< T > & operator=(const T &newValue)
Assignment operator for setting the attribute value.
DtAttributeHandleType(const DtAttributeHandle &other)
Constructor from a generic attribute handle.
DtAttributeHandleType(const DtAttributeHandleType< T > &other)
Copy constructor.
virtual DtAttributeType< T > * operator->() override
Dereference operator for accessing the typed attribute.
DtAttributeHandleType()
Default constructor.
virtual const DtAttributeType< T > * operator->() const override
Const dereference operator for accessing the typed attribute.
virtual ~DtAttributeHandleType() override
Virtual destructor.
const T & operator*() const
Core attribute class for shared data storage.
Definition attribute.h:34
Type-specific attribute class.
Definition attribute.h:516
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
bool operator!=(const makVre::DtAttributeHandleType< T > &a, const T &b)
bool operator==(const makVre::DtAttributeHandleType< T > &a, const T &b)