QmlMaterial 0.1.0
Loading...
Searching...
No Matches
item_snapshot.hpp
1#pragma once
2
3#include <QString>
4#include <QVariant>
5#include <QVector>
6#include <memory>
7#include <optional>
8#include "qml_material/export.hpp"
9
10namespace qml_material
11{
12struct ItemDescriptor {
13 QString key;
14 QString reuseType;
15 quint64 contentRevision = 0;
16 bool operator==(const ItemDescriptor&) const = default;
17};
18
19class QML_MATERIAL_API ItemDataSnapshot {
20public:
21 virtual ~ItemDataSnapshot();
22 virtual int count() const = 0;
23 virtual bool stableKeys() const { return true; }
24 virtual ItemDescriptor descriptorAt(int index) const = 0;
25 virtual std::optional<int> indexOfKey(const QString& key) const = 0;
26 virtual QVariant valueAt(int index) const = 0;
27};
28using ItemDataSnapshotPtr = std::shared_ptr<const ItemDataSnapshot>;
29
30template<class T>
31class TypedItemDataSnapshot : public ItemDataSnapshot {
32public:
33 virtual T itemAt(int index) const = 0;
34 QVariant valueAt(int index) const final { return QVariant::fromValue(itemAt(index)); }
35};
36
37struct ItemRecord {
38 QString key;
39 QString reuseType;
40 QVariant value;
41 quint64 contentRevision = 0;
42};
43
44struct ItemValueUpdate {
45 int index = -1;
46 QVariant value;
47 std::optional<QString> reuseType;
48 quint64 contentRevision = 0;
49};
50
51class ItemSnapshotRows;
52
53class QML_MATERIAL_API ItemSnapshot {
54public:
55 ItemSnapshot();
56 const quint64 revision;
57 int count() const { return m_count; }
58 bool stableKeys() const { return m_data && m_data->stableKeys(); }
59 const ItemDescriptor& descriptorAt(int index) const;
60 int indexOfKey(const QString& key) const;
61 QVariant valueAt(int index) const;
62 // Whether both snapshots retain the same immutable value storage.
63 bool sharesValueAt(int index, const ItemSnapshot&, int otherIndex) const;
64 ItemRecord recordAt(int index) const;
65
66private:
67 friend class ItemSource;
68 ItemSnapshot(quint64, ItemDataSnapshotPtr, QVector<ItemDescriptor>);
69 ItemSnapshot(quint64, const ItemSnapshot&, const QVector<ItemValueUpdate>&);
70 ItemDataSnapshotPtr m_data;
71 std::shared_ptr<const ItemSnapshotRows> m_rows;
72 int m_count = 0;
73};
74using ItemSnapshotPtr = std::shared_ptr<const ItemSnapshot>;
75
76QML_MATERIAL_API ItemDataSnapshotPtr makeItemDataSnapshot(QVector<ItemRecord> rows,
77 bool stableKeys = true,
78 const ItemSnapshotPtr& before = {});
79} // namespace qml_material