Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
Store.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include <phosphorconfig_export.h>
7
8#include <QColor>
9#include <QJsonObject>
10#include <QObject>
11#include <QString>
12#include <QVariant>
13#include <QVariantMap>
14
15#include <memory>
16
17namespace PhosphorConfig {
18
19class IBackend;
20struct Schema;
21
45class PHOSPHORCONFIG_EXPORT Store : public QObject
46{
47 Q_OBJECT
48
49public:
55 Store(IBackend* backend, Schema schema, QObject* parent = nullptr);
56
57 ~Store() override;
58
79 template<typename T>
80 T read(const QString& group, const QString& key) const
81 {
82 static_assert(sizeof(T) == 0,
83 "PhosphorConfig::Store::read<T> is only implemented for QString, int, bool, double, "
84 "QColor, QVariantMap, QVariantList — extend store.cpp with a new specialization to add "
85 "another type.");
86 return T();
87 }
88
92 QVariant readVariant(const QString& group, const QString& key) const;
93
97 void write(const QString& group, const QString& key, const QVariant& value);
98
108 void reset(const QString& group, const QString& key);
109
111 void resetGroup(const QString& group);
112
114 void resetAll();
115
119 QJsonObject exportToJson() const;
120
136 bool importFromJson(const QJsonObject& snapshot);
137
140 bool sync();
141
145
147 const Schema& schema() const;
148
149Q_SIGNALS:
152 void changed(const QString& group, const QString& key);
153
154private:
155 class Private;
156 std::unique_ptr<Private> d;
157};
158
159// ─ Supported read<T> specializations ────────────────────────────────────
160// read<T> is fully specialized in store.cpp for QString / int / bool /
161// double / QColor / QVariantMap / QVariantList — calls with any other T
162// will fail to link. Extend by adding a specialization there and
163// re-declaring it below so consumers see a declaration without a
164// primary-template body in the header.
165
166template<>
167PHOSPHORCONFIG_EXPORT QString Store::read<QString>(const QString&, const QString&) const;
168template<>
169PHOSPHORCONFIG_EXPORT int Store::read<int>(const QString&, const QString&) const;
170template<>
171PHOSPHORCONFIG_EXPORT bool Store::read<bool>(const QString&, const QString&) const;
172template<>
173PHOSPHORCONFIG_EXPORT double Store::read<double>(const QString&, const QString&) const;
174template<>
175PHOSPHORCONFIG_EXPORT QColor Store::read<QColor>(const QString&, const QString&) const;
176template<>
177PHOSPHORCONFIG_EXPORT QVariantMap Store::read<QVariantMap>(const QString&, const QString&) const;
178template<>
179PHOSPHORCONFIG_EXPORT QVariantList Store::read<QVariantList>(const QString&, const QString&) const;
180
181} // namespace PhosphorConfig
Pluggable configuration backend.
Definition IBackend.h:150
High-level declarative configuration facade.
Definition Store.h:46
IBackend * backend() const
Direct access for callers that still need the backend (e.g.
QJsonObject exportToJson() const
Produce a JSON snapshot of every declared key's current value.
Store(IBackend *backend, Schema schema, QObject *parent=nullptr)
Construct a store that borrows backend and applies schema.
const Schema & schema() const
The schema the store was constructed with.
void resetGroup(const QString &group)
Reset every key declared in group. Undeclared extras are left alone.
T read(const QString &group, const QString &key) const
Read a declared key.
Definition Store.h:80
void reset(const QString &group, const QString &key)
Reset one key to its schema default.
void changed(const QString &group, const QString &key)
Emitted after any successful write() or reset() operation.
void resetAll()
Reset every declared key in every declared group. Extras are untouched.
bool sync()
Flush the underlying backend.
bool importFromJson(const QJsonObject &snapshot)
Overwrite declared keys from snapshot.
void write(const QString &group, const QString &key, const QVariant &value)
Write a value.
QVariant readVariant(const QString &group, const QString &key) const
Read as QVariant.
Definition IBackend.h:19
Declarative description of a configuration store.
Definition Schema.h:89