Enable settings object to work with 'const char*' strings

Passing a C style string (const char*) to any of the templated functions
would lead to a compile error. It was necessary to wrap the raw string
in a std::string call, which is obviously not ideal.

Using automatic type deduction was necessary because `std::vector<const
char*` is not the same as `std::vector<std::string>`.
This commit is contained in:
Vasily
2020-01-30 10:36:21 +01:00
committed by username223
parent 15b4d17672
commit bcbc32c812
+17 -11
View File
@@ -9,30 +9,35 @@ class SettingsObjectData
public:
template <typename Value> void addValue( const Value value )
{
std::list<Value>& list = getListObject<Value>();
auto& list = getListObject<Value>();
list.push_back( value );
}
template <typename Value>
Value getNextValueOrDefault( const Value defaultValue )
auto getNextValueOrDefault( const Value defaultValue )
{
Value returnedValue = defaultValue;
std::list<Value>& list = getListObject<Value>();
auto& list = getListObject<Value>();
if ( hasValuesOfType<Value>() )
{
returnedValue = list.front();
auto returnedValue = list.front();
list.pop_front();
return returnedValue;
}
if constexpr ( std::is_same<const char*, Value>::value )
{
return std::string( defaultValue );
}
else
{
return defaultValue;
}
return returnedValue;
}
template <typename Value> bool hasValuesOfType()
{
std::list<Value>& list = getListObject<Value>();
auto& list = getListObject<Value>();
return !list.empty();
}
@@ -48,13 +53,14 @@ public:
}
private:
template <typename Value> std::list<Value>& getListObject()
template <typename Value> auto& getListObject()
{
using std::is_same;
const auto isBool = is_same<bool, Value>::value;
const auto isInt = is_same<int, Value>::value;
const auto isDouble = is_same<double, Value>::value;
const auto isString = is_same<std::string, Value>::value;
const auto isString = is_same<std::string, Value>::value
|| is_same<const char*, Value>::value;
static_assert( isBool || isInt || isDouble || isString,
"Type is not supported for the settings object." );