Skip to content

Commit

Permalink
feat(config): Config::Save saves data if modified
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Mar 10, 2024
1 parent e554510 commit 12f460f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/rime/config/config_component.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ Config::~Config() {}

Config::Config(an<ConfigData> data) : ConfigItemRef(data.get()), data_(data) {}

bool Config::Save() {
return data_->Save();
}

bool Config::LoadFromStream(std::istream& stream) {
return data_->LoadFromStream(stream);
}
Expand Down
2 changes: 2 additions & 0 deletions src/rime/config/config_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Config : public Class<Config, const string&>, public ConfigItemRef {
// in the ConfigComponent
explicit Config(an<ConfigData> data);

// returns whether actually saved to file.
bool Save();
bool LoadFromStream(std::istream& stream);
bool SaveToStream(std::ostream& stream);
RIME_API bool LoadFromFile(const path& file_path);
Expand Down
8 changes: 6 additions & 2 deletions src/rime/config/config_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ an<ConfigItem> ConvertFromYaml(const YAML::Node& yaml_node,
void EmitYaml(an<ConfigItem> node, YAML::Emitter* emitter, int depth);

ConfigData::~ConfigData() {
if (auto_save_ && modified_ && !file_path_.empty())
SaveToFile(file_path_);
if (auto_save_)
Save();
}

bool ConfigData::Save() {
return modified_ && !file_path_.empty() && SaveToFile(file_path_);
}

bool ConfigData::LoadFromStream(std::istream& stream) {
Expand Down
2 changes: 2 additions & 0 deletions src/rime/config/config_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ConfigData {
ConfigData() = default;
~ConfigData();

// returns whether actually saved to file.
bool Save();
bool LoadFromStream(std::istream& stream);
bool SaveToStream(std::ostream& stream);
bool LoadFromFile(const path& file_path, ConfigCompiler* compiler);
Expand Down

0 comments on commit 12f460f

Please sign in to comment.