Skip to content
Snippets Groups Projects
Commit bf0a2891 authored by fxk8y's avatar fxk8y :spider:
Browse files

Adding generic callback-thingy

parent 9ccade5f
No related branches found
No related tags found
No related merge requests found
#pragma once
// C++ system level
// #include <cstring> // memset, strncmp
// #include <cstdlib> // TODO: is this for memcpy?
// #include <cinttypes>
#include <functional>
// ESP32 specific
// #include "esp_log.h"
// project specific
// #include "CyanBusCRC.hpp"
// qthing stuff
// #include ""
namespace SpiderLib {
// Forward declaration
template <class>
class Callback;
template <typename... Args>
class Callback<void(Args...)> {
public:
typedef std::function<void(Args...)> CallbackF;
Callback() : callback(NULL) {};
void operator()(Args&&... args) {
if (callback != NULL) callback(std::forward<Args>(args)...);
}
auto& operator+=(const CallbackF& newCB) {
add(newCB);
return *this;
}
void add(CallbackF newCB) {
if (callback == NULL) {
callback = newCB;
} else {
auto oldCB = callback;
callback = [&](Args&&... args) {
oldCB(std::forward<Args>(args)...);
newCB(std::forward<Args>(args)...);
};
}
}
private:
CallbackF callback = NULL;
};
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment