diff options
author | Alexandru Croitor <alexandru.croitor@qt.io> | 2019-05-28 16:41:49 +0200 |
---|---|---|
committer | Alexandru Croitor <alexandru.croitor@qt.io> | 2019-06-03 15:14:42 +0200 |
commit | e4079eca49adce16e31dac2a18d49d7a55817891 (patch) | |
tree | 1dfb960ec1115b1f552afe8a013058542389505e /src/corelib/tools/qbytearray.h | |
parent | f32a6cfb6b6236533508901f114ab57396da8ff3 (diff) | |
parent | ec6dc5f78453048c4f0604655a34c6c20c79d819 (diff) |
Merge remote-tracking branch 'origin/dev' into wip/cmake
Take 5.
Change-Id: Ifb2d20e95ba824e45e667fba6c2ba45389991cc3
Diffstat (limited to 'src/corelib/tools/qbytearray.h')
-rw-r--r-- | src/corelib/tools/qbytearray.h | 64 |
1 files changed, 54 insertions, 10 deletions
diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index 14fcddce7c..a81051d309 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -209,8 +209,8 @@ public: inline char at(int i) const; inline char operator[](int i) const; inline char operator[](uint i) const; - inline QByteRef operator[](int i); - inline QByteRef operator[](uint i); + Q_REQUIRED_RESULT inline QByteRef operator[](int i); + Q_REQUIRED_RESULT inline QByteRef operator[](uint i); Q_REQUIRED_RESULT char front() const { return at(0); } Q_REQUIRED_RESULT inline QByteRef front(); Q_REQUIRED_RESULT char back() const { return at(size() - 1); } @@ -528,7 +528,27 @@ inline void QByteArray::squeeze() } } -class Q_CORE_EXPORT QByteRef { +namespace QtPrivate { +namespace DeprecatedRefClassBehavior { + enum class EmittingClass { + QByteRef, + QCharRef, + }; + + enum class WarningType { + OutOfRange, + DelayedDetach, + }; + + Q_CORE_EXPORT Q_DECL_COLD_FUNCTION void warn(WarningType w, EmittingClass c); +} // namespace DeprecatedAssignmentOperatorBehavior +} // namespace QtPrivate + +class +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) +Q_CORE_EXPORT +#endif +QByteRef { QByteArray &a; int i; inline QByteRef(QByteArray &array, int idx) @@ -536,13 +556,37 @@ class Q_CORE_EXPORT QByteRef { friend class QByteArray; public: inline operator char() const - { return i < a.d->size ? a.d->data()[i] : char(0); } + { + using namespace QtPrivate::DeprecatedRefClassBehavior; + if (Q_LIKELY(i < a.d->size)) + return a.d->data()[i]; +#ifdef QT_DEBUG + warn(WarningType::OutOfRange, EmittingClass::QByteRef); +#endif + return char(0); + } inline QByteRef &operator=(char c) - { if (i >= a.d->size) a.expand(i); else a.detach(); - a.d->data()[i] = c; return *this; } + { + using namespace QtPrivate::DeprecatedRefClassBehavior; + if (Q_UNLIKELY(i >= a.d->size)) { +#ifdef QT_DEBUG + warn(WarningType::OutOfRange, EmittingClass::QByteRef); +#endif + a.expand(i); + } else { +#ifdef QT_DEBUG + if (Q_UNLIKELY(!a.isDetached())) + warn(WarningType::DelayedDetach, EmittingClass::QByteRef); +#endif + a.detach(); + } + a.d->data()[i] = c; + return *this; + } inline QByteRef &operator=(const QByteRef &c) - { if (i >= a.d->size) a.expand(i); else a.detach(); - a.d->data()[i] = c.a.d->data()[c.i]; return *this; } + { + return operator=(char(c)); + } inline bool operator==(char c) const { return a.d->data()[i] == c; } inline bool operator!=(char c) const @@ -558,9 +602,9 @@ public: }; inline QByteRef QByteArray::operator[](int i) -{ Q_ASSERT(i >= 0); return QByteRef(*this, i); } +{ Q_ASSERT(i >= 0); detach(); return QByteRef(*this, i); } inline QByteRef QByteArray::operator[](uint i) -{ return QByteRef(*this, i); } +{ detach(); return QByteRef(*this, i); } inline QByteRef QByteArray::front() { return operator[](0); } inline QByteRef QByteArray::back() { return operator[](size() - 1); } inline QByteArray::iterator QByteArray::begin() |