VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
noArgumentCallbackList.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 2025 MAK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 
8 
10 #pragma once
11 
13 
14 #include <set>
15 #include <boost/function.hpp>
16 
17 #include <tbb/spin_mutex.h>
18 
29 
30 template <typename T_CallbackFunctionType>
32 {
33  DtCallbackFunctionInfo(T_CallbackFunctionType func, void* usr = 0)
34  : callback(func)
35  , usrData(usr)
36  , deletedFlag(new bool(false))
37  {
38  }
39 
41  : callback(orig.callback)
42  , deletedFlag(orig.deletedFlag)
43  , usrData(orig.usrData)
44  {
45  }
46 
48  {
49  callback = rhs.callback;
51  usrData = rhs.usrData;
52 
53  return *this;
54  }
55 
56  bool operator<(const DtCallbackFunctionInfo& rhs) const
57  {
58  return callback < rhs.callback || (!(rhs.callback < callback) && usrData < rhs.usrData);
59  }
60 
61  bool operator>(const DtCallbackFunctionInfo& rhs) const
62  {
63  return (rhs < *this);
64  }
65 
66  bool operator==(const DtCallbackFunctionInfo& rhs) const
67  {
68  return ((callback == rhs.callback) &&
69  (userData == rhs.userData));
70  }
71 
72  bool operator!=(const DtCallbackFunctionInfo& rhs) const
73  {
74  return !(*this == rhs);
75  }
76 
77  void setIsDeleted(bool b) { *deletedFlag = b; };
78  bool isDeleted() const { return *deletedFlag; };
79 
80  const T_CallbackFunctionType& getCallback() const { return callback; };
81  void* userData() const { return usrData; };
82 
83 protected:
84  std::shared_ptr<bool> deletedFlag;
85  T_CallbackFunctionType callback;
86  void* usrData;
87 };
88 
89 template <typename T_CallbackArg>
91 {
92 public:
94  typedef void (*DtCallbackFunctionType)(T_CallbackArg, void*);
95 
98 
101  explicit DtCallbackList(const DtCallbackList& orig);
102 
107  bool addCallback(DtCallbackFunctionType fcn, void* usrData);
111  bool removeCallback(DtCallbackFunctionType fcn, void* usrData);
112 
115  void invokeCallbacks(T_CallbackArg arg);
116 
118  void removeAll();
119 
121  {
124 
125  return *this;
126  }
127 
128 protected:
129  struct DtCallbackInfoType : public DtCallbackFunctionInfo<DtCallbackFunctionType>
130  {
133  {
134  }
135  };
136 
137  typedef std::set<DtCallbackInfoType> DtCallbackListType;
138 
139  mutable tbb::spin_mutex myMutex;
140 
142  unsigned myCallbackCount;
143 };
144 
152 {
153 public:
155  typedef void (*DtCallbackFunctionType)(void*);
156 
159 
163 
168  bool addCallback(DtCallbackFunctionType fcn, void* usrData);
172  bool removeCallback(DtCallbackFunctionType fcn, void* usrData);
173 
176  void invokeCallbacks();
177 
179  void removeAll();
180 
181 protected:
182  struct DtCallbackInfoType : public DtCallbackFunctionInfo<DtCallbackFunctionType>
183  {
184  DtCallbackInfoType(DtCallbackFunctionType callback, void* userData)
185  : DtCallbackFunctionInfo(callback, userData)
186  {
187  }
188  };
189 
190  typedef std::set<DtCallbackInfoType> DtCallbackListType;
191 
192  mutable tbb::spin_mutex myMutex;
193 
195  unsigned myCallbackCount;
196 
197 private:
198  DtNoArgumentCallbackList& operator=(const DtNoArgumentCallbackList& orig);
199 };
200 
201 template <typename Callback>
203 
210 {
211 public:
214 
219  bool disconnect()
220  {
221  if (myDisconnectFunction)
222  {
223  bool value = myDisconnectFunction();
224  myDisconnectFunction = DisconnectFunctionType();
225  return value;
226  }
227 
228  return false;
229  }
230 
231 private:
232  template <typename Callback>
233  friend class DtCallbackListT;
234 
235  typedef boost::function<bool()> DisconnectFunctionType;
236 
238  : myDisconnectFunction(func) { }
239 
241 };
242 
253 template <typename Callback>
254 class DtCallbackListT
255 {
256 public:
259 
265  DtCallbackConnection addCallback(const Callback& callback, bool* addedFirst = 0);
266 
272  void removeCallback(const Callback& callback, bool* removedLast = 0);
273 
280  void removeCallback(const boost::function<bool(const Callback&)>& removePredicate, bool* removedLast = 0);
281 
283  void removeAll();
284 
287 
288  void invokeCallbacks();
289 
290  template <typename T1>
291  void invokeCallbacks(const T1&);
292 
293  template <typename T1, typename T2>
294  void invokeCallbacks(const T1&, const T2&);
295 
296  template <typename T1, typename T2, typename T3>
297  void invokeCallbacks(const T1&, const T2&, const T3&);
299 
300 protected:
301  struct DtCallbackInfoType : public DtCallbackFunctionInfo<Callback>
302  {
304  : DtCallbackFunctionInfo<Callback>(callback)
305  {
306  }
307  };
308 
309  bool disconnect(const DtCallbackInfoType&);
310 
311  mutable tbb::spin_mutex myMutex;
312 
313  typename DtCallbackInfoType::List myCallbackList;
314  unsigned myCallbackCount;
315 
316 private:
319 };
320 
321 
322 #define callbackList_inl_
323 #include "readerWriter/noArgumentCallbackList.inl"
324 #undef callbackList_inl_
325 
void removeAll()
Removes all callbacks that have been registered.
bool isDeleted() const
Definition: noArgumentCallbackList.h:78
DtCallbackConnection addCallback(const Callback &callback, bool *addedFirst=0)
Adds a new callback to the list. A duplicate entry is checked for first, and if found, this new function is not added.
#define DT_DLL_readerWriter
This file is used to determine how to build Disable inconsistent dll linkage warning Visual Studio 6...
Definition: readerWriterDefines.h:34
std::shared_ptr< bool > deletedFlag
Definition: noArgumentCallbackList.h:81
bool operator<(const DtCallbackFunctionInfo &rhs) const
Definition: noArgumentCallbackList.h:56
DtCallbackList()
Default constructor.
unsigned myCallbackCount
Definition: noArgumentCallbackList.h:142
void invokeCallbacks(T_CallbackArg arg)
Invokes all the callbacks currently on the callback list.
bool operator>(const DtCallbackFunctionInfo &rhs) const
Definition: noArgumentCallbackList.h:61
std::set< DtCallbackInfoType > DtCallbackListType
Definition: noArgumentCallbackList.h:137
void setIsDeleted(bool b)
Definition: noArgumentCallbackList.h:77
void removeAll()
Removes all callbacks that have been registered.
void * usrData
Definition: noArgumentCallbackList.h:86
DtCallbackList; DtNoArgumentCallbackList.
Definition: noArgumentCallbackList.h:31
tbb::spin_mutex myMutex
Definition: noArgumentCallbackList.h:311
Represents the connection of a specific callback to a DtCallbackListT. Saving a DtCallbackConnection ...
Definition: noArgumentCallbackList.h:209
DtCallbackInfoType(DtCallbackFunctionType callback, void *userData)
Definition: noArgumentCallbackList.h:184
bool disconnect()
Disconnect the connection. Can be called multiple times. Can be called on a default constructed objec...
Definition: noArgumentCallbackList.h:219
bool operator==(const DtCallbackFunctionInfo &rhs) const
Definition: noArgumentCallbackList.h:66
T_CallbackFunctionType callback
Definition: noArgumentCallbackList.h:85
bool disconnect(const DtCallbackInfoType &)
Definition: noArgumentCallbackList.h:301
tbb::spin_mutex myMutex
Definition: noArgumentCallbackList.h:192
bool removeCallback(DtCallbackFunctionType fcn, void *usrData)
Removes the specified callback from the list.
DtCallbackList & operator=(const DtCallbackList &orig)
Definition: noArgumentCallbackList.h:120
DtCallbackConnection()
Default constructor.
Definition: noArgumentCallbackList.h:213
DtCallbackListT & operator=(const DtCallbackListT &)
DtCallbackFunctionInfo & operator=(const DtCallbackFunctionInfo &rhs)
Definition: noArgumentCallbackList.h:47
unsigned myCallbackCount
Definition: noArgumentCallbackList.h:195
tbb::spin_mutex myMutex
Definition: noArgumentCallbackList.h:139
bool addCallback(DtCallbackFunctionType fcn, void *usrData)
Adds a new callback to the list. A duplicate entry is checked for first, and if found, this new function is not added.
DtCallbackFunctionInfo(T_CallbackFunctionType func, void *usr=0)
Definition: noArgumentCallbackList.h:33
This is a templated class which manages a list of callbacks. A class is instantiated from this templa...
Definition: noArgumentCallbackList.h:202
DtCallbackInfoType(const Callback &callback)
Definition: noArgumentCallbackList.h:303
unsigned myCallbackCount
Definition: noArgumentCallbackList.h:314
DtCallbackConnection(const DisconnectFunctionType &func)
Definition: noArgumentCallbackList.h:237
This class works just as DtCalllbackTable, but manages callbacks that have no arguments, and therefore don&#39;t fit in the template defined by DtCallbackList.
Definition: noArgumentCallbackList.h:151
boost::function< bool()> DisconnectFunctionType
Definition: noArgumentCallbackList.h:235
Definition: noArgumentCallbackList.h:90
DisconnectFunctionType myDisconnectFunction
Definition: noArgumentCallbackList.h:240
Definition: noArgumentCallbackList.h:129
void removeCallback(const Callback &callback, bool *removedLast=0)
Removes the specified callback from the list if it exists. Uses the equality operator of the supplied...
DtCallbackFunctionInfo(const DtCallbackFunctionInfo &orig)
Definition: noArgumentCallbackList.h:40
bool operator!=(const DtCallbackFunctionInfo &rhs) const
Definition: noArgumentCallbackList.h:72
std::set< DtCallbackInfoType > DtCallbackListType
Definition: noArgumentCallbackList.h:190
void * userData() const
Definition: noArgumentCallbackList.h:81
const T_CallbackFunctionType & getCallback() const
Definition: noArgumentCallbackList.h:80
DtCallbackInfoType(DtCallbackFunctionType callback, void *userData=0)
Definition: noArgumentCallbackList.h:131
DtCallbackInfoType::List myCallbackList
Definition: noArgumentCallbackList.h:313
DtCallbackListT()
Default constructor.
Definition: noArgumentCallbackList.h:258
DtCallbackListType myCallbackList
Definition: noArgumentCallbackList.h:141
void invokeCallbacks()
Invokes all the callbacks currently on the callback list.
Definition: noArgumentCallbackList.h:182
DtCallbackListType myCallbackList
Definition: noArgumentCallbackList.h:194

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)