-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserver_base.h
More file actions
52 lines (48 loc) · 1.23 KB
/
Copy pathobserver_base.h
File metadata and controls
52 lines (48 loc) · 1.23 KB
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
46
47
48
49
50
51
52
#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:
//ownership: the receiving queue's consumer deletes m. If no queue accepted
//it (no observers registered, or every queue full) it is deleted here.
template<typename MType>
bool broadcast( MType * m) {
uint32_t delivered = 0;
typename etl::set<QueueHandle_t,QDepth>::iterator it = Notifications.begin();
for(;it!=Notifications.end();++it) {
QueueHandle_t handle = (*it);
if(pdTRUE==xQueueSend(handle, &m, 0)) {
++delivered;
}
}
if(0==delivered) {
delete m;
return false;
}
return true;
}
protected:
etl::set<QueueHandle_t,QDepth> Notifications;
};
}