Mark QByteArrayList as metatype built-in type.

As a side effects it also adds core templates types to the tests

Change-Id: I0e3338e0bffdf21982aa83d404c83288e54411f4
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@digia.com>
This commit is contained in:
Jędrzej Nowacki 2014-02-13 16:59:28 +01:00
parent e311f7ac8b
commit 918038ad57
7 changed files with 86 additions and 26 deletions

View File

@ -65,6 +65,7 @@
# include "qjsonobject.h"
# include "qjsonarray.h"
# include "qjsondocument.h"
# include "qbytearraylist.h"
#endif
#ifndef QT_NO_GEOM_VARIANT
@ -270,6 +271,7 @@ struct DefinedTypesFilter {
\value QJsonDocument QJsonDocument
\value QModelIndex QModelIndex
\value QUuid QUuid
\value QByteArrayList QByteArrayList
\value User Base value for user types
\value UnknownType This is an invalid type id. It is returned from QMetaType for types that are not registered
@ -1191,6 +1193,9 @@ bool QMetaType::save(QDataStream &stream, int type, const void *data)
case QMetaType::QVariant:
stream << *static_cast<const NS(QVariant)*>(data);
break;
case QMetaType::QByteArrayList:
stream << *static_cast<const NS(QByteArrayList)*>(data);
break;
#endif
case QMetaType::QByteArray:
stream << *static_cast<const NS(QByteArray)*>(data);
@ -1414,6 +1419,9 @@ bool QMetaType::load(QDataStream &stream, int type, void *data)
case QMetaType::QVariant:
stream >> *static_cast< NS(QVariant)*>(data);
break;
case QMetaType::QByteArrayList:
stream >> *static_cast< NS(QByteArrayList)*>(data);
break;
#endif
case QMetaType::QByteArray:
stream >> *static_cast< NS(QByteArray)*>(data);

View File

@ -127,6 +127,7 @@ inline Q_DECL_CONSTEXPR int qMetaTypeId();
F(QVariantMap, 8, QVariantMap) \
F(QVariantList, 9, QVariantList) \
F(QVariantHash, 28, QVariantHash) \
F(QByteArrayList, 49, QByteArrayList) \
#define QT_FOR_EACH_STATIC_GUI_CLASS(F)\
F(QFont, 64, QFont) \
@ -180,6 +181,7 @@ inline Q_DECL_CONSTEXPR int qMetaTypeId();
F(QVariantList, -1, QVariantList, "QList<QVariant>") \
F(QVariantMap, -1, QVariantMap, "QMap<QString,QVariant>") \
F(QVariantHash, -1, QVariantHash, "QHash<QString,QVariant>") \
F(QByteArrayList, -1, QByteArrayList, "QList<QByteArray>") \
#define QT_FOR_EACH_STATIC_TYPE(F)\
QT_FOR_EACH_STATIC_PRIMITIVE_TYPE(F)\
@ -393,7 +395,7 @@ public:
QT_FOR_EACH_STATIC_TYPE(QT_DEFINE_METATYPE_ID)
FirstCoreType = Bool,
LastCoreType = QJsonDocument,
LastCoreType = QByteArrayList,
FirstGuiType = QFont,
LastGuiType = QPolygonF,
FirstWidgetsType = QSizePolicy,
@ -419,7 +421,7 @@ public:
QEasingCurve = 29, QUuid = 30, QVariant = 41, QModelIndex = 42,
QRegularExpression = 44,
QJsonValue = 45, QJsonObject = 46, QJsonArray = 47, QJsonDocument = 48,
QObjectStar = 39, SChar = 40,
QByteArrayList = 49, QObjectStar = 39, SChar = 40,
Void = 43,
QVariantMap = 8, QVariantList = 9, QVariantHash = 28,
QFont = 64, QPixmap = 65, QBrush = 66, QColor = 67, QPalette = 68,
@ -1760,6 +1762,7 @@ QT_FOR_EACH_STATIC_WIDGETS_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER)
typedef QList<QVariant> QVariantList;
typedef QMap<QString, QVariant> QVariantMap;
typedef QHash<QString, QVariant> QVariantHash;
typedef QList<QByteArray> QByteArrayList;
#define Q_DECLARE_METATYPE_TEMPLATE_1ARG(SINGLE_ARG_TEMPLATE) \
QT_BEGIN_NAMESPACE \

View File

@ -207,6 +207,7 @@ template<> struct TypeDefinition<QJsonObject> { static const bool IsAvailable =
template<> struct TypeDefinition<QJsonValue> { static const bool IsAvailable = false; };
template<> struct TypeDefinition<QModelIndex> { static const bool IsAvailable = false; };
template<> struct TypeDefinition<QUrl> { static const bool IsAvailable = false; };
template<> struct TypeDefinition<QByteArrayList> { static const bool IsAvailable = false; };
#endif
#ifdef QT_NO_GEOM_VARIANT
template<> struct TypeDefinition<QRect> { static const bool IsAvailable = false; };

View File

@ -61,6 +61,7 @@
#include "qjsonobject.h"
#include "qjsonarray.h"
#include "qjsondocument.h"
#include "qbytearraylist.h"
#endif
#include "private/qvariant_p.h"
#include "qmetatype_p.h"
@ -2841,6 +2842,7 @@ bool QVariant::canConvert(int targetTypeId) const
if (targetTypeId == QMetaType::QVariantList
&& (d.type == QMetaType::QVariantList
|| d.type == QMetaType::QStringList
|| d.type == QMetaType::QByteArrayList
|| QMetaType::hasRegisteredConverterFunction(d.type,
qMetaTypeId<QtMetaTypePrivate::QSequentialIterableImpl>()))) {
return true;

View File

@ -51,6 +51,9 @@
#include <QtCore/qstring.h>
#include <QtCore/qstringlist.h>
#include <QtCore/qobject.h>
#ifndef QT_BOOTSTRAPPED
#include <QtCore/qbytearraylist.h>
#endif
QT_BEGIN_NAMESPACE
@ -713,6 +716,11 @@ namespace QtPrivate {
if (v.userType() == qMetaTypeId<QStringList>()) {
return QSequentialIterable(QtMetaTypePrivate::QSequentialIterableImpl(reinterpret_cast<const QStringList*>(v.constData())));
}
#ifndef QT_BOOTSTRAPPED
if (v.userType() == qMetaTypeId<QByteArrayList>()) {
return QSequentialIterable(QtMetaTypePrivate::QSequentialIterableImpl(reinterpret_cast<const QByteArrayList*>(v.constData())));
}
#endif
return QSequentialIterable(v.value<QtMetaTypePrivate::QSequentialIterableImpl>());
}
};
@ -735,7 +743,7 @@ namespace QtPrivate {
{
static QVariantList invoke(const QVariant &v)
{
if (v.userType() == qMetaTypeId<QStringList>() || QMetaType::hasRegisteredConverterFunction(v.userType(), qMetaTypeId<QtMetaTypePrivate::QSequentialIterableImpl>())) {
if (v.userType() == qMetaTypeId<QStringList>() || v.userType() == qMetaTypeId<QByteArrayList>() || QMetaType::hasRegisteredConverterFunction(v.userType(), qMetaTypeId<QtMetaTypePrivate::QSequentialIterableImpl>())) {
QSequentialIterable iter = QVariantValueHelperInterface<QSequentialIterable>::invoke(v);
QVariantList l;
l.reserve(iter.size());

View File

@ -398,7 +398,8 @@ void tst_QMetaType::typeName()
QT_FOR_EACH_STATIC_CORE_POINTER(F) \
#define FOR_EACH_COMPLEX_CORE_METATYPE(F) \
QT_FOR_EACH_STATIC_CORE_CLASS(F)
QT_FOR_EACH_STATIC_CORE_CLASS(F) \
QT_FOR_EACH_STATIC_CORE_TEMPLATE(F)
#define FOR_EACH_CORE_METATYPE(F) \
FOR_EACH_PRIMITIVE_METATYPE(F) \
@ -489,6 +490,18 @@ template<> struct TestValueFactory<QMetaType::Double> {
template<> struct TestValueFactory<QMetaType::QByteArray> {
static QByteArray *create() { return new QByteArray(QByteArray("QByteArray")); }
};
template<> struct TestValueFactory<QMetaType::QByteArrayList> {
static QByteArrayList *create() { return new QByteArrayList(QByteArrayList() << "Q" << "Byte" << "Array" << "List"); }
};
template<> struct TestValueFactory<QMetaType::QVariantMap> {
static QVariantMap *create() { return new QVariantMap(); }
};
template<> struct TestValueFactory<QMetaType::QVariantHash> {
static QVariantHash *create() { return new QVariantHash(); }
};
template<> struct TestValueFactory<QMetaType::QVariantList> {
static QVariantList *create() { return new QVariantList(QVariantList() << 123 << "Q" << "Variant" << "List"); }
};
template<> struct TestValueFactory<QMetaType::QChar> {
static QChar *create() { return new QChar(QChar('q')); }
};
@ -1371,12 +1384,12 @@ void tst_QMetaType::automaticTemplateRegistration()
}
{
QList<QByteArray> bytearrayList;
bytearrayList << QByteArray("foo");
QVERIFY(QVariant::fromValue(bytearrayList).value<QList<QByteArray> >().first() == QByteArray("foo"));
QVector<QList<QByteArray> > vectorList;
vectorList << bytearrayList;
QVERIFY(QVariant::fromValue(vectorList).value<QVector<QList<QByteArray> > >().first().first() == QByteArray("foo"));
QList<unsigned> unsignedList;
unsignedList << 123;
QVERIFY(QVariant::fromValue(unsignedList).value<QList<unsigned> >().first() == 123);
QVector<QList<unsigned> > vectorList;
vectorList << unsignedList;
QVERIFY(QVariant::fromValue(vectorList).value<QVector<QList<unsigned> > >().first().first() == 123);
}
QCOMPARE(::qMetaTypeId<QVariantList>(), (int)QMetaType::QVariantList);

View File

@ -3646,6 +3646,24 @@ struct ContainerAPI<Container, QString>
}
};
template<typename Container>
struct ContainerAPI<Container, QByteArray>
{
static void insert(Container &container, int value)
{
container.push_back(QByteArray::number(value));
}
static bool compare(const QVariant &variant, QByteArray value)
{
return variant.value<QByteArray>() == value;
}
static bool compare(QVariant variant, const QVariant &value)
{
return variant == value;
}
};
// We have no built-in defines to check the stdlib features.
// #define TEST_FORWARD_LIST
@ -3762,12 +3780,12 @@ void tst_QVariant::iterateContainerElements()
{
#ifdef Q_COMPILER_RANGE_FOR
#define TEST_RANGE_FOR(CONTAINER, VALUE_TYPE) \
#define TEST_RANGE_FOR(CONTAINER) \
numSeen = 0; \
containerIter = intList.begin(); \
for (QVariant v : listIter) { \
QVERIFY(ContainerAPI<CONTAINER<VALUE_TYPE > >::compare(v, *containerIter)); \
QVERIFY(ContainerAPI<CONTAINER<VALUE_TYPE > >::compare(v, varList.at(numSeen))); \
QVERIFY(ContainerAPI<CONTAINER >::compare(v, *containerIter)); \
QVERIFY(ContainerAPI<CONTAINER >::compare(v, varList.at(numSeen))); \
++containerIter; \
++numSeen; \
} \
@ -3775,17 +3793,17 @@ void tst_QVariant::iterateContainerElements()
#else
#define TEST_RANGE_FOR(CONTAINER, VALUE_TYPE)
#define TEST_RANGE_FOR(CONTAINER)
#endif
#define TEST_SEQUENTIAL_ITERATION(CONTAINER, VALUE_TYPE) \
#define TEST_SEQUENTIAL_ITERATION_ON_FULL_NAME(CONTAINER) \
{ \
int numSeen = 0; \
CONTAINER<VALUE_TYPE > intList; \
ContainerAPI<CONTAINER<VALUE_TYPE > >::insert(intList, 1); \
ContainerAPI<CONTAINER<VALUE_TYPE > >::insert(intList, 2); \
ContainerAPI<CONTAINER<VALUE_TYPE > >::insert(intList, 3); \
CONTAINER intList; \
ContainerAPI<CONTAINER >::insert(intList, 1); \
ContainerAPI<CONTAINER >::insert(intList, 2); \
ContainerAPI<CONTAINER >::insert(intList, 3); \
\
QVariant listVariant = QVariant::fromValue(intList); \
QVERIFY(listVariant.canConvert<QVariantList>()); \
@ -3794,12 +3812,12 @@ void tst_QVariant::iterateContainerElements()
QSequentialIterable listIter = listVariant.value<QSequentialIterable>(); \
QCOMPARE(varList.size(), listIter.size()); \
\
CONTAINER<VALUE_TYPE >::iterator containerIter = intList.begin(); \
const CONTAINER<VALUE_TYPE >::iterator containerEnd = intList.end(); \
CONTAINER::iterator containerIter = intList.begin(); \
const CONTAINER::iterator containerEnd = intList.end(); \
for (int i = 0; i < listIter.size(); ++i, ++containerIter, ++numSeen) \
{ \
QVERIFY(ContainerAPI<CONTAINER<VALUE_TYPE > >::compare(listIter.at(i), *containerIter)); \
QVERIFY(ContainerAPI<CONTAINER<VALUE_TYPE > >::compare(listIter.at(i), varList.at(i))); \
QVERIFY(ContainerAPI<CONTAINER >::compare(listIter.at(i), *containerIter)); \
QVERIFY(ContainerAPI<CONTAINER >::compare(listIter.at(i), varList.at(i))); \
} \
QCOMPARE(numSeen, (int)std::distance(intList.begin(), intList.end())); \
QCOMPARE(containerIter, containerEnd); \
@ -3807,15 +3825,19 @@ void tst_QVariant::iterateContainerElements()
containerIter = intList.begin(); \
numSeen = 0; \
Q_FOREACH (const QVariant &v, listIter) { \
QVERIFY(ContainerAPI<CONTAINER<VALUE_TYPE > >::compare(v, *containerIter)); \
QVERIFY(ContainerAPI<CONTAINER<VALUE_TYPE > >::compare(v, varList.at(numSeen))); \
QVERIFY(ContainerAPI<CONTAINER >::compare(v, *containerIter)); \
QVERIFY(ContainerAPI<CONTAINER >::compare(v, varList.at(numSeen))); \
++containerIter; \
++numSeen; \
} \
QCOMPARE(numSeen, (int)std::distance(intList.begin(), intList.end())); \
TEST_RANGE_FOR(CONTAINER, VALUE_TYPE) \
TEST_RANGE_FOR(CONTAINER) \
}
#define TEST_SEQUENTIAL_ITERATION(CONTAINER, VALUE_TYPE) \
TEST_SEQUENTIAL_ITERATION_ON_FULL_NAME(CONTAINER<VALUE_TYPE > )
TEST_SEQUENTIAL_ITERATION(QVector, int)
TEST_SEQUENTIAL_ITERATION(QVector, QVariant)
TEST_SEQUENTIAL_ITERATION(QVector, QString)
@ -3825,6 +3847,7 @@ void tst_QVariant::iterateContainerElements()
TEST_SEQUENTIAL_ITERATION(QList, int)
TEST_SEQUENTIAL_ITERATION(QList, QVariant)
TEST_SEQUENTIAL_ITERATION(QList, QString)
TEST_SEQUENTIAL_ITERATION(QList, QByteArray)
TEST_SEQUENTIAL_ITERATION(QStack, int)
TEST_SEQUENTIAL_ITERATION(QStack, QVariant)
TEST_SEQUENTIAL_ITERATION(QStack, QString)
@ -3834,6 +3857,8 @@ void tst_QVariant::iterateContainerElements()
TEST_SEQUENTIAL_ITERATION(std::list, int)
TEST_SEQUENTIAL_ITERATION(std::list, QVariant)
TEST_SEQUENTIAL_ITERATION(std::list, QString)
TEST_SEQUENTIAL_ITERATION_ON_FULL_NAME(QStringList)
TEST_SEQUENTIAL_ITERATION_ON_FULL_NAME(QByteArrayList)
#ifdef TEST_FORWARD_LIST
TEST_SEQUENTIAL_ITERATION(std::forward_list, int)