From bf0a28918f8c4acea0b43947f3ccede0b32dba2d Mon Sep 17 00:00:00 2001
From: Jochen Vothknecht <jochen3120@gmail.com>
Date: Thu, 21 Apr 2022 11:21:31 +0200
Subject: [PATCH] Adding generic callback-thingy

---
 CLC-qthing/SpiderLib/Callback.hpp | 59 +++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100644 CLC-qthing/SpiderLib/Callback.hpp

diff --git a/CLC-qthing/SpiderLib/Callback.hpp b/CLC-qthing/SpiderLib/Callback.hpp
new file mode 100644
index 0000000..658b183
--- /dev/null
+++ b/CLC-qthing/SpiderLib/Callback.hpp
@@ -0,0 +1,59 @@
+#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;
+  };
+
+}
-- 
GitLab