From cb35f1e8580c3d2d1b58a2cf6256e117dc51f066 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 29 Aug 2024 12:09:32 -0700 Subject: [PATCH] Containers: add a static constexpr maxSize() max_size() will be made non-static to strictly follow the C++ Standard Library concept, but it makes no sense for the Qt containers to have allocations limited per instance, as they are not allocator-aware. This commit adds a Qt-style camelCase() function to the STL-style snake_case() with the semantics we want. Task-number: QTBUG-128450 Change-Id: I2e4aa228c71c821c01bafc0f37956d29fe652ef1 Reviewed-by: Ivan Solovev (cherry picked from commit 6306ebe749e083126a39b9dd13d7060aa7bdacc2) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/text/qbytearray.cpp | 3 +-- src/corelib/text/qbytearray.h | 10 +++++++--- src/corelib/text/qstring.cpp | 3 +-- src/corelib/text/qstring.h | 9 +++++++-- src/corelib/tools/qarraydata.h | 6 +++++- src/corelib/tools/qlist.h | 3 ++- src/corelib/tools/qlist.qdoc | 3 +-- src/corelib/tools/qvarlengtharray.h | 7 ++++++- src/corelib/tools/qvarlengtharray.qdoc | 3 +-- 9 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index af22ff81bc3..b61686ddde6 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -1421,10 +1421,9 @@ QByteArray &QByteArray::operator=(const char *str) */ /*! \fn qsizetype QByteArray::max_size() + \fn qsizetype QByteArray::maxSize() \since 6.8 - This function is provided for STL compatibility. - It returns the maximum number of elements that the byte array can theoretically hold. In practice, the number can be much smaller, limited by the amount of memory available to the system. diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index 6bc9bd1ce3f..b0907702559 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -68,7 +68,6 @@ private: template using if_input_iterator = QtPrivate::IfIsInputIterator; public: - enum Base64Option { Base64Encoding = 0, Base64UrlEncoding = 1, @@ -483,15 +482,20 @@ public: void shrink_to_fit() { squeeze(); } iterator erase(const_iterator first, const_iterator last); inline iterator erase(const_iterator it) { return erase(it, it + 1); } + static constexpr qsizetype max_size() noexcept { - // -1 to deal with the NUL terminator - return Data::max_size() - 1; + return maxSize(); } static QByteArray fromStdString(const std::string &s); std::string toStdString() const; + static constexpr qsizetype maxSize() noexcept + { + // -1 to deal with the NUL terminator + return Data::maxSize() - 1; + } inline qsizetype size() const noexcept { return d.size; } #if QT_DEPRECATED_SINCE(6, 4) QT_DEPRECATED_VERSION_X_6_4("Use size() or length() instead.") diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 9eecacbf289..23d9291c0d0 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -6415,10 +6415,9 @@ QString& QString::fill(QChar ch, qsizetype size) /*! \fn qsizetype QString::max_size() + \fn qsizetype QString::maxSize() \since 6.8 - This function is provided for STL compatibility. - It returns the maximum number of elements that the string can theoretically hold. In practice, the number can be much smaller, limited by the amount of memory available to the system. diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 0e0aef82670..c2b62a7069c 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -183,6 +183,12 @@ public: = default; QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QString) void swap(QString &other) noexcept { d.swap(other.d); } + + static constexpr qsizetype maxSize() noexcept + { + // -1 to deal with the NUL terminator + return Data::maxSize() - 1; + } inline qsizetype size() const noexcept { return d.size; } #if QT_DEPRECATED_SINCE(6, 4) QT_DEPRECATED_VERSION_X_6_4("Use size() or length() instead.") @@ -965,8 +971,7 @@ public: inline iterator erase(const_iterator it) { return erase(it, it + 1); } static constexpr qsizetype max_size() noexcept { - // -1 to deal with the NUL terminator - return Data::max_size() - 1; + return maxSize(); } static inline QString fromStdString(const std::string &s); diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h index da83fc1a21a..38d1091ac1f 100644 --- a/src/corelib/tools/qarraydata.h +++ b/src/corelib/tools/qarraydata.h @@ -171,11 +171,15 @@ struct QTypedArrayData return static_cast(start); } - constexpr static qsizetype max_size() noexcept + constexpr static qsizetype maxSize() noexcept { // -1 to deal with the pointer one-past-the-end return (QtPrivate::MaxAllocSize - sizeof(QtPrivate::AlignedQArrayData) - 1) / sizeof(T); } + constexpr static qsizetype max_size() noexcept + { + return maxSize(); + } }; namespace QtPrivate { diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 3bac8611094..62779cba069 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -395,6 +395,7 @@ public: bool operator>=(const QList &other) const; #endif // Q_QDOC + static constexpr qsizetype maxSize() { return Data::maxSize(); } qsizetype size() const noexcept { return d->size; } qsizetype count() const noexcept { return size(); } qsizetype length() const noexcept { return size(); } @@ -692,7 +693,7 @@ public: void shrink_to_fit() { squeeze(); } static constexpr qsizetype max_size() noexcept { - return Data::max_size(); + return maxSize(); } // comfort diff --git a/src/corelib/tools/qlist.qdoc b/src/corelib/tools/qlist.qdoc index 6ea38f3c83b..74da71896b6 100644 --- a/src/corelib/tools/qlist.qdoc +++ b/src/corelib/tools/qlist.qdoc @@ -1333,10 +1333,9 @@ */ /*! \fn template qsizetype QList::max_size() + \fn template qsizetype QList::maxSize() \since 6.8 - This function is provided for STL compatibility. - It returns the maximum number of elements that the list can theoretically hold. In practice, the number can be much smaller, limited by the amount of memory available to the system. diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index 78d5a276277..577a5b193e2 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -184,11 +184,15 @@ public: iterator erase(const_iterator begin, const_iterator end); iterator erase(const_iterator pos) { return erase(pos, pos + 1); } - static constexpr qsizetype max_size() noexcept + static constexpr qsizetype maxSize() noexcept { // -1 to deal with the pointer one-past-the-end return (QtPrivate::MaxAllocSize / sizeof(T)) - 1; } + static constexpr qsizetype max_size() noexcept + { + return maxSize(); + } size_t hash(size_t seed) const noexcept(QtPrivate::QNothrowHashable_v) { @@ -404,6 +408,7 @@ public: } #ifdef Q_QDOC inline qsizetype size() const { return this->s; } + static constexpr qsizetype maxSize() noexcept { return QVLABase::maxSize(); } static constexpr qsizetype max_size() noexcept { return QVLABase::max_size(); } #endif using Base::size; diff --git a/src/corelib/tools/qvarlengtharray.qdoc b/src/corelib/tools/qvarlengtharray.qdoc index d010ba4d4c0..8305f495360 100644 --- a/src/corelib/tools/qvarlengtharray.qdoc +++ b/src/corelib/tools/qvarlengtharray.qdoc @@ -141,10 +141,9 @@ */ /*! \fn template qsizetype QVarLengthArray::max_size() + \fn template qsizetype QVarLengthArray::maxSize() \since 6.8 - This function is provided for STL compatibility. - It returns the maximum number of elements that the array can theoretically hold. In practice, the number can be much smaller, limited by the amount of memory available to the system.