Procházet zdrojové kódy

refactor: 调整代码结构

SongZihuan před 3 roky
rodič
revize
a8cc9742f3

+ 7 - 6
include/core/activation.hpp

@@ -6,7 +6,7 @@
 #include "value.hpp"
 
 namespace aFuncore {
-    class Activation {
+    AFUN_CORE_EXPORT class Activation {
     protected:
         Activation *prev;
 
@@ -23,6 +23,7 @@ namespace aFuncore {
 
         explicit Activation(Inter *inter_);
         virtual ~Activation();
+        Activation &operator=(const Activation &)=delete;
 
         virtual ActivationStatus getCode(Code *&code)=0;
         virtual void runCode(Code *code);
@@ -33,11 +34,11 @@ namespace aFuncore {
         [[nodiscard]] UpMessage *getUpStream() const {return up;}
         [[nodiscard]] DownMessage *getDownStream() const {return down;}
 
-        [[nodiscard]] FileLine getFileLine() {return line;}
-        [[nodiscard]] StringFilePath &getFilePath() {return path;}
+        [[nodiscard]] FileLine getFileLine() const {return line;}
+        [[nodiscard]] const StringFilePath &getFilePath() const {return path;}
     };
 
-    class ExeActivation : public Activation {
+    AFUN_CORE_EXPORT class ExeActivation : public Activation {
         Code *start;
         Code *next;
         bool first=true;
@@ -47,13 +48,13 @@ namespace aFuncore {
         [[nodiscard]] Code *getStart() const {return start;}
     };
 
-    class TopActivation : public ExeActivation {
+    AFUN_CORE_EXPORT class TopActivation : public ExeActivation {
     public:
         explicit TopActivation(Code *code, Inter *inter_);
         ~TopActivation() override;
     };
 
-    class FuncActivation : public Activation {
+    AFUN_CORE_EXPORT class FuncActivation : public Activation {
         enum {
             func_first = 0,
             func_get_func = 1,

+ 37 - 18
include/core/code.hpp

@@ -5,7 +5,7 @@
 #include "core.hpp"
 
 namespace aFuncore {
-    class Code {
+    AFUN_CORE_EXPORT class Code {
         CodeType type;
         char prefix=NUL;
 
@@ -23,24 +23,43 @@ namespace aFuncore {
 
         aFuntool::FileLine line;
         aFuntool::FilePath file;
+
+    protected:
+        explicit Code(FileLine line, ConstFilePath file="");
+        Code (const std::string &element, aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL);
+        Code (BlockType block_type, Code *son, aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL);
+        ~Code();
     public:
-        AFUN_CORE_EXPORT explicit Code(FileLine line, ConstFilePath file="");
-        AFUN_CORE_EXPORT Code (const std::string &element, aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL);
-        AFUN_CORE_EXPORT Code (BlockType block_type, Code *son, aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL);
-        AFUN_CORE_EXPORT ~Code();
-
-        AFUN_CORE_EXPORT Code *connect(Code *code);
-        AFUN_CORE_EXPORT void destructAll();
-        AFUN_CORE_EXPORT void display() const;
-        AFUN_CORE_EXPORT void displayAll() const;
-        AFUN_CORE_EXPORT bool write_v1(FILE *f, bool debug=false) const;
-        AFUN_CORE_EXPORT bool writeAll_v1(FILE *f, bool debug=false) const;
-        AFUN_CORE_EXPORT Code *read_v1(FILE *f, bool debug=false, int8_t read_type=code_element, bool to_son=false);
-        AFUN_CORE_EXPORT bool readAll_v1(FILE *f, bool debug=false);
-        [[nodiscard]] AFUN_CORE_EXPORT std::string getMD5_v1() const;
-        [[nodiscard]] AFUN_CORE_EXPORT std::string getMD5All_v1() const;
-        AFUN_CORE_EXPORT bool writeByteCode(ConstFilePath file_path, bool debug=false) const;  // NOLINT 允许忽略返回值
-        AFUN_CORE_EXPORT bool readByteCode(ConstFilePath file_path);
+        Code(const Code &)=delete;
+        Code &operator=(const Code &)=delete;
+
+        static Code *create(FileLine line, ConstFilePath file="") {
+            return new Code(line, file);
+        }
+
+        static Code *create(const std::string &element,
+                            aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL) {
+            return new Code(element, line, file, prefix);
+        }
+
+        static Code *create(BlockType block_type, Code *son,
+                            aFuntool::FileLine line, aFuntool::ConstFilePath file="", char prefix=NUL) {
+            return new Code(block_type, son, line, file);
+        }
+
+        static void destruct(Code *code);
+
+        Code *connect(Code *code);
+        void display() const;
+        void displayAll() const;
+        bool write_v1(FILE *f, bool debug=false) const;
+        bool writeAll_v1(FILE *f, bool debug=false) const;
+        Code *read_v1(FILE *f, bool debug=false, int8_t read_type=code_element, bool to_son=false);
+        bool readAll_v1(FILE *f, bool debug=false);
+        [[nodiscard]] std::string getMD5_v1() const;
+        [[nodiscard]] std::string getMD5All_v1() const;
+        bool writeByteCode(ConstFilePath file_path, bool debug=false) const;  // NOLINT 允许忽略返回值
+        bool readByteCode(ConstFilePath file_path);
 
         [[nodiscard]] CodeType getType() const {return type;}
         [[nodiscard]] char getPrefix() const {return prefix;}

+ 22 - 20
include/core/core.hpp

@@ -13,11 +13,11 @@ namespace aFuncore {
         block_b = '[',
         block_c = '{',
     } BlockType;
-    typedef class Code Code;
+    AFUN_CORE_EXPORT class Code;
 
-    class EnvVarSpace;
+    AFUN_CORE_EXPORT class EnvVarSpace;
 
-    class Inter;
+    AFUN_CORE_EXPORT class Inter;
     enum InterStatus {
         inter_creat = 0,
         inter_init = 1,  // 执行初始化程序
@@ -44,33 +44,35 @@ namespace aFuncore {
     static const std::string E_PREFIX = "$`'";  /* NOLINT element前缀 */
     static const std::string B_PREFIX = "$`'%^&<?>";  /* NOLINT block前缀 */
 
-    class Message;
-    class NormalMessage;
-    class ErrorMessage;
+    AFUN_CORE_EXPORT class Message;
+    AFUN_CORE_EXPORT class NormalMessage;
+    AFUN_CORE_EXPORT class ErrorMessage;
 
-    class MessageStream;
-    class UpMessage;
-    class DownMessage;
+    AFUN_CORE_EXPORT class MessageStream;
+    AFUN_CORE_EXPORT class UpMessage;
+    AFUN_CORE_EXPORT class DownMessage;
 
-    class Activation;
-    class ExeActivation;
-    class TopActivation;
-    class FuncActivation;
+    AFUN_CORE_EXPORT class Activation;
+    AFUN_CORE_EXPORT class ExeActivation;
+    AFUN_CORE_EXPORT class TopActivation;
+    AFUN_CORE_EXPORT class FuncActivation;
     typedef enum ActivationStatus {
         as_run = 0,
         as_end = 1,
         as_end_run = 2,
     } ActivationStatus;
 
-    class GcList;
+    AFUN_CORE_EXPORT class GcList;
 
-    class Object;
-    class Function;
+    AFUN_CORE_EXPORT class Object;
+    AFUN_CORE_EXPORT class Function;
+    AFUN_CORE_EXPORT class Literaler;
+    AFUN_CORE_EXPORT class CallBackVar;
 
-    class Var;
-    class VarSpace;
-    class VarList;
-    class ProtectVarSpace;
+    AFUN_CORE_EXPORT class Var;
+    AFUN_CORE_EXPORT class VarSpace;
+    AFUN_CORE_EXPORT class VarList;
+    AFUN_CORE_EXPORT class ProtectVarSpace;
     typedef enum VarOperationFlat {
         vof_success = 0,  // 成功
         vof_not_var = 1,  // 变量不存在

+ 9 - 7
include/core/env-var.hpp

@@ -4,7 +4,7 @@
 #include "aFunCoreExport.h"
 
 namespace aFuncore {
-    class EnvVarSpace {  // 环境变量
+    AFUN_CORE_EXPORT class EnvVarSpace {  // 环境变量
         static const size_t ENV_VAR_HASH_SIZE = 100;  // 环境变量哈希表大小
         struct EnvVar {  // 环境变量
             std::string name;
@@ -17,15 +17,17 @@ namespace aFuncore {
         EnvVar *var[ENV_VAR_HASH_SIZE] {};
         pthread_rwlock_t lock;
     public:
-        AFUN_CORE_EXPORT EnvVarSpace();
-        AFUN_CORE_EXPORT ~EnvVarSpace();
+        EnvVarSpace();
+        ~EnvVarSpace();
+        EnvVarSpace(const EnvVarSpace &)=delete;
+        EnvVarSpace &operator=(const EnvVarSpace &)=delete;
 
         [[nodiscard]] size_t getCount() const {return count;}
-        AFUN_CORE_EXPORT bool findString(const std::string &name, std::string &str) const;
-        AFUN_CORE_EXPORT bool findNumber(const std::string &name, int32_t &num) const;
+        bool findString(const std::string &name, std::string &str) const;
+        bool findNumber(const std::string &name, int32_t &num) const;
 
-        AFUN_CORE_EXPORT void setString(const std::string &name, const std::string &str);
-        AFUN_CORE_EXPORT void setNumber(const std::string &name, int32_t num);
+        void setString(const std::string &name, const std::string &str);
+        void setNumber(const std::string &name, int32_t num);
     };
 }
 

+ 33 - 10
include/core/gc.hpp

@@ -7,12 +7,16 @@
 namespace aFuncore {
     typedef unsigned GcCount;
 
-    class GcObjectBase {
+    AFUN_CORE_EXPORT class GcObjectBase {
         bool not_clear;  // 不清除
         bool reachable;  // 可达标记 [同时标识已迭代]
         GcCount reference;  // 引用计数
-    public:
+    protected:
         GcObjectBase() : not_clear{false}, reference{0}, reachable{false} {}
+        virtual ~GcObjectBase()=default;
+        GcObjectBase(const GcObjectBase &)=delete;
+        GcObjectBase &operator=(const GcObjectBase &)=delete;
+    public:
         void addReference() {reference++;}
         void delReference() {reference--;}
         void setClear(bool clear=false) {not_clear=!clear;}
@@ -23,19 +27,38 @@ namespace aFuncore {
     class GcObject : public GcObjectBase {
         T *prev;
         T *next;
+    protected:
+        GcObject() : GcObjectBase(), prev {nullptr}, next {nullptr} {}
     public:
-        GcObject();
-        virtual ~GcObject()=default;
-        void addObject(T *&chain);
-        void delObject(T *&chain);
-        static void destruct(T *&chain);
+        void addObject(T *&chain) {
+            if (chain != nullptr) {
+                next = chain;
+                chain->prev = dynamic_cast<T *>(this);
+            }
+            chain = dynamic_cast<T *>(this);
+        }
+        void delObject(T *&chain) {
+            if (next != nullptr)
+                next->prev = prev;
+
+            if (prev == nullptr)
+                chain = next;
+            else
+                prev->next = next;
+        }
+        static void destruct(T *&chain) {
+            for (T *tmp = chain, *n; tmp != nullptr; tmp = n) {
+                n = tmp->next;
+                delete tmp;
+            }
+        }
     };
 
-    class GcList {
+    AFUN_CORE_EXPORT class GcList {
         std::queue<GcObjectBase *> queue;
     public :
-        AFUN_CORE_EXPORT size_t add(GcObjectBase *obj);
-        AFUN_CORE_EXPORT GcObjectBase *pop();
+        size_t add(GcObjectBase *obj);
+        GcObjectBase *pop();
 
         [[nodiscard]] size_t getSize() const {return queue.size();}
         [[nodiscard]] size_t isEmpty() const {return queue.empty();}

+ 4 - 1
include/core/inter.hpp

@@ -6,7 +6,7 @@
 #include "core.hpp"
 
 namespace aFuncore {
-    class Inter {
+    AFUN_CORE_EXPORT class Inter {
         friend class Object;
         friend class Var;
         friend class VarSpace;
@@ -58,6 +58,9 @@ namespace aFuncore {
     public:
         explicit Inter(int argc=0, char **argv=nullptr, ExitMode em=em_activity);
         ~Inter();
+        Inter(const Inter &)=delete;
+        Inter &operator=(const Inter &)=delete;
+
         void enable();
 
         [[nodiscard]] InterStatus getStatus() const {return status;}

+ 25 - 23
include/core/msg.hpp

@@ -6,7 +6,7 @@
 #include "list"
 
 namespace aFuncore {
-    class Message {
+    AFUN_CORE_EXPORT class Message {
         friend class MessageStream;
         friend class UpMessage;
         friend class DownMessage;
@@ -14,8 +14,9 @@ namespace aFuncore {
         Message *next;  // 下一条消息
     public:
         const std::string type;  // 消息类型标注
-        AFUN_CORE_EXPORT explicit Message(const std::string &type_) : type {type_}, next {nullptr} {}
-        AFUN_CORE_EXPORT virtual ~Message() = default;
+        explicit Message(const std::string &type_) : type {type_}, next {nullptr} {}
+        virtual ~Message() = default;
+        Message &operator=(const Message &)=delete;
     };
 
     class TopMessage : public Message {
@@ -24,40 +25,40 @@ namespace aFuncore {
         virtual void topProgress()=0;
     };
 
-    class NormalMessage : public TopMessage {
+    AFUN_CORE_EXPORT class NormalMessage : public TopMessage {
         Object *obj;
     public:
-        AFUN_CORE_EXPORT explicit NormalMessage(Object *obj_) : TopMessage("NORMAL"), obj {obj_} {}
-        AFUN_CORE_EXPORT ~NormalMessage() override;
+        explicit NormalMessage(Object *obj_) : TopMessage("NORMAL"), obj {obj_} {}
+        ~NormalMessage() override;
         void topProgress() override;
         Object *getObject() {return obj;}
     };
 
-    class ErrorMessage : public TopMessage {
+    AFUN_CORE_EXPORT class ErrorMessage : public TopMessage {
         Inter *inter;
 
         std::string error_type;
         std::string error_info;
         struct TrackBack{
-            StringFilePath path;
+            const StringFilePath path;
             FileLine line;
         };
         std::list<TrackBack> trackback;
     public:
-        AFUN_CORE_EXPORT explicit ErrorMessage(const std::string &error_type_, const std::string &error_info_, Activation *activation);
+        explicit ErrorMessage(const std::string &error_type_, const std::string &error_info_, Activation *activation);
         void topProgress() override;
         std::string getErrorType() {return error_type;}
         std::string getErrorInfo() {return error_info;}
     };
 
-    class MessageStream {
+    AFUN_CORE_EXPORT class MessageStream {
     protected:
         Message *stream;
-        [[nodiscard]] AFUN_CORE_EXPORT virtual Message *_getMessage(const std::string &type) const;
-
+        [[nodiscard]] virtual Message *_getMessage(const std::string &type) const;
     public:
-        AFUN_CORE_EXPORT MessageStream();
-        AFUN_CORE_EXPORT virtual ~MessageStream();
+        MessageStream();
+        virtual ~MessageStream();
+        MessageStream &operator=(const MessageStream &)=delete;
 
         template<class T>
         [[nodiscard]] T *getMessage(const std::string &type) const {
@@ -66,29 +67,30 @@ namespace aFuncore {
             return ret;
         }
 
-        virtual AFUN_CORE_EXPORT Message *popMessage(const std::string &type);
-        AFUN_CORE_EXPORT void pushMessage(Message *msg);
+        virtual Message *popMessage(const std::string &type);
+        void pushMessage(Message *msg);
 
         template <typename T>
-        AFUN_CORE_EXPORT void forEach(void (*func)(Message *, T), T arg) {
+        void forEach(void (*func)(Message *, T), T arg) {
             for (Message *msg = stream; msg != nullptr; msg = msg->next) {
                 func(msg, arg);
             }
         }
     };
 
-    class UpMessage : public MessageStream {
+    AFUN_CORE_EXPORT class UpMessage : public MessageStream {
     protected:
         Message *old;
     public:
-        AFUN_CORE_EXPORT explicit UpMessage(const UpMessage *old=nullptr);
-        AFUN_CORE_EXPORT ~UpMessage() override;
-        AFUN_CORE_EXPORT Message *popMessage(const std::string &type) override;
+        explicit UpMessage(const UpMessage *old=nullptr);
+        ~UpMessage() override;
+
+        Message *popMessage(const std::string &type) override;
     };
 
-    class DownMessage : public MessageStream {
+    AFUN_CORE_EXPORT class DownMessage : public MessageStream {
     public:
-        AFUN_CORE_EXPORT void joinMsg(DownMessage *msg);
+        void joinMsg(DownMessage *msg);
     };
 }
 

+ 14 - 7
include/core/value.hpp

@@ -7,25 +7,30 @@
 #include "gc.hpp"
 
 namespace aFuncore {
-    class Object : public GcObject<class Object> {
+    AFUN_CORE_EXPORT class Object : public GcObject<class Object> {
     public:
         Inter *const inter;
         const std::string type;  // 标识 Object 的字符串
 
-        AFUN_CORE_EXPORT explicit Object(const std::string &type_, Inter *inter_);
-        AFUN_CORE_EXPORT ~Object() override =default;
+        explicit Object(const std::string &type_, Inter *inter_);
+        ~Object() override =default;
     };
 
-    class Function : public Object {
+    AFUN_CORE_EXPORT class Function : public Object {
     public:
         Function(const std::string &type_, Inter *inter_) : Object(type_ + ":Function", inter_) {}
-        class CallFunction {
+
+        AFUN_CORE_EXPORT class CallFunction {
         public:
             struct ArgCodeList {
                 Code *code = nullptr;
                 Object *ret = nullptr;
             };
+            CallFunction()=default;
             virtual ~CallFunction()=default;
+            CallFunction(const CallFunction &)=delete;
+            CallFunction &operator=(const CallFunction &)=delete;
+
             virtual std::list<ArgCodeList> *getArgCodeList()=0;
             virtual void runFunction()=0;
         };
@@ -33,15 +38,17 @@ namespace aFuncore {
         virtual bool isInfix() {return false;}
     };
 
-    class Literaler : public Object {
+    AFUN_CORE_EXPORT class Literaler : public Object {
     public:
         Literaler(const std::string &type_, Inter *inter_) : Object(type_ + ":Literaler", inter_) {}
+
         virtual void getObject(const std::string &literal, char prefix)=0;
     };
 
-    class CallBackVar : public Object {
+    AFUN_CORE_EXPORT class CallBackVar : public Object {
     public:
         CallBackVar(const std::string &type_, Inter *inter_) : Object(type_ + ":CallBackVar", inter_) {}
+
         virtual bool isCallBack() {return true;}
         virtual void callBack()=0;
     };

+ 14 - 6
include/core/var.hpp

@@ -6,7 +6,7 @@
 #include "gc.hpp"
 
 namespace aFuncore {
-    class Var : public GcObject<class Var> {
+    AFUN_CORE_EXPORT class Var : public GcObject<class Var> {
         Object *data;
     public:
         Inter *const inter;
@@ -18,7 +18,7 @@ namespace aFuncore {
         virtual void setData(Object *data_) {data = data_;}
     };
 
-    class VarSpace : public GcObject<class VarSpace> {
+    AFUN_CORE_EXPORT class VarSpace : public GcObject<class VarSpace> {
     public:
         static const size_t VAR_HASH_SIZE = 100;  // 环境变量哈希表大小
     private:
@@ -47,7 +47,7 @@ namespace aFuncore {
         }
     };
 
-    class ProtectVarSpace : public VarSpace {
+    AFUN_CORE_EXPORT class ProtectVarSpace : public VarSpace {
         bool is_protect;
     public:
         explicit ProtectVarSpace(Inter *inter_) : VarSpace(inter_), is_protect{false} {}
@@ -61,13 +61,21 @@ namespace aFuncore {
         VarOperationFlat delVar(const std::string &name) override;
     };
 
-    class VarList {
+    AFUN_CORE_EXPORT class VarList {
         VarList *next;
+
+        explicit VarList(VarSpace *vs) : varspace {vs}, next {nullptr} {};
+        ~VarList()=default;
+        VarList(const VarList &)=delete;
+        VarList &operator=(const VarList &)=delete;
     public:
         VarSpace *const varspace;
 
-        explicit VarList(VarSpace *vs) : varspace {vs}, next {nullptr} {};
-        void destructAll();
+        static VarList *create(VarSpace *vs) {
+            return new VarList(vs);
+        }
+
+        static void destruct(VarList *varlist);
 
         [[nodiscard]] virtual Var *findVar(const std::string &name);
         virtual bool defineVar(const std::string &name, Object *data);

+ 24 - 5
include/tool/dlc.hpp

@@ -35,7 +35,7 @@ namespace aFuntool {
      * 注意: 仅能通过 openLibrary生成
      * 不需要 delete 释放 (自动管理释放)
      */
-    class DlcHandle {
+    AFUN_TOOL_EXPORT class DlcHandle {
         friend AFUN_TOOL_EXPORT void dlcExit();
         friend AFUN_TOOL_EXPORT DlcHandle *openLibrary(const char *file, int mode);
 
@@ -45,7 +45,9 @@ namespace aFuntool {
         struct DlcHandle *next;
         struct DlcHandle *prev;
     public:
-        AFUN_TOOL_EXPORT ~DlcHandle();
+        DlcHandle(const DlcHandle &dlc)=delete;
+        DlcHandle &operator=(const DlcHandle *dlc)=delete;
+        ~DlcHandle();
 
         /**
          * 获得动态库中指定名字的符号
@@ -64,9 +66,9 @@ namespace aFuntool {
         /**
          * 关闭动态库句柄
          */
-        AFUN_TOOL_EXPORT void close();
-        AFUN_TOOL_EXPORT int operator++(int);
-        AFUN_TOOL_EXPORT int operator--(int);
+        void close();
+        int operator++(int);
+        int operator--(int);
     };
 
     /**
@@ -90,6 +92,23 @@ namespace aFuntool {
                 this->dlc++;
         }
 
+        DlcSymbol(const DlcSymbol &dlc_symbol) : symbol{dlc_symbol.symbol}, dlc {dlc_symbol.dlc} {
+            if (this->dlc != nullptr)
+                this->dlc++;
+        }
+
+        DlcSymbol &operator=(const DlcSymbol &dlc_symbol) {
+            if (this == &dlc_symbol)
+                return *this;
+            if (this->dlc != nullptr)
+                this->dlc--;
+            symbol = dlc_symbol.symbol;
+            dlc = dlc_symbol.dlc;
+            if (this->dlc != nullptr)
+                this->dlc++;
+            return *this;
+        }
+
         /**
          * 复制符号
          * @param symbol

+ 38 - 44
include/tool/log.hpp

@@ -20,35 +20,26 @@ namespace aFuntool {
     };
     typedef enum LogLevel LogLevel;
 
-    class LogFactory;
+    AFUN_TOOL_EXPORT class LogFactory;
 
-    class Logger {
+    AFUN_TOOL_EXPORT class Logger {
         const std::string id;
         LogLevel level = log_debug;
         bool exit = true;
-
         friend class LogFactory;
-
     public:
-        AFUN_TOOL_EXPORT explicit Logger(const std::string &id_, LogLevel level_ = log_warning, bool exit_ = true);
-        AFUN_TOOL_EXPORT int writeTrackLog(const char *file, int line, const char *func,
-                                           const char *format, ...);
-        AFUN_TOOL_EXPORT int writeDebugLog(const char *file, int line, const char *func,
-                                           const char *format, ...);
-        AFUN_TOOL_EXPORT int writeInfoLog(const char *file, int line, const char *func,
-                                          const char *format, ...);
-        AFUN_TOOL_EXPORT int writeWarningLog(const char *file, int line, const char *func,
-                                             const char *format, ...);
-        AFUN_TOOL_EXPORT int writeErrorLog(const char *file, int line, const char *func,
-                                           const char *format, ...);
-        AFUN_TOOL_EXPORT int writeSendErrorLog(const char *file, int line, const char *func,
-                                               const char *format, ...);
-        AFUN_TOOL_EXPORT int writeFatalErrorLog(const char *file, int line, const char *func,
-                                               int exit_code, const char *format, ...);
+        explicit Logger(const std::string &id_, LogLevel level_ = log_warning, bool exit_ = true);
+        int writeTrackLog(const char *file, int line, const char *func, const char *format, ...);
+        int writeDebugLog(const char *file, int line, const char *func, const char *format, ...);
+        int writeInfoLog(const char *file, int line, const char *func, const char *format, ...);
+        int writeWarningLog(const char *file, int line, const char *func, const char *format, ...);
+        int writeErrorLog(const char *file, int line, const char *func, const char *format, ...);
+        int writeSendErrorLog(const char *file, int line, const char *func, const char *format, ...);
+        int writeFatalErrorLog(const char *file, int line, const char *func, int exit_code, const char *format, ...);
     };
 
 
-    class LogFactory {
+    AFUN_TOOL_EXPORT class LogFactory {
         bool init;  // 是否已经初始化
         pid_t pid;
 
@@ -64,33 +55,36 @@ namespace aFuntool {
 
     public:
         Logger sys_log = Logger("SYSTEM");
-        AFUN_TOOL_EXPORT LogFactory();
-        AFUN_TOOL_EXPORT ~LogFactory();
-        AFUN_TOOL_EXPORT int initLogSystem(ConstFilePath path, bool is_asyn = true);
-        AFUN_TOOL_EXPORT bool destruct();
-        AFUN_TOOL_EXPORT void writeLog(LogLevel level,
-                                       const char *id, pid_t tid,
-                                       const char *ti, time_t t,
-                                       const char *file, int line, const char *func,
-                                       const char *info);
-        AFUN_TOOL_EXPORT static void writeConsole(LogLevel level,
-                                                  const char *id, pid_t tid,
-                                                  const char *ti, time_t t,
-                                                  const char *file, int line, const char *func,
-                                                  const char *info);
-        AFUN_TOOL_EXPORT void writeLogAsyn(LogLevel level,
-                                           const char *id, pid_t tid,
-                                           const char *ti, time_t t,
-                                           const char *file, int line, const char *func, const char *info);
-        AFUN_TOOL_EXPORT int newLog(Logger *logger,
-                                    bool pc,
-                                    LogLevel level,
-                                    const char *file, int line, const char *func,
-                                    const char *format, va_list ap);
+        LogFactory();
+        ~LogFactory();
+        LogFactory(const LogFactory &)=delete;
+        LogFactory &operator=(const LogFactory &)=delete;
+
+        int initLogSystem(ConstFilePath path, bool is_asyn = true);
+        bool destruct();
+        void writeLog(LogLevel level,
+                      const char *id, pid_t tid,
+                      const char *ti, time_t t,
+                      const char *file, int line, const char *func,
+                      const char *info);
+        static void writeConsole(LogLevel level,
+                                 const char *id, pid_t tid,
+                                 const char *ti, time_t t,
+                                 const char *file, int line, const char *func,
+                                 const char *info);
+        void writeLogAsyn(LogLevel level,
+                          const char *id, pid_t tid,
+                          const char *ti, time_t t,
+                          const char *file, int line, const char *func, const char *info);
+        int newLog(Logger *logger,
+                   bool pc,
+                   LogLevel level,
+                   const char *file, int line, const char *func,
+                   const char *format, va_list ap);
         bool news(){ return !init || log_buf != nullptr; }
         int wait(){ return pthread_cond_wait(&cond, &mutex); }
         bool stop(){ return !init && log_buf == nullptr; }
-        AFUN_TOOL_EXPORT struct LogNode *pop();
+        struct LogNode *pop();
     };
 
     AFUN_TOOL_EXPORT extern LogFactory log_factory;

+ 0 - 1
include/tool/macro.hpp

@@ -11,7 +11,6 @@
 #include "base.h"
 
 #define NUL ((char)0)
-#define W_NUL ((wchar_t)0)
 
 namespace aFuntool {
     typedef uint32_t FileLine;  // 文件行号

+ 7 - 5
include/tool/regex.hpp

@@ -9,14 +9,16 @@
 namespace aFuntool {
     const int REGEX_ERROR_SIZE = 512;
 
-    class Regex {
+    AFUN_TOOL_EXPORT class Regex {
         pcre2_code *re;  // 正则表达式
         const std::string pattern;  // 正则表达式的字符串
     public:
-        AFUN_TOOL_EXPORT explicit Regex(const std::string &pattern_);
-        AFUN_TOOL_EXPORT ~Regex ();
-        AFUN_TOOL_EXPORT int match(const char *subject);
-        AFUN_TOOL_EXPORT int match(const std::string &subject) {return match(subject.c_str());}
+        explicit Regex(const std::string &pattern_);
+        Regex(const Regex &regex);
+        Regex &operator=(const Regex &regex);
+        ~Regex ();
+        int match(const char *subject);
+        int match(const std::string &subject) {return match(subject.c_str());}
     };
 }
 

+ 0 - 42
src/core/__gc.hpp

@@ -1,42 +0,0 @@
-#ifndef AFUN_GC_HPP_
-#define AFUN_GC_HPP_
-#include "gc.hpp"
-
-namespace aFuncore {
-    template<class T>
-    GcObject<T>::GcObject() : GcObjectBase() {
-        prev = nullptr;
-        next = nullptr;
-    }
-
-    template<class T>
-    void aFuncore::GcObject<T>::addObject(T *&chain){
-        if (chain != nullptr) {
-            next = chain;
-            chain->prev = dynamic_cast<T *>(this);
-        }
-        chain = dynamic_cast<T *>(this);
-    }
-
-    template<class T>
-    void aFuncore::GcObject<T>::delObject(T *&chain){
-        if (next != nullptr)
-            next->prev = prev;
-
-        if (prev == nullptr)
-            chain = next;
-        else
-            prev->next = next;
-    }
-
-    template<class T>
-    void GcObject<T>::destruct(T *&chain){
-        for (T *tmp = chain, *n; tmp != nullptr; tmp = n) {
-            n = tmp->next;
-            delete tmp;
-        }
-    }
-
-};
-
-#endif //AFUN_GC_HPP_

+ 14 - 15
src/core/code.cpp

@@ -99,31 +99,30 @@ Code *Code::connect(Code *code){
 /**
  * 删除自己以及其子、兄代码块
  */
-void Code::destructAll(){
-    if (this->type != code_start) {
+void Code::destruct(Code *code){
+    if (code->type != code_start) {
         errorLog(aFunCoreLogger, "Code delete did not with `start`");
         return;
     }
 
-    Code *tmp = this;
     Code *next_tmp;
-    while (tmp != nullptr) {
-        if (tmp->type != code_block || tmp->son == nullptr) {
-            if (tmp->next == nullptr) {
-                if (tmp->father == nullptr)
+    while (code != nullptr) {
+        if (code->type != code_block || code->son == nullptr) {
+            if (code->next == nullptr) {
+                if (code->father == nullptr)
                     next_tmp = nullptr;
                 else {
-                    next_tmp = tmp->father;
+                    next_tmp = code->father;
                     next_tmp->son = nullptr;
                 }
             } else
-                next_tmp = tmp->next;
-            delete tmp;
-            tmp = next_tmp;
+                next_tmp = code->next;
+            delete code;
+            code = next_tmp;
         } else
-            tmp = tmp->son;
+            code = code->son;
     }
-    delete tmp;
+    delete code;
 }
 
 /**
@@ -314,7 +313,7 @@ Code *Code::read_v1(FILE *f, bool debug, int8_t read_type, bool to_son) {
             std::string element_;
             Done(byteReadInt(f, &prefix_));
             Done(byteReadStr(f, element_));
-            ret = new Code(element_, 0, "", char(prefix_));
+            ret = Code::create(element_, 0, "", char(prefix_));
             break;
         }
         case 4:
@@ -323,7 +322,7 @@ Code *Code::read_v1(FILE *f, bool debug, int8_t read_type, bool to_son) {
             int8_t block_type = NUL;
             Done(byteReadInt(f, &prefix_));
             Done(byteReadInt(f, &block_type));
-            ret = new Code(BlockType(block_type), nullptr, 0, "", char(prefix_));
+            ret = Code::create(BlockType(block_type), nullptr, 0, "", char(prefix_));
             break;
         }
         default:

+ 2 - 3
src/core/inter.cpp

@@ -4,7 +4,6 @@
 #include "env-var.hpp"
 #include "var.hpp"
 #include "msg.hpp"
-#include "__gc.hpp"
 
 using namespace aFuncore;
 using namespace aFuntool;
@@ -46,7 +45,7 @@ Inter::Inter(int argc, char **argv, ExitMode em)
 
     protect = new ProtectVarSpace(this);  // 放到最后
     global = new VarSpace(this);  // 放到最后
-    global_varlist = (new VarList(global))->connect(new VarList(protect));
+    global_varlist = (VarList::create(global))->connect(VarList::create(protect));
     status = inter_init;
 }
 
@@ -56,7 +55,7 @@ Inter::~Inter(){
     pthread_cond_destroy(&monitor_cond);
 
     if (!is_derive) {
-        global_varlist->destructAll();
+        VarList::destruct(global_varlist);
 
         Object::destruct(gc->obj);
         Var::destruct(gc->var);

+ 0 - 1
src/core/value.cpp

@@ -1,6 +1,5 @@
 #include "value.hpp"
 #include "inter.hpp"
-#include "__gc.hpp"
 
 using namespace aFuncore;
 using namespace aFuntool;

+ 4 - 5
src/core/var.cpp

@@ -1,6 +1,5 @@
 #include "var.hpp"
 #include "inter.hpp"
-#include "__gc.hpp"
 
 using namespace aFuncore;
 using namespace aFuntool;
@@ -256,9 +255,9 @@ void aFuncore::VarList::disconnect(VarList *varlist){
 /**
  * 删除所有varlist
  */
-void VarList::destructAll(){
-    for (VarList *tmp=this, *n; tmp != nullptr; tmp = n) {
-        n = tmp->next;
-        delete tmp;
+void VarList::destruct(VarList *varlist){
+    for (VarList *n; varlist != nullptr; varlist = n) {
+        n = varlist->next;
+        delete varlist;
     }
 }

+ 34 - 0
src/tool/regex.cpp

@@ -23,6 +23,40 @@ aFuntool::Regex::Regex(const std::string &pattern_) : pattern {pattern_} {
     }
 }
 
+Regex::Regex(const Regex &regex){
+    int error_code;
+    size_t erroroffset;
+    char regex_error[REGEX_ERROR_SIZE];
+
+    this->re = pcre2_compile((PCRE2_SPTR)regex.pattern.c_str(), PCRE2_ZERO_TERMINATED, 0, &error_code, &erroroffset, nullptr);
+    if (re == nullptr) {
+        PCRE2_UCHAR buffer[256];
+        pcre2_get_error_message(error_code, buffer, sizeof(buffer));
+        snprintf(regex_error, sizeof(regex_error), "R%d: %s\n", (int) erroroffset, buffer);
+        throw RegexException(regex_error);
+    }
+}
+
+Regex &Regex::operator=(const Regex &regex){
+    if (this == &regex)
+        return *this;
+
+    this->~Regex();
+
+    int error_code;
+    size_t erroroffset;
+    char regex_error[REGEX_ERROR_SIZE];
+
+    this->re = pcre2_compile((PCRE2_SPTR)regex.pattern.c_str(), PCRE2_ZERO_TERMINATED, 0, &error_code, &erroroffset, nullptr);
+    if (re == nullptr) {
+        PCRE2_UCHAR buffer[256];
+        pcre2_get_error_message(error_code, buffer, sizeof(buffer));
+        snprintf(regex_error, sizeof(regex_error), "R%d: %s\n", (int) erroroffset, buffer);
+        throw RegexException(regex_error);
+    }
+    return *this;
+}
+
 aFuntool::Regex::~Regex() {
     if (re != nullptr)
         pcre2_code_free(re);

+ 5 - 5
test/src/core-code.cpp

@@ -3,8 +3,8 @@ using namespace aFuncore;
 using namespace aFuntool;
 
 int main() {
-    Code *start = new Code(1, "test.aun");
-    start->connect(new Code("Test", 1))->connect(new Code(block_p, new Code(block_p, new Code("Test3", 2), 2), 2));
+    Code *start = Code::create(1, "test.aun");
+    start->connect(Code::create("Test", 1))->connect(Code::create(block_p, Code::create(block_p, Code::create("Test3", 2), 2), 2));
     start->displayAll();
     std::string md5 = start->getMD5All_v1();
     printf("md5: %s\n", md5.c_str());
@@ -12,15 +12,15 @@ int main() {
     getEndian();
 
     start->writeByteCode("test.aun");
-    start->destructAll();
-    start = new Code(1, "test.aun");
+    Code::destruct(start);
+    start = Code::create(1, "test.aun");
 
     start->readByteCode("test.aun");
 
     start->displayAll();
     md5 = start->getMD5All_v1();
     printf("md5: %s\n", md5.c_str());
-    start->destructAll();
+    Code::destruct(start);
 
     return 0;
 }

+ 32 - 31
test/src/run-code.cpp

@@ -38,12 +38,12 @@ class Func1 : public Function {
     Code *func_code;
 public:
     explicit Func1(Inter *inter_) : Function("Function", inter_) {
-        func_code = (new Code(0, "run-code.aun"));
-        func_code->connect(new Code(block_p, new Code("test-var", 1), 0));
+        func_code = (Code::create(0, "run-code.aun"));
+        func_code->connect(Code::create(block_p, Code::create("test-var", 1), 0));
     }
 
     ~Func1() override {
-        func_code->destructAll();
+        Code::destruct(func_code);
     }
 
     CallFunction *getCallFunction(Code *code, Inter *inter) override {
@@ -57,12 +57,12 @@ class Literaler1 : public Literaler {
     Code *func_code;
 public:
     explicit Literaler1(Inter *inter_) : Literaler("Data", inter_) {
-        func_code = (new Code(0, "run-code.aun"));
-        func_code->connect(new Code(block_p, new Code("test-var", 1), 0));
+        func_code = (Code::create(0, "run-code.aun"));
+        func_code->connect(Code::create(block_p, Code::create("test-var", 1), 0));
     }
 
     ~Literaler1() override {
-        func_code->destructAll();
+        Code::destruct(func_code);
     }
 
     void getObject(const std::string &literal, char prefix) override {
@@ -75,12 +75,12 @@ class CBV1 : public CallBackVar {
     Code *func_code;
 public:
     explicit CBV1(Inter *inter_) : CallBackVar("CBV1", inter_) {
-        func_code = (new Code(0, "run-code.aun"));
-        func_code->connect(new Code(block_p, new Code("test-var", 1), 0));
+        func_code = (Code::create(0, "run-code.aun"));
+        func_code->connect(Code::create(block_p, Code::create("test-var", 1), 0));
     }
 
     ~CBV1() override {
-        func_code->destructAll();
+        Code::destruct(func_code);
     }
 
     void callBack() override {
@@ -107,59 +107,60 @@ int main() {
     auto cbv = new CBV1(inter);
     inter->getGlobalVarlist()->defineVar("test-cbv", cbv);
     printf_stdout(0, "cbv: %p\n", cbv);
+    fputs_stdout("\n");
 
     {
-        auto code = (new Code(0, "run-code.aun"));
-        code->connect(new Code(block_p, new Code("test-var", 1), 0));
+        auto code = (Code::create(0, "run-code.aun"));
+        code->connect(Code::create(block_p, Code::create("test-var", 1), 0));
         inter->runCode(code);
-        code->destructAll();
+        Code::destruct(code);
         fputs_stdout("\n");
     }
 
     {
-        auto arg = new Code("test-func", 1);
-        arg->connect(new Code("test-var", 1));
+        auto arg = Code::create("test-func", 1);
+        arg->connect(Code::create("test-var", 1));
 
-        auto code = (new Code(0, "run-code.aun"));
-        code->connect(new Code(block_c, arg, 0));
+        auto code = (Code::create(0, "run-code.aun"));
+        code->connect(Code::create(block_c, arg, 0));
         inter->runCode(code);
-        code->destructAll();
+        Code::destruct(code);
         fputs_stdout("\n");
     }
 
     {
-        auto arg = new Code("test-var", 1);
-        arg->connect(new Code("test-func", 1));
+        auto arg = Code::create("test-var", 1);
+        arg->connect(Code::create("test-func", 1));
 
-        auto code = (new Code(0, "run-code.aun"));
-        code->connect(new Code(block_b, arg, 0));
+        auto code = (Code::create(0, "run-code.aun"));
+        code->connect(Code::create(block_b, arg, 0));
         inter->runCode(code);
-        code->destructAll();
+        Code::destruct(code);
         fputs_stdout("\n");
     }
 
     {
         inter->pushLiteral("data[0-9]", "test-literaler", false);
-        auto code = (new Code(0, "run-code.aun"));
-        code->connect(new Code("data3", 1));
+        auto code = (Code::create(0, "run-code.aun"));
+        code->connect(Code::create("data3", 1));
         inter->runCode(code);
-        code->destructAll();
+        Code::destruct(code);
         fputs_stdout("\n");
     }
 
     {
-        auto code = (new Code(0, "run-code.aun"));
-        code->connect(new Code("test-cbv", 1));
+        auto code = (Code::create(0, "run-code.aun"));
+        code->connect(Code::create("test-cbv", 1));
         inter->runCode(code);
-        code->destructAll();
+        Code::destruct(code);
         fputs_stdout("\n");
     }
 
     {
-        auto code = (new Code(0, "run-code.aun"));
-        code->connect(new Code("test-not-var", 1));
+        auto code = (Code::create(0, "run-code.aun"));
+        code->connect(Code::create("test-not-var", 1));
         inter->runCode(code);
-        code->destructAll();
+        Code::destruct(code);
         fputs_stdout("\n");
     }