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 <ivan.solovev@qt.io>
(cherry picked from commit 6306ebe749e083126a39b9dd13d7060aa7bdacc2)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Thiago Macieira 2024-08-29 12:09:32 -07:00 committed by Qt Cherry-pick Bot
parent 6978951d5e
commit cb35f1e858
9 changed files with 31 additions and 16 deletions

View File

@ -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.

View File

@ -68,7 +68,6 @@ private:
template <typename InputIterator>
using if_input_iterator = QtPrivate::IfIsInputIterator<InputIterator>;
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.")

View File

@ -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.

View File

@ -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);

View File

@ -171,11 +171,15 @@ struct QTypedArrayData
return static_cast<T *>(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 {

View File

@ -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

View File

@ -1333,10 +1333,9 @@
*/
/*! \fn template <typename T> qsizetype QList<T>::max_size()
\fn template <typename T> qsizetype QList<T>::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.

View File

@ -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<T>)
{
@ -404,6 +408,7 @@ public:
}
#ifdef Q_QDOC
inline qsizetype size() const { return this->s; }
static constexpr qsizetype maxSize() noexcept { return QVLABase<T>::maxSize(); }
static constexpr qsizetype max_size() noexcept { return QVLABase<T>::max_size(); }
#endif
using Base::size;

View File

@ -141,10 +141,9 @@
*/
/*! \fn template<class T, qsizetype Prealloc> qsizetype QVarLengthArray<T, Prealloc>::max_size()
\fn template<class T, qsizetype Prealloc> qsizetype QVarLengthArray<T, Prealloc>::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.