VR-Engage  2.2
Loading...
Searching...
No Matches
delegate.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 delegate.h
9//! \brief Defines a lightweight single-parameter delegate system for callback functions
10//! \ingroup vreRadarFxShared
11//!
12//! This delegate implementation provides a lightweight alternative to boost::function+boost::bind
13//! for registering and invoking callbacks with a single parameter
14
15namespace makRadarFx
16{
17
18
19//! \brief Abstract base callback class for the delegate system
20//! \tparam Ret Return type of the callback function
21//! \tparam Param0 Type of the single parameter passed to the callback
22//!
23//! This abstract base class defines the interface for all callback types
24//! used internally by the delegate system
25template <typename Ret, typename Param0>
27{
28public:
29 //! \brief Invokes the callback with the provided parameter
30 //! \param param0 The parameter to pass to the callback
31 //! \return Result of the callback invocation
32 //!
33 //! Pure virtual method that executes the actual callback code
34 virtual Ret invoke(Param0 param0) = 0;
35
36 //! \brief Creates a copy of this callback
37 //! \return Pointer to a newly allocated copy of this callback
38 //!
39 //! Pure virtual method that implements deep copying of callback objects
40 virtual Callback* clone() = 0;
41
42 //! \brief Virtual destructor for proper cleanup of derived classes
43 virtual ~Callback() {};
44
45 //! \brief Equality comparison operator
46 //! \param rhs The right-hand side callback to compare with
47 //! \return True if the callbacks are functionally equivalent
48 //!
49 //! Pure virtual method that implements equality comparison between callbacks
50 virtual bool operator==(const Callback& rhs) = 0;
51
52 //! \brief Inequality comparison operator
53 //! \param rhs The right-hand side callback to compare with
54 //! \return True if the callbacks are not functionally equivalent
55 virtual bool operator!=(const Callback& rhs) { return !(*this == rhs); }
56};
57
58
59//! \brief Callback implementation for static or global functions
60//! \tparam Ret Return type of the callback function
61//! \tparam Param0 Type of the single parameter passed to the callback
62//!
63//! This class implements the Callback interface for global functions or static
64//! class methods that accept a single parameter and return a value
65template <typename Ret, typename Param0>
66class StaticFunctionCallback : public Callback<Ret, Param0>
67{
68private:
69 //! \brief Pointer to the static or global function
70 Ret (*myFunction)(Param0);
71
72public:
73 //! \brief Constructor
74 //! \param func Pointer to the static or global function to bind
75 StaticFunctionCallback(Ret (*func)(Param0))
76 : myFunction(func)
77 {
78 }
79
80 //! \brief Copy constructor
81 //! \param orig Original callback to copy from
83
84 //! \brief Creates a copy of this callback
85 //! \return Pointer to a newly allocated copy of this callback
86 //!
87 //! Implements the clone method from the base class for deep copying
89 {
91 return nc;
92 }
93
94 //! \brief Invokes the bound function with the provided parameter
95 //! \param param0 The parameter to pass to the function
96 //! \return Result of the function invocation
97 virtual Ret invoke(Param0 param0) { return (*myFunction)(param0); }
98
99 //! \brief Equality comparison operator
100 //! \param rhs The right-hand side callback to compare with
101 //! \return True if both callbacks reference the same function
102 virtual bool operator==(const Callback<Ret, Param0>& rhs)
103 {
105 return myFunction == rhsp->myFunction;
106 }
107};
108
109
110//! \brief Callback implementation for class member functions
111//! \tparam Ret Return type of the callback function
112//! \tparam Param0 Type of the single parameter passed to the callback
113//! \tparam T Type of the class containing the member function
114//! \tparam Method Type of the member function pointer
115//!
116//! This class implements the Callback interface for non-static class member
117//! methods that accept a single parameter and return a value
118template <typename Ret, typename Param0, typename T, typename Method>
119class MethodCallback : public Callback<Ret, Param0>
120{
121private:
122 //! \brief Pointer to the object instance
123 void* myObject;
124
125 //! \brief Pointer to the member function
126 Method myMethod;
127
128public:
129 //! \brief Constructor
130 //! \param object Pointer to the object instance
131 //! \param method Pointer to the member function to bind
132 MethodCallback(void* object, Method method)
133 : myObject(object)
134 , myMethod(method)
135 {
136 }
137
138 //! \brief Copy constructor
139 //! \param orig Original callback to copy from
141 {
142 myObject = orig.myObject;
143 myMethod = orig.myMethod;
144 }
145
146 //! \brief Creates a copy of this callback
147 //! \return Pointer to a newly allocated copy of this callback
148 //!
149 //! Implements the clone method from the base class for deep copying
151 {
153 return nc;
154 }
155
156 //! \brief Invokes the bound member function with the provided parameter
157 //! \param param0 The parameter to pass to the member function
158 //! \return Result of the member function invocation
159 virtual Ret invoke(Param0 param0)
160 {
161 T* obj = static_cast<T*>(myObject);
162 return (obj->*myMethod)(param0);
163 }
164
165 //! \brief Equality comparison operator
166 //! \param rhs The right-hand side callback to compare with
167 //! \return True if both callbacks reference the same object and method
168 virtual bool operator==(const Callback<Ret, Param0>& rhs)
169 {
170 MethodCallback* rhsp = (MethodCallback*)&rhs;
171 return (myObject == rhsp->myObject) && (myMethod == rhsp->myMethod);
172 }
173};
174
175
176//! \brief Delegate class for binding and invoking callbacks
177//! \tparam Ret Return type of the callback function
178//! \tparam Param0 Type of the single parameter passed to the callback
179//!
180//! This class provides a lightweight mechanism for binding either static/global
181//! functions or class member functions for invocation at a later time. The delegate
182//! supports a single parameter and return value, and provides comparison operators
183//! to check if two delegates refer to the same callback.
184template <typename Ret, typename Param0>
186{
187private:
188 //! \brief Pointer to the underlying callback implementation
190
191 //! \brief Flag indicating if this is a static/global function callback
193
194public:
195 //! \brief Default constructor
196 //!
197 //! Creates an empty delegate with no bound callback
199 : myCallback(0)
200 , myIsStatic(false)
201 {
202 }
203
204 //! \brief Constructor for static or global functions
205 //! \param func Pointer to the static or global function to bind
206 //!
207 //! Creates a delegate bound to a static or global function
208 DtDelegate(Ret (*func)(Param0))
209 : myCallback(new StaticFunctionCallback<Ret, Param0>(func))
210 , myIsStatic(true)
211 {
212 }
213
214 //! \brief Constructor for class member functions
215 //! \tparam T Type of the class containing the member function
216 //! \tparam Method Type of the member function pointer
217 //! \param object Pointer to the object instance
218 //! \param method Pointer to the member function to bind
219 //!
220 //! Creates a delegate bound to a non-static class member function
221 template <typename T, typename Method>
222 DtDelegate(T* object, Method method)
223 : myCallback(new MethodCallback<Ret, Param0, T, Method>(object, method))
224 , myIsStatic(false)
225 {
226 }
227
228 //! \brief Copy constructor
229 //! \param orig Original delegate to copy from
230 //!
231 //! Creates a new delegate that is a deep copy of the original
233 {
234 myCallback = orig.myCallback->clone();
235 myIsStatic = orig.myIsStatic;
236 }
237
238 //! \brief Assignment operator
239 //! \param orig Original delegate to assign from
240 //! \return Reference to this delegate after assignment
241 //!
242 //! Assigns this delegate to be a deep copy of the original
244 {
245 if (this != &orig)
246 {
247 myCallback = orig.myCallback->clone();
248 myIsStatic = orig.myIsStatic;
249 }
250
251 return *this;
252 }
253
254 //! \brief Virtual destructor
255 //!
256 //! Cleans up the underlying callback implementation
257 virtual ~DtDelegate(void) { delete myCallback; }
258
259 //! \brief Function call operator for invoking the delegate
260 //! \param param0 The parameter to pass to the callback
261 //! \return Result of the callback invocation
262 //!
263 //! Executes the bound callback with the provided parameter
264 Ret operator()(Param0 param0)
265 {
266#ifdef _DEBUG
267 if (myCallback == 0)
268 {
269 throw "Missing callback";
270 }
271#endif
272
273 return myCallback->invoke(param0);
274 }
275
276 //! \brief Equality comparison operator
277 //! \param rhs The right-hand side delegate to compare with
278 //! \return True if both delegates reference the same callback
279 //!
280 //! Compares two delegates to determine if they reference the same callback
281 bool operator==(const DtDelegate& rhs) const
282 {
283 if (myIsStatic != rhs.myIsStatic)
284 {
285 return false;
286 }
287
288 return (*myCallback) == (*(rhs.myCallback));
289 }
290
291 //! \brief Inequality comparison operator
292 //! \param rhs The right-hand side delegate to compare with
293 //! \return True if the delegates do not reference the same callback
294 bool operator!=(const DtDelegate& rhs) { return !(*this == rhs); }
295};
296} // namespace makRadarFx
Abstract base callback class for the delegate system.
Definition delegate.h:27
virtual bool operator==(const Callback &rhs)=0
Equality comparison operator.
virtual Callback * clone()=0
Creates a copy of this callback.
virtual bool operator!=(const Callback &rhs)
Inequality comparison operator.
Definition delegate.h:55
virtual Ret invoke(Param0 param0)=0
Invokes the callback with the provided parameter.
virtual ~Callback()
Virtual destructor for proper cleanup of derived classes.
Definition delegate.h:43
DtDelegate(Ret(*func)(Param0))
Constructor for static or global functions.
Definition delegate.h:208
DtDelegate & operator=(const DtDelegate &orig)
Assignment operator.
Definition delegate.h:243
bool operator!=(const DtDelegate &rhs)
Inequality comparison operator.
Definition delegate.h:294
Callback< void, DtRadarFxConnector * > * myCallback
Definition delegate.h:189
virtual ~DtDelegate(void)
Virtual destructor.
Definition delegate.h:257
DtDelegate(const DtDelegate &orig)
Copy constructor.
Definition delegate.h:232
bool operator==(const DtDelegate &rhs) const
Equality comparison operator.
Definition delegate.h:281
Ret operator()(Param0 param0)
Function call operator for invoking the delegate.
Definition delegate.h:264
DtDelegate()
Default constructor.
Definition delegate.h:198
DtDelegate(T *object, Method method)
Constructor for class member functions.
Definition delegate.h:222
Callback implementation for class member functions.
Definition delegate.h:120
virtual Ret invoke(Param0 param0)
Invokes the bound member function with the provided parameter.
Definition delegate.h:159
virtual bool operator==(const Callback< Ret, Param0 > &rhs)
Equality comparison operator.
Definition delegate.h:168
Method myMethod
Pointer to the member function.
Definition delegate.h:126
void * myObject
Pointer to the object instance.
Definition delegate.h:123
MethodCallback(void *object, Method method)
Constructor.
Definition delegate.h:132
MethodCallback(const MethodCallback &orig)
Copy constructor.
Definition delegate.h:140
virtual MethodCallback * clone()
Creates a copy of this callback.
Definition delegate.h:150
Callback implementation for static or global functions.
Definition delegate.h:67
Ret(* myFunction)(Param0)
Pointer to the static or global function.
Definition delegate.h:70
virtual Ret invoke(Param0 param0)
Invokes the bound function with the provided parameter.
Definition delegate.h:97
virtual bool operator==(const Callback< Ret, Param0 > &rhs)
Equality comparison operator.
Definition delegate.h:102
StaticFunctionCallback(Ret(*func)(Param0))
Constructor.
Definition delegate.h:75
virtual StaticFunctionCallback * clone()
Creates a copy of this callback.
Definition delegate.h:88
StaticFunctionCallback(const StaticFunctionCallback &orig)
Copy constructor.
Definition delegate.h:82
Definition radarFxConnectorDriver.h:21