Utilities
Utilities header only library
Notifier.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_NOTIFIER_H
7 #define UTILS_NOTIFIER_H
8 
9 #include <vector>
10 #include <functional>
11 #include <mutex>
12 #include <condition_variable>
13 
14 
15 
16 namespace utils
17 {
18 
20  template <typename... Args>
21  class Notifier
22  {
23  public:
25  using CallbackType = std::function<void(Args...)>;
26 
28  inline void AddCallback(CallbackType callback)
29  {
30  std::lock_guard<std::mutex> lock(notifier_access_);
31  if (callback)
32  {
33  callbacks_.push_back(callback);
34  }
35  }
36 
38  inline virtual void Notify(Args... args)
39  {
40  std::lock_guard<std::mutex> lock(notifier_access_);
41  for (auto &callback : callbacks_)
42  {
43  callback(args...);
44  }
45  }
46 
48  inline bool HasCallback() const
49  {
50  return !callbacks_.empty();
51  }
52 
53  protected:
55  std::vector<CallbackType> callbacks_;
57  mutable std::mutex notifier_access_;
58  };
59 
60 
68  template <typename... Args>
70  : public Notifier<Args...>
71  {
72  public:
74  inline virtual void Notify(Args... args) override
75  {
76  std::lock_guard<std::mutex> lock(this->notifier_access_);
77  if (!this->callbacks_.empty())
78  {
79  tuples_.emplace_back(std::tuple<Args...>(args...));
80  }
81  notifier_condition_.notify_all();
82  }
83 
85  virtual void SendAllNotifications()
86  {
87  std::lock_guard<std::mutex> lock(this->notifier_access_);
88  for (const auto &tuple : tuples_)
89  {
90  for (auto &callback : this->callbacks_)
91  {
92  callback(std::get<Args>(tuple) ...);
93  }
94  }
95  tuples_.clear();
96  notifier_condition_.notify_all();
97  }
98 
100  virtual void WaitUntilAllSent()
101  {
102  std::unique_lock<std::mutex> lock(this->notifier_access_);
103  notifier_condition_.wait(lock, [this]() { return tuples_.empty(); });
104  }
105 
106  protected:
108  std::vector<std::tuple<Args...>> tuples_;
110  std::condition_variable notifier_condition_;
111  };
112 }
113 
114 #endif /* !UTILS_NOTIFIER_H */
std::condition_variable notifier_condition_
Condition variable to trigger rechecking conditions.
Definition: Notifier.h:110
std::vector< CallbackType > callbacks_
The callbacks.
Definition: Notifier.h:55
An extension to Notifier - does not notify by itself. Instead it collects notifications and allows ex...
Definition: Notifier.h:69
bool HasCallback() const
Returns whether it has any callback registered.
Definition: Notifier.h:48
std::function< void(Args...)> CallbackType
The callback is a void function taking the variadic templated arguments of the Notifier class...
Definition: Notifier.h:25
virtual void Notify(Args... args)
Send all notifications at this moment.
Definition: Notifier.h:38
std::mutex notifier_access_
mutex to ensure thread safety.
Definition: Notifier.h:57
A notifier is a mechanism to allow inter-object notifications.
Definition: Notifier.h:21
virtual void SendAllNotifications()
Send all collected notifications at this moment.
Definition: Notifier.h:85
void AddCallback(CallbackType callback)
Add a single callback function.
Definition: Notifier.h:28
std::vector< std::tuple< Args... > > tuples_
The collected arguments to be executed on the next execution loop call.
Definition: Notifier.h:108
virtual void Notify(Args... args) override
Notify does not execute all notifications - it merely collects the arguments and allows the Owner to ...
Definition: Notifier.h:74
virtual void WaitUntilAllSent()
Wait until all notifications are sent.
Definition: Notifier.h:100
General namespace for utilities.
Definition: BulletConversions.h:9