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
8
namespace
sensesp
{
9
16
class
Observable
{
17
public
:
18
Observable
() {}
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
sensesp::Observable
A base class which allow observers to attach callbacks to themselves. The callbacks will be called wh...
Definition
observable.h:16
sensesp::Observable::Observable
Observable(Observable &&other)
Move constructor.
Definition
observable.h:21
sensesp::Observable::attach
void attach(std::function< void()> observer)
Definition
observable.h:29
sensesp::Observable::Observable
Observable()
Definition
observable.h:18
sensesp::Observable::notify
void notify()
Definition
observable.h:23
sensesp
Definition
sensesp.cpp:7
src
sensesp
system
observable.h
Generated by
1.12.0