Ocean
Event.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_BASE_EVENT_H
9 #define META_OCEAN_BASE_EVENT_H
10 
11 #include "ocean/base/Base.h"
12 #include "ocean/base/Callback.h"
13 #include "ocean/base/ObjectRef.h"
14 #include "ocean/base/Singleton.h"
15 #include "ocean/base/Thread.h"
16 
17 #include <queue>
18 
19 namespace Ocean
20 {
21 
22 // Forward declaration.
23 class Event;
24 
25 /**
26  * Definition of an object reference holding an event object.
27  * @see Event.
28  * @ingroup base
29  */
31 
32 /**
33  * This class implements the base class for all event classes.
34  * Events can be used to share, to forward or to distribute arbitrary information from one component to another component while dependencies can be avoided.<br>
35  * Further, events may be used to synchronize asynchronous tasks.<br>
36  *
37  * @see EventRef, EventManager.
38  * @ingroup base
39  */
40 class OCEAN_BASE_EXPORT Event
41 {
42  public:
43 
44  /**
45  * Destructs an event object.
46  */
47  virtual ~Event();
48 
49  /**
50  * Returns the type of this event.
51  * @return Event type
52  */
53  inline unsigned int type() const;
54 
55  /**
56  * Returns the sub-type of this event.
57  * @return Event sub-type
58  */
59  inline unsigned int subtype() const;
60 
61  /**
62  * Casts this event to an event with specified type.
63  * @return Casted event object
64  * @tparam T Data type of the resulting event
65  */
66  template <typename T> const T& cast() const;
67 
68  /**
69  * Casts this event to an event with specified type.
70  * @return Casted event object
71  * @tparam T Data type of the resulting event
72  */
73  template <typename T> T& cast();
74 
75  protected:
76 
77  /**
78  * Creates a new event object.
79  * @param type The type of the event
80  * @param subtype The subtype of the event
81  */
82  inline Event(const unsigned int type, const unsigned int subtype);
83 
84  protected:
85 
86  /// The type of the event.
87  unsigned int eventType;
88 
89  /// The sub-type of the event.
90  unsigned int eventSubtype;
91 };
92 
93 /**
94  * This class implements a scoped event.
95  * A scoped event stores three individual events.<br>
96  * One event is invoked when the scope is entered.<br>
97  * One event is invoked when the scope is left.<br>
98  * One optional event is invoked when the scope is paused.
99  * @tparam T Event type that will be handled
100  * @see Event.
101  * @ingroup base
102  */
103 template <typename T>
105 {
106  public:
107 
108  /**
109  * Definition of a callback function providing the event as single parameter.
110  */
112 
113  /**
114  * Definition of a list of callback functions.
115  */
117 
118  public:
119 
120  /**
121  * Creates a new scoped event object.
122  * @param enterEvent Event that will be invoked during the creation of this event
123  * @param breakEvent Event that will be invoked when this object is disposed as long as the leave() function hasn't been called
124  * @param leaveEvent Event that will be invoked when this object is disposed as long as the leave() function has been called
125  * @param callbacks List of callback functions to those the events will be sent
126  */
127  inline ScopedEvent(const T& enterEvent, T& breakEvent, T& leaveEvent, const EventCallbacks& callbacks);
128 
129  /**
130  * Destructs this scoped event object.
131  */
132  inline ~ScopedEvent();
133 
134  /**
135  * Activates the leave event of this object.
136  * If the leave event is activated, the leave event will be invoked instead of the break event in the moment this object is disposed.
137  */
138  inline void leave() const;
139 
140  /**
141  * Returns the break-event of this object, nothing else will be done.
142  * @return Event object
143  */
144  inline const T& breakEvent() const;
145 
146  /**
147  * Returns the break-event of this object, nothing else will be done.
148  * @return Event object
149  */
150  inline T& breakEvent();
151 
152  /**
153  * Returns the leave-event of this object, nothing else will be done.
154  * @return Event object
155  */
156  inline const T& leaveEvent() const;
157 
158  /**
159  * Returns the leave-event of this object, nothing else will be done.
160  * @return Event object
161  */
162  inline T& leaveEvent();
163 
164  protected:
165 
166  /// Break event which will be invoked if the leave() function has not been called before this object is disposed.
168 
169  /// Leave event which will be invoked if the leave() function has been called before this object is disposed.
171 
172  /// A list of callback functions to those the events will be sent.
174 
175  /// Leave statement of this object.
176  mutable bool scopedEventLeave;
177 };
178 
179 /**
180  * This class implements a central manager for events that can be pushed and received from/in individual components of an application or a software package.
181  * The manager has an own thread distributing the events.
182  * @see Event.
183  * @ingroup base
184  */
186  public Singleton<EventManager>,
187  public Thread
188 {
189  friend class Singleton<EventManager>;
190 
191  public:
192 
193  /**
194  * Definition of a callback function for events.
195  */
197 
198  protected:
199 
200  /**
201  * Definition of a set of callback functions.
202  */
204 
205  /**
206  * Definition of a map mapping event types to callback functions.
207  */
208  typedef std::map<unsigned int, EventCallbacks> EventCallbacksMap;
209 
210  /**
211  * Definition of a class encapsulating a registration request.
212  */
214  {
215  public:
216 
217  /**
218  * Creates a new registration request.
219  * @param registerCallback True, to register the callback function; False, to unregister the callback function
220  * @param id The registration request id of the new object
221  * @param eventTypes The event type(s) for which the callback function will be registered or unregistered
222  * @param callback The callback function
223  */
224  inline RegistrationRequest(const bool registerCallback, const unsigned int id, const unsigned int eventTypes, const EventCallback& callback);
225 
226  /**
227  * Returns whether the request intends an register or unregister execution.
228  * @return True, to register the callback function; False, to unregister the callback function
229  */
230  inline bool registerCallback() const;
231 
232  /**
233  * Returns the registration request id of this object.
234  * @return The request id
235  */
236  inline unsigned int id() const;
237 
238  /**
239  * Returns the event types for which the callback function will be registered or unregistered
240  * @return The event types
241  */
242  inline unsigned int eventTypes() const;
243 
244  /**
245  * Returns the callback function of this request object.
246  * @return The callback function
247  */
248  inline const EventCallback& callback() const;
249 
250  protected:
251 
252  /// True, to register the callback function; False, to unregister the callback function.
254 
255  /// The registration request id of the new object.
256  unsigned int requestId;
257 
258  /// The event types for which the callback function will be registered or unregistered.
259  unsigned int requestEventTypes;
260 
261  /// The callback function of this request.
263  };
264 
265  /**
266  * Definition of a vector holding registration requests.
267  */
268  typedef std::vector<RegistrationRequest> RegistrationRequests;
269 
270  /**
271  * Definition of a queue holding events.
272  */
273  typedef std::queue<EventRef> EventQueue;
274 
275  public:
276 
277  /**
278  * Pushes a new event to the event queue.
279  * @param eventObject The new event to push
280  */
281  void pushEvent(const EventRef& eventObject);
282 
283  /**
284  * Registers a callback function for a specific type or types of events.
285  * Each registered callback function must be unregistered later.
286  * @param eventTypes The type(s) of events which will be forwarded to the provided callback function
287  * @param eventCallback The callback function which will receive the events with the defined type
288  * @see unregisterEventFunction().
289  */
290  void registerEventFunction(const unsigned int eventTypes, const EventCallback& eventCallback);
291 
292  /**
293  * Unregisters a callback function for a specific type or types of events.
294  * The callback function must have been registered before.
295  * @param eventTypes The type(s) of events which have been registered
296  * @param eventCallback The callback function which has been registered
297  * @see registerEventFunction().
298  */
299  void unregisterEventFunction(const unsigned int eventTypes, const EventCallback& eventCallback);
300 
301  protected:
302 
303  /**
304  * Creates a new event manager object.
305  */
307 
308  /**
309  * Destructs an event manger object.
310  */
312 
313  /**
314  * The internal thread function.
315  * @see Thread::threadRun().
316  */
317  virtual void threadRun();
318 
319  private:
320 
321  /**
322  * Adds an event function.
323  * @param eventTypes The type(s) of events which will be forwarded to the provided callback function
324  * @param eventCallback The callback function which will receive the events with the defined type
325  */
326  void addEventFunction(const unsigned int eventTypes, const EventCallback& eventCallback);
327 
328  /**
329  * Removes an event function.
330  * @param eventTypes The type(s) of events which have been added
331  * @param eventCallback The callback function which has been added
332  */
333  void removeEventFunction(const unsigned int eventTypes, const EventCallback& eventCallback);
334 
335  protected:
336 
337  /// The queue of events waiting to be distributed.
339 
340  /// The map of callback functions.
342 
343  /// The registration requests which are used to modify the map of callback functions.
345 
346  /// The counter for registration requests.
347  unsigned int managerRequestCounter;
348 
349  /// The set of pending registration requests.
351 
352  /// The lock for the entire manager.
354 
355  /// The lock for the registration requests.
357 };
358 
359 inline Event::Event(const unsigned int type, const unsigned int subtype) :
360  eventType(type),
361  eventSubtype(subtype)
362 {
363  // nothing to do here
364 }
365 
366 inline unsigned int Event::type() const
367 {
368  return eventType;
369 }
370 
371 inline unsigned int Event::subtype() const
372 {
373  return eventSubtype;
374 }
375 
376 template <typename T>
377 const T& Event::cast() const
378 {
379  ocean_assert(dynamic_cast<const T*>(this));
380  return dynamic_cast<const T&>(*this);
381 }
382 
383 template <typename T>
385 {
386  ocean_assert(dynamic_cast<const T*>(this));
387  return dynamic_cast<T&>(*this);
388 }
389 
390 template <typename T>
391 inline ScopedEvent<T>::ScopedEvent(const T& enterEvent, T& breakEvent, T& leaveEvent, const EventCallbacks& callbacks) :
392  scopedBreakEvent(breakEvent),
393  scopedLeaveEvent(leaveEvent),
394  scopedCallbacks(callbacks),
395  scopedEventLeave(false)
396 {
397  scopedCallbacks(enterEvent);
398 }
399 
400 template <typename T>
402 {
403  if (scopedEventLeave)
404  scopedCallbacks(scopedLeaveEvent);
405  else
406  scopedCallbacks(scopedBreakEvent);
407 }
408 
409 template <typename T>
410 inline void ScopedEvent<T>::leave() const
411 {
412  scopedEventLeave = true;
413 }
414 
415 template <typename T>
416 inline const T& ScopedEvent<T>::breakEvent() const
417 {
418  return scopedBreakEvent;
419 }
420 
421 template <typename T>
423 {
424  return scopedBreakEvent;
425 }
426 
427 template <typename T>
428 inline const T& ScopedEvent<T>::leaveEvent() const
429 {
430  return scopedLeaveEvent;
431 }
432 
433 template <typename T>
435 {
436  return scopedLeaveEvent;
437 }
438 
439 inline EventManager::RegistrationRequest::RegistrationRequest(const bool registerCallback, const unsigned int id, const unsigned int eventTypes, const EventCallback& callback) :
440  requestRegisterCallback(registerCallback),
441  requestId(id),
442  requestEventTypes(eventTypes),
443  requestCallback(callback)
444 {
445  // nothing to do here
446 }
447 
449 {
450  return requestRegisterCallback;
451 }
452 
453 inline unsigned int EventManager::RegistrationRequest::id() const
454 {
455  return requestId;
456 }
457 
459 {
460  return requestEventTypes;
461 }
462 
464 {
465  return requestCallback;
466 }
467 
468 }
469 
470 #endif // META_OCEAN_BASE_EVENT_H
This class implements a container for callback functions.
Definition: Callback.h:3456
This class implements the base class for all event classes.
Definition: Event.h:41
Event(const unsigned int type, const unsigned int subtype)
Creates a new event object.
Definition: Event.h:359
unsigned int eventSubtype
The sub-type of the event.
Definition: Event.h:90
unsigned int subtype() const
Returns the sub-type of this event.
Definition: Event.h:371
unsigned int eventType
The type of the event.
Definition: Event.h:87
unsigned int type() const
Returns the type of this event.
Definition: Event.h:366
virtual ~Event()
Destructs an event object.
const T & cast() const
Casts this event to an event with specified type.
Definition: Event.h:377
Definition of a class encapsulating a registration request.
Definition: Event.h:214
unsigned int requestId
The registration request id of the new object.
Definition: Event.h:256
unsigned int id() const
Returns the registration request id of this object.
Definition: Event.h:453
RegistrationRequest(const bool registerCallback, const unsigned int id, const unsigned int eventTypes, const EventCallback &callback)
Creates a new registration request.
Definition: Event.h:439
bool requestRegisterCallback
True, to register the callback function; False, to unregister the callback function.
Definition: Event.h:253
const EventCallback & callback() const
Returns the callback function of this request object.
Definition: Event.h:463
bool registerCallback() const
Returns whether the request intends an register or unregister execution.
Definition: Event.h:448
unsigned int eventTypes() const
Returns the event types for which the callback function will be registered or unregistered.
Definition: Event.h:458
unsigned int requestEventTypes
The event types for which the callback function will be registered or unregistered.
Definition: Event.h:259
EventCallback requestCallback
The callback function of this request.
Definition: Event.h:262
This class implements a central manager for events that can be pushed and received from/in individual...
Definition: Event.h:188
Lock managerRegistrationRequestLock
The lock for the registration requests.
Definition: Event.h:356
void pushEvent(const EventRef &eventObject)
Pushes a new event to the event queue.
~EventManager()
Destructs an event manger object.
Callback< void, const EventRef & > EventCallback
Definition of a callback function for events.
Definition: Event.h:196
Callbacks< EventCallback > EventCallbacks
Definition of a set of callback functions.
Definition: Event.h:203
std::queue< EventRef > EventQueue
Definition of a queue holding events.
Definition: Event.h:273
RegistrationRequests managerRegistrationRequests
The registration requests which are used to modify the map of callback functions.
Definition: Event.h:344
EventCallbacksMap managerEventCallbacksMap
The map of callback functions.
Definition: Event.h:341
void addEventFunction(const unsigned int eventTypes, const EventCallback &eventCallback)
Adds an event function.
Lock managerLock
The lock for the entire manager.
Definition: Event.h:353
EventManager()
Creates a new event manager object.
void removeEventFunction(const unsigned int eventTypes, const EventCallback &eventCallback)
Removes an event function.
EventQueue managerEventQueue
The queue of events waiting to be distributed.
Definition: Event.h:338
virtual void threadRun()
The internal thread function.
std::map< unsigned int, EventCallbacks > EventCallbacksMap
Definition of a map mapping event types to callback functions.
Definition: Event.h:208
unsigned int managerRequestCounter
The counter for registration requests.
Definition: Event.h:347
void unregisterEventFunction(const unsigned int eventTypes, const EventCallback &eventCallback)
Unregisters a callback function for a specific type or types of events.
IndexSet32 managerRequestSet
The set of pending registration requests.
Definition: Event.h:350
void registerEventFunction(const unsigned int eventTypes, const EventCallback &eventCallback)
Registers a callback function for a specific type or types of events.
std::vector< RegistrationRequest > RegistrationRequests
Definition of a vector holding registration requests.
Definition: Event.h:268
This class implements a recursive lock object.
Definition: Lock.h:31
This template class implements a object reference with an internal reference counter.
Definition: base/ObjectRef.h:58
This class implements a scoped event.
Definition: Event.h:105
T & scopedBreakEvent
Break event which will be invoked if the leave() function has not been called before this object is d...
Definition: Event.h:167
bool scopedEventLeave
Leave statement of this object.
Definition: Event.h:176
T & scopedLeaveEvent
Leave event which will be invoked if the leave() function has been called before this object is dispo...
Definition: Event.h:170
~ScopedEvent()
Destructs this scoped event object.
Definition: Event.h:401
const T & leaveEvent() const
Returns the leave-event of this object, nothing else will be done.
Definition: Event.h:428
void leave() const
Activates the leave event of this object.
Definition: Event.h:410
ScopedEvent(const T &enterEvent, T &breakEvent, T &leaveEvent, const EventCallbacks &callbacks)
Creates a new scoped event object.
Definition: Event.h:391
const EventCallbacks & scopedCallbacks
A list of callback functions to those the events will be sent.
Definition: Event.h:173
Callbacks< EventCallback > EventCallbacks
Definition of a list of callback functions.
Definition: Event.h:116
const T & breakEvent() const
Returns the break-event of this object, nothing else will be done.
Definition: Event.h:416
Callback< void, const T & > EventCallback
Definition of a callback function providing the event as single parameter.
Definition: Event.h:111
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
This class implements a thread.
Definition: Thread.h:115
std::set< Index32 > IndexSet32
Definition of a set holding 32 bit indices.
Definition: Base.h:114
ObjectRef< Event > EventRef
Definition of an object reference holding an event object.
Definition: Event.h:23
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15