Loading src/sled/config.cc +68 −0 Original line number Diff line number Diff line Loading @@ -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) { Loading @@ -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; } Loading Loading @@ -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 { Loading src/sled/config.h +15 −2 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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); Loading @@ -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 Loading
src/sled/config.cc +68 −0 Original line number Diff line number Diff line Loading @@ -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) { Loading @@ -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; } Loading Loading @@ -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 { Loading
src/sled/config.h +15 −2 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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); Loading @@ -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