Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlpropertycachevector_p.h
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#ifndef QQMLPROPERTYCACHEVECTOR_P_H
6#define QQMLPROPERTYCACHEVECTOR_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qqmlpropertycache_p.h>
20#include <private/qbipointer_p.h>
21
22#include <QtCore/qtaggedpointer.h>
23
24QT_BEGIN_NAMESPACE
25
26class QQmlPropertyCacheVector
27{
28public:
29 QQmlPropertyCacheVector() = default;
30 QQmlPropertyCacheVector(QQmlPropertyCacheVector &&) = default;
31 QQmlPropertyCacheVector &operator=(QQmlPropertyCacheVector &&) = default;
32
33 ~QQmlPropertyCacheVector() { clear(); }
34 void resize(int size)
35 {
36 Q_ASSERT(size >= data.size());
37 return data.resize(size);
38 }
39
40 int count() const {
41 // the property cache vector will never contain more thant INT_MAX many elements
42 return int(data.size());
43 }
44
45 bool isEmpty() const { return data.isEmpty(); }
46
47 void clear()
48 {
49 for (int i = 0; i < data.size(); ++i)
50 releaseElement(i);
51 data.clear();
52 }
53
54 void resetAndResize(int size)
55 {
56 for (int i = 0; i < data.size(); ++i) {
57 releaseElement(i);
58 data[i] = BiPointer();
59 }
60 data.resize(size);
61 }
62
63 void append(const QQmlPropertyCache::ConstPtr &cache) {
64 cache->addref();
65 data.append(BiPointer(cache.data()));
66 Q_ASSERT(data.last().isT1());
67 Q_ASSERT(data.size() <= std::numeric_limits<int>::max());
68 }
69
70 void appendOwn(const QQmlPropertyCache::Ptr &cache) {
71 cache->addref();
72 data.append(BiPointer(cache.data()));
73 Q_ASSERT(data.last().isT2());
74 Q_ASSERT(data.size() <= std::numeric_limits<int>::max());
75 }
76
77 QQmlPropertyCache::ConstPtr at(int index) const
78 {
79 const auto entry = data.at(index);
80 if (entry.isT2())
81 return entry.asT2();
82 return entry.asT1();
83 }
84
85 QQmlPropertyCache::Ptr ownAt(int index) const
86 {
87 const auto entry = data.at(index);
88 if (entry.isT2())
89 return entry.asT2();
90 return QQmlPropertyCache::Ptr();
91 }
92
93 void set(int index, const QQmlPropertyCache::ConstPtr &replacement) {
94 if (QQmlPropertyCache::ConstPtr oldCache = at(index)) {
95 // If it is our own, we keep it our own
96 if (replacement.data() == oldCache.data())
97 return;
98 oldCache->release();
99 }
100 data[index] = replacement.data();
101 if (replacement)
102 replacement->addref();
103 Q_ASSERT(data[index].isT1());
104 }
105
106 void setOwn(int index, const QQmlPropertyCache::Ptr &replacement) {
107 if (QQmlPropertyCache::ConstPtr oldCache = at(index)) {
108 if (replacement.data() != oldCache.data()) {
109 oldCache->release();
110 replacement->addref();
111 }
112 } else {
113 replacement->addref();
114 }
115 data[index] = replacement.data();
116 Q_ASSERT(data[index].isT2());
117 }
118
119 void setNeedsVMEMetaObject(int index) { data[index].setFlag(); }
120 bool needsVMEMetaObject(int index) const { return data.at(index).flag(); }
121
122 void seal()
123 {
124 for (auto &entry: data) {
125 if (entry.isT2())
126 entry = static_cast<const QQmlPropertyCache *>(entry.asT2());
127 Q_ASSERT(entry.isT1());
128 }
129 }
130
131private:
132 void releaseElement(int i)
133 {
134 const auto &cache = data.at(i);
135 if (cache.isT2()) {
136 if (QQmlPropertyCache *data = cache.asT2())
137 data->release();
138 } else if (const QQmlPropertyCache *data = cache.asT1()) {
139 data->release();
140 }
141 }
142
143 Q_DISABLE_COPY(QQmlPropertyCacheVector)
144 using BiPointer = QBiPointer<const QQmlPropertyCache, QQmlPropertyCache>;
145 QList<BiPointer> data;
146};
147
148QT_END_NAMESPACE
149
150#endif // QQMLPROPERTYCACHEVECTOR_P_H