AGX Dynamics  2.35.0.1
Event.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. 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_EVENT_H
18 #define AGX_EVENT_H
19 
20 #include <iosfwd>
21 #include <agx/Name.h>
22 #include <agx/Callback.h>
23 #include <agx/Vector.h>
24 
25 namespace agx
26 {
27  class Thread;
28 
33  template <typename T>
34  class EventT
35  {
36  public:
37  typedef T CallbackType;
39 
40  public:
41  EventT( const agx::Name& name = agx::Name());
42 
46  const agx::Name& getName() const;
47 
51  void addCallback( const T* callback );
52 
56  void addCallbackFirst( const T* callback );
57 
58  /*
59  Remove a callback.
60  \return false if the callback was not previously added
61  **/
62  bool removeCallback( const T* callback );
63 
68 
72  bool containsCallback( const T* callback) const;
73 
78 
79  protected:
84  };
85 
86  //---------------------------------------------------------------
87 
89  class AGXCORE_EXPORT Event : public EventT< Callback >
90  {
91  public:
92  Event( const agx::Name& name = agx::Name());
93 
95  void trigger();
96  };
97 
98  //---------------------------------------------------------------
99 
101  template <typename T1>
102  class Event1 : public EventT< Callback1<T1> >
103  {
104  public:
105  Event1( const agx::Name& name = agx::Name(), const T1& defaultArg = T1());
106 
108  void trigger();
109 
111  void trigger( const T1& arg );
112 
113  private:
114  T1 m_defaultArg;
115  };
116 
117  //---------------------------------------------------------------
118 
120  template <typename T1, typename T2>
121  class Event2 : public EventT< Callback2<T1, T2> >
122  {
123  public:
124  Event2( const agx::Name& name = agx::Name(), const T1& defaultArg = T1());
125 
127  void trigger( const T2& arg2 );
128 
130  void trigger( const T1& arg1, const T2& arg2 );
131 
132  private:
133  T1 m_defaultArg;
134  };
135 
136  //---------------------------------------------------------------
137 
139  template <typename T1, typename T2, typename T3>
140  class Event3 : public EventT< Callback3<T1, T2, T3> >
141  {
142  public:
143  Event3( const agx::Name& name = agx::Name(), const T1& defaultArg = T1());
144 
146  void trigger( const T2& arg2, const T3& arg3 );
147 
149  void trigger( const T1& arg1, const T2& arg2, const T3& arg3 );
150 
151  private:
152  T1 m_defaultArg;
153  };
154 
155 
156  //---------------------------------------------------------------
157 
158 
159  // implementation
160  #define INDEX this->m_iterationIndex
162 
163  template <typename T>
164  AGX_FORCE_INLINE EventT<T>::EventT( const Name& name ) : m_name(name), m_activeThread(nullptr), m_iterationIndex(0)
165  {
166  }
167 
168  template <typename T>
169  AGX_FORCE_INLINE const Name& EventT<T>::getName() const { return m_name; }
170 
171  template <typename T>
172  AGX_FORCE_INLINE void EventT<T>::addCallback( const T* callback )
173  {
174  agxAssert(!m_activeThread || m_activeThread == __agx_Event_getActiveThread());
175  m_callbacks.push_back( callback );
176 
177  #if 0
178  /* Move callback past those with lower prio */
179  for (size_t i = m_callbacks.size() - 1; i > 0 ; --i)
180  {
181  if (m_callbacks[i].priority <= m_callbacks[i-1].priority)
182  break;
183 
184  std::swap(m_callbacks[i], m_callbacks[i-1]);
185  }
186  #endif
187  }
188 
189  template <typename T>
191  {
192  agxAssert(!m_activeThread || m_activeThread == __agx_Event_getActiveThread());
193  m_callbacks.insert( (size_t)0, callback );
194  m_iterationIndex++;
195  }
196 
197 
198  template <typename T>
199  AGX_FORCE_INLINE bool EventT<T>::removeCallback( const T* callback )
200  {
201  agxAssert(!m_activeThread || m_activeThread == __agx_Event_getActiveThread());
202  size_t index = m_callbacks.find(callback);
203 
204  if (index == m_callbacks.size())
205  return false;
206 
207 
208  m_callbacks.erase(index);
209 
210  if (index <= m_iterationIndex)
211  m_iterationIndex--;
212 
213  return true;
214  }
215 
216  template <typename T>
217  AGX_FORCE_INLINE void EventT<T>::removeAllCallbacks() { m_callbacks.clear(); }
218 
219  template <typename T>
220  AGX_FORCE_INLINE const typename EventT<T>::CallbackPtrVector& EventT<T>::getCallbacks() const { return m_callbacks; }
221 
222  template <typename T>
223  AGX_FORCE_INLINE bool EventT<T>::containsCallback(const T* callback) const { return m_callbacks.contains(callback); }
224 
226 
227  #ifdef AGX_DEBUG
228  #define AGX_EVENT_TRIGGER_START(); this->m_activeThread = __agx_Event_getActiveThread();
229  #define AGX_EVENT_TRIGGER_END(); this->m_activeThread = nullptr
230  #else
231  #define AGX_EVENT_TRIGGER_START();
232  #define AGX_EVENT_TRIGGER_END();
233  #endif
234 
236  {
238  for (INDEX = 0; INDEX < m_callbacks.size(); ++INDEX)
239  {
240  const Callback *callback = m_callbacks[INDEX];
241  if (!callback->isRepeating())
242  {
245  INDEX--;
246  }
247  callback->run();
248  }
250  }
251 
252  template< typename T1 >
253  AGX_FORCE_INLINE Event1<T1>::Event1(const Name& name, const T1& defaultArg) : EventT<Callback1<T1> >(name), m_defaultArg(defaultArg) {}
254 
255  template< typename T1 >
257  {
258  this->trigger(m_defaultArg);
259  }
260 
261  template< typename T1 >
263  {
265 
266  for (INDEX = 0; INDEX < this->m_callbacks.size(); ++INDEX)
267  {
268  const Callback1<T1> *callback = this->m_callbacks[INDEX];
269  if (!callback->isRepeating())
270  {
271  this->m_callbacks[INDEX] = this->m_callbacks.back();
272  this->m_callbacks.pop_back();
273  INDEX--;
274  }
275  callback->run(arg);
276  }
277 
279  }
280 
281 
282  template< typename T1, typename T2 >
283  AGX_FORCE_INLINE Event2<T1, T2>::Event2(const Name& name, const T1& defaultArg) : EventT<Callback2<T1, T2> >(name), m_defaultArg(defaultArg) {}
284 
285  template< typename T1, typename T2 >
287  {
288  this->trigger(m_defaultArg, arg2);
289  }
290 
291 
292  template< typename T1, typename T2 >
293  AGX_FORCE_INLINE void Event2<T1,T2>::trigger(const T1& arg1, const T2& arg2)
294  {
296 
297  for (INDEX = 0; INDEX < this->m_callbacks.size(); ++INDEX)
298  {
299  const Callback2<T1,T2> *callback = this->m_callbacks[INDEX];
300  if (!callback->isRepeating())
301  {
302  this->m_callbacks[INDEX] = this->m_callbacks.back();
303  this->m_callbacks.pop_back();
304  INDEX--;
305  }
306  callback->run(arg1, arg2);
307  }
308 
310  }
311 
312 
313  template< typename T1, typename T2, typename T3 >
314  AGX_FORCE_INLINE Event3<T1, T2, T3>::Event3(const Name& name, const T1& defaultArg) : EventT<Callback3<T1, T2, T3> >(name), m_defaultArg(defaultArg) {}
315 
316  template< typename T1, typename T2, typename T3 >
317  AGX_FORCE_INLINE void Event3<T1,T2,T3>::trigger(const T2& arg2, const T3& arg3)
318  {
319  this->trigger(m_defaultArg, arg2, arg3);
320  }
321 
322  template< typename T1, typename T2, typename T3 >
323  AGX_FORCE_INLINE void Event3<T1,T2,T3>::trigger(const T1& arg1, const T2& arg2, const T3& arg3)
324  {
326 
327  for (INDEX = 0; INDEX < this->m_callbacks.size(); ++INDEX)
328  {
329  const Callback3<T1,T2,T3> *callback = this->m_callbacks[INDEX];
330  if (!callback->isRepeating())
331  {
332  this->m_callbacks[INDEX] = this->m_callbacks.back();
333  this->m_callbacks.pop_back();
334  INDEX--;
335  }
336 
337  callback->run(arg1, arg2, arg3);
338  }
339 
341  }
342 
343 
344  template <typename T>
345  AGX_FORCE_INLINE std::ostream& operator<<(std::ostream& stream, const agx::EventT<T>& e)
346  {
347  stream << e.getName();
348  return stream;
349  }
350 
351  #undef INDEX
352 }
353 
354 #endif
355 
#define AGX_EVENT_TRIGGER_END()
Definition: Event.h:229
#define AGX_EVENT_TRIGGER_START()
Definition: Event.h:228
#define INDEX
Definition: Event.h:160
#define AGXCORE_EXPORT
Templated callback with one argument.
Definition: Callback.h:101
void run(T arg1) const
Definition: Callback.h:304
bool isRepeating() const
Definition: Callback.h:314
Templated callback with two arguments.
Definition: Callback.h:140
bool isRepeating() const
Definition: Callback.h:355
void run(T1 arg1, T2 arg2) const
Definition: Callback.h:345
Templated callback with three arguments.
Definition: Callback.h:182
void run(T1 arg1, T2 arg2, T3 arg3) const
Definition: Callback.h:391
bool isRepeating() const
Definition: Callback.h:401
Generalized callback, using std::function.
Definition: Callback.h:54
bool isRepeating() const
Definition: Callback.h:272
void run() const
Definition: Callback.h:276
size_t size() const
Definition: Container.h:134
An event with one argument.
Definition: Event.h:103
void trigger(const T1 &arg)
Trigger the event.
Definition: Event.h:262
void trigger()
Trigger the event with the default argument.
Definition: Event.h:256
Event1(const agx::Name &name=agx::Name(), const T1 &defaultArg=T1())
Definition: Event.h:253
An event with two arguments.
Definition: Event.h:122
Event2(const agx::Name &name=agx::Name(), const T1 &defaultArg=T1())
Definition: Event.h:283
void trigger(const T1 &arg1, const T2 &arg2)
Trigger the event.
Definition: Event.h:293
void trigger(const T2 &arg2)
Trigger the event with default first argument.
Definition: Event.h:286
An event with three arguments.
Definition: Event.h:141
void trigger(const T1 &arg1, const T2 &arg2, const T3 &arg3)
Trigger the event.
Definition: Event.h:323
Event3(const agx::Name &name=agx::Name(), const T1 &defaultArg=T1())
Definition: Event.h:314
void trigger(const T2 &arg2, const T3 &arg3)
Trigger the event with default first argument.
Definition: Event.h:317
An event has a list of listeners (agx::Callback), which are called when the event is triggered.
Definition: Event.h:35
Thread * m_activeThread
Definition: Event.h:82
agx::UInt32 m_iterationIndex
Definition: Event.h:83
void removeAllCallbacks()
Remove all callbacks.
Definition: Event.h:217
bool removeCallback(const T *callback)
Definition: Event.h:199
T CallbackType
Definition: Event.h:37
void addCallbackFirst(const T *callback)
Add a callback to the front of the callback list.
Definition: Event.h:190
CallbackPtrVector m_callbacks
Definition: Event.h:80
const agx::Name & getName() const
Definition: Event.h:169
VectorPOD< const CallbackType * > CallbackPtrVector
Definition: Event.h:38
EventT(const agx::Name &name=agx::Name())
Definition: Event.h:164
bool containsCallback(const T *callback) const
Definition: Event.h:223
const CallbackPtrVector & getCallbacks() const
Definition: Event.h:220
void addCallback(const T *callback)
Add a callback to the event.
Definition: Event.h:172
Name m_name
Definition: Event.h:81
An event with no arguments.
Definition: Event.h:90
void trigger()
Trigger the event.
Definition: Event.h:235
Event(const agx::Name &name=agx::Name())
Definition: Event.h:225
Representation of a name string.
Definition: Name.h:33
agx::Thread is a representation of an OS specific implementation of a computational thread.
Definition: Thread.h:198
T & back() const
Definition: agx/Vector.h:1354
#define agxAssert(expr)
Definition: debug.h:143
#define AGX_FORCE_INLINE
Definition: macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint32_t UInt32
Definition: Integer.h:40
AGXCORE_EXPORT Thread * __agx_Event_getActiveThread()
std::ostream & operator<<(std::ostream &stream, const AtomicValueT< T > &value)
Definition: AtomicValue.h:109