-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserver_base.h
45 lines (41 loc) · 1010 Bytes
/
observer_base.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#pragma once
#include <etl/set.h>
#include "freertos.h"
namespace libesp {
template<size_t QDepth = 3>
class ObserverBase {
public:
ObserverBase() : Notifications() {}
public:
/*
* add / remove observers
*/
bool addObserver(const QueueHandle_t &o) {
return Notifications.insert(o).second;
}
bool removeObserver(const QueueHandle_t &o) {
return Notifications.erase(o)==1;
}
//returns number of itesm broadcasted or -1 if error
int32_t broadcast() {
return onBroadcast();
}
virtual ~ObserverBase() {}
protected:
virtual int32_t onBroadcast()=0;
protected:
template<typename MType>
bool broadcast( MType * m) {
typename etl::set<QueueHandle_t,QDepth>::iterator it = Notifications.begin();
for(;it!=Notifications.end();++it) {
xQueueHandle handle = (*it);
if(errQUEUE_FULL==xQueueSend(handle, &m, 0)) {
return false;
}
}
return true;
}
protected:
etl::set<QueueHandle_t,QDepth> Notifications;
};
}