Simplify Q_ARRAY_LITERAL

And clean up some unused pieces of code.

Change-Id: I285b6862dc67b7130af66d3e08f652b1a56b990e
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Lars Knoll 2019-11-19 14:21:10 +01:00
parent 76004502ba
commit a46caf087c
8 changed files with 21 additions and 96 deletions

View File

@ -8619,7 +8619,7 @@ bool QString::isRightToLeft() const
*/
QString QString::fromRawData(const QChar *unicode, int size)
{
return QString(Data::fromRawData(const_cast<char16_t *>(reinterpret_cast<const char16_t *>(unicode)), size));
return QString(DataPointer::fromRawData(const_cast<char16_t *>(reinterpret_cast<const char16_t *>(unicode)), size));
}
/*!

View File

@ -165,23 +165,6 @@ struct Q_CORE_EXPORT QArrayData
Q_DECLARE_OPERATORS_FOR_FLAGS(QArrayData::ArrayOptions)
template <class T, size_t N>
struct QStaticArrayData
{
// static arrays are of type RawDataType
QArrayData header;
T data[N];
};
// Support for returning QArrayDataPointer<T> from functions
template <class T>
struct QArrayDataPointerRef
{
QTypedArrayData<T> *ptr;
T *data;
uint size;
};
template <class T>
struct QTypedArrayData
: QArrayData
@ -218,34 +201,8 @@ struct QTypedArrayData
static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData));
QArrayData::deallocate(data, sizeof(T), alignof(AlignmentDummy));
}
static QArrayDataPointerRef<T> fromRawData(const T *data, size_t n)
{
static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData));
QArrayDataPointerRef<T> result = {
nullptr, const_cast<T *>(data), uint(n)
};
return result;
}
};
////////////////////////////////////////////////////////////////////////////////
// Q_ARRAY_LITERAL
// The idea here is to place a (read-only) copy of header and array data in an
// mmappable portion of the executable (typically, .rodata section). This is
// accomplished by hiding a static const instance of QStaticArrayData, which is
// POD.
// Hide array inside a lambda
#define Q_ARRAY_LITERAL(Type, ...) \
([]() -> QArrayDataPointerRef<Type> { \
static Type const data[] = { __VA_ARGS__ }; \
enum { Size = sizeof(data) / sizeof(data[0]) }; \
return { nullptr, const_cast<Type *>(data), Size }; \
}())
/**/
namespace QtPrivate {
struct Q_CORE_EXPORT QContainerImplHelper
{

View File

@ -81,9 +81,10 @@ public:
Q_CHECK_PTR(d);
}
QArrayDataPointer(QArrayDataPointerRef<T> dd) noexcept
: d(dd.ptr), ptr(dd.data), size(dd.size)
static QArrayDataPointer fromRawData(const T *rawData, size_t length)
{
Q_ASSERT(rawData || !length);
return { nullptr, const_cast<T *>(rawData), length };
}
QArrayDataPointer &operator=(const QArrayDataPointer &other) noexcept
@ -235,6 +236,20 @@ inline void qSwap(QArrayDataPointer<T> &p1, QArrayDataPointer<T> &p2) noexcept
p1.swap(p2);
}
////////////////////////////////////////////////////////////////////////////////
// Q_ARRAY_LITERAL
// The idea here is to place a (read-only) copy of header and array data in an
// mmappable portion of the executable (typically, .rodata section).
// Hide array inside a lambda
#define Q_ARRAY_LITERAL(Type, ...) \
([]() -> QArrayDataPointer<Type> { \
static Type const data[] = { __VA_ARGS__ }; \
return QArrayDataPointer<Type>::fromRawData(const_cast<Type *>(data), std::size(data)); \
}())
/**/
QT_END_NAMESPACE
#endif // include guard

View File

@ -267,11 +267,6 @@
The value type of \c InputIterator must be convertible to \c T.
*/
/*!
\fn template <typename T> QList<T>::QList(QArrayDataPointerRef<T> ref)
\internal
*/
/*! \fn template <typename T> QList<T>::~QList()
Destroys the list.

View File

@ -39,7 +39,7 @@
// This file is auto-generated by gradientgen. DO NOT EDIT!
static QArrayDataPointerRef<QGradientStop> qt_preset_gradient_stops(QGradient::Preset preset)
static QList<QGradientStop> qt_preset_gradient_stops(QGradient::Preset preset)
{
Q_ASSERT(preset < QGradient::NumPresets);
switch (preset) {

View File

@ -71,17 +71,6 @@ public:
d->copyAppend(begin, end);
}
SimpleVector(QArrayDataPointerRef<T> ptr)
: d(ptr)
{
}
template <size_t N>
explicit SimpleVector(QStaticArrayData<T, N> &ptr)
: d(static_cast<Data *>(&ptr.header), ptr.data, N)
{
}
SimpleVector(Data *header, T *data, size_t len = 0)
: d(header, data, len)
{
@ -340,7 +329,7 @@ public:
static SimpleVector fromRawData(const T *data, size_t size)
{
return SimpleVector(Data::fromRawData(data, size));
return SimpleVector({ nullptr, const_cast<T *>(data), size });
}
private:

View File

@ -1241,22 +1241,6 @@ void tst_QArrayData::literals()
QCOMPARE(d.data()[i], wchar_t('A' + i));
}
{
SimpleVector<char> v = Q_ARRAY_LITERAL(char, "ABCDEFGHIJ");
QVERIFY(!v.isNull());
QVERIFY(!v.isEmpty());
QCOMPARE(v.size(), size_t(11));
// v.capacity() is unspecified, for now
QVERIFY(v.isStatic());
QCOMPARE((void*)(const char*)(v.constBegin() + v.size()), (void*)(const char*)v.constEnd());
for (int i = 0; i < 10; ++i)
QCOMPARE(const_(v)[i], char('A' + i));
QCOMPARE(const_(v)[10], char('\0'));
}
struct LiteralType {
int value;
Q_DECL_CONSTEXPR LiteralType(int v = 0) : value(v) {}
@ -1313,21 +1297,6 @@ void tst_QArrayData::variadicLiterals()
QCOMPARE(d.data()[i][1], '\0');
}
}
{
SimpleVector<int> v = Q_ARRAY_LITERAL(int, 0, 1, 2, 3, 4, 5, 6);
QVERIFY(!v.isNull());
QVERIFY(!v.isEmpty());
QCOMPARE(v.size(), size_t(7));
// v.capacity() is unspecified, for now
QVERIFY(v.isStatic());
QCOMPARE((const int *)(v.constBegin() + v.size()), (const int *)v.constEnd());
for (int i = 0; i < 7; ++i)
QCOMPARE(const_(v)[i], i);
}
}
// std::remove_reference is in C++11, but requires library support

View File

@ -264,7 +264,7 @@ int main()
p.printLine("// This file is auto-generated by gradientgen. DO NOT EDIT!");
p.printLine();
p.printLine("static QArrayDataPointerRef<QGradientStop> qt_preset_gradient_stops(QGradient::Preset preset)");
p.printLine("static QList<QGradientStop> qt_preset_gradient_stops(QGradient::Preset preset)");
p.printLine("{");
{
Printer::Indenter i(p);