VR-Engage  2.3
Loading...
Searching...
No Matches
attributeCallback.h
Go to the documentation of this file.
1/******************************************************************************
2 ** Copyright (c) 2026 MAK Technologies
3 ** All rights reserved.
4 ******************************************************************************/
5
6//! \file attributeCallback.h
7//! \ingroup vreUtil
8//! \brief Provides a callback mechanism for attribute changes in the VREngage system
9//!
10//! This file defines the DtAttributeChangeCallback class which provides a scoped
11//! callback wrapper for attribute changes. It allows objects to register callbacks
12//! that are invoked when attributes are modified.
13
14#pragma once
15
16#include "vreUtil/export.h"
17#include "vreutil/delegate.h"
18
19#include <list>
20#include <map>
21
22namespace makVre
23{
24class DtAttribute;
26
27//! \brief Abstract base class for all attribute change callbacks
28//!
29//! Provides the connection management interface for registering and deregistering
30//! callbacks with DtAttribute instances. Derived classes implement the invoke()
31//! method to define how they respond to attribute changes. The destructor
32//! automatically disconnects from all connected attributes.
34{
35public:
36 //! \brief Constructor
37 //!
38 //! Captures the calling thread's ID so that callbacks are dispatched on
39 //! the correct thread.
41
42 //! \brief Virtual destructor
43 //!
44 //! Automatically disconnects from all connected attributes
46
47 //! \brief Connects the callback to an attribute
48 //! \param attribute Reference to the attribute to connect to
49 //! \param invokeNowIfSet Optional, default False. If set True, the callback is
50 //! invoked immediately, if the attribute value has been set.
51 virtual void connect(DtAttributeHandle attribute, bool invokeNowIfSet = false);
52
53 //! \brief Disconnects the callback from a specific attribute
54 //! \param attribute Reference to the attribute to disconnect from
55 virtual void disconnect(DtAttributeHandle attribute);
56
57 //! \brief Disconnects the callback from all connected attributes
58 virtual void disconnectAll();
59
60 //! \brief Checks if the callback is connected to a specific attribute
61 //! \param attribute Reference to the attribute to check
62 //! \return True if connected to the attribute, false otherwise
63 virtual bool isConnected(const DtAttributeHandle& attribute) const;
64
65 //! \brief Checks if the callback is connected to any attributes
66 //! \return True if connected to at least one attribute, false otherwise
67 virtual bool hasConnection() const;
68
69protected:
70 //! \brief Invoked by DtAttribute when the connected attribute's value changes
71 //!
72 //! Derived classes implement this method to deliver the change notification
73 //! to the registered function or functor.
74 //!
75 //! \param changedAttribute Handle to the attribute whose value changed
76 virtual void invoke(const DtAttributeHandle& changedAttribute) const = 0;
77
78 friend class DtAttribute;
79
80protected:
81 //! \brief Type definition for the list of connected attributes
82 using AttributeList = std::list<DtAttributeHandle>;
83
84 //! \brief List of attributes this callback is connected to
86};
87
88
89//! \brief Scoped attribute change callback wrapper
90//!
91//! This class provides a mechanism for registering callbacks that are triggered when attributes are changed.
92//! It maintains a list of connected attributes and manages the lifecycle of these connections.
93//!
94//! This is a type-agnostic callback. It passes to the callback function an un-typed DtAttributeHandle
95//! representing the attribute which was changed. If includeChildren is set false (default), then this
96//! will be the same attribute to which the callback was connected. If includeChildren is set true,
97//! the changed attribute may also be any child of the connected attribute.
100 public DtDelegate<void, const DtAttributeHandle&>
101{
102public:
103 //! \brief Constructor for attribute callbacks using member functions
104 //!
105 //! \tparam OBJECT_T Type of the object containing the method
106 //!
107 //! \param object Pointer to the object containing the method
108 //! \param method Method pointer to be called when an attribute changes
109 //! \param includeChildren If true, also invokes the callback for changes to descendants
110 //! of connected attributes (default: false)
111 template <class OBJECT_T>
113 OBJECT_T* object, void (OBJECT_T::*method)(const DtAttributeHandle&), bool includeChildren = false);
114
115 //! \brief Constructor for attribute callbacks using static function
116 //! \param func Pointer to function to be called when attribute changes
117 //! \param includeChildren If true, also invokes the callback for changes to descendants
118 //! of connected attributes (default: false)
120
121 //! \brief Constructor for attribute callbacks using a callable (lambda, functor, etc.)
122 //!
123 //! \tparam Callable Type of the callable object. Must be invocable with const DtAttributeHandle&.
124 //!
125 //! \param callable The callable to invoke when an attribute changes
126 //! \param includeChildren If true, also invokes the callback for changes to descendants
127 //! of connected attributes (default: false)
128 template <typename Callable,
129 typename = std::enable_if_t<!std::is_same<std::decay_t<Callable>, DtAttributeChangeCallback>::value>>
130 DtAttributeChangeCallback(Callable&& callable, bool includeChildren = false);
131
132 //! \brief Virtual destructor
133 //!
134 //! Automatically disconnects from all connected attributes
135 virtual ~DtAttributeChangeCallback() override;
136
137 //! \brief Set whether this callback should also be invoked for any change to any child attribute
138 //! \param on Set true for callbacks on change to any child. Set false for callbacks only to the
139 //! connected attribute. Default is false.
140 //!
141 //! Check current state with includesChildren()
142 virtual void includeChildren(bool on);
143
144 //! \brief Get whether this callback is set to trigger on changes to child attributes.
145 //!
146 //! Set state with includeChildren(bool on)
147 virtual bool includesChildren() const;
148
149protected:
150 //! \brief Delivers the change notification to the registered callback function
151 //!
152 //! If the changed attribute is the directly connected attribute, the callback is
153 //! invoked immediately. If includeChildren() is true, the callback is also invoked
154 //! when the changed attribute is a descendant of a connected attribute.
155 //!
156 //! \param changedAttribute Handle to the attribute whose value changed
157 virtual void invoke(const DtAttributeHandle& changedAttribute) const override;
158
159protected:
160 //! \brief Defines whether this callback is also invoked for any changes to children of the connected attribute.
162};
163
164//! \brief Scoped attribute change callback wrapper
165//!
166//! This class provides a mechanism for registering callbacks that are triggered when attributes are changed.
167//! It maintains a list of connected attributes and manages the lifecycle of these connections.
168//!
169//! This is a type-specific callback. The new value of the connected attribute is passed to the callback function.
170template <typename T>
172{
173public:
174 //! \brief Constructor for attribute callbacks using member functions
175 //!
176 //! \tparam OBJECT_T Type of the object containing the method
177 //! \tparam METHOD_T Pattern of the callback function, should be one of:
178 //! void(OBJECT_T::*method)(const T&) or void(OBJECT_T::*method)(T)
179 //!
180 //! \param object Pointer to the object containing the method
181 //! \param method Method pointer to be called when an attribute changes
182 template <class OBJECT_T, typename METHOD_T>
183 DtAttributeValueCallback(OBJECT_T* object, METHOD_T method);
184
185 //! \brief Constructor for attribute callbacks using static function
186 //!
187 //! \param func Pointer to function to be called when attribute changes
188 DtAttributeValueCallback(void (*func)(const T&));
189 DtAttributeValueCallback(void (*func)(T));
190
191 //! \brief Constructor for attribute callbacks using a callable (lambda, functor, etc.)
192 //!
193 //! \tparam Callable Type of the callable object. Must be invocable with const T&.
194 //!
195 //! \param callable The callable to invoke when an attribute changes
196 template <typename Callable,
197 typename = std::enable_if_t<!std::is_same<std::decay_t<Callable>, DtAttributeValueCallback>::value>>
198 DtAttributeValueCallback(Callable&& callable);
199
200 //! \brief Virtual destructor
201 //!
202 //! Automatically disconnects from all connected attributes
203 virtual ~DtAttributeValueCallback() override;
204
205protected:
206 //! \brief Delivers the typed attribute value to the registered callback function
207 //!
208 //! Retrieves the current value of the changed attribute as type T and passes it
209 //! to the registered functor. Emits a warning if the attribute's runtime type
210 //! does not match T.
211 //!
212 //! \param changedAttribute Handle to the attribute whose value changed
213 virtual void invoke(const DtAttributeHandle& changedAttribute) const override;
214};
215
216//! \brief Convenience manager that owns and organises a collection of attribute callbacks
217//!
218//! DtAttributeCallbackManager simplifies callback lifetime management by owning all
219//! registered callbacks internally. Each attribute path maps to exactly one callback;
220//! registering a new callback for an attribute that already has one replaces the old
221//! entry. All connections are automatically cleaned up when the manager is destroyed.
223{
224public:
225 //! \brief Default constructor
227
228 //! \brief Virtual destructor
229 //!
230 //! Destroys all owned callbacks, which disconnects them from their attributes.
232
235
236 // Attribute handle callbacks
237
238 //! \brief Connects a member function callback for untyped attribute changes
239 //!
240 //! The callback receives a DtAttributeHandle whenever the attribute at \p attr changes.
241 //!
242 //! \tparam OBJECT_T Type of the object that owns the method
243 //! \param attr The attribute to observe
244 //! \param object Pointer to the owning object
245 //! \param method Member function to invoke on change
246 //! \param includeChildren If true, also observes changes to descendants of attr (default: false)
247 //! \param invokeNowIfSet If true, invokes the callback immediately when connecting if attr already
248 //! has a value (default: false)
249 template <class OBJECT_T>
251 OBJECT_T* object,
252 void (OBJECT_T::*method)(const DtAttributeHandle&),
253 bool includeChildren = false,
254 bool invokeNowIfSet = false);
255
256 //! \brief Connects a free-function callback for untyped attribute changes
257 //!
258 //! \param attr The attribute to observe
259 //! \param func Free function to invoke when the attribute changes
260 //! \param includeChildren If true, also observes changes to descendants of attr (default: false)
261 //! \param invokeNowIfSet If true, invokes the callback immediately when connecting if attr already
262 //! has a value (default: false)
264 void (*func)(const DtAttributeHandle&),
265 bool includeChildren = false,
266 bool invokeNowIfSet = false);
267
268 // Value callbacks
269
270 //! \brief Connects a member function callback receiving the new attribute value by const reference
271 //!
272 //! \tparam OBJECT_T Type of the object that owns the method
273 //! \tparam ATTRIBUTE_T Value type of the attribute
274 //! \param attr The attribute to observe
275 //! \param object Pointer to the owning object
276 //! \param method Member function invoked with the new value as `const ATTRIBUTE_T&`
277 //! \param invokeNowIfSet If true, invokes the callback immediately when connecting if attr already
278 //! has a value (default: false)
279 template <class OBJECT_T, typename ATTRIBUTE_T>
281 OBJECT_T* object,
282 void (OBJECT_T::*method)(const ATTRIBUTE_T&),
283 bool invokeNowIfSet = false);
284
285 //! \brief Connects a member function callback receiving the new attribute value by value
286 //!
287 //! \tparam OBJECT_T Type of the object that owns the method
288 //! \tparam ATTRIBUTE_T Value type of the attribute
289 //! \param attr The attribute to observe
290 //! \param object Pointer to the owning object
291 //! \param method Member function invoked with the new value as `ATTRIBUTE_T`
292 //! \param invokeNowIfSet If true, invokes the callback immediately when connecting if attr already
293 //! has a value (default: false)
294 template <class OBJECT_T, typename ATTRIBUTE_T>
296 OBJECT_T* object,
297 void (OBJECT_T::*method)(ATTRIBUTE_T),
298 bool invokeNowIfSet = false);
299
300 //! \brief Connects a free-function callback receiving the new attribute value by const reference
301 //!
302 //! \tparam ATTRIBUTE_T Value type of the attribute
303 //! \param attr The attribute to observe
304 //! \param func Free function invoked with the new value as `const ATTRIBUTE_T&`
305 //! \param invokeNowIfSet If true, invokes the callback immediately when connecting if attr already
306 //! has a value (default: false)
307 template <typename ATTRIBUTE_T>
309 void (*func)(const ATTRIBUTE_T&),
310 bool invokeNowIfSet = false);
311
312 //! \brief Connects a free-function callback receiving the new attribute value by value
313 //!
314 //! \tparam ATTRIBUTE_T Value type of the attribute
315 //! \param attr The attribute to observe
316 //! \param func Free function invoked with the new value as `ATTRIBUTE_T`
317 //! \param invokeNowIfSet If true, invokes the callback immediately when connecting if attr already
318 //! has a value (default: false)
319 template <typename ATTRIBUTE_T>
321 void (*func)(ATTRIBUTE_T),
322 bool invokeNowIfSet = false);
323
324 // Callable (lambda / functor) callbacks
325
326 //! \brief Connects a callable (lambda, functor) for untyped attribute changes
327 //!
328 //! The callback receives a DtAttributeHandle whenever the attribute at \p attr changes.
329 //!
330 //! \tparam Callable Type of the callable object
331 //! \param attr The attribute to observe
332 //! \param callable Callable to invoke on change; must accept const DtAttributeHandle&
333 //! \param includeChildren If true, also observes changes to descendants of attr (default: false)
334 //! \param invokeNowIfSet If true, invokes the callback immediately when connecting if attr already
335 //! has a value (default: false)
336 template <typename Callable>
338 Callable&& callable,
339 bool includeChildren = false,
340 bool invokeNowIfSet = false);
341
342 //! \brief Connects a callable (lambda, functor) for typed attribute value changes
343 //!
344 //! The callback receives the new value of type ATTRIBUTE_T when the attribute changes.
345 //! The value type must be explicitly specified, e.g. connect<int>(attr, callable).
346 //!
347 //! \tparam ATTRIBUTE_T Value type of the attribute
348 //! \tparam Callable Type of the callable object
349 //! \param attr The attribute to observe
350 //! \param callable Callable to invoke with the new attribute value
351 //! \param invokeNowIfSet If true, invokes the callback immediately when connecting if attr already
352 //! has a value (default: false)
353 template <typename ATTRIBUTE_T, typename Callable>
355 Callable&& callable,
356 bool invokeNowIfSet = false);
357
358 //! \brief Checks whether a callback is currently registered for the given attribute
359 //!
360 //! \param attr The attribute to check
361 //! \return True if a callback is registered and connected for \p attr
363
364 //! \brief Returns a non-owning pointer to the callback registered for the given attribute
365 //!
366 //! \param attr The attribute whose callback is requested
367 //! \return Pointer to the registered callback, or nullptr if none is registered
369
370 //! \brief Removes and destroys the callback registered for the given attribute
371 //!
372 //! \param attr The attribute whose callback should be removed
374
375 //! \brief Removes and destroys all registered callbacks
377
378protected:
379 //! \brief Owns all registered callbacks, keyed by the full attribute path
380 std::map<std::string, std::unique_ptr<DtAttributeCallbackBase>> myCallbackMap;
381};
382
383} // namespace makVre
Abstract base class for all attribute change callbacks.
Definition attributeCallback.h:34
friend class DtAttribute
Definition attributeCallback.h:78
std::list< DtAttributeHandle > AttributeList
Type definition for the list of connected attributes.
Definition attributeCallback.h:82
virtual ~DtAttributeCallbackBase()
Virtual destructor.
virtual void disconnectAll()
Disconnects the callback from all connected attributes.
virtual void disconnect(DtAttributeHandle attribute)
Disconnects the callback from a specific attribute.
virtual bool isConnected(const DtAttributeHandle &attribute) const
Checks if the callback is connected to a specific attribute.
AttributeList myConnections
List of attributes this callback is connected to.
Definition attributeCallback.h:85
virtual void connect(DtAttributeHandle attribute, bool invokeNowIfSet=false)
Connects the callback to an attribute.
virtual bool hasConnection() const
Checks if the callback is connected to any attributes.
virtual void invoke(const DtAttributeHandle &changedAttribute) const =0
Invoked by DtAttribute when the connected attribute's value changes.
void connect(DtAttributeHandle attr, OBJECT_T *object, void(OBJECT_T::*method)(ATTRIBUTE_T), bool invokeNowIfSet=false)
Connects a member function callback receiving the new attribute value by value.
std::map< std::string, std::unique_ptr< DtAttributeCallbackBase > > myCallbackMap
Owns all registered callbacks, keyed by the full attribute path.
Definition attributeCallback.h:380
virtual ~DtAttributeCallbackManager()
Virtual destructor.
void connect(DtAttributeHandle attr, Callable &&callable, bool invokeNowIfSet=false)
Connects a callable (lambda, functor) for typed attribute value changes.
void connect(DtAttributeHandle attr, OBJECT_T *object, void(OBJECT_T::*method)(const DtAttributeHandle &), bool includeChildren=false, bool invokeNowIfSet=false)
Connects a member function callback for untyped attribute changes.
DtAttributeCallbackManager(const DtAttributeCallbackManager &)=delete
DtAttributeCallbackManager & operator=(const DtAttributeCallbackManager &)=delete
void connect(DtAttributeHandle attr, Callable &&callable, bool includeChildren=false, bool invokeNowIfSet=false)
Connects a callable (lambda, functor) for untyped attribute changes.
void connect(DtAttributeHandle attr, void(*func)(const ATTRIBUTE_T &), bool invokeNowIfSet=false)
Connects a free-function callback receiving the new attribute value by const reference.
void disconnect(DtAttributeHandle attr)
Removes and destroys the callback registered for the given attribute.
void connect(DtAttributeHandle attr, void(*func)(ATTRIBUTE_T), bool invokeNowIfSet=false)
Connects a free-function callback receiving the new attribute value by value.
void disconnectAll()
Removes and destroys all registered callbacks.
DtAttributeCallbackManager()
Default constructor.
bool isConnected(DtAttributeHandle attr) const
Checks whether a callback is currently registered for the given attribute.
DtAttributeCallbackBase * connection(DtAttributeHandle attr)
Returns a non-owning pointer to the callback registered for the given attribute.
void connect(DtAttributeHandle attr, OBJECT_T *object, void(OBJECT_T::*method)(const ATTRIBUTE_T &), bool invokeNowIfSet=false)
Connects a member function callback receiving the new attribute value by const reference.
void connect(DtAttributeHandle attr, void(*func)(const DtAttributeHandle &), bool includeChildren=false, bool invokeNowIfSet=false)
Connects a free-function callback for untyped attribute changes.
bool myIncludeChildren
Defines whether this callback is also invoked for any changes to children of the connected attribute.
Definition attributeCallback.h:161
DtAttributeChangeCallback(OBJECT_T *object, void(OBJECT_T::*method)(const DtAttributeHandle &), bool includeChildren=false)
Constructor for attribute callbacks using member functions.
DtAttributeChangeCallback(void(*func)(const DtAttributeHandle &), bool includeChildren=false)
Constructor for attribute callbacks using static function.
virtual void includeChildren(bool on)
Set whether this callback should also be invoked for any change to any child attribute.
virtual bool includesChildren() const
Get whether this callback is set to trigger on changes to child attributes.
virtual void invoke(const DtAttributeHandle &changedAttribute) const override
Delivers the change notification to the registered callback function.
virtual ~DtAttributeChangeCallback() override
Virtual destructor.
DtAttributeChangeCallback(Callable &&callable, bool includeChildren=false)
Constructor for attribute callbacks using a callable (lambda, functor, etc.)
Handle class for safe access to attributes.
Definition attributeHandle.h:30
Core attribute class for shared data storage.
Definition attribute.h:35
virtual ~DtAttributeValueCallback() override
Virtual destructor.
DtAttributeValueCallback(void(*func)(const T &))
Constructor for attribute callbacks using static function.
DtAttributeValueCallback(void(*func)(T))
DtAttributeValueCallback(Callable &&callable)
Constructor for attribute callbacks using a callable (lambda, functor, etc.)
virtual void invoke(const DtAttributeHandle &changedAttribute) const override
Delivers the typed attribute value to the registered callback function.
DtAttributeValueCallback(OBJECT_T *object, METHOD_T method)
Constructor for attribute callbacks using member functions.
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