From 9d1bc4c0e086a35d5fc5f5934d4c4c790f23d324 Mon Sep 17 00:00:00 2001 From: ninemine <1371605831@qq.com> Date: Sun, 27 Jul 2025 01:01:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Convention/[Runtime]/Config.hpp | 8 ++++- Convention/[Runtime]/GlobalConfig.hpp | 45 ++++++++++++++++++--------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/Convention/[Runtime]/Config.hpp b/Convention/[Runtime]/Config.hpp index 5c12bca..2832487 100644 --- a/Convention/[Runtime]/Config.hpp +++ b/Convention/[Runtime]/Config.hpp @@ -1724,7 +1724,7 @@ namespace Convention if constexpr (index == 0) return sizeof(Element); else - return sizeof(Element) + _MyNext::ElementOffset(); + return sizeof(Element) + _MyNext:: template ElementOffset(); } template decltype(auto) GetValue() const noexcept @@ -1900,6 +1900,12 @@ namespace Convention GetStaticMyAllocator().destroy(ptr); GetStaticMyAllocator().deallocate(ptr, 1); } + protected: + template + static T* ConstructMyPtr(Args&&... args) + { + return BuildMyPtr(std::forward(args)...); + } public: /** * @brief 任意匹配的构造函数 diff --git a/Convention/[Runtime]/GlobalConfig.hpp b/Convention/[Runtime]/GlobalConfig.hpp index 1535662..133394d 100644 --- a/Convention/[Runtime]/GlobalConfig.hpp +++ b/Convention/[Runtime]/GlobalConfig.hpp @@ -166,22 +166,32 @@ namespace Convention GlobalConfig& LoadProperties() { auto configFile = GetConfigFile(); - if (!configFile.Exists()) { + if (!configFile.Exists()) + { data_pair.clear(); - } else { - try { + } + else + { + try + { auto content = configFile.LoadAsText(); auto config = nlohmann::json::parse(content); - if (config.contains("properties") && config["properties"].is_object()) { + if (config.contains("properties") && config["properties"].is_object()) + { data_pair.clear(); - for (auto& [key, value] : config["properties"].items()) { + for (auto& [key, value] : config["properties"].items()) + { data_pair[key] = value; } - } else { + } + else + { throw std::runtime_error("Can't find properties in config file"); } - } catch (const nlohmann::json::exception& e) { + } + catch (const nlohmann::json::exception& e) + { throw std::runtime_error("JSON parsing error: " + std::string(e.what())); } } @@ -240,7 +250,7 @@ namespace Convention Log(messageType, message, nullptr); } - void LogPropertyNotFound(const std::string& message, std::function logger = nullptr, const std::string& defaultValue = "") + void LogPropertyNotFound(const std::string& message, std::function logger, const std::string& defaultValue) { std::string fullMessage = message; if (!defaultValue.empty()) { @@ -249,7 +259,7 @@ namespace Convention Log("Property not found", fullMessage, logger); } - void LogPropertyNotFound(const std::string& message, const std::string& defaultValue = "") + void LogPropertyNotFound(const std::string& message, const std::string& defaultValue) { LogPropertyNotFound(message, nullptr, defaultValue); } @@ -263,14 +273,19 @@ namespace Convention T FindItem(const std::string& key, const T& defaultValue = T{}) const { auto it = data_pair.find(key); - if (it != data_pair.end()) { - try { + if (it != data_pair.end()) + { + try + { return it->second.get(); - } catch (const nlohmann::json::exception&) { - LogPropertyNotFound("Cannot convert value for key: " + key); } - } else { - LogPropertyNotFound("Key not found: " + key); + catch (const nlohmann::json::exception&) + { + LogPropertyNotFound(std::string("Cannot convert value for key: ") + key, std::to_string(defaultValue)); + } + } else + { + LogPropertyNotFound(std::string("Key not found: ") + key); } return defaultValue; }