修改了一些格式问题

This commit is contained in:
2025-07-27 01:01:05 +08:00
parent 407de7999c
commit 9d1bc4c0e0
2 changed files with 37 additions and 16 deletions

View File

@@ -1724,7 +1724,7 @@ namespace Convention
if constexpr (index == 0) if constexpr (index == 0)
return sizeof(Element); return sizeof(Element);
else else
return sizeof(Element) + _MyNext::ElementOffset<index - 1>(); return sizeof(Element) + _MyNext:: template ElementOffset<index - 1>();
} }
template<size_t index> template<size_t index>
decltype(auto) GetValue() const noexcept decltype(auto) GetValue() const noexcept
@@ -1900,6 +1900,12 @@ namespace Convention
GetStaticMyAllocator().destroy(ptr); GetStaticMyAllocator().destroy(ptr);
GetStaticMyAllocator().deallocate(ptr, 1); GetStaticMyAllocator().deallocate(ptr, 1);
} }
protected:
template<typename... Args>
static T* ConstructMyPtr(Args&&... args)
{
return BuildMyPtr(std::forward<Args>(args)...);
}
public: public:
/** /**
* @brief 任意匹配的构造函数 * @brief 任意匹配的构造函数

View File

@@ -166,22 +166,32 @@ namespace Convention
GlobalConfig& LoadProperties() GlobalConfig& LoadProperties()
{ {
auto configFile = GetConfigFile(); auto configFile = GetConfigFile();
if (!configFile.Exists()) { if (!configFile.Exists())
{
data_pair.clear(); data_pair.clear();
} else { }
try { else
{
try
{
auto content = configFile.LoadAsText(); auto content = configFile.LoadAsText();
auto config = nlohmann::json::parse(content); 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(); data_pair.clear();
for (auto& [key, value] : config["properties"].items()) { for (auto& [key, value] : config["properties"].items())
{
data_pair[key] = value; data_pair[key] = value;
} }
} else { }
else
{
throw std::runtime_error("Can't find properties in config file"); 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())); throw std::runtime_error("JSON parsing error: " + std::string(e.what()));
} }
} }
@@ -240,7 +250,7 @@ namespace Convention
Log(messageType, message, nullptr); Log(messageType, message, nullptr);
} }
void LogPropertyNotFound(const std::string& message, std::function<void(const std::string&)> logger = nullptr, const std::string& defaultValue = "") void LogPropertyNotFound(const std::string& message, std::function<void(const std::string&)> logger, const std::string& defaultValue)
{ {
std::string fullMessage = message; std::string fullMessage = message;
if (!defaultValue.empty()) { if (!defaultValue.empty()) {
@@ -249,7 +259,7 @@ namespace Convention
Log("Property not found", fullMessage, logger); 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); LogPropertyNotFound(message, nullptr, defaultValue);
} }
@@ -263,14 +273,19 @@ namespace Convention
T FindItem(const std::string& key, const T& defaultValue = T{}) const T FindItem(const std::string& key, const T& defaultValue = T{}) const
{ {
auto it = data_pair.find(key); auto it = data_pair.find(key);
if (it != data_pair.end()) { if (it != data_pair.end())
try { {
try
{
return it->second.get<T>(); return it->second.get<T>();
} catch (const nlohmann::json::exception&) {
LogPropertyNotFound("Cannot convert value for key: " + key);
} }
} else { catch (const nlohmann::json::exception&)
LogPropertyNotFound("Key not found: " + key); {
LogPropertyNotFound(std::string("Cannot convert value for key: ") + key, std::to_string(defaultValue));
}
} else
{
LogPropertyNotFound(std::string("Key not found: ") + key);
} }
return defaultValue; return defaultValue;
} }