Woman, Life, Freedom


Adaptive AUTOSAR
ARA public interface header documentation
notifier.h
1#ifndef NOTIFIER_H
2#define NOTIFIER_H
3
4#include <functional>
5#include <vector>
6
7namespace ara
8{
9 namespace sm
10 {
13 template <typename T>
14 using NotificationHandler = std::function<void(T)>;
15
18 template <typename T>
20 {
21 private:
22 T &mState;
23 std::vector<NotificationHandler<T>> mSubsribers;
24
25 public:
28 Notifier(T &state) : mState{state}
29 {
30 }
31
32 Notifier() = delete;
33 ~Notifier() noexcept = default;
34 Notifier(const Notifier &) = delete;
35 Notifier(Notifier &&) = delete;
36 Notifier &operator=(const Notifier &) = delete;
37 Notifier &operator=(Notifier &&) = delete;
38
41 T Read() const noexcept
42 {
43 return mState;
44 }
45
49 {
50 mSubsribers.push_back(handler);
51 }
52
55 void Notify()
56 {
57 for (auto subscriber : mSubsribers)
58 {
59 subscriber(mState);
60 }
61 }
62 };
63 }
64}
65#endif
State changing notifier wrapper.
Definition: notifier.h:20
void Subscribe(NotificationHandler< T > handler)
Subscribe to the state change.
Definition: notifier.h:48
Notifier(T &state)
Constructor.
Definition: notifier.h:28
T Read() const noexcept
Read the current state.
Definition: notifier.h:41
void Notify()
Notify state change.
Definition: notifier.h:55
std::function< void(T)> NotificationHandler
Callback on notification invocation.
Definition: notifier.h:14