feat support override value
All checks were successful
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (push) Successful in 1m20s
linux-mips64-gcc / linux-gcc-mips64el (Debug) (push) Successful in 1m52s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 1m51s
linux-mips64-gcc / linux-gcc-mips64el (Release) (push) Successful in 1m58s
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 2m5s
linux-arm-gcc / linux-gcc-armhf (push) Successful in 2m20s
All checks were successful
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (push) Successful in 1m20s
linux-mips64-gcc / linux-gcc-mips64el (Debug) (push) Successful in 1m52s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 1m51s
linux-mips64-gcc / linux-gcc-mips64el (Release) (push) Successful in 1m58s
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 2m5s
linux-arm-gcc / linux-gcc-armhf (push) Successful in 2m20s
This commit is contained in:
parent
9d64455d57
commit
4ad27532b8
@ -156,9 +156,50 @@ Config::SetDefault(sled::string_view key, const double &value)
|
||||
default_values_.insert({key.to_string(), value});
|
||||
}
|
||||
|
||||
void
|
||||
Config::SetValue(sled::string_view key, const bool &value)
|
||||
{
|
||||
values_.insert({key.to_string(), value});
|
||||
}
|
||||
|
||||
void
|
||||
Config::SetValue(sled::string_view key, const char *value)
|
||||
{
|
||||
SetValue(key, std::string(value));
|
||||
}
|
||||
|
||||
void
|
||||
Config::SetValue(sled::string_view key, const std::string &value)
|
||||
{
|
||||
|
||||
values_.insert({key.to_string(), value});
|
||||
}
|
||||
|
||||
void
|
||||
Config::SetValue(sled::string_view key, sled::string_view value)
|
||||
{
|
||||
SetValue(key, std::string(value));
|
||||
}
|
||||
|
||||
void
|
||||
Config::SetValue(sled::string_view key, const int &value)
|
||||
{
|
||||
values_.insert({key.to_string(), value});
|
||||
}
|
||||
|
||||
void
|
||||
Config::SetValue(sled::string_view key, const double &value)
|
||||
{
|
||||
values_.insert({key.to_string(), value});
|
||||
}
|
||||
|
||||
bool
|
||||
Config::GetNode(sled::string_view key, toml::value &value) const
|
||||
{
|
||||
// 1.优先使用用户设定的值
|
||||
if (GetValueNode(key, value)) { return true; }
|
||||
|
||||
// 2. 然后从配置文件获取
|
||||
auto keys = StrSplit(key.to_string(), ".");
|
||||
auto cur = toml_;
|
||||
for (const auto &k : keys) {
|
||||
@ -166,6 +207,7 @@ Config::GetNode(sled::string_view key, toml::value &value) const
|
||||
auto next = toml::find(cur, k);
|
||||
cur = next;
|
||||
} catch (...) {
|
||||
// 3. 最后使用默认值
|
||||
if (GetDefaultNode(key, value)) { return true; }
|
||||
return false;
|
||||
}
|
||||
@ -201,6 +243,32 @@ Config::AddDefaultNode(sled::string_view key, ValueType value)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool
|
||||
Config::GetValueNode(sled::string_view key, toml::value &value) const
|
||||
{
|
||||
|
||||
auto iter = values_.find(key.to_string());
|
||||
if (iter == values_.end()) { return false; }
|
||||
auto &default_value = iter->second;
|
||||
switch (default_value.index()) {
|
||||
case 0:
|
||||
value = sled::get<bool>(default_value);
|
||||
break;
|
||||
case 1:
|
||||
value = sled::get<std::string>(default_value);
|
||||
break;
|
||||
case 2:
|
||||
value = sled::get<int>(default_value);
|
||||
break;
|
||||
case 3:
|
||||
value = sled::get<double>(default_value);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
Config::GetDefaultNode(sled::string_view key, toml::value &value) const
|
||||
{
|
||||
|
@ -20,6 +20,7 @@ public:
|
||||
void SetConfigName(sled::string_view name);
|
||||
void AddConfigPath(sled::string_view path);
|
||||
|
||||
// medium priority
|
||||
bool ReadInConfig();
|
||||
|
||||
bool IsSet(sled::string_view key) const;
|
||||
@ -28,6 +29,7 @@ public:
|
||||
double GetDoubleOr(sled::string_view key, const double &def = 0.0) const;
|
||||
std::string GetStringOr(sled::string_view key, const sled::string_view def = "") const;
|
||||
|
||||
// low priority
|
||||
void SetDefault(sled::string_view key, const bool &value);
|
||||
void SetDefault(sled::string_view key, const char *value);
|
||||
void SetDefault(sled::string_view key, const std::string &value);
|
||||
@ -35,12 +37,23 @@ public:
|
||||
void SetDefault(sled::string_view key, const int &value);
|
||||
void SetDefault(sled::string_view key, const double &value);
|
||||
|
||||
// high priority
|
||||
void SetValue(sled::string_view key, const bool &value);
|
||||
void SetValue(sled::string_view key, const char *value);
|
||||
void SetValue(sled::string_view key, const std::string &value);
|
||||
void SetValue(sled::string_view key, sled::string_view value);
|
||||
void SetValue(sled::string_view key, const int &value);
|
||||
void SetValue(sled::string_view key, const double &value);
|
||||
|
||||
private:
|
||||
bool GetNode(sled::string_view key, toml::value &value) const;
|
||||
bool AddDefaultNode(sled::string_view, ValueType value);
|
||||
bool GetDefaultNode(sled::string_view key, toml::value &value) const;
|
||||
bool GetValueNode(sled::string_view key, toml::value &value) const;
|
||||
bool AddDefaultNode(sled::string_view, ValueType value);
|
||||
|
||||
std::unordered_map<std::string, ValueType> default_values_;// low_priority
|
||||
std::unordered_map<std::string, ValueType> values_; // high_priority
|
||||
|
||||
std::unordered_map<std::string, ValueType> default_values_;
|
||||
std::vector<std::string> config_paths_;
|
||||
std::string config_name_;
|
||||
toml::value toml_;
|
||||
|
Loading…
Reference in New Issue
Block a user