From d98093ebbac88e7bec471e4831a0a4729052fd83 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Thu, 25 Jan 2018 18:13:27 +1100 Subject: [PATCH] Config Attribute Value: added write_if_not_empty helper Takes a string and assigns a value only if that string is not empty. --- src/config_attribute_value.cpp | 7 +++++++ src/config_attribute_value.hpp | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/src/config_attribute_value.cpp b/src/config_attribute_value.cpp index ed6e74fd27de..5899349274d9 100644 --- a/src/config_attribute_value.cpp +++ b/src/config_attribute_value.cpp @@ -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(&value_)) diff --git a/src/config_attribute_value.hpp b/src/config_attribute_value.hpp index 6d734e043bcb..901fa483b155 100644 --- a/src/config_attribute_value.hpp +++ b/src/config_attribute_value.hpp @@ -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;