瀏覽代碼

refactor & feat: 添加部分forEach函数

SongZihuan 3 年之前
父節點
當前提交
327dc92238

+ 2 - 2
include/core/activation.h

@@ -26,8 +26,8 @@ namespace aFuncore {
     public:
     public:
         Inter *const inter;
         Inter *const inter;
 
 
-        template <typename T>
-        static void forEach(Activation *activation, void (*func)(Activation *activation, Message *, T), T arg);
+        template <typename Callable,typename...T>
+        static void forEach(Activation *activation, Callable func, T...arg);
 
 
         explicit Activation(Inter *inter_);
         explicit Activation(Inter *inter_);
         virtual ~Activation();
         virtual ~Activation();

+ 3 - 3
include/core/activation.template.h

@@ -3,10 +3,10 @@
 #include "activation.h"
 #include "activation.h"
 
 
 namespace aFuncore {
 namespace aFuncore {
-    template<typename T>
-    void Activation::forEach(Activation *activation, void (*func)(Activation *activation, Message *, T), T arg){
+    template <typename Callable, typename...T>
+    inline void Activation::forEach(Activation *activation, Callable func, T...arg) {
         for (NULL; activation != nullptr; activation = activation->prev)
         for (NULL; activation != nullptr; activation = activation->prev)
-            func(activation, arg);
+            func(activation, arg...);
     }
     }
 }
 }
 
 

+ 2 - 2
include/core/msg.h

@@ -67,8 +67,8 @@ namespace aFuncore {
         virtual Message *popMessage(const std::string &type);
         virtual Message *popMessage(const std::string &type);
         void pushMessage(Message *msg);
         void pushMessage(Message *msg);
 
 
-        template <typename T>
-        void forEach(void (*func)(Message *, T), T arg);
+        template <typename Callable, typename...T>
+        void forEach(Callable func, T...arg);
     };
     };
 
 
     class AFUN_CORE_EXPORT UpMessage : public MessageStream {
     class AFUN_CORE_EXPORT UpMessage : public MessageStream {

+ 3 - 3
include/core/msg.template.h

@@ -11,10 +11,10 @@ namespace aFuncore {
     }
     }
 
 
 
 
-    template <typename T>
-    void MessageStream::forEach(void (*func)(Message *, T), T arg) {
+    template <typename Callable, typename...T>
+    inline void MessageStream::forEach(Callable func, T...arg) {
         for (Message *msg = stream; msg != nullptr; msg = msg->next) {
         for (Message *msg = stream; msg != nullptr; msg = msg->next) {
-            func(msg, arg);
+            func(msg, arg...);
         }
         }
     }
     }
 }
 }

+ 7 - 0
include/core/var.h

@@ -35,6 +35,9 @@ namespace aFuncore {
         explicit VarSpace(Inter *inter_);
         explicit VarSpace(Inter *inter_);
         ~VarSpace() override;
         ~VarSpace() override;
 
 
+        template <typename Callable,typename...T>
+        void forEach(Callable func, T...arg);
+
         [[nodiscard]] size_t getCount() const;
         [[nodiscard]] size_t getCount() const;
         [[nodiscard]] virtual Var *findVar(const std::string &name);
         [[nodiscard]] virtual Var *findVar(const std::string &name);
         virtual VarOperationFlat defineVar(const std::string &name, Object *data);
         virtual VarOperationFlat defineVar(const std::string &name, Object *data);
@@ -72,6 +75,9 @@ namespace aFuncore {
         void connect(VarList *varlist);
         void connect(VarList *varlist);
         void push(VarSpace *varspace_);
         void push(VarSpace *varspace_);
 
 
+        template <typename Callable,typename...T>
+        void forEach(Callable func, T...arg);
+
         [[nodiscard]] virtual Var *findVar(const std::string &name);
         [[nodiscard]] virtual Var *findVar(const std::string &name);
         virtual bool defineVar(const std::string &name, Object *data);
         virtual bool defineVar(const std::string &name, Object *data);
         virtual bool defineVar(const std::string &name, Var *data);
         virtual bool defineVar(const std::string &name, Var *data);
@@ -82,5 +88,6 @@ namespace aFuncore {
 }
 }
 
 
 #include "var.inline.h"
 #include "var.inline.h"
+#include "var.template.h"
 
 
 #endif //AFUN_VAR_H
 #endif //AFUN_VAR_H

+ 20 - 0
include/core/var.template.h

@@ -0,0 +1,20 @@
+#ifndef AFUN_VAR_TEMPLATE_H
+#define AFUN_VAR_TEMPLATE_H
+
+namespace aFuncore {
+    template <typename Callable,typename...T>
+    void VarSpace::forEach(Callable func, T...arg) {
+        for (int i = 0; i < VAR_HASH_SIZE; i++) {
+            for (auto tmp = var[i]; tmp != nullptr; tmp = tmp->next)
+                func(tmp, arg...);
+        }
+    }
+
+    template <typename Callable,typename...T>
+    void VarList::forEach(Callable func, T...arg) {
+        for (auto vs : varspace)
+            func(vs, arg...);
+    }
+}
+
+#endif //AFUN_VAR_TEMPLATE_H

+ 1 - 1
src/core/activation.cpp

@@ -149,7 +149,7 @@ static void ActivationTopProgress(Message *msg, void *) {
 };
 };
 
 
 TopActivation::~TopActivation() {
 TopActivation::~TopActivation() {
-    down->forEach<void *>(ActivationTopProgress, nullptr);
+    down->forEach(ActivationTopProgress, nullptr);
 }
 }
 
 
 FuncActivation::~FuncActivation(){
 FuncActivation::~FuncActivation(){