File: | /home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp |
Warning: | line 167, column 9 Use of memory after it is freed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* ============================================================ | ||||
2 | * | ||||
3 | * This file is a part of digiKam project | ||||
4 | * https://www.digikam.org | ||||
5 | * | ||||
6 | * Date : 2023-05-15 | ||||
7 | * Description : geolocation engine based on Marble. | ||||
8 | * (c) 2007-2022 Marble Team | ||||
9 | * https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html | ||||
10 | * | ||||
11 | * SPDX-FileCopyrightText: 2023-2024 by Gilles Caulier <caulier dot gilles at gmail dot com> | ||||
12 | * | ||||
13 | * SPDX-License-Identifier: LGPL-2.1-or-later | ||||
14 | * | ||||
15 | * ============================================================ */ | ||||
16 | |||||
17 | #include "ElevationModel.h" | ||||
18 | |||||
19 | // Qt includes | ||||
20 | |||||
21 | #include <QCache> | ||||
22 | #include <QImage> | ||||
23 | #include <qmath.h> | ||||
24 | |||||
25 | // Local includes | ||||
26 | |||||
27 | #include "GeoSceneHead.h" | ||||
28 | #include "GeoSceneLayer.h" | ||||
29 | #include "GeoSceneMap.h" | ||||
30 | #include "GeoSceneDocument.h" | ||||
31 | #include "GeoSceneTextureTileDataset.h" | ||||
32 | #include "HttpDownloadManager.h" | ||||
33 | #include "Tile.h" | ||||
34 | #include "TileLoader.h" | ||||
35 | #include "TileLoaderHelper.h" | ||||
36 | #include "MapThemeManager.h" | ||||
37 | #include "TileId.h" | ||||
38 | #include "PluginManager.h" | ||||
39 | #include "digikam_debug.h" | ||||
40 | |||||
41 | namespace Marble | ||||
42 | { | ||||
43 | |||||
44 | class Q_DECL_HIDDEN__attribute__((visibility("hidden"))) ElevationModelPrivate | ||||
45 | { | ||||
46 | public: | ||||
47 | |||||
48 | ElevationModelPrivate(ElevationModel* _q, HttpDownloadManager* downloadManager, PluginManager* pluginManager) | ||||
49 | : q(_q), | ||||
50 | m_tileLoader(downloadManager, pluginManager), | ||||
51 | m_textureLayer(nullptr), | ||||
52 | m_srtmTheme(nullptr) | ||||
53 | { | ||||
54 | m_cache.setMaxCost(10); //keep 10 tiles in memory (~17MB) | ||||
55 | |||||
56 | m_srtmTheme = MapThemeManager::loadMapTheme(QString::fromUtf8("earth/srtm2/srtm2.dgml")); | ||||
57 | |||||
58 | if (!m_srtmTheme) | ||||
59 | { | ||||
60 | qCDebug(DIGIKAM_MARBLE_LOG)for (QLoggingCategoryMacroHolder<QtDebugMsg> qt_category ((DIGIKAM_MARBLE_LOG)()); qt_category; qt_category.control = false ) QMessageLogger(static_cast<const char *>("/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" ), 60, static_cast<const char *>(__PRETTY_FUNCTION__), qt_category .name()).debug() << "Failed to load map theme earth/srtm2/srtm2.dgml. Check your installation. No elevation will be returned."; | ||||
61 | return; | ||||
62 | } | ||||
63 | |||||
64 | const GeoSceneHead* head = m_srtmTheme->head(); | ||||
65 | Q_ASSERT(head)((head) ? static_cast<void>(0) : qt_assert("head", "/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" , 65)); | ||||
66 | |||||
67 | const GeoSceneMap* map = m_srtmTheme->map(); | ||||
68 | Q_ASSERT(map)((map) ? static_cast<void>(0) : qt_assert("map", "/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" , 68)); | ||||
69 | |||||
70 | const GeoSceneLayer* sceneLayer = map->layer(head->theme()); | ||||
71 | |||||
72 | if (!sceneLayer) | ||||
73 | { | ||||
74 | qCDebug(DIGIKAM_MARBLE_LOG)for (QLoggingCategoryMacroHolder<QtDebugMsg> qt_category ((DIGIKAM_MARBLE_LOG)()); qt_category; qt_category.control = false ) QMessageLogger(static_cast<const char *>("/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" ), 74, static_cast<const char *>(__PRETTY_FUNCTION__), qt_category .name()).debug() << "Failed to instantiate elevation map. No elevation will be returned."; | ||||
75 | return; | ||||
76 | } | ||||
77 | |||||
78 | Q_ASSERT(sceneLayer)((sceneLayer) ? static_cast<void>(0) : qt_assert("sceneLayer" , "/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" , 78)); | ||||
79 | |||||
80 | m_textureLayer = dynamic_cast<GeoSceneTextureTileDataset*>(sceneLayer->datasets().first()); | ||||
81 | Q_ASSERT(m_textureLayer)((m_textureLayer) ? static_cast<void>(0) : qt_assert("m_textureLayer" , "/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" , 81)); | ||||
82 | } | ||||
83 | |||||
84 | ~ElevationModelPrivate() | ||||
85 | { | ||||
86 | delete m_srtmTheme; | ||||
87 | } | ||||
88 | |||||
89 | void tileCompleted(const TileId& tileId, const QImage& image) | ||||
90 | { | ||||
91 | m_cache.insert(tileId, new QImage(image)); | ||||
92 | Q_EMIT q->updateAvailable(); | ||||
93 | } | ||||
94 | |||||
95 | public: | ||||
96 | |||||
97 | ElevationModel* q = nullptr; | ||||
98 | |||||
99 | TileLoader m_tileLoader; | ||||
100 | const GeoSceneTextureTileDataset* m_textureLayer = nullptr; | ||||
101 | QCache<TileId, const QImage> m_cache; | ||||
102 | GeoSceneDocument* m_srtmTheme = nullptr; | ||||
103 | }; | ||||
104 | |||||
105 | ElevationModel::ElevationModel(HttpDownloadManager* downloadManager, PluginManager* pluginManager, QObject* parent) : | ||||
106 | QObject(parent), | ||||
107 | d(new ElevationModelPrivate(this, downloadManager, pluginManager)) | ||||
108 | { | ||||
109 | connect(&d->m_tileLoader, SIGNAL(tileCompleted(TileId, QImage))qFlagLocation("2" "tileCompleted(TileId, QImage)" "\0" "/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" ":" "109"), | ||||
110 | this, SLOT(tileCompleted(TileId, QImage))qFlagLocation("1" "tileCompleted(TileId, QImage)" "\0" "/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" ":" "110")); | ||||
111 | } | ||||
112 | |||||
113 | ElevationModel::~ElevationModel() | ||||
114 | { | ||||
115 | delete d; | ||||
116 | } | ||||
117 | |||||
118 | |||||
119 | qreal ElevationModel::height(qreal lon, qreal lat) const | ||||
120 | { | ||||
121 | if (!d->m_textureLayer
| ||||
122 | { | ||||
123 | return invalidElevationData; | ||||
124 | } | ||||
125 | |||||
126 | const int tileZoomLevel = TileLoader::maximumTileLevel(*(d->m_textureLayer)); | ||||
127 | Q_ASSERT(tileZoomLevel == 9)((tileZoomLevel == 9) ? static_cast<void>(0) : qt_assert ("tileZoomLevel == 9", "/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" , 127)); | ||||
128 | |||||
129 | const int width = d->m_textureLayer->tileSize().width(); | ||||
130 | const int height = d->m_textureLayer->tileSize().height(); | ||||
131 | |||||
132 | const int numTilesX = TileLoaderHelper::levelToColumn(d->m_textureLayer->levelZeroColumns(), tileZoomLevel); | ||||
133 | const int numTilesY = TileLoaderHelper::levelToRow(d->m_textureLayer->levelZeroRows(), tileZoomLevel); | ||||
134 | Q_ASSERT(numTilesX > 0)((numTilesX > 0) ? static_cast<void>(0) : qt_assert( "numTilesX > 0", "/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" , 134)); | ||||
135 | Q_ASSERT(numTilesY > 0)((numTilesY > 0) ? static_cast<void>(0) : qt_assert( "numTilesY > 0", "/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" , 135)); | ||||
136 | |||||
137 | qreal textureX = 180 + lon; | ||||
138 | textureX *= numTilesX * width / 360; | ||||
139 | |||||
140 | qreal textureY = 90 - lat; | ||||
141 | textureY *= numTilesY * height / 180; | ||||
142 | |||||
143 | qreal ret = 0; | ||||
144 | bool hasHeight = false; | ||||
145 | qreal noData = 0; | ||||
146 | |||||
147 | for (int i = 0; i < 4; ++i) | ||||
148 | { | ||||
149 | const int x = static_cast<int>(textureX + (i % 2)); | ||||
150 | const int y = static_cast<int>(textureY + (i / 2)); | ||||
151 | |||||
152 | //qCDebug(DIGIKAM_MARBLE_LOG) << "x" << x << ( x / width ); | ||||
153 | //qCDebug(DIGIKAM_MARBLE_LOG) << "y" << y << ( y / height ); | ||||
154 | |||||
155 | const TileId id(0, tileZoomLevel, (x % (numTilesX * width)) / width, (y % (numTilesY * height)) / height); | ||||
156 | //qCDebug(DIGIKAM_MARBLE_LOG) << "LAT" << lat << "LON" << lon << "tile" << ( x % ( numTilesX * width ) ) / width << ( y % ( numTilesY * height ) ) / height; | ||||
157 | |||||
158 | const QImage* image = d->m_cache[id]; | ||||
159 | |||||
160 | if (image == nullptr) | ||||
161 | { | ||||
162 | image = new QImage(d->m_tileLoader.loadTileImage(d->m_textureLayer, id, DownloadBrowse)); | ||||
163 | d->m_cache.insert(id, image); | ||||
164 | } | ||||
165 | |||||
166 | Q_ASSERT(image)((image) ? static_cast<void>(0) : qt_assert("image", "/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" , 166)); | ||||
167 | Q_ASSERT(!image->isNull())((!image->isNull()) ? static_cast<void>(0) : qt_assert ("!image->isNull()", "/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" , 167)); | ||||
| |||||
168 | Q_ASSERT(width == image->width())((width == image->width()) ? static_cast<void>(0) : qt_assert ("width == image->width()", "/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" , 168)); | ||||
169 | Q_ASSERT(height == image->height())((height == image->height()) ? static_cast<void>(0) : qt_assert("height == image->height()", "/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" , 169)); | ||||
170 | |||||
171 | const qreal dx = (textureX > (qreal)x) ? textureX - (qreal)x : (qreal)x - textureX; | ||||
172 | const qreal dy = (textureY > (qreal)y) ? textureY - (qreal)y : (qreal)y - textureY; | ||||
173 | |||||
174 | Q_ASSERT(0 <= dx && dx <= 1)((0 <= dx && dx <= 1) ? static_cast<void> (0) : qt_assert("0 <= dx && dx <= 1", "/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" , 174)); | ||||
175 | Q_ASSERT(0 <= dy && dy <= 1)((0 <= dy && dy <= 1) ? static_cast<void> (0) : qt_assert("0 <= dy && dy <= 1", "/home/gilles/devel/GIT/8.x/core/utilities/geolocation/engine/models/ElevationModel.cpp" , 175)); | ||||
176 | unsigned int pixel = image->pixel(x % width, y % height) & 0xffff; // 16 valid bits | ||||
177 | short int elevation = (short int) pixel; // and signed type, so just cast it | ||||
178 | |||||
179 | //qCDebug(DIGIKAM_MARBLE_LOG) << "(1-dx)" << (1-dx) << "(1-dy)" << (1-dy); | ||||
180 | if (pixel != invalidElevationData) //no data? | ||||
181 | { | ||||
182 | //qCDebug(DIGIKAM_MARBLE_LOG) << "got at x" << x % width << "y" << y % height << "a height of" << pixel << "** RGB" << qRed(pixel) << qGreen(pixel) << qBlue(pixel); | ||||
183 | ret += (qreal)elevation * (1 - dx) * (1 - dy); | ||||
184 | hasHeight = true; | ||||
185 | } | ||||
186 | |||||
187 | else | ||||
188 | { | ||||
189 | //qCDebug(DIGIKAM_MARBLE_LOG) << "no data at" << x % width << "y" << y % height; | ||||
190 | noData += (1 - dx) * (1 - dy); | ||||
191 | } | ||||
192 | } | ||||
193 | |||||
194 | if (!hasHeight) | ||||
195 | { | ||||
196 | ret = invalidElevationData; //no data | ||||
197 | } | ||||
198 | |||||
199 | else | ||||
200 | { | ||||
201 | if (noData) | ||||
202 | { | ||||
203 | //qCDebug(DIGIKAM_MARBLE_LOG) << "NO DATA" << noData; | ||||
204 | ret += (ret / (1 - noData)) * noData; | ||||
205 | } | ||||
206 | } | ||||
207 | |||||
208 | //qCDebug(DIGIKAM_MARBLE_LOG) << ">>>" << lat << lon << "returning an elevation of" << ret; | ||||
209 | return ret; | ||||
210 | } | ||||
211 | |||||
212 | QVector<GeoDataCoordinates> ElevationModel::heightProfile(qreal fromLon, qreal fromLat, qreal toLon, qreal toLat) const | ||||
213 | { | ||||
214 | if (!d->m_textureLayer) | ||||
| |||||
215 | { | ||||
216 | return QVector<GeoDataCoordinates>(); | ||||
217 | } | ||||
218 | |||||
219 | const int tileZoomLevel = TileLoader::maximumTileLevel(*(d->m_textureLayer)); | ||||
220 | const int width = d->m_textureLayer->tileSize().width(); | ||||
221 | const int numTilesX = TileLoaderHelper::levelToColumn(d->m_textureLayer->levelZeroColumns(), tileZoomLevel); | ||||
222 | |||||
223 | qreal distPerPixel = (qreal)360 / (width * numTilesX); | ||||
224 | //qCDebug(DIGIKAM_MARBLE_LOG) << "heightProfile" << fromLat << fromLon << toLat << toLon << "distPerPixel" << distPerPixel; | ||||
225 | |||||
226 | qreal lat = fromLat; | ||||
227 | qreal lon = fromLon; | ||||
228 | char dirLat = fromLat < toLat ? 1 : -1; | ||||
229 | char dirLon = fromLon < toLon ? 1 : -1; | ||||
230 | qreal k = qAbs((fromLat - toLat) / (fromLon - toLon)); | ||||
231 | //qCDebug(DIGIKAM_MARBLE_LOG) << "fromLon" << fromLon << "fromLat" << fromLat; | ||||
232 | //qCDebug(DIGIKAM_MARBLE_LOG) << "diff lon" << ( fromLon - toLon ) << "diff lat" << ( fromLat - toLat ); | ||||
233 | //qCDebug(DIGIKAM_MARBLE_LOG) << "dirLon" << QString::number(dirLon) << "dirLat" << QString::number(dirLat) << "k" << k; | ||||
234 | QVector<GeoDataCoordinates> ret; | ||||
235 | |||||
236 | while (lat * dirLat <= toLat * dirLat && lon * dirLon <= toLon * dirLon) | ||||
237 | { | ||||
238 | //qCDebug(DIGIKAM_MARBLE_LOG) << lat << lon; | ||||
239 | qreal h = height(lon, lat); | ||||
240 | |||||
241 | if (h < 32000) | ||||
242 | { | ||||
243 | ret << GeoDataCoordinates(lon, lat, h, GeoDataCoordinates::Degree); | ||||
244 | } | ||||
245 | |||||
246 | if (k < 0.5) | ||||
247 | { | ||||
248 | //qCDebug(DIGIKAM_MARBLE_LOG) << "lon(x) += distPerPixel"; | ||||
249 | lat += distPerPixel * k * dirLat; | ||||
250 | lon += distPerPixel * dirLon; | ||||
251 | } | ||||
252 | |||||
253 | else | ||||
254 | { | ||||
255 | //qCDebug(DIGIKAM_MARBLE_LOG) << "lat(y) += distPerPixel"; | ||||
256 | lat += distPerPixel * dirLat; | ||||
257 | lon += distPerPixel / k * dirLon; | ||||
258 | } | ||||
259 | } | ||||
260 | |||||
261 | //qCDebug(DIGIKAM_MARBLE_LOG) << ret; | ||||
262 | return ret; | ||||
263 | } | ||||
264 | |||||
265 | } // namespace Marble | ||||
266 | |||||
267 | #include "moc_ElevationModel.cpp" |
1 | // Copyright (C) 2016 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 | |
4 | #ifndef QCACHE_H |
5 | #define QCACHE_H |
6 | |
7 | #include <QtCore/qhash.h> |
8 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | |
12 | template <class Key, class T> |
13 | class QCache |
14 | { |
15 | struct Value |
16 | { |
17 | T *t = nullptr; |
18 | qsizetype cost = 0; |
19 | Value() noexcept = default; |
20 | Value(T *tt, qsizetype c) noexcept |
21 | : t(tt), cost(c) |
22 | {} |
23 | Value(Value &&other) noexcept |
24 | : t(other.t), |
25 | cost(other.cost) |
26 | { |
27 | other.t = nullptr; |
28 | } |
29 | Value &operator=(Value &&other) noexcept |
30 | { |
31 | qt_ptr_swap(t, other.t); |
32 | std::swap(cost, other.cost); |
33 | return *this; |
34 | } |
35 | ~Value() { delete t; } |
36 | |
37 | private: |
38 | Q_DISABLE_COPY(Value)Value(const Value &) = delete; Value &operator=(const Value &) = delete; |
39 | }; |
40 | |
41 | struct Chain |
42 | { |
43 | Chain() noexcept : prev(this), next(this) { } |
44 | Chain *prev; |
45 | Chain *next; |
46 | }; |
47 | |
48 | struct Node : public Chain |
49 | { |
50 | using KeyType = Key; |
51 | using ValueType = Value; |
52 | |
53 | Key key; |
54 | Value value; |
55 | |
56 | Node(const Key &k, Value &&t) noexcept(std::is_nothrow_move_assignable_v<Key>) |
57 | : Chain(), |
58 | key(k), |
59 | value(std::move(t)) |
60 | { |
61 | } |
62 | Node(Key &&k, Value &&t) noexcept(std::is_nothrow_move_assignable_v<Key>) |
63 | : Chain(), |
64 | key(std::move(k)), |
65 | value(std::move(t)) |
66 | { |
67 | } |
68 | static void createInPlace(Node *n, const Key &k, T *o, qsizetype cost) |
69 | { |
70 | new (n) Node{ Key(k), Value(o, cost) }; |
71 | } |
72 | void emplace(T *o, qsizetype cost) |
73 | { |
74 | value = Value(o, cost); |
75 | } |
76 | |
77 | Node(Node &&other) |
78 | : Chain(other), |
79 | key(std::move(other.key)), |
80 | value(std::move(other.value)) |
81 | { |
82 | Q_ASSERT(this->prev)((this->prev) ? static_cast<void>(0) : qt_assert("this->prev" , "/opt/qt6/include/QtCore/qcache.h", 82)); |
83 | Q_ASSERT(this->next)((this->next) ? static_cast<void>(0) : qt_assert("this->next" , "/opt/qt6/include/QtCore/qcache.h", 83)); |
84 | this->prev->next = this; |
85 | this->next->prev = this; |
86 | } |
87 | private: |
88 | Q_DISABLE_COPY(Node)Node(const Node &) = delete; Node &operator=(const Node &) = delete; |
89 | }; |
90 | |
91 | using Data = QHashPrivate::Data<Node>; |
92 | |
93 | mutable Chain chain; |
94 | Data d; |
95 | qsizetype mx = 0; |
96 | qsizetype total = 0; |
97 | |
98 | void unlink(Node *n) noexcept(std::is_nothrow_destructible_v<Node>) |
99 | { |
100 | Q_ASSERT(n->prev)((n->prev) ? static_cast<void>(0) : qt_assert("n->prev" , "/opt/qt6/include/QtCore/qcache.h", 100)); |
101 | Q_ASSERT(n->next)((n->next) ? static_cast<void>(0) : qt_assert("n->next" , "/opt/qt6/include/QtCore/qcache.h", 101)); |
102 | n->prev->next = n->next; |
103 | n->next->prev = n->prev; |
104 | total -= n->value.cost; |
105 | auto it = d.findBucket(n->key); |
106 | d.erase(it); |
107 | } |
108 | T *relink(const Key &key) const noexcept |
109 | { |
110 | if (isEmpty()) |
111 | return nullptr; |
112 | Node *n = d.findNode(key); |
113 | if (!n) |
114 | return nullptr; |
115 | |
116 | if (chain.next != n) { |
117 | Q_ASSERT(n->prev)((n->prev) ? static_cast<void>(0) : qt_assert("n->prev" , "/opt/qt6/include/QtCore/qcache.h", 117)); |
118 | Q_ASSERT(n->next)((n->next) ? static_cast<void>(0) : qt_assert("n->next" , "/opt/qt6/include/QtCore/qcache.h", 118)); |
119 | n->prev->next = n->next; |
120 | n->next->prev = n->prev; |
121 | n->next = chain.next; |
122 | chain.next->prev = n; |
123 | n->prev = &chain; |
124 | chain.next = n; |
125 | } |
126 | return n->value.t; |
127 | } |
128 | |
129 | void trim(qsizetype m) noexcept(std::is_nothrow_destructible_v<Node>) |
130 | { |
131 | while (chain.prev != &chain && total > m) { |
132 | Node *n = static_cast<Node *>(chain.prev); |
133 | unlink(n); |
134 | } |
135 | } |
136 | |
137 | |
138 | Q_DISABLE_COPY(QCache)QCache(const QCache &) = delete; QCache &operator=(const QCache &) = delete; |
139 | |
140 | public: |
141 | inline explicit QCache(qsizetype maxCost = 100) noexcept |
142 | : mx(maxCost) |
143 | { |
144 | } |
145 | inline ~QCache() |
146 | { |
147 | static_assert(std::is_nothrow_destructible_v<Key>, "Types with throwing destructors are not supported in Qt containers."); |
148 | static_assert(std::is_nothrow_destructible_v<T>, "Types with throwing destructors are not supported in Qt containers."); |
149 | |
150 | clear(); |
151 | } |
152 | |
153 | inline qsizetype maxCost() const noexcept { return mx; } |
154 | void setMaxCost(qsizetype m) noexcept(std::is_nothrow_destructible_v<Node>) |
155 | { |
156 | mx = m; |
157 | trim(mx); |
158 | } |
159 | inline qsizetype totalCost() const noexcept { return total; } |
160 | |
161 | inline qsizetype size() const noexcept { return qsizetype(d.size); } |
162 | inline qsizetype count() const noexcept { return qsizetype(d.size); } |
163 | inline bool isEmpty() const noexcept { return !d.size; } |
164 | inline QList<Key> keys() const |
165 | { |
166 | QList<Key> k; |
167 | if (size()) { |
168 | k.reserve(size()); |
169 | for (auto it = d.begin(); it != d.end(); ++it) |
170 | k << it.node()->key; |
171 | } |
172 | Q_ASSERT(k.size() == size())((k.size() == size()) ? static_cast<void>(0) : qt_assert ("k.size() == size()", "/opt/qt6/include/QtCore/qcache.h", 172 )); |
173 | return k; |
174 | } |
175 | |
176 | void clear() noexcept(std::is_nothrow_destructible_v<Node>) |
177 | { |
178 | d.clear(); |
179 | total = 0; |
180 | chain.next = &chain; |
181 | chain.prev = &chain; |
182 | } |
183 | |
184 | bool insert(const Key &key, T *object, qsizetype cost = 1) |
185 | { |
186 | if (cost > mx) { |
187 | remove(key); |
188 | delete object; |
189 | return false; |
190 | } |
191 | trim(mx - cost); |
192 | auto result = d.findOrInsert(key); |
193 | Node *n = result.it.node(); |
194 | if (result.initialized) { |
195 | auto prevCost = n->value.cost; |
196 | result.it.node()->emplace(object, cost); |
197 | cost -= prevCost; |
198 | relink(key); |
199 | } else { |
200 | Node::createInPlace(n, key, object, cost); |
201 | n->prev = &chain; |
202 | n->next = chain.next; |
203 | chain.next->prev = n; |
204 | chain.next = n; |
205 | } |
206 | total += cost; |
207 | return true; |
208 | } |
209 | T *object(const Key &key) const noexcept |
210 | { |
211 | return relink(key); |
212 | } |
213 | T *operator[](const Key &key) const noexcept |
214 | { |
215 | return relink(key); |
216 | } |
217 | inline bool contains(const Key &key) const noexcept |
218 | { |
219 | return !isEmpty() && d.findNode(key) != nullptr; |
220 | } |
221 | |
222 | bool remove(const Key &key) noexcept(std::is_nothrow_destructible_v<Node>) |
223 | { |
224 | if (isEmpty()) |
225 | return false; |
226 | Node *n = d.findNode(key); |
227 | if (!n) { |
228 | return false; |
229 | } else { |
230 | unlink(n); |
231 | return true; |
232 | } |
233 | } |
234 | |
235 | T *take(const Key &key) noexcept(std::is_nothrow_destructible_v<Key>) |
236 | { |
237 | if (isEmpty()) |
238 | return nullptr; |
239 | Node *n = d.findNode(key); |
240 | if (!n) |
241 | return nullptr; |
242 | |
243 | T *t = n->value.t; |
244 | n->value.t = nullptr; |
245 | unlink(n); |
246 | return t; |
247 | } |
248 | |
249 | }; |
250 | |
251 | QT_END_NAMESPACE |
252 | |
253 | #endif // QCACHE_H |