SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
observable.h
Go to the documentation of this file.
1#ifndef SENSESP_SYSTEM_OBSERVABLE_H
2#define SENSESP_SYSTEM_OBSERVABLE_H
3
4#include <forward_list>
5#include <functional>
6#include <vector>
7
8namespace sensesp {
9
17 public:
19
21 Observable(Observable&& other) : observers_{other.observers_} {}
22
23 void notify() {
24 for (auto o : observers_) {
25 o();
26 }
27 }
28
29 void attach(std::function<void()> observer) {
30 // First iterate to the last element
31 auto before_end = observers_.before_begin();
32 for (auto& _ : observers_) {
33 ++before_end;
34 }
35 // Then insert the new observer
36 observers_.insert_after(before_end, observer);
37 }
38
39 private:
40 std::forward_list<std::function<void()> > observers_;
41};
42
43} // namespace sensesp
44
45#endif
A base class which allow observers to attach callbacks to themselves. The callbacks will be called wh...
Definition observable.h:16
Observable(Observable &&other)
Move constructor.
Definition observable.h:21
void attach(std::function< void()> observer)
Definition observable.h:29