Просмотр исходного кода

refactor: 强制类型转换改为使用static_cast

SongZihuan 3 лет назад
Родитель
Сommit
15eeb79194
4 измененных файлов с 20 добавлено и 20 удалено
  1. 11 11
      src/core/code.cpp
  2. 2 2
      src/tool/byte.cpp
  3. 4 4
      src/tool/log.cpp
  4. 3 3
      src/tool/stdio.cpp

+ 11 - 11
src/core/code.cpp

@@ -77,7 +77,7 @@ namespace aFuncore {
             if (tmp->next == nullptr) {
                 do {
                     tmp = tmp->father;
-                    Done(aFuntool::byteWriteInt(f, (int8_t) 3));
+                    Done(aFuntool::byteWriteInt(f, static_cast<int8_t>(3)));
                 } while (tmp != nullptr && tmp->next == nullptr);
                 if (tmp == nullptr)
                     break;
@@ -85,7 +85,7 @@ namespace aFuncore {
             } else
                 tmp = tmp->next;
         }
-        Done(aFuntool::byteWriteInt(f, (int8_t) 0));
+        Done(aFuntool::byteWriteInt(f, static_cast<int8_t>(0)));
         return true;
     }
 
@@ -388,24 +388,24 @@ RETURN_FALSE:
     bool Code::ByteCode::write_v1(FILE *f, bool debug) const{
         switch (type) {
             case code_element:
-                Done(aFuntool::byteWriteInt(f, (int8_t) code_element));
-                Done(aFuntool::byteWriteInt(f, (int8_t) prefix));
-                Done(aFuntool::byteWriteStr(f, data.element));
+                Done(aFuntool::byteWriteInt(f, static_cast<int8_t>(code_element)));
+                Done(aFuntool::byteWriteInt(f, static_cast<int8_t>(prefix)));
+                Done(aFuntool::byteWriteStr(f, (data.element)));
                 break;
             case code_block:
                 if (data.son == nullptr)
-                    Done(aFuntool::byteWriteInt(f, (int8_t) 4));  // 空 block 标注为 4
+                    Done(aFuntool::byteWriteInt(f, static_cast<int8_t>(4)));  // 空 block 标注为 4
                 else
-                    Done(aFuntool::byteWriteInt(f, (int8_t) code_block));
-                Done(aFuntool::byteWriteInt(f, (int8_t) prefix));
-                Done(aFuntool::byteWriteInt(f, (int8_t) data.block_type));
+                    Done(aFuntool::byteWriteInt(f, static_cast<int8_t>(code_block)));
+                Done(aFuntool::byteWriteInt(f, static_cast<int8_t>(prefix)));
+                Done(aFuntool::byteWriteInt(f, static_cast<int8_t>(data.block_type)));
                 break;
             default:
                 break;
 
         }
         if (debug)
-            Done(aFuntool::byteWriteInt(f, (int16_t) line));
+            Done(aFuntool::byteWriteInt(f, static_cast<int16_t>(line)));
         return true;
     }
 
@@ -475,7 +475,7 @@ RETURN_FALSE:
         char md5_value[aFuntool::MD5_SIZE];
         aFuntool::MD5_CTX *md5 = aFuntool::MD5Init();
 
-        char head[] = {(char) type, prefix, 'x', 'x', aFuntool::NUL};
+        char head[] = {static_cast<char>(type), prefix, 'x', 'x', aFuntool::NUL};
         if (prefix == aFuntool::NUL)
             head[1] = '-';
         if (type == code_block) {

+ 2 - 2
src/tool/byte.cpp

@@ -30,7 +30,7 @@ namespace aFuntool {
      * 写入一个C风格字符串
      */
     bool byteWriteStr(FILE *file, const char *str){
-        if (!byteWriteInt<uint16_t>(file, (uint16_t) strlen(str)))
+        if (!byteWriteInt<uint16_t>(file, static_cast<uint16_t>(strlen(str))))
             return false;
         return fwrite(str, sizeof(char), strlen(str), file) == strlen(str);
     }
@@ -41,7 +41,7 @@ namespace aFuntool {
      */
     bool byteWriteStr(FILE *file, const std::string &str){
         size_t size = str.size();
-        if (!byteWriteInt<uint16_t>(file, (uint16_t) size))
+        if (!byteWriteInt<uint16_t>(file, static_cast<uint16_t>(size)))
             return false;
         return fwrite(str.c_str(), sizeof(char), size, file) == size;
     }

+ 4 - 4
src/tool/log.cpp

@@ -29,14 +29,14 @@
 
 #include <windows.h>
 
-#define getpid() (long)GetCurrentProcessId()
-#define gettid() (long)GetCurrentThreadId()
+#define getpid() static_cast<long>(GetCurrentProcessId())
+#define gettid() static_cast<long>(GetCurrentThreadId())
 // cygwin没有syscall.h, 因此需要依赖 windows 的 api
 #else
 #include <unistd.h>
 #include "sys/syscall.h"
-#define gettid() (long)syscall(SYS_gettid)
-#define getpid() (long)getpid()
+#define gettid() static_cast<long>(syscall(SYS_gettid))
+#define getpid() static_cast<long>(getpid())
 #endif
 
 namespace aFuntool {

+ 3 - 3
src/tool/stdio.cpp

@@ -248,7 +248,7 @@ namespace aFuntool {
 
     char *fgets_stdin_(char *buf, size_t len){
         if (!_isatty(_fileno(stdin)))
-            return fgets(buf, (int) len, stdin);
+            return fgets(buf, static_cast<int>(len), stdin);
 
         HANDLE std_i = GetStdHandle(STD_INPUT_HANDLE);
         HANDLE std_o = GetStdHandle(STD_OUTPUT_HANDLE);
@@ -402,12 +402,12 @@ namespace aFuntool {
 
         if (index != 0) {
             index--;
-            buffer[index] = (char) ch;
+            buffer[index] = static_cast<char>(ch);
         } else if (end != BUFF_SIZE) {  // index == 0;
             memmove(buffer, buffer + 1, end);  // 往回移动
             end++;
             next++;
-            buffer[0] = (char) ch;
+            buffer[0] = static_cast<char>(ch);
         }
 
         return 1;