|
| 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__ |
0 commit comments