AGX Dynamics  2.37.2.0
Callback.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_CALLBACK_H
18 #define AGX_CALLBACK_H
19 
20 #include <agx/agx.h>
21 #include <functional>
22 
23 
24 #ifdef _MSC_VER
25 # pragma warning(push)
26 # pragma warning(disable: 4251) // warning C4251: class X needs to have dll-interface to be used by clients of class Y
27 #endif
28 
29 
30 namespace agx
31 {
54  {
55  public:
56  typedef std::function<void ()> CallbackFunction;
57 
58  Callback();
59 
64  Callback(const CallbackFunction& func, bool repeatEnable = true);
65 
70  template<class ClassT>
71  Callback(void (ClassT::*fun)(), ClassT* obj, bool repeatEnable = true);
72 
73 
74  public:
76  bool isValid() const;
77 
78  operator bool() const;
79 
81  void operator() () const;
82  void run() const;
83 
85  bool isRepeating() const;
86 
88  const CallbackFunction& implementation() const;
89  operator const CallbackFunction& () const;
90 
91  private:
92  CallbackFunction m_callback;
93  bool m_repeating;
94  };
95 
97 
99  template< typename T >
100  class Callback1
101  {
102  public:
103  typedef std::function<void (T)> CallbackFunction;
104 
105  Callback1();
106  Callback1(const CallbackFunction& func, bool repeatEnable = true);
107 
108  template<class ClassT>
109  Callback1(void (ClassT::*fun)(T), ClassT* obj, bool repeatEnable = true);
110 
111  template<class ClassT>
112  Callback1(void (ClassT::*fun)(), ClassT* obj, bool repeatEnable = true);
113 
114  public:
115 
117  bool isValid() const;
118 
120  void operator() ( T arg1 ) const;
121  void run( T arg1 ) const;
122 
124  bool isRepeating() const;
125 
127  const CallbackFunction& implementation() const;
128  operator const CallbackFunction& () const;
129 
130  private:
131  CallbackFunction m_callback;
132  bool m_repeating;
133  };
134 
136 
138  template< typename T1, typename T2 >
139  class Callback2
140  {
141  public:
142  typedef std::function<void (T1, T2)> CallbackFunction;
143 
144  Callback2();
145  Callback2(const CallbackFunction& func, bool repeatEnable = true);
146 
147  template<class ClassT>
148  Callback2(void (ClassT::*fun)(T1, T2), ClassT* obj, bool repeatEnable = true);
149 
150  template<class ClassT>
151  Callback2(void (ClassT::*fun)(T1), ClassT* obj, bool repeatEnable = true);
152 
153  template<class ClassT>
154  Callback2(void (ClassT::*fun)(), ClassT* obj, bool repeatEnable = true);
155 
156  public:
157 
159  bool isValid() const;
160 
162  void operator() ( T1 arg1, T2 arg2 ) const;
163  void run( T1 arg1, T2 arg2 ) const;
164 
166  bool isRepeating() const;
167 
169  const CallbackFunction& implementation() const;
170  operator const CallbackFunction& () const;
171 
172  private:
173  CallbackFunction m_callback;
174  bool m_repeating;
175  };
176 
178 
180  template< typename T1, typename T2, typename T3 >
181  class Callback3
182  {
183  public:
184  typedef std::function<void (T1, T2, T3)> CallbackFunction;
185 
186  Callback3();
187  Callback3(const CallbackFunction& func, bool repeatEnable = true);
188 
189  template<class ClassT>
190  Callback3(void (ClassT::*fun)(T1, T2, T3), ClassT* obj, bool repeatEnable = true);
191 
192  template<class ClassT>
193  Callback3(void (ClassT::*fun)(T1, T2), ClassT* obj, bool repeatEnable = true);
194 
195  template<class ClassT>
196  Callback3(void (ClassT::*fun)(T1), ClassT* obj, bool repeatEnable = true);
197 
198  template<class ClassT>
199  Callback3(void (ClassT::*fun)(), ClassT* obj, bool repeatEnable = true);
200 
201  public:
202 
204  bool isValid() const;
205 
207  void operator() ( T1 arg1, T2 arg2, T3 arg3 ) const;
208  void run( T1 arg1, T2 arg2, T3 arg3 ) const;
209 
211  bool isRepeating() const;
212 
214  const CallbackFunction& implementation() const;
215  operator const CallbackFunction& () const;
216 
217  private:
218  CallbackFunction m_callback;
219  bool m_repeating;
220  };
221 
222 
223  template< typename T1, typename T2, typename T3, typename T4 >
224  class Callback4
225  {
226  public:
227  typedef std::function<void (T1, T2, T3, T4)> CallbackFunction;
228 
229  Callback4();
230  Callback4(const CallbackFunction& func, bool repeatEnable = true);
231 
232  template<class ClassT>
233  Callback4(void (ClassT::*fun)(T1, T2, T3, T4), ClassT* obj, bool repeatEnable = true);
234 
235  template<class ClassT>
236  Callback4(void (ClassT::*fun)(T1, T2, T3), ClassT* obj, bool repeatEnable = true);
237 
238  template<class ClassT>
239  Callback4(void (ClassT::*fun)(T1, T2), ClassT* obj, bool repeatEnable = true);
240 
241  template<class ClassT>
242  Callback4(void (ClassT::*fun)(T1), ClassT* obj, bool repeatEnable = true);
243 
244  template<class ClassT>
245  Callback4(void (ClassT::*fun)(), ClassT* obj, bool repeatEnable = true);
246 
247  public:
248  operator const CallbackFunction& () const;
249  bool isValid() const;
250 
251  void operator() ( T1 arg1, T2 arg2, T3 arg3, T4 arg4 ) const;
252  void run( T1 arg1, T2 arg2, T3 arg3, T4 arg4 ) const;
253 
254  const CallbackFunction& implementation() const;
255  bool isRepeating() const;
256 
257  private:
258  bool m_repeating;
259  CallbackFunction m_callback;
260  };
261 
262  /* Implementation */
263  template<class ClassT>
264  AGX_FORCE_INLINE Callback::Callback(void (ClassT::*fun)(), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj)), m_repeating(repeatEnable)
265  {
266  }
267 
268  AGX_FORCE_INLINE Callback::Callback() : m_repeating(true) {}
269  AGX_FORCE_INLINE Callback::Callback( const CallbackFunction& func, bool repeatEnable ) : m_callback(func), m_repeating(repeatEnable) {}
270 
272  AGX_FORCE_INLINE bool Callback::isRepeating() const { return m_repeating; }
273 
274 
275  AGX_FORCE_INLINE void Callback::operator() () const { this->run(); }
277  {
278  agxAssert(m_callback);
279  m_callback();
280  }
281 
282  AGX_FORCE_INLINE Callback::operator const Callback::CallbackFunction& () const { return m_callback; }
283  AGX_FORCE_INLINE bool Callback::isValid () const { return m_callback != nullptr; }
284  AGX_FORCE_INLINE Callback::operator bool() const { return this->isValid(); }
285 
286 
288  template <typename T> template<class ClassT>
289  inline Callback1<T>::Callback1(void (ClassT::*fun)(T), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj,std::placeholders:: _1)), m_repeating(repeatEnable) {}
290 
291  template <typename T> template<class ClassT>
292  AGX_FORCE_INLINE Callback1<T>::Callback1(void (ClassT::*fun)(), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj)), m_repeating(repeatEnable) {}
293 
294  template <typename T>
295  AGX_FORCE_INLINE Callback1<T>::Callback1() : m_repeating(true) {}
296 
297  template <typename T>
298  AGX_FORCE_INLINE Callback1<T>::Callback1( const CallbackFunction& func, bool repeatEnable ) : m_callback(func), m_repeating(repeatEnable) {}
299 
300  template <typename T>
301  AGX_FORCE_INLINE void Callback1<T>::operator() (T arg1) const { this->run(arg1); }
302 
303  template< typename T >
305  {
306  agxAssert(m_callback);
307  m_callback(arg1);
308  }
309 
310  template <typename T>
311  AGX_FORCE_INLINE const typename Callback1<T>::CallbackFunction& Callback1<T>::implementation() const { return m_callback; }
312 
313  template <typename T>
314  AGX_FORCE_INLINE bool Callback1<T>::isRepeating() const { return m_repeating; }
315 
316  template <typename T>
317  AGX_FORCE_INLINE Callback1<T>::operator const typename Callback1<T>::CallbackFunction& () const { return m_callback; }
318 
319  template <typename T>
320  AGX_FORCE_INLINE bool Callback1<T>::isValid() const { return m_callback != nullptr; }
321 
323  template <typename T1, typename T2> template<class ClassT>
324 #if defined( __GNUC__ ) && __GNUC__ == 4 && __GNUC_MINOR__ >= 4
325  inline Callback2<T1, T2>::Callback2(void (ClassT::*fun)(T1, T2), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj,std::placeholders:: _1,std::placeholders:: _2)), m_repeating(repeatEnable) {}
326 #else
327  AGX_FORCE_INLINE Callback2<T1, T2>::Callback2(void (ClassT::*fun)(T1, T2), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj,std::placeholders:: _1,std::placeholders:: _2)), m_repeating(repeatEnable) {}
328 #endif
329 
330  template <typename T1, typename T2> template<class ClassT>
331  AGX_FORCE_INLINE Callback2<T1, T2>::Callback2(void (ClassT::*fun)(T1), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj,std::placeholders:: _1)), m_repeating(repeatEnable) {}
332 
333  template <typename T1, typename T2> template<class ClassT>
334  AGX_FORCE_INLINE Callback2<T1, T2>::Callback2(void (ClassT::*fun)(), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj)), m_repeating(repeatEnable) {}
335 
336  template <typename T1, typename T2>
338  template <typename T1, typename T2>
339  AGX_FORCE_INLINE Callback2<T1, T2>::Callback2( const CallbackFunction& func, bool repeatEnable ) : m_callback(func), m_repeating(repeatEnable) {}
340 
341  template <typename T1, typename T2>
342  AGX_FORCE_INLINE void Callback2<T1,T2>::operator() (T1 arg1, T2 arg2) const { this->run(arg1, arg2); }
343 
344  template< typename T1, typename T2 >
345  AGX_FORCE_INLINE void Callback2<T1,T2>::run(T1 arg1, T2 arg2 ) const
346  {
347  agxAssert(m_callback);
348  m_callback(arg1,arg2);
349  }
350 
351  template <typename T1, typename T2>
353 
354  template <typename T1, typename T2>
355  AGX_FORCE_INLINE bool Callback2<T1, T2>::isRepeating() const { return m_repeating; }
356 
357  template <typename T1, typename T2>
358  AGX_FORCE_INLINE Callback2<T1, T2>::operator const typename Callback2<T1, T2>::CallbackFunction& () const { return m_callback; }
359 
360  template <typename T1, typename T2>
361  AGX_FORCE_INLINE bool Callback2<T1, T2>::isValid() const { return m_callback != nullptr; }
362 
363 
365 template <typename T1, typename T2, typename T3> template<class ClassT>
366 #if defined( __GNUC__ ) && __GNUC__ == 4 && __GNUC_MINOR__ >= 4
367 inline Callback3<T1, T2, T3>::Callback3(void (ClassT::*fun)(T1, T2, T3), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj,std::placeholders:: _1,std::placeholders:: _2,std::placeholders:: _3)), m_repeating(repeatEnable) {}
368 #else
369  AGX_FORCE_INLINE Callback3<T1, T2, T3>::Callback3(void (ClassT::*fun)(T1, T2, T3), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj,std::placeholders:: _1,std::placeholders:: _2,std::placeholders:: _3)), m_repeating(repeatEnable) {}
370 #endif
371 
372  template <typename T1, typename T2, typename T3> template<class ClassT>
373  AGX_FORCE_INLINE Callback3<T1, T2, T3>::Callback3(void (ClassT::*fun)(T1, T2), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj,std::placeholders:: _1,std::placeholders:: _2)), m_repeating(repeatEnable) {}
374 
375  template <typename T1, typename T2, typename T3> template<class ClassT>
376  AGX_FORCE_INLINE Callback3<T1, T2, T3>::Callback3(void (ClassT::*fun)(T1), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj,std::placeholders:: _1)), m_repeating(repeatEnable) {}
377 
378  template <typename T1, typename T2, typename T3> template<class ClassT>
379  AGX_FORCE_INLINE Callback3<T1, T2, T3>::Callback3(void (ClassT::*fun)(), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj)), m_repeating(repeatEnable) {}
380 
381  template <typename T1, typename T2, typename T3>
383 
384  template <typename T1, typename T2, typename T3>
385  AGX_FORCE_INLINE Callback3<T1, T2, T3>::Callback3( const CallbackFunction& func, bool repeatEnable ) : m_callback(func), m_repeating(repeatEnable) {}
386 
387  template <typename T1, typename T2, typename T3>
388  AGX_FORCE_INLINE void Callback3<T1,T2,T3>::operator() (T1 arg1, T2 arg2, T3 arg3) const { this->run(arg1, arg2, arg3); }
389 
390  template< typename T1, typename T2, typename T3 >
391  AGX_FORCE_INLINE void Callback3<T1,T2,T3>::run( T1 arg1, T2 arg2, T3 arg3 ) const
392  {
393  agxAssert(m_callback);
394  m_callback(arg1,arg2,arg3);
395  }
396 
397  template <typename T1, typename T2, typename T3>
399 
400  template <typename T1, typename T2, typename T3>
401  AGX_FORCE_INLINE bool Callback3<T1, T2, T3>::isRepeating() const { return m_repeating; }
402 
403  template <typename T1, typename T2, typename T3>
405 
406  template <typename T1, typename T2, typename T3>
407  AGX_FORCE_INLINE bool Callback3<T1, T2, T3>::isValid() const { return m_callback != nullptr; }
408 
410  template <typename T1, typename T2, typename T3, typename T4> template<class ClassT>
411  AGX_FORCE_INLINE Callback4<T1, T2, T3, T4>::Callback4(void (ClassT::*fun)(T1, T2, T3, T4), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj,std::placeholders:: _1,std::placeholders:: _2,std::placeholders:: _3,std::placeholders:: _4)), m_repeating(repeatEnable) {}
412 
413  template <typename T1, typename T2, typename T3, typename T4> template<class ClassT>
414  AGX_FORCE_INLINE Callback4<T1, T2, T3, T4>::Callback4(void (ClassT::*fun)(T1, T2, T3), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj,std::placeholders:: _1,std::placeholders:: _2,std::placeholders:: _3)), m_repeating(repeatEnable) {}
415 
416  template <typename T1, typename T2, typename T3, typename T4> template<class ClassT>
417  AGX_FORCE_INLINE Callback4<T1, T2, T3, T4>::Callback4(void (ClassT::*fun)(T1, T2), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj,std::placeholders:: _1,std::placeholders:: _2)), m_repeating(repeatEnable) {}
418 
419  template <typename T1, typename T2, typename T3, typename T4> template<class ClassT>
420  AGX_FORCE_INLINE Callback4<T1, T2, T3, T4>::Callback4(void (ClassT::*fun)(T1), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj,std::placeholders:: _1)), m_repeating(repeatEnable) {}
421 
422  template <typename T1, typename T2, typename T3, typename T4> template<class ClassT>
423  AGX_FORCE_INLINE Callback4<T1, T2, T3, T4>::Callback4(void (ClassT::*fun)(), ClassT* obj, bool repeatEnable) : m_callback(std::bind(fun, obj)), m_repeating(repeatEnable) {}
424 
425  template <typename T1, typename T2, typename T3, typename T4>
427 
428  template <typename T1, typename T2, typename T3, typename T4>
429  AGX_FORCE_INLINE Callback4<T1, T2, T3, T4>::Callback4( const CallbackFunction& func, bool repeatEnable ) : m_callback(func), m_repeating(repeatEnable) {}
430 
431  template <typename T1, typename T2, typename T3, typename T4>
432  AGX_FORCE_INLINE void Callback4<T1, T2, T3, T4>::operator() (T1 arg1, T2 arg2, T3 arg3, T4 arg4) const { this->run(arg1, arg2, arg3, arg4); }
433 
434  template <typename T1, typename T2, typename T3, typename T4>
435  AGX_FORCE_INLINE void Callback4<T1, T2, T3, T4>::run( T1 arg1, T2 arg2, T3 arg3, T4 arg4 ) const
436  {
437  agxAssert(m_callback);
438  m_callback(arg1, arg2, arg3, arg4);
439  }
440 
441  template <typename T1, typename T2, typename T3, typename T4>
443 
444  template <typename T1, typename T2, typename T3, typename T4>
445  AGX_FORCE_INLINE bool Callback4<T1, T2, T3, T4>::isRepeating() const { return m_repeating; }
446 
447  template <typename T1, typename T2, typename T3, typename T4>
449 
450  template <typename T1, typename T2, typename T3, typename T4>
451  AGX_FORCE_INLINE bool Callback4<T1, T2, T3, T4>::isValid() const { return m_callback != nullptr; }
452 }
453 
454 
455 #ifdef _MSC_VER
456 # pragma warning(pop)
457 #endif
458 
459 
460 #endif
void operator()() const
Execute the callback.
Definition: Callback.h:275
void operator()(T1 arg1, T2 arg2, T3 arg3) const
Execute the callback.
Definition: Callback.h:388
void run(T1 arg1, T2 arg2, T3 arg3, T4 arg4) const
Definition: Callback.h:435
std::function< void(T1, T2, T3)> CallbackFunction
Definition: Callback.h:184
Generalized callback, using std::function.
Definition: Callback.h:53
void run(T1 arg1, T2 arg2, T3 arg3) const
Definition: Callback.h:391
bool isRepeating() const
Definition: Callback.h:272
STL namespace.
const CallbackFunction & implementation() const
Return the std representation.
Definition: Callback.h:398
bool isRepeating() const
Definition: Callback.h:401
std::function< void()> CallbackFunction
Definition: Callback.h:56
#define AGX_FORCE_INLINE
Definition: Object.h:59
bool isValid() const
Definition: Callback.h:283
std::function< void(T1, T2, T3, T4)> CallbackFunction
Definition: Callback.h:227
const CallbackFunction & implementation() const
Definition: Callback.h:442
bool isRepeating() const
Definition: Callback.h:445
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Templated callback with one argument.
Definition: Callback.h:100
Templated callback with three arguments.
Definition: Callback.h:181
#define agxAssert(expr)
Definition: debug.h:143
Templated callback with two arguments.
Definition: Callback.h:139
std::function< void(T)> CallbackFunction
Definition: Callback.h:103
bool isValid() const
Definition: Callback.h:407
std::function< void(T1, T2)> CallbackFunction
Definition: Callback.h:142
const CallbackFunction & implementation() const
Return the std representation.
Definition: Callback.h:271
void operator()(T1 arg1, T2 arg2, T3 arg3, T4 arg4) const
Definition: Callback.h:432
bool isValid() const
Definition: Callback.h:451
void run() const
Definition: Callback.h:276
#define AGXCORE_EXPORT