Repository navigation
Expand file tree
/
Copy pathAttributeRegistry.cpp
More file actions
65 lines (56 loc) · 1.73 KB
/
Copy pathAttributeRegistry.cpp
File metadata and controls
65 lines (56 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "AttributeRegistry.h"
#include <QJsonArray>
#include <QDebug>
// ─────────────────────────────────────────────
// AttributeRegistry (Singleton)
// ─────────────────────────────────────────────
AttributeRegistry& AttributeRegistry::instance()
{
static AttributeRegistry s_instance;
return s_instance;
}
void AttributeRegistry::registerAttribute(const Attribute& attr)
{
if (attr.key.isEmpty())
{
qWarning() << "AttributeRegistry: attempted to register attribute with empty key – ignored.";
return;
}
m_attributes[attr.key] = attr;
}
bool AttributeRegistry::hasAttribute(const QString& key) const
{
return m_attributes.contains(key);
}
Attribute AttributeRegistry::getAttribute(const QString& key) const
{
Q_ASSERT_X(m_attributes.contains(key), "AttributeRegistry::getAttribute",
qPrintable("Unknown attribute key: " + key));
return m_attributes.value(key);
}
QList<Attribute> AttributeRegistry::allAttributes() const
{
return m_attributes.values();
}
void AttributeRegistry::removeAttribute(const QString& key)
{
m_attributes.remove(key);
}
QJsonArray AttributeRegistry::saveToJson() const
{
QJsonArray arr;
for (const Attribute& attr : m_attributes)
arr.append(attr.saveToJson());
return arr;
}
void AttributeRegistry::loadFromJson(const QJsonArray& arr)
{
m_attributes.clear();
for (const QJsonValue& val : arr)
{
Attribute attr;
attr.loadFromJson(val.toObject());
if (!attr.key.isEmpty())
m_attributes[attr.key] = attr;
}
}