summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qbytearray.h
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2012-06-25 17:41:51 +0200
committerLars Knoll <lars.knoll@qt.io>2019-12-08 18:20:04 +0100
commit00fbc087dd54b31e97b1557766b037b00120c43f (patch)
treeacb1341467ca05d47ee3326983d9dd7718cc41f5 /src/corelib/text/qbytearray.h
parente58b44d557b859b7b55869f1e137aa1bc8968307 (diff)
Inline the size and data pointers in QByteArray
Change-Id: I82feeb2c9bd2900f421fc0c8d78698b1e83db043 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/corelib/text/qbytearray.h')
-rw-r--r--src/corelib/text/qbytearray.h139
1 files changed, 61 insertions, 78 deletions
diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h
index 67a7e5da3d..f97f64f556 100644
--- a/src/corelib/text/qbytearray.h
+++ b/src/corelib/text/qbytearray.h
@@ -114,40 +114,22 @@ class QByteRef;
class QString;
class QDataStream;
-typedef QArrayData QByteArrayData;
-
-template<int N> struct QStaticByteArrayData
-{
- QByteArrayData ba;
- char data[N + 1];
-
- QByteArrayData *data_ptr() const
- {
- Q_ASSERT(ba.isStatic());
- return const_cast<QByteArrayData *>(&ba);
- }
-};
-
-struct QByteArrayDataPtr
+struct QByteArrayData
{
- QByteArrayData *ptr;
+ QArrayData *d;
+ char *b;
+ uint size;
};
-#define Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset) \
- Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset)
- /**/
-
-#define Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER(size) \
- Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, sizeof(QByteArrayData)) \
- /**/
-
# define QByteArrayLiteral(str) \
([]() -> QByteArray { \
enum { Size = sizeof(str) - 1 }; \
- static const QStaticByteArrayData<Size> qbytearray_literal = { \
- Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER(Size), \
- str }; \
- QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; \
+ static const QArrayData qbytearray_literal = { \
+ Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, Size, 0, sizeof(QArrayData) }; \
+ QByteArrayData holder = { \
+ const_cast<QArrayData *>(&qbytearray_literal), \
+ const_cast<char *>(str), \
+ Size }; \
const QByteArray ba(holder); \
return ba; \
}()) \
@@ -177,10 +159,10 @@ public:
QByteArray &operator=(const QByteArray &) noexcept;
QByteArray &operator=(const char *str);
- inline QByteArray(QByteArray && other) noexcept : d(other.d) { other.d = Data::sharedNull(); }
+ inline QByteArray(QByteArray && other) noexcept : d(std::move(other.d))
+ { other.d.d = Data::sharedNull(); other.d.b = Data::sharedNullData(); other.d.size = 0; }
inline QByteArray &operator=(QByteArray &&other) noexcept
{ qSwap(d, other.d); return *this; }
-
inline void swap(QByteArray &other) noexcept
{ qSwap(d, other.d); }
@@ -203,7 +185,8 @@ public:
inline const char *constData() const;
inline void detach();
inline bool isDetached() const;
- inline bool isSharedWith(const QByteArray &other) const { return d == other.d; }
+ inline bool isSharedWith(const QByteArray &other) const
+ { return data() == other.data() && size() == other.size(); }
void clear();
inline char at(int i) const;
@@ -431,18 +414,18 @@ public:
static inline QByteArray fromStdString(const std::string &s);
inline std::string toStdString() const;
- inline int count() const { return d->size; }
- int length() const { return d->size; }
+ inline int count() const { return int(d.size); }
+ int length() const { return int(d.size); }
bool isNull() const;
- inline QByteArray(QByteArrayDataPtr dd)
- : d(static_cast<Data *>(dd.ptr))
+ explicit inline QByteArray(const QByteArrayData &dd)
+ : d(dd)
{
}
private:
operator QNoImplicitBoolCast() const;
- Data *d;
+ QByteArrayData d;
void reallocData(uint alloc, Data::ArrayOptions options);
void expand(int i);
QByteArray nulTerminated() const;
@@ -460,63 +443,63 @@ private:
friend class QString;
friend Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, int nbytes);
public:
- typedef Data * DataPtr;
+ typedef QByteArrayData DataPtr;
inline DataPtr &data_ptr() { return d; }
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QByteArray::Base64Options)
-inline QByteArray::QByteArray() noexcept : d(Data::sharedNull()) { }
-inline QByteArray::~QByteArray() { if (!d->deref()) Data::deallocate(d); }
+inline QByteArray::QByteArray() noexcept { d.d = Data::sharedNull(); d.b = Data::sharedNullData(); d.size = 0; }
+inline QByteArray::~QByteArray() { if (!d.d->deref()) Data::deallocate(d.d); }
inline int QByteArray::size() const
-{ return d->size; }
+{ return int(d.size); }
inline char QByteArray::at(int i) const
-{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
+{ Q_ASSERT(uint(i) < uint(size())); return d.b[i]; }
inline char QByteArray::operator[](int i) const
-{ Q_ASSERT(uint(i) < uint(size())); return d->data()[i]; }
+{ Q_ASSERT(uint(i) < uint(size())); return d.b[i]; }
inline bool QByteArray::isEmpty() const
-{ return d->size == 0; }
+{ return size() == 0; }
#ifndef QT_NO_CAST_FROM_BYTEARRAY
inline QByteArray::operator const char *() const
-{ return d->data(); }
+{ return data(); }
inline QByteArray::operator const void *() const
-{ return d->data(); }
+{ return data(); }
#endif
inline char *QByteArray::data()
-{ detach(); return d->data(); }
+{ detach(); return d.b; }
inline const char *QByteArray::data() const
-{ return d->data(); }
+{ return d.b; }
inline const char *QByteArray::constData() const
-{ return d->data(); }
+{ return d.b; }
inline void QByteArray::detach()
-{ if (d->needsDetach()) reallocData(uint(d->size) + 1u, d->detachFlags()); }
+{ if (d.d->needsDetach()) reallocData(uint(size()) + 1u, d.d->detachFlags()); }
inline bool QByteArray::isDetached() const
-{ return !d->isShared(); }
+{ return !d.d->isShared(); }
inline QByteArray::QByteArray(const QByteArray &a) noexcept : d(a.d)
-{ d->ref(); }
+{ d.d->ref(); }
inline int QByteArray::capacity() const
-{ int realCapacity = d->constAllocatedCapacity(); return realCapacity ? realCapacity - 1 : 0; }
+{ int realCapacity = d.d->constAllocatedCapacity(); return realCapacity ? realCapacity - 1 : 0; }
inline void QByteArray::reserve(int asize)
{
- if (d->needsDetach() || asize > capacity()) {
- reallocData(qMax(uint(size()), uint(asize)) + 1u, d->detachFlags() | Data::CapacityReserved);
+ if (d.d->needsDetach() || asize > capacity()) {
+ reallocData(qMax(uint(size()), uint(asize)) + 1u, d.d->detachFlags() | Data::CapacityReserved);
} else {
- d->flags |= Data::CapacityReserved;
+ d.d->flags |= Data::CapacityReserved;
}
}
inline void QByteArray::squeeze()
{
- if ((d->flags & Data::CapacityReserved) == 0)
+ if ((d.d->flags & Data::CapacityReserved) == 0)
return;
- if (d->needsDetach() || d->size < capacity()) {
- reallocData(uint(d->size) + 1u, d->detachFlags() & ~Data::CapacityReserved);
+ if (d.d->needsDetach() || size() < capacity()) {
+ reallocData(uint(size()) + 1u, d.d->detachFlags() & ~Data::CapacityReserved);
} else {
- d->flags &= ~Data::CapacityReserved;
+ d.d->flags &= uint(~Data::CapacityReserved);
}
}
@@ -550,8 +533,8 @@ public:
inline operator char() const
{
using namespace QtPrivate::DeprecatedRefClassBehavior;
- if (Q_LIKELY(i < a.d->size))
- return a.d->data()[i];
+ if (Q_LIKELY(i < a.size()))
+ return a.constData()[i];
#ifdef QT_DEBUG
warn(WarningType::OutOfRange, EmittingClass::QByteRef);
#endif
@@ -560,7 +543,7 @@ public:
inline QByteRef &operator=(char c)
{
using namespace QtPrivate::DeprecatedRefClassBehavior;
- if (Q_UNLIKELY(i >= a.d->size)) {
+ if (Q_UNLIKELY(i >= a.size())) {
#ifdef QT_DEBUG
warn(WarningType::OutOfRange, EmittingClass::QByteRef);
#endif
@@ -572,7 +555,7 @@ public:
#endif
a.detach();
}
- a.d->data()[i] = c;
+ a.d.b[i] = c;
return *this;
}
inline QByteRef &operator=(const QByteRef &c)
@@ -580,17 +563,17 @@ public:
return operator=(char(c));
}
inline bool operator==(char c) const
- { return a.d->data()[i] == c; }
+ { return a.data()[i] == c; }
inline bool operator!=(char c) const
- { return a.d->data()[i] != c; }
+ { return a.data()[i] != c; }
inline bool operator>(char c) const
- { return a.d->data()[i] > c; }
+ { return a.data()[i] > c; }
inline bool operator>=(char c) const
- { return a.d->data()[i] >= c; }
+ { return a.data()[i] >= c; }
inline bool operator<(char c) const
- { return a.d->data()[i] < c; }
+ { return a.data()[i] < c; }
inline bool operator<=(char c) const
- { return a.d->data()[i] <= c; }
+ { return a.data()[i] <= c; }
};
inline QByteRef QByteArray::operator[](int i)
@@ -598,23 +581,23 @@ inline QByteRef QByteArray::operator[](int i)
inline QByteRef QByteArray::front() { return operator[](0); }
inline QByteRef QByteArray::back() { return operator[](size() - 1); }
inline QByteArray::iterator QByteArray::begin()
-{ detach(); return d->data(); }
+{ return data(); }
inline QByteArray::const_iterator QByteArray::begin() const
-{ return d->data(); }
+{ return data(); }
inline QByteArray::const_iterator QByteArray::cbegin() const
-{ return d->data(); }
+{ return data(); }
inline QByteArray::const_iterator QByteArray::constBegin() const
-{ return d->data(); }
+{ return data(); }
inline QByteArray::iterator QByteArray::end()
-{ detach(); return d->data() + d->size; }
+{ return data() + size(); }
inline QByteArray::const_iterator QByteArray::end() const
-{ return d->data() + d->size; }
+{ return data() + size(); }
inline QByteArray::const_iterator QByteArray::cend() const
-{ return d->data() + d->size; }
+{ return data() + size(); }
inline QByteArray::const_iterator QByteArray::constEnd() const
-{ return d->data() + d->size; }
+{ return data() + size(); }
inline QByteArray &QByteArray::append(int n, char ch)
-{ return insert(d->size, n, ch); }
+{ return insert(size(), n, ch); }
inline QByteArray &QByteArray::prepend(int n, char ch)
{ return insert(0, n, ch); }
inline QByteArray &QByteArray::operator+=(char c)