From 7a15449dfe5e9f9c2aa767efc87f506fb63877f0 Mon Sep 17 00:00:00 2001 From: lhhxxxxx Date: Mon, 6 Jun 2022 19:31:48 +0800 Subject: [PATCH] perf. use nullptr --- src/MeoAssistant/AipOcr.cpp | 2 +- src/MeoAssistant/Assistant.cpp | 2 +- src/MeoAssistant/AsstUtils.hpp | 10 +++---- src/MeoAssistant/BattleImageAnalyzer.cpp | 2 +- src/MeoAssistant/Controller.cpp | 2 +- src/MeoAssistant/OcrPack.cpp | 6 ++--- src/MeoAssistant/OcrPack.h | 2 +- .../OcrWithPreprocessImageAnalyzer.cpp | 2 +- src/MeoAssistant/Resource.cpp | 26 +++++++++---------- .../RoguelikeBattleTaskPlugin.cpp | 4 +-- .../RoguelikeSkillSelectionTaskPlugin.cpp | 2 +- src/MeoAssistant/StageDropsImageAnalyzer.cpp | 6 ++--- src/MeoAssistant/TaskData.cpp | 2 +- src/MeoAssistant/TilePack.cpp | 4 +-- 14 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/MeoAssistant/AipOcr.cpp b/src/MeoAssistant/AipOcr.cpp index 6eabf37275..f8f57d1138 100644 --- a/src/MeoAssistant/AipOcr.cpp +++ b/src/MeoAssistant/AipOcr.cpp @@ -90,7 +90,7 @@ bool asst::AipOcr::request_ocr_and_parse(std::string_view cmd_fmt, const cv::Mat std::string encoded = base64_encode(enc_msg, buf.size()); size_t cmd_len = encoded.size() + cmd_fmt.size() + m_access_token.size(); - char* cmd = new char[cmd_len]; + auto cmd = new char[cmd_len]; memset(cmd, 0, cmd_len); #ifdef _MSC_VER sprintf_s(cmd, cmd_len, cmd_fmt.data(), m_access_token.c_str(), encoded.c_str()); diff --git a/src/MeoAssistant/Assistant.cpp b/src/MeoAssistant/Assistant.cpp index ffca0304d2..04fa1c2b07 100644 --- a/src/MeoAssistant/Assistant.cpp +++ b/src/MeoAssistant/Assistant.cpp @@ -293,7 +293,7 @@ void Assistant::msg_proc() void Assistant::task_callback(AsstMsg msg, const json::value& detail, void* custom_arg) { - Assistant* p_this = static_cast(custom_arg); + auto p_this = static_cast(custom_arg); json::value more_detail = detail; more_detail["uuid"] = p_this->m_uuid; diff --git a/src/MeoAssistant/AsstUtils.hpp b/src/MeoAssistant/AsstUtils.hpp index 67be640045..e393734d72 100644 --- a/src/MeoAssistant/AsstUtils.hpp +++ b/src/MeoAssistant/AsstUtils.hpp @@ -90,13 +90,13 @@ namespace asst const char* src_str = ansi_str.c_str(); int len = MultiByteToWideChar(CP_ACP, 0, src_str, -1, nullptr, 0); const std::size_t wstr_length = static_cast(len) + 1U; - wchar_t *wstr = new wchar_t[wstr_length]; + auto wstr = new wchar_t[wstr_length]; memset(wstr, 0, sizeof(wstr[0]) * wstr_length); MultiByteToWideChar(CP_ACP, 0, src_str, -1, wstr, len); len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr); const std::size_t str_length = static_cast(len) + 1; - char *str = new char[str_length]; + auto str = new char[str_length]; memset(str, 0, sizeof(str[0]) * str_length); WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, nullptr, nullptr); std::string strTemp = str; @@ -121,13 +121,13 @@ namespace asst const char* src_str = utf8_str.c_str(); int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, nullptr, 0); const std::size_t wsz_ansi_length = static_cast(len) + 1; - wchar_t *wsz_ansi = new wchar_t[wsz_ansi_length]; + auto wsz_ansi = new wchar_t[wsz_ansi_length]; memset(wsz_ansi, 0, sizeof(wsz_ansi[0]) * wsz_ansi_length); MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wsz_ansi, len); len = WideCharToMultiByte(CP_ACP, 0, wsz_ansi, -1, nullptr, 0, nullptr, nullptr); const std::size_t sz_ansi_length = static_cast(len) + 1; - char *sz_ansi = new char[sz_ansi_length]; + auto sz_ansi = new char[sz_ansi_length]; memset(sz_ansi, 0, sizeof(sz_ansi[0]) * sz_ansi_length); WideCharToMultiByte(CP_ACP, 0, wsz_ansi, -1, sz_ansi, len, nullptr, nullptr); std::string strTemp(sz_ansi); @@ -197,7 +197,7 @@ namespace asst si.hStdOutput = pipe_child_write; si.hStdError = pipe_child_write; - PROCESS_INFORMATION pi = { 0 }; + PROCESS_INFORMATION pi = { nullptr }; BOOL p_ret = CreateProcessA(nullptr, const_cast(cmdline.c_str()), nullptr, nullptr, TRUE, CREATE_NO_WINDOW, nullptr, nullptr, &si, &pi); if (p_ret) { diff --git a/src/MeoAssistant/BattleImageAnalyzer.cpp b/src/MeoAssistant/BattleImageAnalyzer.cpp index 93e53e3a7b..8aa62cf2e7 100644 --- a/src/MeoAssistant/BattleImageAnalyzer.cpp +++ b/src/MeoAssistant/BattleImageAnalyzer.cpp @@ -172,7 +172,7 @@ asst::BattleRole asst::BattleImageAnalyzer::oper_role_analyze(const Rect& roi) MatchImageAnalyzer role_analyzer(m_image); - BattleRole result = BattleRole::Unknown; + auto result = BattleRole::Unknown; double max_score = 0; for (auto&& [role, role_name] : RolesName) { role_analyzer.set_task_info("BattleOperRole" + role_name); diff --git a/src/MeoAssistant/Controller.cpp b/src/MeoAssistant/Controller.cpp index 066724f659..c93150b617 100644 --- a/src/MeoAssistant/Controller.cpp +++ b/src/MeoAssistant/Controller.cpp @@ -259,7 +259,7 @@ std::optional> asst::Controller::call_command(const s }; #ifdef _WIN32 - PROCESS_INFORMATION process_info = { 0 }; // 进程信息结构体 + PROCESS_INFORMATION process_info = { nullptr }; // 进程信息结构体 BOOL create_ret = ::CreateProcessA(nullptr, const_cast(cmd.c_str()), nullptr, nullptr, TRUE, CREATE_NO_WINDOW, nullptr, nullptr, &m_child_startup_info, &process_info); if (!create_ret) { Log.error("Call `", cmd, "` create error, ret", create_ret); diff --git a/src/MeoAssistant/OcrPack.cpp b/src/MeoAssistant/OcrPack.cpp index 86da93c47d..0e14974375 100644 --- a/src/MeoAssistant/OcrPack.cpp +++ b/src/MeoAssistant/OcrPack.cpp @@ -34,10 +34,10 @@ bool asst::OcrPack::load(const std::string& dir) return false; } - constexpr static const char* DetName = "/det"; + constexpr static auto DetName = "/det"; //constexpr static const char* ClsName = "/cls"; - constexpr static const char* RecName = "/rec"; - constexpr static const char* KeysName = "/ppocr_keys_v1.txt"; + constexpr static auto RecName = "/rec"; + constexpr static auto KeysName = "/ppocr_keys_v1.txt"; const std::string dst_filename = dir + DetName; //const std::string cls_filename = dir + ClsName; diff --git a/src/MeoAssistant/OcrPack.h b/src/MeoAssistant/OcrPack.h index 3fda6504df..f6cda6fa93 100644 --- a/src/MeoAssistant/OcrPack.h +++ b/src/MeoAssistant/OcrPack.h @@ -31,7 +31,7 @@ namespace asst // each box has 8 value ( 4 points, x and y ) int m_boxes_buffer[MaxBoxSize * 8] = { 0 }; - char* m_strs_buffer[MaxBoxSize] = { 0 }; + char* m_strs_buffer[MaxBoxSize] = { nullptr }; float m_scores_buffer[MaxBoxSize] = { 0 }; }; } diff --git a/src/MeoAssistant/OcrWithPreprocessImageAnalyzer.cpp b/src/MeoAssistant/OcrWithPreprocessImageAnalyzer.cpp index 979a150b41..6ab312ecf3 100644 --- a/src/MeoAssistant/OcrWithPreprocessImageAnalyzer.cpp +++ b/src/MeoAssistant/OcrWithPreprocessImageAnalyzer.cpp @@ -14,7 +14,7 @@ bool asst::OcrWithPreprocessImageAnalyzer::analyze() cv::Rect bounding_rect = cv::boundingRect(bin); bounding_rect.x += m_roi.x; bounding_rect.y += m_roi.y; - Rect new_roi = utils::make_rect(bounding_rect); + auto new_roi = utils::make_rect(bounding_rect); if (new_roi.empty()) { return false; diff --git a/src/MeoAssistant/Resource.cpp b/src/MeoAssistant/Resource.cpp index 63ab024db0..b1d581128c 100644 --- a/src/MeoAssistant/Resource.cpp +++ b/src/MeoAssistant/Resource.cpp @@ -13,20 +13,20 @@ bool asst::Resource::load(const std::string& dir) { LogTraceFunction; - constexpr static const char* TemplsFilename = "template"; - constexpr static const char* GeneralCfgFilename = "config.json"; - constexpr static const char* TaskDataFilename = "tasks.json"; - constexpr static const char* RoguelikeRecruitCfgFilename = "roguelike_recruit.json"; - constexpr static const char* RecruitCfgFilename = "recruit.json"; - constexpr static const char* ItemCfgFilename = "item_index.json"; - constexpr static const char* InfrastCfgFilename = "infrast.json"; - constexpr static const char* InfrastTempls = "template/infrast"; + constexpr static auto TemplsFilename = "template"; + constexpr static auto GeneralCfgFilename = "config.json"; + constexpr static auto TaskDataFilename = "tasks.json"; + constexpr static auto RoguelikeRecruitCfgFilename = "roguelike_recruit.json"; + constexpr static auto RecruitCfgFilename = "recruit.json"; + constexpr static auto ItemCfgFilename = "item_index.json"; + constexpr static auto InfrastCfgFilename = "infrast.json"; + constexpr static auto InfrastTempls = "template/infrast"; //constexpr static const char* CopilotCfgDirname = "copilot"; - constexpr static const char* RoguelikeCfgDirname = "roguelike"; - constexpr static const char* OcrResourceFilename = "PaddleOCR"; - constexpr static const char* TilesCalcResourceFilename = "Arknights-Tile-Pos"; - constexpr static const char* StageDropsCfgFilename = "stages.json"; - constexpr static const char* StageDropsTempls = "template/items"; + constexpr static auto RoguelikeCfgDirname = "roguelike"; + constexpr static auto OcrResourceFilename = "PaddleOCR"; + constexpr static auto TilesCalcResourceFilename = "Arknights-Tile-Pos"; + constexpr static auto StageDropsCfgFilename = "stages.json"; + constexpr static auto StageDropsTempls = "template/items"; bool overload = false; diff --git a/src/MeoAssistant/RoguelikeBattleTaskPlugin.cpp b/src/MeoAssistant/RoguelikeBattleTaskPlugin.cpp index 35521d5cae..1fe16e5a85 100644 --- a/src/MeoAssistant/RoguelikeBattleTaskPlugin.cpp +++ b/src/MeoAssistant/RoguelikeBattleTaskPlugin.cpp @@ -253,7 +253,7 @@ bool asst::RoguelikeBattleTaskPlugin::auto_battle() } // 将干员拖动到场上 - Loc loc = Loc::All; + auto loc = Loc::All; switch (opt_oper.role) { case BattleRole::Medic: case BattleRole::Support: @@ -388,7 +388,7 @@ bool asst::RoguelikeBattleTaskPlugin::try_possible_skill(const cv::Mat& image) bool used = false; for (auto& [loc, name] : m_used_tiles) { std::string status_key = "RoguelikeSkillUsage-" + name; - BattleSkillUsage usage = BattleSkillUsage::Possibly; + auto usage = BattleSkillUsage::Possibly; auto usage_opt = m_status->get_data(status_key); if (usage_opt) { usage = static_cast(usage_opt.value()); diff --git a/src/MeoAssistant/RoguelikeSkillSelectionTaskPlugin.cpp b/src/MeoAssistant/RoguelikeSkillSelectionTaskPlugin.cpp index 1f85670bba..cd96701425 100644 --- a/src/MeoAssistant/RoguelikeSkillSelectionTaskPlugin.cpp +++ b/src/MeoAssistant/RoguelikeSkillSelectionTaskPlugin.cpp @@ -42,7 +42,7 @@ bool asst::RoguelikeSkillSelectionTaskPlugin::_run() } const auto& oper_info = rg_src.get_oper_info(name); int index = 0; - BattleSkillUsage usage = BattleSkillUsage::Possibly; + auto usage = BattleSkillUsage::Possibly; if (skill_vec.size() >= oper_info.skill) { index = oper_info.skill - 1; usage = oper_info.skill_usage; diff --git a/src/MeoAssistant/StageDropsImageAnalyzer.cpp b/src/MeoAssistant/StageDropsImageAnalyzer.cpp index 9f37cb5ce6..2dc37e1a72 100644 --- a/src/MeoAssistant/StageDropsImageAnalyzer.cpp +++ b/src/MeoAssistant/StageDropsImageAnalyzer.cpp @@ -132,7 +132,7 @@ bool asst::StageDropsImageAnalyzer::analyze_difficulty() }; MatchImageAnalyzer analyzer(m_image); - StageDifficulty matched = StageDifficulty::Normal; + auto matched = StageDifficulty::Normal; double max_score = 0.0; #ifdef ASST_DEBUG @@ -188,7 +188,7 @@ bool asst::StageDropsImageAnalyzer::analyze_drops() for (int i = 1; i <= size; ++i) { // 因为第一个黄色的 baseline 是渐变的,圈出来的一般左边会少一段,所以这里直接从右边开始往左推 int x = baseline.x + baseline.width - i * roi.width; - Rect item_roi = Rect(x, baseline.y + roi.y, roi.width, roi.height); + auto item_roi = Rect(x, baseline.y + roi.y, roi.width, roi.height); std::string item = match_item(item_roi, droptype, size - i, size); int quantity = match_quantity(item_roi); @@ -322,7 +322,7 @@ asst::StageDropType asst::StageDropsImageAnalyzer::match_droptype(const Rect& ro }; MatchImageAnalyzer analyzer(m_image); - StageDropType matched = StageDropType::Unknown; + auto matched = StageDropType::Unknown; double max_score = 0.0; #ifdef ASST_DEBUG diff --git a/src/MeoAssistant/TaskData.cpp b/src/MeoAssistant/TaskData.cpp index 6e48acf179..7c23e79396 100644 --- a/src/MeoAssistant/TaskData.cpp +++ b/src/MeoAssistant/TaskData.cpp @@ -40,7 +40,7 @@ bool asst::TaskData::parse(const json::value& json) for (const auto& [name, task_json] : json.as_object()) { std::string algorithm_str = task_json.get("algorithm", "matchtemplate"); std::transform(algorithm_str.begin(), algorithm_str.end(), algorithm_str.begin(), to_lower); - AlgorithmType algorithm = AlgorithmType::Invalid; + auto algorithm = AlgorithmType::Invalid; if (algorithm_str == "matchtemplate") { algorithm = AlgorithmType::MatchTemplate; } diff --git a/src/MeoAssistant/TilePack.cpp b/src/MeoAssistant/TilePack.cpp index aee9e8bbf7..daa68ab9c7 100644 --- a/src/MeoAssistant/TilePack.cpp +++ b/src/MeoAssistant/TilePack.cpp @@ -14,7 +14,7 @@ bool asst::TilePack::load(const std::string& dir) return false; } - constexpr static const char* filename = "/levels.json"; + constexpr static auto filename = "/levels.json"; try { m_tile_calculator = std::make_unique( @@ -62,7 +62,7 @@ std::unordered_map asst::TilePack::calc( const auto& cv_p = pos[y][x]; const auto& tile = tiles[y][x]; - TileKey key = TileKey::Invalid; + auto key = TileKey::Invalid; if (auto iter = TileKeyMapping.find(tile.tileKey); iter != TileKeyMapping.cend()) { key = iter->second;