Phosphor
Qt6 / Wayland library suite for window-management tools
Loading...
Searching...
No Matches
PathKeyedOverrides.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 <QHash>
7#include <QString>
8#include <QStringList>
9
10namespace PhosphorRegistry {
11
54template<typename Payload>
56{
57public:
58 // ─────── Baseline ───────
59
60 const Payload& baseline() const
61 {
62 return m_baseline;
63 }
64
65 void setBaseline(const Payload& payload)
66 {
67 m_baseline = payload;
68 }
69
70 // ─────── Overrides ───────
71
72 bool hasOverride(const QString& key) const
73 {
74 return m_overrides.contains(key);
75 }
76
80 Payload directOverride(const QString& key) const
81 {
82 return m_overrides.value(key);
83 }
84
98 const Payload* findOverride(const QString& key) const
99 {
100 const auto it = m_overrides.constFind(key);
101 return it == m_overrides.constEnd() ? nullptr : &it.value();
102 }
103
105 const QStringList& keys() const
106 {
107 return m_insertionOrder;
108 }
109
116 void setOverride(const QString& key, const Payload& payload)
117 {
118 if (key.isEmpty()) {
119 return;
120 }
121 if (!m_overrides.contains(key)) {
122 m_insertionOrder.append(key);
123 }
124 m_overrides.insert(key, payload);
125 }
126
135 bool clearOverride(const QString& key)
136 {
137 if (m_overrides.remove(key) == 0) {
138 return false;
139 }
140 m_insertionOrder.removeAll(key);
141 return true;
142 }
143
145 {
146 m_overrides.clear();
147 m_insertionOrder.clear();
148 }
149
155 bool hasOverrides() const
156 {
157 return !m_overrides.isEmpty();
158 }
159
160 // ─────── Ordered traversal ───────
161
174 template<typename Fn>
175 void forEachInOrder(Fn fn) const
176 {
177 for (const QString& key : m_insertionOrder) {
178 const auto it = m_overrides.constFind(key);
179 if (it == m_overrides.constEnd()) {
180 // Unreachable in practice: every mutator above writes the map and
181 // the order list together, and they are the complete set. Kept as
182 // a cheap guard rather than an assert, because a desync here would
183 // only drop the orphaned entry rather than corrupt anything.
184 continue;
185 }
186 fn(key, *it);
187 }
188 }
189
190 // ─────── Equality pieces ───────
191 //
192 // Deliberately not an operator==. The trees disagree about whether insertion
193 // ORDER is part of identity: the three ARRAY-form trees (the motion and shader
194 // trees in phosphor-animation, and the decoration one) compare it, because the
195 // order is observable on their wire format; the overlay tree is order-free by
196 // construction, because every ordered view it exposes is sorted and its overrides
197 // serialise as a key-addressed object. Handing each of them the piece it needs
198 // keeps that a stated decision rather than something a shared operator quietly
199 // picks for all four.
200
201 bool sameBaseline(const PathKeyedOverrides& other) const
202 {
203 return m_baseline == other.m_baseline;
204 }
205
207 bool sameOverrides(const PathKeyedOverrides& other) const
208 {
209 if (m_overrides.size() != other.m_overrides.size()) {
210 return false;
211 }
212 for (auto it = m_overrides.constBegin(); it != m_overrides.constEnd(); ++it) {
213 const auto mine = other.m_overrides.constFind(it.key());
214 if (mine == other.m_overrides.constEnd() || !(*mine == it.value())) {
215 return false;
216 }
217 }
218 return true;
219 }
220
221 bool sameKeyOrder(const PathKeyedOverrides& other) const
222 {
223 return m_insertionOrder == other.m_insertionOrder;
224 }
225
226private:
227 Payload m_baseline;
228 QHash<QString, Payload> m_overrides;
229 QStringList m_insertionOrder;
230};
231
232} // namespace PhosphorRegistry
A baseline payload plus keyed overrides, in insertion order.
Definition PathKeyedOverrides.h:56
bool sameOverrides(const PathKeyedOverrides &other) const
Compares the overrides as a SET: same keys, same payloads, order ignored.
Definition PathKeyedOverrides.h:207
bool hasOverride(const QString &key) const
Definition PathKeyedOverrides.h:72
const QStringList & keys() const
Every overridden key, in the order it was first set.
Definition PathKeyedOverrides.h:105
bool clearOverride(const QString &key)
Definition PathKeyedOverrides.h:135
const Payload & baseline() const
Definition PathKeyedOverrides.h:60
bool sameKeyOrder(const PathKeyedOverrides &other) const
Definition PathKeyedOverrides.h:221
Payload directOverride(const QString &key) const
The override stored at key, or a default-constructed payload when there is none.
Definition PathKeyedOverrides.h:80
bool sameBaseline(const PathKeyedOverrides &other) const
Definition PathKeyedOverrides.h:201
bool hasOverrides() const
True when at least one override is present.
Definition PathKeyedOverrides.h:155
void forEachInOrder(Fn fn) const
Invoke fn(key, payload) for every override, in insertion order.
Definition PathKeyedOverrides.h:175
void setBaseline(const Payload &payload)
Definition PathKeyedOverrides.h:65
const Payload * findOverride(const QString &key) const
A POINTER to the override at key, or nullptr when there is none.
Definition PathKeyedOverrides.h:98
void clearAllOverrides()
Definition PathKeyedOverrides.h:144
void setOverride(const QString &key, const Payload &payload)
Store payload at key.
Definition PathKeyedOverrides.h:116
Definition IBarWidgetFactory.h:16