20#ifndef LIBREPCB_CORE_SERIALIZABLEKEYVALUEMAP_H
21#define LIBREPCB_CORE_SERIALIZABLEKEYVALUEMAP_H
26#include "../exceptions.h"
27#include "../types/elementname.h"
28#include "../utils/signalslot.h"
33#include <optional.hpp>
74 const typename T::ValueType& defaultValue) noexcept
76 mValues.insert(
"", defaultValue);
91 tr(
"Key \"%1\" defined multiple times.").arg(key));
94 deserialize<typename T::ValueType>(
value));
96 if (!
mValues.contains(QString(
""))) {
98 tr(
"No default %1 defined.").arg(T::tagname));
106 auto i =
mValues.find(QString(
""));
108 Q_ASSERT((i !=
mValues.end()) && (i.key() == QString(
"")));
114 tl::optional<typename T::ValueType>
tryGet(
115 const QString& key)
const noexcept {
117 if ((i !=
mValues.end()) && (i.key() == key)) {
124 const QStringList& keyOrder, QString* usedKey =
nullptr) const noexcept {
126 foreach (
const QString& key, keyOrder) {
128 if ((i !=
mValues.end()) && (i.key() == key)) {
129 if (usedKey) *usedKey = key;
134 if (usedKey) *usedKey = QString(
"");
144 void insert(
const QString& key,
const typename T::ValueType&
value)
noexcept {
149 }
else if (it.value() !=
value) {
161 for (
auto i =
mValues.constBegin(); i !=
mValues.constEnd(); ++i) {
164 if (!i.key().isEmpty()) {
175 foreach (
const QString& key,
mValues.keys()) {
180 QMapIterator<QString, typename T::ValueType> i(rhs.mValues);
181 while (i.hasNext()) {
183 insert(i.key(), i.value());
204 static constexpr const char*
tagname =
"name";
205 static constexpr const char*
keyname =
"locale";
215 static constexpr const char*
tagname =
"description";
216 static constexpr const char*
keyname =
"locale";
227 static constexpr const char*
tagname =
"keywords";
228 static constexpr const char*
keyname =
"locale";
The RuntimeError class.
Definition: exceptions.h:218
The SExpression class.
Definition: sexpression.h:69
SExpression & appendList(const QString &name)
Definition: sexpression.cpp:212
QList< SExpression * > getChildren(Type type) noexcept
Definition: sexpression.cpp:94
bool isList() const noexcept
Definition: sexpression.h:93
const QString & getValue() const
Definition: sexpression.cpp:71
void ensureLineBreak()
Definition: sexpression.cpp:206
SExpression & getChild(int index)
Definition: sexpression.cpp:86
void appendChild(std::unique_ptr< SExpression > child)
Definition: sexpression.cpp:217
The SerializableKeyValueMap class provides an easy way to serialize and deserialize ordered key value...
Definition: serializablekeyvaluemap.h:56
SerializableKeyValueMap(const SExpression &node)
Definition: serializablekeyvaluemap.h:78
void insert(const QString &key, const typename T::ValueType &value) noexcept
Definition: serializablekeyvaluemap.h:144
const T::ValueType & getDefaultValue() const noexcept
Definition: serializablekeyvaluemap.h:105
SerializableKeyValueMap(const typename T::ValueType &defaultValue) noexcept
Definition: serializablekeyvaluemap.h:73
SerializableKeyValueMap< T > & operator=(const SerializableKeyValueMap< T > &rhs) noexcept
Definition: serializablekeyvaluemap.h:173
Slot< SerializableKeyValueMap< T >, const QString &, Event > OnEditedSlot
Definition: serializablekeyvaluemap.h:67
QStringList keys() const noexcept
Definition: serializablekeyvaluemap.h:104
Event
Definition: serializablekeyvaluemap.h:61
Signal< SerializableKeyValueMap< T >, const QString &, Event > onEdited
Definition: serializablekeyvaluemap.h:66
QMap< QString, typename T::ValueType > mValues
Definition: serializablekeyvaluemap.h:195
SerializableKeyValueMap(const SerializableKeyValueMap< T > &other) noexcept
Definition: serializablekeyvaluemap.h:71
bool contains(const QString &key) const noexcept
Definition: serializablekeyvaluemap.h:111
void serialize(SExpression &root) const
Serialize into librepcb::SExpression node.
Definition: serializablekeyvaluemap.h:160
tl::optional< typename T::ValueType > tryGet(const QString &key) const noexcept
Definition: serializablekeyvaluemap.h:114
bool operator==(const SerializableKeyValueMap< T > &rhs) const noexcept
Definition: serializablekeyvaluemap.h:187
SerializableKeyValueMap()=delete
bool operator!=(const SerializableKeyValueMap< T > &rhs) const noexcept
Definition: serializablekeyvaluemap.h:190
~SerializableKeyValueMap() noexcept
Definition: serializablekeyvaluemap.h:101
const T::ValueType & value(const QStringList &keyOrder, QString *usedKey=nullptr) const noexcept
Definition: serializablekeyvaluemap.h:123
void setDefaultValue(const typename T::ValueType &value) noexcept
Definition: serializablekeyvaluemap.h:140
The Signal class is used to emit signals on non-QObject derived classes.
Definition: signalslot.h:65
The Slot class is used to receive signals from non-QObject derived classes.
Definition: signalslot.h:170
Definition: occmodel.cpp:77
type_safe::constrained_type< QString, ElementNameConstraint, ElementNameVerifier > ElementName
Definition: elementname.h:84
Definition: serializablekeyvaluemap.h:213
static constexpr const char * keyname
Definition: serializablekeyvaluemap.h:216
QString ValueType
Definition: serializablekeyvaluemap.h:214
static constexpr const char * tagname
Definition: serializablekeyvaluemap.h:215
Definition: serializablekeyvaluemap.h:225
static constexpr const char * keyname
Definition: serializablekeyvaluemap.h:228
QString ValueType
Definition: serializablekeyvaluemap.h:226
static constexpr const char * tagname
Definition: serializablekeyvaluemap.h:227
Definition: serializablekeyvaluemap.h:202
static constexpr const char * keyname
Definition: serializablekeyvaluemap.h:205
static constexpr const char * tagname
Definition: serializablekeyvaluemap.h:204
ElementName ValueType
Definition: serializablekeyvaluemap.h:203