Skip to content

Commit

Permalink
refactor(ConfigValue): Add getter for found
Browse files Browse the repository at this point in the history
This lets callers query whether the config value was actually found
rather than having to just get a default.

It also takes the opportunity to make some of the accessors const, since
they can be.
  • Loading branch information
sfoster1 committed Jul 16, 2020
1 parent 44d2a06 commit d300742
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
15 changes: 9 additions & 6 deletions src/libs/ConfigValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ ConfigValue *ConfigValue::required()
return this;
}

float ConfigValue::as_number()
float ConfigValue::as_number() const
{
if( this->found == false && this->default_set == true ) {
return this->default_double;
Expand All @@ -79,7 +79,7 @@ float ConfigValue::as_number()
}
}

int ConfigValue::as_int()
int ConfigValue::as_int() const
{
if( this->found == false && this->default_set == true ) {
return this->default_int;
Expand All @@ -95,12 +95,12 @@ int ConfigValue::as_int()
}
}

std::string ConfigValue::as_string()
std::string ConfigValue::as_string() const
{
return this->value;
}

bool ConfigValue::as_bool()
bool ConfigValue::as_bool() const
{
if( this->found == false && this->default_set == true ) {
return this->default_int;
Expand Down Expand Up @@ -134,7 +134,7 @@ ConfigValue *ConfigValue::by_default(string val)
return this;
}

bool ConfigValue::has_characters( const char *mask )
bool ConfigValue::has_characters( const char *mask ) const
{
if( this->value.find_first_of(mask) != string::npos ) {
return true;
Expand All @@ -143,8 +143,11 @@ bool ConfigValue::has_characters( const char *mask )
}
}

bool ConfigValue::is_inverted()
bool ConfigValue::is_inverted() const
{
return this->has_characters("!");
}

bool ConfigValue::value_found() const {
return this->found;
}
14 changes: 7 additions & 7 deletions src/libs/ConfigValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ class ConfigValue{
ConfigValue& operator= (const ConfigValue& to_copy);
void clear();
ConfigValue* required();
float as_number();
int as_int();
bool as_bool();
string as_string();
float as_number() const;
int as_int() const;
bool as_bool() const;
string as_string() const;
bool value_found() const;

ConfigValue* by_default(float val);
ConfigValue* by_default(string val);
ConfigValue* by_default(int val);
bool is_inverted();

bool is_inverted() const;

friend class ConfigCache;
friend class Config;
Expand All @@ -37,7 +37,7 @@ class ConfigValue{
friend class FileConfigSource;

private:
bool has_characters( const char* mask );
bool has_characters( const char* mask ) const;
string value;
int default_int;
float default_double;
Expand Down

0 comments on commit d300742

Please sign in to comment.