VR-Engage  2.2
Loading...
Searching...
No Matches
attributeCallback.h
Go to the documentation of this file.
1/******************************************************************************
2 ** Copyright (c) 2025 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
28{
29public:
31
32 //! \brief Virtual destructor
33 //!
34 //! Automatically disconnects from all connected attributes
36
37 //! \brief Connects the callback to an attribute
38 //! \param attribute Reference to the attribute to connect to
39 virtual void connect(DtAttributeHandle attribute);
40
41 //! \brief Disconnects the callback from a specific attribute
42 //! \param attribute Reference to the attribute to disconnect from
43 virtual void disconnect(DtAttributeHandle attribute);
44
45 //! \brief Disconnects the callback from all connected attributes
46 virtual void disconnectAll();
47
48 //! \brief Checks if the callback is connected to a specific attribute
49 //! \param attribute Reference to the attribute to check
50 //! \return True if connected to the attribute, false otherwise
51 virtual bool isConnected(const DtAttributeHandle& attribute) const;
52
53 //! \brief Checks if the callback is connected to any attributes
54 //! \return True if connected to at least one attribute, false otherwise
55 virtual bool hasConnection() const;
56
57 virtual unsigned int threadId() const;
58
59protected:
60 virtual void invoke(const DtAttributeHandle& changedAttribute) const = 0;
61
62 friend class DtAttribute;
63
64protected:
65 //! \brief Type definition for the list of connected attributes
66 using AttributeList = std::list<DtAttributeHandle>;
67
68 //! \brief List of attributes this callback is connected to
70
71 unsigned int myThreadId;
72};
73
74
75//! \brief Scoped attribute change callback wrapper
76//!
77//! This class provides a mechanism for registering callbacks that are triggered when attributes are changed.
78//! It maintains a list of connected attributes and manages the lifecycle of these connections.
79//!
80//! This is a type-agnostic callback. It passes to the callback function an un-typed DtAttributeHandle
81//! representing the attribute which was changed. If includeChildren is set false (default), then this
82//! will be the same attribute to which the callback was connected. If includeChildren is set true,
83//! the changed attribute may also be any child of the connected attribute.
85 public DtDelegate<void, const DtAttributeHandle&>
86{
87public:
88 //! \brief Constructor for attribute callbacks using member functions
89 //!
90 //! \tparam OBJECT_T Type of the object containing the method
91 //!
92 //! \param object Pointer to the object containing the method
93 //! \param method Method pointer to be called when an attribute changes
94 template <class OBJECT_T>
95 DtAttributeChangeCallback(OBJECT_T* object, void (OBJECT_T::*method)(const DtAttributeHandle&));
96
97 //! \brief Constructor for attribute callbacks using static function
98 //! \param func Pointer to function to be called when attribute changes
100
101 //! \brief Virtual destructor
102 //!
103 //! Automatically disconnects from all connected attributes
104 virtual ~DtAttributeChangeCallback() override;
105
106 //! \brief Set whether this callback should also be invoked for any change to any child attribute
107 //! \param on Set true for callbacks on change to any child. Set false for callbacks only to the
108 //! connected attribute. Default is false.
109 //!
110 //! Check current state with includesChildren()
111 virtual void includeChildren(bool on);
112
113 //! \brief Get whether this callback is set to trigger on changes to child attributes.
114 //!
115 //! Set state with includeChildren(bool on)
116 virtual bool includesChildren() const;
117
118protected:
119 virtual void invoke(const DtAttributeHandle& changedAttribute) const override;
120
121protected:
122 //! \brief Defines whether this callback is also invoked for any changes to children of the connected attribute.
123 bool myIncludeChildren = false;
124};
125
126//! \brief Scoped attribute change callback wrapper
127//!
128//! This class provides a mechanism for registering callbacks that are triggered when attributes are changed.
129//! It maintains a list of connected attributes and manages the lifecycle of these connections.
130//!
131//! This is a type-specific callback. The new value of the connected attribute is passed to the callback function.
132template <typename T>
134{
135public:
136 //! \brief Constructor for attribute callbacks using member functions
137 //!
138 //! \tparam OBJECT_T Type of the object containing the method
139 //! \tparam METHOD_T Pattern of the callback function, should be one of:
140 //! void(OBJECT_T::*method)(const T&) or void(OBJECT_T::*method)(T)
141 //!
142 //! \param object Pointer to the object containing the method
143 //! \param method Method pointer to be called when an attribute changes
144 template <class OBJECT_T, typename METHOD_T>
145 DtAttributeValueCallback(OBJECT_T* object, METHOD_T method);
146
147 //! \brief Constructor for attribute callbacks using static function
148 //!
149 //! \tparam FUNC_T Pattern for the callback function, should be one of:
150 //! void(*func)(const T&) or void(*func)(T)
151 //!
152 //! \param func Pointer to function to be called when attribute changes
153 template <typename FUNC_T>
155
156 //! \brief Virtual destructor
157 //!
158 //! Automatically disconnects from all connected attributes
159 virtual ~DtAttributeValueCallback() override;
160
161protected:
162 virtual void invoke(const DtAttributeHandle& changedAttribute) const override;
163};
164
166{
167public:
170
173
174 // Attribute handle callbacks
175 template <class OBJECT_T>
176 void connect(DtAttributeHandle attr, OBJECT_T* object, void (OBJECT_T::*method)(const DtAttributeHandle&));
177
178 void connect(DtAttributeHandle attr, void (*func)(const DtAttributeHandle&));
179
180 // Value callbacks
181 template <class OBJECT_T, typename ATTRIBUTE_T>
182 void connect(DtAttributeHandle attr, OBJECT_T* object, void (OBJECT_T::*method)(const ATTRIBUTE_T&));
183
184 template <class OBJECT_T, typename ATTRIBUTE_T>
185 void connect(DtAttributeHandle attr, OBJECT_T* object, void (OBJECT_T::*method)(ATTRIBUTE_T));
186
187 template <typename ATTRIBUTE_T>
188 void connect(DtAttributeHandle attr, void (*func)(const ATTRIBUTE_T&));
189
190 template <typename ATTRIBUTE_T>
191 void connect(DtAttributeHandle attr, void (*func)(ATTRIBUTE_T));
192
195
198
199protected:
200 std::map<std::string, std::unique_ptr<DtAttributeCallbackBase>> myCallbackMap;
201};
202
203} // namespace makVre
Definition attributeCallback.h:28
friend class DtAttribute
Definition attributeCallback.h:62
virtual void connect(DtAttributeHandle attribute)
Connects the callback to an attribute.
std::list< DtAttributeHandle > AttributeList
Type definition for the list of connected attributes.
Definition attributeCallback.h:66
virtual unsigned int threadId() const
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:69
virtual bool hasConnection() const
Checks if the callback is connected to any attributes.
unsigned int myThreadId
Definition attributeCallback.h:71
virtual void invoke(const DtAttributeHandle &changedAttribute) const =0
void connect(DtAttributeHandle attr, void(*func)(const DtAttributeHandle &))
std::map< std::string, std::unique_ptr< DtAttributeCallbackBase > > myCallbackMap
Definition attributeCallback.h:200
void connect(DtAttributeHandle attr, void(*func)(ATTRIBUTE_T))
DtAttributeCallbackManager(const DtAttributeCallbackManager &)=delete
DtAttributeCallbackManager & operator=(const DtAttributeCallbackManager &)=delete
void connect(DtAttributeHandle attr, OBJECT_T *object, void(OBJECT_T::*method)(ATTRIBUTE_T))
void connect(DtAttributeHandle attr, OBJECT_T *object, void(OBJECT_T::*method)(const DtAttributeHandle &))
void disconnect(DtAttributeHandle attr)
bool isConnected(DtAttributeHandle attr) const
void connect(DtAttributeHandle attr, OBJECT_T *object, void(OBJECT_T::*method)(const ATTRIBUTE_T &))
DtAttributeCallbackBase * connection(DtAttributeHandle attr)
void connect(DtAttributeHandle attr, void(*func)(const ATTRIBUTE_T &))
bool myIncludeChildren
Defines whether this callback is also invoked for any changes to children of the connected attribute.
Definition attributeCallback.h:123
DtAttributeChangeCallback(OBJECT_T *object, void(OBJECT_T::*method)(const DtAttributeHandle &))
Constructor for attribute callbacks using member functions.
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
virtual ~DtAttributeChangeCallback() override
Virtual destructor.
DtAttributeChangeCallback(void(*func)(const DtAttributeHandle &))
Constructor for attribute callbacks using static function.
Handle class for safe access to attributes.
Definition attributeHandle.h:30
Core attribute class for shared data storage.
Definition attribute.h:34
virtual ~DtAttributeValueCallback() override
Virtual destructor.
virtual void invoke(const DtAttributeHandle &changedAttribute) const override
DtAttributeValueCallback(FUNC_T func)
Constructor for attribute callbacks using static 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
Include export definitions for this library.
Definition glsVreMessageUtil.h:49