Skip to content

Commit

Permalink
Config Attribute Value: added write_if_not_empty helper
Browse files Browse the repository at this point in the history
Takes a string and assigns a value only if that string is not empty.
  • Loading branch information
Vultraz committed Jan 25, 2018
1 parent 4a6042a commit d98093e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/config_attribute_value.cpp
Expand Up @@ -235,6 +235,13 @@ config_attribute_value& config_attribute_value::operator=(const t_string& v)
return *this;
}

void config_attribute_value::write_if_not_empty(const std::string& v)
{
if(!v.empty()) {
*this = v;
}
}

bool config_attribute_value::to_bool(bool def) const
{
if(const yes_no* p = boost::get<const yes_no>(&value_))
Expand Down
4 changes: 4 additions & 0 deletions src/config_attribute_value.hpp
Expand Up @@ -177,6 +177,10 @@ class config_attribute_value
{
return operator=(T::enum_to_string(v));
}

/** Calls @ref operator=(const std::string&) if @a v is not empty. */
void write_if_not_empty(const std::string& v);

// Extracting as a specific type:
bool to_bool(bool def = false) const;
int to_int(int def = 0) const;
Expand Down

1 comment on commit d98093e

@CelticMinstrel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor complaint: why "write" rather than "set"?

Please sign in to comment.