Utilities
Utilities header only library
EventHandler.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef UTILS_EVENT_HANDLER_H
7 #define UTILS_EVENT_HANDLER_H
8 #include <Utils/AtomicQueue.h>
9 #include <memory>
10 
11 namespace utils
12 {
13 
23  {
24  public:
26  virtual void PollEvents()
27  {
28  while (!events_.IsEmpty())
29  {
30  auto ev = events_.Pop();
31  ProcessEvent(ev.get());
32  }
33  };
34  protected:
36  using EventId = std::string;
38  struct Event
39  {
40  Event(const std::string &_id) : id(_id) {};
41  std::string id;
42  };
44  virtual void ProcessEvent([[maybe_unused]] const Event *e) {};
46  template<typename T = Event, typename... Args>
47  void RegisterEvent(Args... args)
48  {
49  events_.Push(std::make_shared<T>(args...));
50  }
51  private:
53  };
54 
55 }
56 #endif /* !UTILS_EVENT_HANDLER_H */
Queue which allows calling members from multiple threads without interference.
Definition: AtomicQueue.h:16
Default event. Inherit this structure to create custom event.
Definition: EventHandler.h:38
void RegisterEvent(Args... args)
Adds new event to the event queue.
Definition: EventHandler.h:47
virtual void PollEvents()
Request to process all registered events.
Definition: EventHandler.h:26
General namespace for utilities.
Definition: BulletConversions.h:9
std::string EventId
Unique identifier of event.
Definition: EventHandler.h:36
virtual void ProcessEvent([[maybe_unused]] const Event *e)
Process a single event during polling.
Definition: EventHandler.h:44
Provides an interface for registering and processing events for inherited classes.
Definition: EventHandler.h:22