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:
56 Store(IBackend* backend, Schema schema, QObject* parent = nullptr);
57
58 ~Store() override;
59
80 template<typename T>
81 T read(const QString& group, const QString& key) const
82 {
83 static_assert(sizeof(T) == 0,
84 "PhosphorConfig::Store::read<T> is only implemented for QString, int, bool, double, "
85 "QColor, QVariantMap, QVariantList — extend store.cpp with a new specialization to add "
86 "another type.");
87 return T();
88 }
89
96 QVariant readVariant(const QString& group, const QString& key) const;
97
108 void write(const QString& group, const QString& key, const QVariant& value);
109
119 void reset(const QString& group, const QString& key);
120
126 void resetGroup(const QString& group);
127
129 void resetAll();
130
134 QJsonObject exportToJson() const;
135
140 QJsonObject defaultsToJson() const;
141
157 bool importFromJson(const QJsonObject& snapshot);
158
163 bool sync();
164
167 bool commit();
168
172
174 const Schema& schema() const;
175
176Q_SIGNALS:
179 void changed(const QString& group, const QString& key);
180
181private:
182 class Private;
183 std::unique_ptr<Private> d;
184};
185
186// ─ Supported read<T> specializations ────────────────────────────────────
187// read<T> is fully specialized in store.cpp for QString / int / bool /
188// double / QColor / QVariantMap / QVariantList — calls with any other T
189// will fail to link. Extend by adding a specialization there and
190// re-declaring it below so consumers see a declaration without a
191// primary-template body in the header.
192
193template<>
194PHOSPHORCONFIG_EXPORT QString Store::read<QString>(const QString&, const QString&) const;
195template<>
196PHOSPHORCONFIG_EXPORT int Store::read<int>(const QString&, const QString&) const;
197template<>
198PHOSPHORCONFIG_EXPORT bool Store::read<bool>(const QString&, const QString&) const;
199template<>
200PHOSPHORCONFIG_EXPORT double Store::read<double>(const QString&, const QString&) const;
201template<>
202PHOSPHORCONFIG_EXPORT QColor Store::read<QColor>(const QString&, const QString&) const;
203template<>
204PHOSPHORCONFIG_EXPORT QVariantMap Store::read<QVariantMap>(const QString&, const QString&) const;
205template<>
206PHOSPHORCONFIG_EXPORT QVariantList Store::read<QVariantList>(const QString&, const QString&) const;
207
208} // namespace PhosphorConfig
Pluggable configuration backend.
Definition IBackend.h:151
High-level declarative configuration facade.
Definition Store.h:46
QJsonObject defaultsToJson() const
Produce a JSON snapshot of every declared key's SCHEMA DEFAULT, independent of current values.
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.
bool commit()
Durable flush: the backend's commit(), which never defers.
T read(const QString &group, const QString &key) const
Read a declared key.
Definition Store.h:81
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:127