2
0
Эх сурвалжийг харах

refactor & feat: 添加部分辅助函数

SongZihuan 3 жил өмнө
parent
commit
d5e966ef8d

+ 4 - 0
include/core/activation.h

@@ -26,6 +26,9 @@ namespace aFuncore {
     public:
         Inter *const inter;
 
+        template <typename T>
+        static void forEach(Activation *activation, void (*func)(Activation *activation, Message *, T), T arg);
+
         explicit Activation(Inter *inter_);
         virtual ~Activation();
         Activation &operator=(const Activation &)=delete;
@@ -84,5 +87,6 @@ namespace aFuncore {
 }
 
 #include "activation.inline.h"
+#include "activation.template.h"
 
 #endif //AFUN_ACTIVATION_H

+ 13 - 0
include/core/activation.template.h

@@ -0,0 +1,13 @@
+#ifndef AFUN_ACTIVATION_TEMPLATE_H
+#define AFUN_ACTIVATION_TEMPLATE_H
+#include "activation.h"
+
+namespace aFuncore {
+    template<typename T>
+    void Activation::forEach(Activation *activation, void (*func)(Activation *activation, Message *, T), T arg){
+        for (NULL; activation != nullptr; activation = activation->prev)
+            func(activation, arg);
+    }
+}
+
+#endif //AFUN_ACTIVATION_TEMPLATE_H

+ 3 - 0
include/core/env-var.h

@@ -28,6 +28,9 @@ namespace aFuncore {
 
         void setString(const std::string &name, const std::string &str);
         void setNumber(const std::string &name, int32_t num);
+
+        void addString(const std::string &name, const std::string &str);
+        void addNumber(const std::string &name, int32_t num);
     };
 }
 

+ 2 - 1
include/core/gc.h

@@ -14,9 +14,10 @@ namespace aFuncore {
     protected:
         GcObjectBase();
         virtual ~GcObjectBase() = default;
+    public:
         GcObjectBase(const GcObjectBase &) = delete;
         GcObjectBase &operator=(const GcObjectBase &) = delete;
-    public:
+
         void addReference();
         void delReference();
         void setClear(bool clear=false);

+ 39 - 0
src/core/env-var.cpp

@@ -96,3 +96,42 @@ void EnvVarSpace::setNumber(const std::string &name, int32_t num){
     (*tmp)->num = num;
 }
 
+/**
+ * 设置环境变量文本
+ * @param name 变量名
+ * @param str 文本
+ */
+void EnvVarSpace::addString(const std::string &name, const std::string &str){
+    size_t index = time33(name) % ENV_VAR_HASH_SIZE;
+    auto tmp = &var[index];
+    for (NULL; *tmp != nullptr; tmp = &((*tmp)->next)) {
+        if (name == (*tmp)->name) {
+            (*tmp)->str += str;
+            return;
+        }
+    }
+
+    (*tmp) = new EnvVar;
+    (*tmp)->name = name;
+    (*tmp)->str = str;
+}
+
+/**
+ * 设置环境变量数值
+ * @param name 变量名
+ * @param num 数值
+ */
+void EnvVarSpace::addNumber(const std::string &name, int32_t num){
+    size_t index = time33(name) % ENV_VAR_HASH_SIZE;
+    auto tmp = &var[index];
+    for (NULL; *tmp != nullptr; tmp = &((*tmp)->next)) {
+        if (name == (*tmp)->name) {
+            (*tmp)->num += num;
+            return;
+        }
+    }
+
+    (*tmp) = new EnvVar;
+    (*tmp)->name = name;
+    (*tmp)->num = num;
+}