Added boolean attribute "immediate" to [set_global_variable]...
...and [clear_global_variable]. If yes, the changes will be written to file immediately, independently of any ongoing transaction. Defaults to no.
This commit is contained in:
parent
4ef19a7180
commit
84938e08c1
3 changed files with 64 additions and 14 deletions
|
@ -62,13 +62,21 @@ persist_file_context::persist_file_context(const std::string &name_space)
|
|||
active_ = &(root_node_.child(namespace_.next()));
|
||||
}
|
||||
|
||||
bool persist_file_context::clear_var(const std::string &global)
|
||||
bool persist_file_context::clear_var(const std::string &global, bool immediate)
|
||||
{
|
||||
// if (cfg_.empty()) {
|
||||
// load_persist_data(namespace_.root(),cfg_);
|
||||
// }
|
||||
|
||||
config &cfg = active_->cfg_.child("variables");
|
||||
config bak;
|
||||
config bactive;
|
||||
if (immediate) {
|
||||
bak = cfg_;
|
||||
bactive = active_->cfg_.child_or_add("variables");
|
||||
load();
|
||||
root_node_.init();
|
||||
}
|
||||
config &cfg = active_->cfg_.child_or_add("variables");
|
||||
bool ret = cfg;
|
||||
if (ret) {
|
||||
bool exists = cfg.has_attribute(global);
|
||||
|
@ -82,13 +90,16 @@ bool persist_file_context::clear_var(const std::string &global)
|
|||
const std::string index_str(index_start+1,index_end);
|
||||
size_t index = static_cast<size_t>(lexical_cast_default<int>(index_str));
|
||||
cfg.remove_child(global,index);
|
||||
if (immediate) bactive.remove_child(global,index);
|
||||
} else {
|
||||
cfg.clear_children(global);
|
||||
if (immediate) bactive.clear_children(global);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (exists) {
|
||||
cfg.remove_attribute(global);
|
||||
if (immediate) bactive.remove_attribute(global);
|
||||
if (cfg.empty()) {
|
||||
active_->cfg_.clear_children("variables");
|
||||
active_->cfg_.remove_attribute("variables");
|
||||
|
@ -101,7 +112,24 @@ bool persist_file_context::clear_var(const std::string &global)
|
|||
// dirty_ = true;
|
||||
if (!in_transaction_)
|
||||
ret = save_context();
|
||||
else
|
||||
else if (immediate) {
|
||||
ret = save_context();
|
||||
cfg_ = bak;
|
||||
root_node_.init();
|
||||
active_->cfg_.clear_children("variables");
|
||||
active_->cfg_.remove_attribute("variables");
|
||||
active_->cfg_.add_child("variables",bactive);
|
||||
config &cfg = active_->cfg_.child("variables");
|
||||
if (cfg.empty()) {
|
||||
active_->cfg_.clear_children("variables");
|
||||
active_->cfg_.remove_attribute("variables");
|
||||
while ((active_->cfg_.empty()) && (active_->parent_ != NULL)) {
|
||||
active_ = active_->parent_;
|
||||
active_->remove_child(namespace_.node_);
|
||||
namespace_ = namespace_.prev();
|
||||
}
|
||||
}
|
||||
} else
|
||||
ret = true;
|
||||
} else {
|
||||
ret = exists;
|
||||
|
@ -155,26 +183,48 @@ bool persist_file_context::save_context() {
|
|||
}
|
||||
return success;
|
||||
}
|
||||
bool persist_file_context::set_var(const std::string &global,const config &val)
|
||||
bool persist_file_context::set_var(const std::string &global,const config &val, bool immediate)
|
||||
{
|
||||
// if (cfg_.empty()) {
|
||||
// load_persist_data(namespace_,cfg_);
|
||||
// }
|
||||
config bak;
|
||||
config bactive;
|
||||
if (immediate) {
|
||||
bak = cfg_;
|
||||
bactive = active_->cfg_.child_or_add("variables");
|
||||
load();
|
||||
root_node_.init();
|
||||
}
|
||||
|
||||
config &cfg = active_->cfg_.child_or_add("variables");
|
||||
if (val.has_attribute(global)) {
|
||||
if (val[global].empty())
|
||||
clear_var(global);
|
||||
else
|
||||
if (val[global].empty()) {
|
||||
clear_var(global,immediate);
|
||||
} else {
|
||||
cfg[global] = val[global];
|
||||
if (immediate) bactive[global] = val[global];
|
||||
}
|
||||
} else {
|
||||
cfg.clear_children(global);
|
||||
cfg.append(val);
|
||||
if (immediate) {
|
||||
bactive.clear_children(global);
|
||||
bactive.append(val);
|
||||
}
|
||||
}
|
||||
// dirty_ = true;
|
||||
if (!in_transaction_)
|
||||
return save_context();
|
||||
else
|
||||
else if (immediate) {
|
||||
bool ret = save_context();
|
||||
cfg_ = bak;
|
||||
root_node_.init();
|
||||
active_->cfg_.clear_children("variables");
|
||||
active_->cfg_.remove_attribute("variables");
|
||||
active_->cfg_.add_child("variables",bactive);
|
||||
return ret;
|
||||
} else
|
||||
return true;
|
||||
}
|
||||
void persist_context::set_node(const std::string &name) {
|
||||
|
|
|
@ -188,9 +188,9 @@ protected:
|
|||
|
||||
persist_context &add_child(const std::string &key);
|
||||
public:
|
||||
virtual bool clear_var(const std::string &) = 0;
|
||||
virtual bool clear_var(const std::string &, bool immediate = false) = 0;
|
||||
virtual config get_var(const std::string &) const = 0;
|
||||
virtual bool set_var(const std::string &, const config &) = 0;
|
||||
virtual bool set_var(const std::string &, const config &, bool immediate = false) = 0;
|
||||
virtual bool start_transaction () = 0;
|
||||
virtual bool end_transaction () = 0;
|
||||
virtual bool cancel_transaction () = 0;
|
||||
|
@ -211,9 +211,9 @@ private:
|
|||
|
||||
public:
|
||||
persist_file_context(const std::string &name_space);
|
||||
bool clear_var(const std::string &);
|
||||
bool clear_var(const std::string &, bool immediate = false);
|
||||
config get_var(const std::string &) const;
|
||||
bool set_var(const std::string &, const config &);
|
||||
bool set_var(const std::string &, const config &, bool immediate = false);
|
||||
|
||||
bool start_transaction () {
|
||||
if (in_transaction_)
|
||||
|
|
|
@ -72,7 +72,7 @@ static void get_global_variable(persist_context &ctx, const vconfig &pcfg)
|
|||
static void clear_global_variable(persist_context &ctx, const vconfig &pcfg)
|
||||
{
|
||||
std::string global = pcfg["global"];
|
||||
ctx.clear_var(global);
|
||||
ctx.clear_var(global,utils::string_bool(pcfg["immediate"]));
|
||||
}
|
||||
|
||||
static void set_global_variable(persist_context &ctx, const vconfig &pcfg)
|
||||
|
@ -91,7 +91,7 @@ static void set_global_variable(persist_context &ctx, const vconfig &pcfg)
|
|||
for (size_t i = 0; i < arraylen; i++)
|
||||
val.add_child(global,vars.child(local,i));
|
||||
}
|
||||
ctx.set_var(global,val);
|
||||
ctx.set_var(global,val,utils::string_bool(pcfg["immediate"]));
|
||||
}
|
||||
}
|
||||
void verify_and_get_global_variable(const vconfig &pcfg)
|
||||
|
|
Loading…
Add table
Reference in a new issue