mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
event.h
Go to the documentation of this file.
1 #ifndef MF_UTILITY_EVENT_H_
2 #define MF_UTILITY_EVENT_H_
3 
4 #include "../common.h"
5 #include <cstdint>
6 #include <utility>
7 #include <functional>
8 #include <iterator>
9 #include <memory>
10 #include <type_traits>
11 #include <array>
12 
13 namespace mf {
14 
16 
21 class event {
22 public:
23  std::uintptr_t handle_;
24 
25  static event* wait_any_(event** begin, event** end);
26 
27 public:
28  event();
29  event(const event&) = delete;
30  event(event&&);
31  virtual ~event();
32 
33  event& operator=(const event&) = delete;
34  event& operator=(event&&);
35 
36  friend bool operator==(const event& a, const event& b);
37  friend bool operator!=(const event& a, const event& b) { return !(a == b); }
38 
39  virtual void notify();
40  virtual void wait();
41 
42  template<typename It>
43  static event& wait_any_list(It begin_it, It end_it) {
44  static_assert(
45  std::is_same<typename std::iterator_traits<It>::value_type, event*>::value,
46  "arguments must be contiguous iterators with value_type event*"
47  );
48  event** begin = std::addressof(*begin_it);
49  event** end = std::addressof(*end_it);
50  MF_ASSERT_MSG( (end - begin) == std::distance(begin_it, end_it), "iterators must be contiguous");
51  return *wait_any_(begin, end);
52  }
53 
54  template<typename... Events>
55  static event& wait_any(Events&&... events) {
56  std::array<event*, sizeof...(Events)> evs { &events... };
57  return wait_any_list(evs.begin(), evs.end());
58  }
59 };
60 
61 
63 
65 class sticky_event : public event {
66 private:
67  bool notified_ = false;
68 
69 public:
70  sticky_event() = default;
71 
72  void notify() override {
73  notified_ = true;
74  event::notify();
75  }
76 
77  void wait() override {
78  event::wait();
79  event::notify();
80  }
81 
82  void reset() {
83  if(notified_) event::wait();
84  notified_ = false;
85  }
86 };
87 
88 }
89 
90 #endif
void notify() override
Definition: event.h:72
static event & wait_any(Events &&...events)
Definition: event.h:55
Synchronization primitive representing event that a thread can wait for.
Definition: event.h:21
#define MF_ASSERT_MSG(condition, msg)
Definition: common.h:18
virtual void wait()
Event which is repeatedly received after having been notified once.
Definition: event.h:65
void reset()
Definition: event.h:82
sticky_event()=default
event & operator=(const event &)=delete
virtual void notify()
virtual ~event()
static event & wait_any_list(It begin_it, It end_it)
Definition: event.h:43
friend bool operator!=(const event &a, const event &b)
Definition: event.h:37
float distance(const Eigen::Vector3f &pt, const plane &pl)
Definition: plane.cc:50
void wait() override
Definition: event.h:77
std::uintptr_t handle_
OS-specific handle.
Definition: event.h:23
static event * wait_any_(event **begin, event **end)
friend bool operator==(const event &a, const event &b)