30 #ifndef KEYVALUECOLLECTION_H
31 #define KEYVALUECOLLECTION_H
83 std::string
get(
const std::string& name)
const;
90 bool getBool(
const std::string& name)
const;
97 QColor
getColor(
const std::string& name)
const;
104 QString
getQString(
const std::string& name)
const;
111 double getDouble(
const std::string& name)
const;
118 int getInt(
const std::string& name)
const;
127 template<
typename ValueType,
int N>
128 std::array<ValueType, N>
getValues(
const std::string& name)
const
130 static_assert(std::is_same<ValueType, double>::value
131 || std::is_same<ValueType, int>::value,
132 "Invalid template type argument for getValues");
133 static_assert(N > 0,
"Invalid template integer argument for getValues");
136 setlocale(LC_NUMERIC,
"C");
138 std::array<ValueType, N> values;
139 QStringList parts =
getQString(name).split(
';');
140 if (parts.size() != N)
143 if (parts.size() != N)
146 for (
int i = 0; i < N; ++i)
152 assert(parts.size() == N);
153 for (
int i = 0; i < N; i++)
155 std::string current = parts.at(i).toStdString();
158 if (std::is_same<ValueType, int>::value)
160 values[i] = std::stoi(current);
162 else if (std::is_same<ValueType, double>::value)
164 values[i] = std::stod(current);
167 catch (
const std::invalid_argument&)
169 qDebug() <<
"The key"
170 << QString::fromStdString(name)
171 <<
"was not convertible to numeric value:"
172 << QString::fromStdString(current)
176 catch (
const std::out_of_range&)
178 qDebug() <<
"The key"
179 << QString::fromStdString(name)
180 <<
"was out of range:"
181 << QString::fromStdString(current)
194 void set(
const std::string& name,
const std::string& value);
201 void setBool(
const std::string& name,
bool value);
208 void setColor(
const std::string& name,
const QColor& value);
215 void setInt(
const std::string& name,
int value);
224 template<
typename... T>
227 auto vals = {values...};
229 std::string separator;
232 std::string piece = std::to_string(v);
236 std::replace(piece.begin(), piece.end(),
',',
'.');
238 str += separator + piece;
249 void setQString(
const std::string& name,
const QString& value);
257 bool trySet(
const std::string& name,
const std::string& value);
261 #endif // KEYVALUECOLLECTION_H
QColor getColor(const std::string &name) const
Gets the value of the specified key as a QColor.
Definition: keyvaluecollection.cpp:78
double getDouble(const std::string &name) const
Gets the value of the specified key as a double.
Definition: keyvaluecollection.cpp:91
void setBool(const std::string &name, bool value)
Sets the value of the specified key as a bool.
Definition: keyvaluecollection.cpp:119
const_iterator begin() const
Returns a const iterator to the beginning of the collection.
Definition: keyvaluecollection.cpp:38
void setColor(const std::string &name, const QColor &value)
Sets the value of the specified key as a QColor.
Definition: keyvaluecollection.cpp:114
Represents a collection of key-value pairs.
Definition: keyvaluecollection.h:51
void setInt(const std::string &name, int value)
Sets the value of the specified key as an int.
Definition: keyvaluecollection.cpp:131
std::map< std::string, std::string > keyValueMap
Contains the key-value mappings in an std::map.
Definition: keyvaluecollection.h:57
std::array< ValueType, N > getValues(const std::string &name) const
Gets an array of values from the specified key.
Definition: keyvaluecollection.h:128
void setQString(const std::string &name, const QString &value)
Sets the value of the specified key as a QString.
Definition: keyvaluecollection.cpp:136
bool trySet(const std::string &name, const std::string &value)
Attempts to set the value of the specified key.
Definition: keyvaluecollection.cpp:142
int getInt(const std::string &name) const
Gets the value of the specified key as an integer.
Definition: keyvaluecollection.cpp:96
bool getBool(const std::string &name) const
Gets the value of the specified key as a bool.
Definition: keyvaluecollection.cpp:61
void set(const std::string &name, const std::string &value)
Sets the value of the specified key.
Definition: keyvaluecollection.cpp:101
void setValues(const std::string &name, T...values)
Sets the value of the specified key as an array.
Definition: keyvaluecollection.h:225
const_iterator end() const
Returns a const iterator to the end of the collection.
Definition: keyvaluecollection.cpp:43
std::map< std::string, std::string >::const_iterator const_iterator
Provides a bidirectional iterator that can read a const element in the collection.
Definition: keyvaluecollection.h:64
QString getQString(const std::string &name) const
Gets the value of the specified key as a QString.
Definition: keyvaluecollection.cpp:86