diff --git a/src/corelib/tools/qcache.h b/src/corelib/tools/qcache.h index fbfa2ab997..c8de12af60 100644 --- a/src/corelib/tools/qcache.h +++ b/src/corelib/tools/qcache.h @@ -50,9 +50,9 @@ class QCache { struct Value { T *t = nullptr; - int cost = 0; + qsizetype cost = 0; Value() noexcept = default; - Value(T *tt, int c) noexcept + Value(T *tt, qsizetype c) noexcept : t(tt), cost(c) {} Value(Value &&other) noexcept @@ -99,11 +99,11 @@ class QCache value(std::move(t)) { } - static void createInPlace(Node *n, const Key &k, T *o, int cost) + static void createInPlace(Node *n, const Key &k, T *o, qsizetype cost) { new (n) Node{ Key(k), Value(o, cost) }; } - void emplace(T *o, int cost) + void emplace(T *o, qsizetype cost) { value = Value(o, cost); } @@ -143,8 +143,8 @@ class QCache mutable Chain chain; Data d; - int mx = 0; - int total = 0; + qsizetype mx = 0; + qsizetype total = 0; void unlink(Node *n) noexcept(std::is_nothrow_destructible_v) { @@ -175,7 +175,7 @@ class QCache return n->value.t; } - void trim(int m) noexcept(std::is_nothrow_destructible_v) + void trim(qsizetype m) noexcept(std::is_nothrow_destructible_v) { Chain *n = chain.prev; while (n != &chain && total > m) { @@ -189,31 +189,31 @@ class QCache Q_DISABLE_COPY(QCache) public: - inline explicit QCache(int maxCost = 100) noexcept + inline explicit QCache(qsizetype maxCost = 100) noexcept : mx(maxCost) {} inline ~QCache() { clear(); } - inline int maxCost() const noexcept { return mx; } - void setMaxCost(int m) noexcept(std::is_nothrow_destructible_v) + inline qsizetype maxCost() const noexcept { return mx; } + void setMaxCost(qsizetype m) noexcept(std::is_nothrow_destructible_v) { mx = m; trim(mx); } - inline int totalCost() const noexcept { return total; } + inline qsizetype totalCost() const noexcept { return total; } - inline qsizetype size() const noexcept { return d.size; } - inline qsizetype count() const noexcept { return d.size; } + inline qsizetype size() const noexcept { return qsizetype(d.size); } + inline qsizetype count() const noexcept { return qsizetype(d.size); } inline bool isEmpty() const noexcept { return !d.size; } inline QList keys() const { QList k; - if (d.size) { - k.reserve(typename QList::size_type(d.size)); + if (size()) { + k.reserve(size()); for (auto it = d.begin(); it != d.end(); ++it) k << it.node()->key; } - Q_ASSERT(k.size() == qsizetype(d.size)); + Q_ASSERT(k.size() == size()); return k; } @@ -225,7 +225,7 @@ public: chain.prev = &chain; } - bool insert(const Key &key, T *object, int cost = 1) + bool insert(const Key &key, T *object, qsizetype cost = 1) { remove(key); diff --git a/src/corelib/tools/qcache.qdoc b/src/corelib/tools/qcache.qdoc index ffc21318f4..f9b1ffa8f4 100644 --- a/src/corelib/tools/qcache.qdoc +++ b/src/corelib/tools/qcache.qdoc @@ -80,7 +80,7 @@ \sa QPixmapCache, QHash, QMap */ -/*! \fn template QCache::QCache(int maxCost = 100) +/*! \fn template QCache::QCache(qsizetype maxCost = 100) Constructs a cache whose contents will never have a total cost greater than \a maxCost. @@ -91,14 +91,14 @@ Destroys the cache. Deletes all the objects in the cache. */ -/*! \fn template int QCache::maxCost() const +/*! \fn template qsizetype QCache::maxCost() const Returns the maximum allowed total cost of the cache. \sa setMaxCost(), totalCost() */ -/*! \fn template void QCache::setMaxCost(int cost) +/*! \fn template void QCache::setMaxCost(qsizetype cost) Sets the maximum allowed total cost of the cache to \a cost. If the current total cost is greater than \a cost, some objects are @@ -107,7 +107,7 @@ \sa maxCost(), totalCost() */ -/*! \fn template int QCache::totalCost() const +/*! \fn template qsizetype QCache::totalCost() const Returns the total cost of the objects in the cache. @@ -120,14 +120,14 @@ \sa setMaxCost() */ -/*! \fn template int QCache::size() const +/*! \fn template qsizetype QCache::size() const Returns the number of objects in the cache. \sa isEmpty() */ -/*! \fn template int QCache::count() const +/*! \fn template qsizetype QCache::count() const Same as size(). */ @@ -153,7 +153,7 @@ */ -/*! \fn template bool QCache::insert(const Key &key, T *object, int cost = 1) +/*! \fn template bool QCache::insert(const Key &key, T *object, qsizetype cost = 1) Inserts \a object into the cache with key \a key and associated cost \a cost. Any object with the same key already in