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
51class PHOSPHORCONFIG_EXPORT Store : public QObject
52{
53 Q_OBJECT
54
55public:
62 Store(IBackend* backend, Schema schema, QObject* parent = nullptr);
63
64 ~Store() override;
65
86 template<typename T>
87 T read(const QString& group, const QString& key) const
88 {
89 static_assert(sizeof(T) == 0,
90 "PhosphorConfig::Store::read<T> is only implemented for QString, int, bool, double, "
91 "QColor, QVariantMap, QVariantList — extend store.cpp with a new specialization to add "
92 "another type.");
93 return T();
94 }
95
102 QVariant readVariant(const QString& group, const QString& key) const;
103
119 bool hasExplicitValue(const QString& group, const QString& key) const;
120
138 void write(const QString& group, const QString& key, const QVariant& value);
139
149 void reset(const QString& group, const QString& key);
150
156 void resetGroup(const QString& group);
157
159 void resetAll();
160
165 QJsonObject exportToJson() const;
166
171 QJsonObject defaultsToJson() const;
172
188 bool importFromJson(const QJsonObject& snapshot);
189
194 bool sync();
195
198 bool commit();
199
203
205 const Schema& schema() const;
206
207Q_SIGNALS:
210 void changed(const QString& group, const QString& key);
211
212private:
213 class Private;
214 std::unique_ptr<Private> d;
215};
216
217// ─ Supported read<T> specializations ────────────────────────────────────
218// read<T> is fully specialized in store.cpp for QString / int / bool /
219// double / QColor / QVariantMap / QVariantList — calls with any other T
220// will fail to link. Extend by adding a specialization there and
221// re-declaring it below so consumers see a declaration without a
222// primary-template body in the header.
223
224template<>
225PHOSPHORCONFIG_EXPORT QString Store::read<QString>(const QString&, const QString&) const;
226template<>
227PHOSPHORCONFIG_EXPORT int Store::read<int>(const QString&, const QString&) const;
228template<>
229PHOSPHORCONFIG_EXPORT bool Store::read<bool>(const QString&, const QString&) const;
230template<>
231PHOSPHORCONFIG_EXPORT double Store::read<double>(const QString&, const QString&) const;
232template<>
233PHOSPHORCONFIG_EXPORT QColor Store::read<QColor>(const QString&, const QString&) const;
234template<>
235PHOSPHORCONFIG_EXPORT QVariantMap Store::read<QVariantMap>(const QString&, const QString&) const;
236template<>
237PHOSPHORCONFIG_EXPORT QVariantList Store::read<QVariantList>(const QString&, const QString&) const;
238
239} // namespace PhosphorConfig
Pluggable configuration backend.
Definition IBackend.h:151
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 hasExplicitValue(const QString &group, const QString &key) const
Whether key is physically stored, as opposed to being served from the schema default.
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:87
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 HostDirectory.h:19
Declarative description of a configuration store.
Definition Schema.h:127