Skip to content

Commit 2abcd16

Browse files
hreintkedevyte
authored andcommitted
Implementation of a generic CallBackList (#5710)
* Initial version Move to experimental namespace Change namespace
1 parent e2959ee commit 2abcd16

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

cores/esp8266/CallBackList.h

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#ifndef __CALLBACKLIST_H__
2+
#define __CALLBACKLIST_H__
3+
4+
5+
/*
6+
CallBackList, An implemention for handling callback execution
7+
8+
Copyright (c) 2019 Herman Reintke. All rights reserved.
9+
This file is part of the esp8266 core for Arduino environment.
10+
11+
This library is free software; you can redistribute it and/or
12+
modify it under the terms of the GNU Lesser General Public
13+
License as published by the Free Software Foundation; either
14+
version 2.1 of the License, or (at your option) any later version.
15+
16+
This library is distributed in the hope that it will be useful,
17+
but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19+
Lesser General Public License for more details.
20+
21+
You should have received a copy of the GNU Lesser General Public
22+
License along with this library; if not, write to the Free Software
23+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24+
*/
25+
26+
#include <Arduino.h>
27+
#include <memory>
28+
#include <list>
29+
#include <utility>
30+
31+
namespace experimental
32+
{
33+
namespace CBListImplentation
34+
{
35+
36+
template<class cbFunctionT>
37+
class CallBackList
38+
{
39+
public:
40+
CallBackList (){};
41+
42+
struct CallBackInfo
43+
{
44+
CallBackInfo(cbFunctionT f) : cbFunction(f, true){};
45+
CallBackInfo(cbFunctionT f, bool ar) : cbFunction(f), _allowRemove(ar) {};
46+
cbFunctionT cbFunction;
47+
bool _allowRemove = true;
48+
bool allowRemove()
49+
{
50+
return _allowRemove;
51+
}
52+
};
53+
using CallBackHandler = std::shared_ptr<CallBackInfo> ;
54+
std::list<CallBackHandler> callBackEventList;
55+
56+
CallBackHandler add(cbFunctionT af, bool ad = true) {
57+
CallBackHandler handler = std::make_shared<CallBackInfo>(CallBackInfo(af,ad));
58+
callBackEventList.emplace_back(handler);
59+
return handler;
60+
}
61+
62+
void remove(CallBackHandler& dh) {
63+
callBackEventList.remove(dh);
64+
}
65+
66+
template<typename... Args>
67+
void execute(Args... params) {
68+
for(auto it = std::begin(callBackEventList); it != std::end(callBackEventList); ) {
69+
CallBackHandler &handler = *it;
70+
if (handler->allowRemove() && handler.unique()) {
71+
it = callBackEventList.erase(it);
72+
}
73+
else {
74+
handler->cbFunction(params...);
75+
++it;
76+
}
77+
}
78+
}
79+
};
80+
81+
} //CBListImplementation
82+
}//experimental
83+
84+
#endif // __CALLBACKLIST_H__
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <Arduino.h>
2+
#include <Ticker.h>
3+
#include "CallBackList.h"
4+
5+
using namespace experimental::CBListImplentation;
6+
7+
class exampleClass {
8+
public:
9+
exampleClass() {};
10+
11+
using exCallBack = std::function<void(int)>;
12+
using exHandler = CallBackList<exCallBack>::CallBackHandler;
13+
14+
CallBackList<exCallBack> myHandlers;
15+
16+
exHandler setHandler(exCallBack cb) {
17+
return myHandlers.add(cb);
18+
}
19+
20+
void removeHandler(exHandler hnd) {
21+
myHandlers.remove(hnd);
22+
}
23+
24+
void trigger(int t) {
25+
myHandlers.execute(t);
26+
}
27+
};
28+
29+
exampleClass myExample;
30+
31+
void cb1(int in) {
32+
Serial.printf("Callback 1, in = %d\n", in);
33+
}
34+
35+
void cb2(int in) {
36+
Serial.printf("Callback 2, in = %d\n", in);
37+
}
38+
39+
void cb3(int in, int s) {
40+
Serial.printf("Callback 3, in = %d, s = %d\n", in, s);
41+
}
42+
43+
Ticker tk, tk2, tk3;
44+
exampleClass::exHandler e1 = myExample.setHandler(cb1);
45+
exampleClass::exHandler e2 = myExample.setHandler(cb2);
46+
exampleClass::exHandler e3 = myExample.setHandler(std::bind(cb3, std::placeholders::_1, 10));
47+
48+
void setup() {
49+
Serial.begin(115200);
50+
51+
tk.attach_ms(2000, []() {
52+
Serial.printf("trigger %d\n", (uint32_t)millis());
53+
myExample.trigger(millis());
54+
});
55+
tk2.once_ms(10000, []() {
56+
myExample.removeHandler(e2);
57+
});
58+
tk3.once_ms(20000, []() {
59+
e3.reset();
60+
});
61+
}
62+
63+
void loop() {
64+
}

0 commit comments

Comments
 (0)