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:
@@ -9,30 +9,35 @@ class SettingsObjectData
|
|||||||
public:
|
public:
|
||||||
template <typename Value> void addValue( const Value value )
|
template <typename Value> void addValue( const Value value )
|
||||||
{
|
{
|
||||||
std::list<Value>& list = getListObject<Value>();
|
auto& list = getListObject<Value>();
|
||||||
|
|
||||||
list.push_back( value );
|
list.push_back( value );
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Value>
|
template <typename Value>
|
||||||
Value getNextValueOrDefault( const Value defaultValue )
|
auto getNextValueOrDefault( const Value defaultValue )
|
||||||
{
|
{
|
||||||
Value returnedValue = defaultValue;
|
auto& list = getListObject<Value>();
|
||||||
|
|
||||||
std::list<Value>& list = getListObject<Value>();
|
|
||||||
|
|
||||||
if ( hasValuesOfType<Value>() )
|
if ( hasValuesOfType<Value>() )
|
||||||
{
|
{
|
||||||
returnedValue = list.front();
|
auto returnedValue = list.front();
|
||||||
list.pop_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()
|
template <typename Value> bool hasValuesOfType()
|
||||||
{
|
{
|
||||||
std::list<Value>& list = getListObject<Value>();
|
auto& list = getListObject<Value>();
|
||||||
|
|
||||||
return !list.empty();
|
return !list.empty();
|
||||||
}
|
}
|
||||||
@@ -48,13 +53,14 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template <typename Value> std::list<Value>& getListObject()
|
template <typename Value> auto& getListObject()
|
||||||
{
|
{
|
||||||
using std::is_same;
|
using std::is_same;
|
||||||
const auto isBool = is_same<bool, Value>::value;
|
const auto isBool = is_same<bool, Value>::value;
|
||||||
const auto isInt = is_same<int, Value>::value;
|
const auto isInt = is_same<int, Value>::value;
|
||||||
const auto isDouble = is_same<double, 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,
|
static_assert( isBool || isInt || isDouble || isString,
|
||||||
"Type is not supported for the settings object." );
|
"Type is not supported for the settings object." );
|
||||||
|
|||||||
Reference in New Issue
Block a user