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

refactor: 类部分成员使用const限定符

SongZihuan 3 жил өмнө
parent
commit
9491326ccd

+ 3 - 5
include/tool/dlc.hpp

@@ -76,8 +76,8 @@ namespace aFuntool {
      */
     template <typename SYMBOL>
     class DlcSymbol {
-        SYMBOL *symbol;
-        class DlcHandle *dlc = nullptr;
+        const SYMBOL *symbol;
+        const DlcHandle *dlc = nullptr;
 
     public:
         /**
@@ -85,9 +85,7 @@ namespace aFuntool {
          * @param symbol 符号指针
          * @param dlc 句柄
          */
-        explicit DlcSymbol(SYMBOL *symbol, class DlcHandle *dlc) {
-            this->symbol = symbol;
-            this->dlc = dlc;
+        explicit DlcSymbol(SYMBOL *symbol_, class DlcHandle *dlc_) : symbol {symbol_}, dlc {dlc_} {
             if (this->dlc != nullptr)
                 this->dlc++;
         }

+ 2 - 2
include/tool/log.hpp

@@ -23,13 +23,13 @@ namespace aFuntool {
     class LogFactory;
 
     class Logger {
-        std::string id;
+        const std::string id;
         LogLevel level = log_debug;
         bool exit = true;
         friend class LogFactory;
 
     public:
-        explicit Logger(const std::string &id, LogLevel level=log_warning, bool exit=true);
+        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,

+ 2 - 2
include/tool/regex.hpp

@@ -11,9 +11,9 @@ namespace aFuntool {
 
     class Regex {
         pcre2_code *re;  // 正则表达式
-        std::string pattern;  // 正则表达式的字符串
+        const std::string pattern;  // 正则表达式的字符串
     public:
-        explicit Regex (const std::string &pattern);
+        explicit Regex (const std::string &pattern_);
         ~Regex ();
         int match(const char *subject);
     };

+ 4 - 6
src/tool/log.cpp

@@ -59,7 +59,7 @@ struct ansyData {
 static void destructLogSystemAtExit(void *);
 static void *ansyWritrLog(void *);
 
-aFuntool::LogFactory::LogFactory() {  // NOLINT cond 通过 pthread_cond_init 实现初始化
+aFuntool::LogFactory::LogFactory() : sys_log("SYSTEM", log_info) {  // NOLINT cond 通过 pthread_cond_init 实现初始化
     init=false;
     pid=0;
     log = nullptr;
@@ -141,7 +141,6 @@ int aFuntool::LogFactory::initLogSystem(ConstFilePath path, bool is_asyn){
         pthread_create(&pt, nullptr, ansyWritrLog, data);
     }
 
-    sys_log = Logger("SYSTEM", log_info);  // 设置为 debug, 记录 success 信息
     pthread_mutex_unlock(&mutex);
     infoLog(nullptr, "Log system init success");
     infoLog(nullptr, "Log .log size %lld", log_size);
@@ -401,10 +400,9 @@ int aFuntool::LogFactory::newLog(Logger *logger,
 #define CHECK_LOGGER() do {if (logger == nullptr) {logger = &(log_factory.sys_log);} \
                            if (logger == nullptr) return -1;} while(0)
 
-aFuntool::Logger::Logger(const std::string &id, LogLevel level, bool exit) {
-    this->id = id;
-    this->level = level;
-    this->exit = exit;
+aFuntool::Logger::Logger(const std::string &id_, LogLevel level_, bool exit_) : id {id_} {
+    this->level = level_;
+    this->exit = exit_;
 }
 
 #undef trackLog

+ 1 - 2
src/tool/regex.cpp

@@ -6,7 +6,7 @@
 #include "regex.hpp"
 using namespace aFuntool;
 
-aFuntool::Regex::Regex(const std::string &pattern) {
+aFuntool::Regex::Regex(const std::string &pattern_) : pattern {pattern_} {
     if (!isCharUTF8(pattern))
         throw RegexException("Pattern not utf-8");
 
@@ -14,7 +14,6 @@ aFuntool::Regex::Regex(const std::string &pattern) {
     size_t erroroffset;
     char regex_error[REGEX_ERROR_SIZE];
 
-    this->pattern = pattern;
     this->re = pcre2_compile((PCRE2_SPTR)pattern.c_str(), PCRE2_ZERO_TERMINATED, 0, &error_code, &erroroffset, nullptr);
     if (re == nullptr) {
         PCRE2_UCHAR buffer[256];