Implement implicit constructors for built-in classes.

Change-Id: I6b0b104bc1da3252d014615c50b81830de42e722
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
This commit is contained in:
Stephen Kelly 2012-07-24 16:10:52 +02:00 committed by Qt by Nokia
parent 17c74529e4
commit 9784dd8d06
7 changed files with 266 additions and 0 deletions

View File

@ -1227,6 +1227,48 @@ QVariant::QVariant(const char *val)
Constructs a new variant with an easing curve value, \a val. Constructs a new variant with an easing curve value, \a val.
*/ */
/*!
\since 5.0
\fn QVariant::QVariant(const QUuid &val)
Constructs a new variant with an uuid value, \a val.
*/
/*!
\since 5.0
\fn QVariant::QVariant(const QModelIndex &val)
Constructs a new variant with an modelIndex value, \a val.
*/
/*!
\since 5.0
\fn QVariant::QVariant(const QJsonValue &val)
Constructs a new variant with a json value, \a val.
*/
/*!
\since 5.0
\fn QVariant::QVariant(const QJsonObject &val)
Constructs a new variant with a json object value, \a val.
*/
/*!
\since 5.0
\fn QVariant::QVariant(const QJsonArray &val)
Constructs a new variant with a json array value, \a val.
*/
/*!
\since 5.0
\fn QVariant::QVariant(const QJsonDocument &val)
Constructs a new variant with a json document value, \a val.
*/
/*! /*!
\fn QVariant::QVariant(const QByteArray &val) \fn QVariant::QVariant(const QByteArray &val)
@ -1448,6 +1490,12 @@ QVariant::QVariant(const QLocale &l) { d.is_null = false; d.type = Locale; v_con
QVariant::QVariant(const QRegExp &regExp) { d.is_null = false; d.type = RegExp; v_construct<QRegExp>(&d, regExp); } QVariant::QVariant(const QRegExp &regExp) { d.is_null = false; d.type = RegExp; v_construct<QRegExp>(&d, regExp); }
#ifndef QT_BOOTSTRAPPED #ifndef QT_BOOTSTRAPPED
QVariant::QVariant(const QRegularExpression &re) { d.is_null = false; d.type = QMetaType::QRegularExpression; v_construct<QRegularExpression>(&d, re); } QVariant::QVariant(const QRegularExpression &re) { d.is_null = false; d.type = QMetaType::QRegularExpression; v_construct<QRegularExpression>(&d, re); }
QVariant::QVariant(const QUuid &uuid) { d.is_null = false; d.type = QMetaType::QUuid; v_construct<QUuid>(&d, uuid); }
QVariant::QVariant(const QModelIndex &modelIndex) { d.is_null = false; d.type = QMetaType::QModelIndex; v_construct<QModelIndex>(&d, modelIndex); }
QVariant::QVariant(const QJsonValue &jsonValue) { d.is_null = false; d.type = QMetaType::QJsonValue; v_construct<QJsonValue>(&d, jsonValue); }
QVariant::QVariant(const QJsonObject &jsonObject) { d.is_null = false; d.type = QMetaType::QJsonObject; v_construct<QJsonObject>(&d, jsonObject); }
QVariant::QVariant(const QJsonArray &jsonArray) { d.is_null = false; d.type = QMetaType::QJsonArray; v_construct<QJsonArray>(&d, jsonArray); }
QVariant::QVariant(const QJsonDocument &jsonDocument) { d.is_null = false; d.type = QMetaType::QJsonDocument; v_construct<QJsonDocument>(&d, jsonDocument); }
#endif // QT_BOOTSTRAPPED #endif // QT_BOOTSTRAPPED
#endif // QT_NO_REGEXP #endif // QT_NO_REGEXP
@ -2127,6 +2175,84 @@ QRegularExpression QVariant::toRegularExpression() const
return qVariantToHelper<QRegularExpression>(d, handlerManager); return qVariantToHelper<QRegularExpression>(d, handlerManager);
} }
#endif #endif
/*!
\since 5.0
Returns the variant as a QUuid if the variant has type() \l
QUuid; otherwise returns a default constructed QUuid.
\sa canConvert(), convert()
*/
QUuid QVariant::toUuid() const
{
return qVariantToHelper<QUuid>(d, handlerManager);
}
/*!
\since 5.0
Returns the variant as a QModelIndex if the variant has type() \l
QModelIndex; otherwise returns a default constructed QModelIndex.
\sa canConvert(), convert()
*/
QModelIndex QVariant::toModelIndex() const
{
return qVariantToHelper<QModelIndex>(d, handlerManager);
}
/*!
\since 5.0
Returns the variant as a QJsonValue if the variant has type() \l
QJsonValue; otherwise returns a default constructed QJsonValue.
\sa canConvert(), convert()
*/
QJsonValue QVariant::toJsonValue() const
{
return qVariantToHelper<QJsonValue>(d, handlerManager);
}
/*!
\since 5.0
Returns the variant as a QJsonObject if the variant has type() \l
QJsonObject; otherwise returns a default constructed QJsonObject.
\sa canConvert(), convert()
*/
QJsonObject QVariant::toJsonObject() const
{
return qVariantToHelper<QJsonObject>(d, handlerManager);
}
/*!
\since 5.0
Returns the variant as a QJsonArray if the variant has type() \l
QJsonArray; otherwise returns a default constructed QJsonArray.
\sa canConvert(), convert()
*/
QJsonArray QVariant::toJsonArray() const
{
return qVariantToHelper<QJsonArray>(d, handlerManager);
}
/*!
\since 5.0
Returns the variant as a QJsonDocument if the variant has type() \l
QJsonDocument; otherwise returns a default constructed QJsonDocument.
\sa canConvert(), convert()
*/
QJsonDocument QVariant::toJsonDocument() const
{
return qVariantToHelper<QJsonDocument>(d, handlerManager);
}
#endif #endif
/*! /*!

View File

@ -247,6 +247,12 @@ class Q_CORE_EXPORT QVariant
#ifndef QT_BOOTSTRAPPED #ifndef QT_BOOTSTRAPPED
QVariant(const QUrl &url); QVariant(const QUrl &url);
QVariant(const QEasingCurve &easing); QVariant(const QEasingCurve &easing);
QVariant(const QUuid &uuid);
QVariant(const QModelIndex &modelIndex);
QVariant(const QJsonValue &jsonValue);
QVariant(const QJsonObject &jsonObject);
QVariant(const QJsonArray &jsonArray);
QVariant(const QJsonDocument &jsonDocument);
#endif #endif
QVariant& operator=(const QVariant &other); QVariant& operator=(const QVariant &other);
@ -314,6 +320,12 @@ class Q_CORE_EXPORT QVariant
#ifndef QT_BOOTSTRAPPED #ifndef QT_BOOTSTRAPPED
QUrl toUrl() const; QUrl toUrl() const;
QEasingCurve toEasingCurve() const; QEasingCurve toEasingCurve() const;
QUuid toUuid() const;
QModelIndex toModelIndex() const;
QJsonValue toJsonValue() const;
QJsonObject toJsonObject() const;
QJsonArray toJsonArray() const;
QJsonDocument toJsonDocument() const;
#endif #endif
#ifndef QT_NO_DATASTREAM #ifndef QT_NO_DATASTREAM

View File

@ -990,4 +990,9 @@ QPolygonF QPolygonF::subtracted(const QPolygonF &r) const
return subject.subtracted(clip).toFillPolygon(); return subject.subtracted(clip).toFillPolygon();
} }
QPolygonF::operator QVariant() const
{
return QVariant(QMetaType::QPolygonF, this);
}
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -142,6 +142,8 @@ public:
/*implicit*/ QPolygonF(const QPolygon &a); /*implicit*/ QPolygonF(const QPolygon &a);
inline void swap(QPolygonF &other) { QVector<QPointF>::swap(other); } // prevent QVector<QPointF><->QPolygonF swaps inline void swap(QPolygonF &other) { QVector<QPointF>::swap(other); } // prevent QVector<QPointF><->QPolygonF swaps
operator QVariant() const;
inline void translate(qreal dx, qreal dy); inline void translate(qreal dx, qreal dy);
void translate(const QPointF &offset); void translate(const QPointF &offset);

View File

@ -234,6 +234,8 @@ private slots:
void loadQt5Stream(); void loadQt5Stream();
void saveQt5Stream_data(); void saveQt5Stream_data();
void saveQt5Stream(); void saveQt5Stream();
void implicitConstruction();
private: private:
void dataStream_data(QDataStream::Version version); void dataStream_data(QDataStream::Version version);
void loadQVariantFromDataStream(QDataStream::Version version); void loadQVariantFromDataStream(QDataStream::Version version);
@ -3254,5 +3256,53 @@ void tst_QVariant::debugStreamType()
QVERIFY(msgHandler.testPassed()); QVERIFY(msgHandler.testPassed());
} }
void tst_QVariant::implicitConstruction()
{
// This is a compile-time test
QVariant v;
#define FOR_EACH_CORE_CLASS(F) \
F(Char) \
F(String) \
F(StringList) \
F(ByteArray) \
F(BitArray) \
F(Date) \
F(Time) \
F(DateTime) \
F(Url) \
F(Locale) \
F(Rect) \
F(RectF) \
F(Size) \
F(SizeF) \
F(Line) \
F(LineF) \
F(Point) \
F(PointF) \
F(RegExp) \
F(EasingCurve) \
F(Uuid) \
F(ModelIndex) \
F(RegularExpression) \
F(JsonValue) \
F(JsonObject) \
F(JsonArray) \
F(JsonDocument) \
#define CONSTRUCT(TYPE) \
{ \
Q##TYPE t; \
v = t; \
t = v.to##TYPE(); \
QVERIFY(true); \
}
FOR_EACH_CORE_CLASS(CONSTRUCT)
#undef CONSTRUCT
#undef FOR_EACH_CORE_CLASS
}
QTEST_MAIN(tst_QVariant) QTEST_MAIN(tst_QVariant)
#include "tst_qvariant.moc" #include "tst_qvariant.moc"

View File

@ -59,6 +59,8 @@
#include <qvector3d.h> #include <qvector3d.h>
#include <qvector4d.h> #include <qvector4d.h>
#include <qquaternion.h> #include <qquaternion.h>
#include <qtextdocument.h>
#include <qtextformat.h>
#include <qfont.h> #include <qfont.h>
#include "tst_qvariant_common.h" #include "tst_qvariant_common.h"
@ -118,6 +120,8 @@ private slots:
void debugStream_data(); void debugStream_data();
void debugStream(); void debugStream();
void implicitConstruction();
void guiVariantAtExit(); void guiVariantAtExit();
}; };
@ -626,6 +630,49 @@ void tst_QGuiVariant::debugStream()
QVERIFY(msgHandler.testPassed()); QVERIFY(msgHandler.testPassed());
} }
void tst_QGuiVariant::implicitConstruction()
{
// This is a compile-time test
QVariant v;
#define FOR_EACH_GUI_CLASS(F) \
F(Font) \
F(Pixmap) \
F(Brush) \
F(Color) \
F(Palette) \
F(Icon) \
F(Image) \
F(Polygon) \
F(Region) \
F(Bitmap) \
F(Cursor) \
F(KeySequence) \
F(Pen) \
F(TextLength) \
F(TextFormat) \
F(Matrix) \
F(Transform) \
F(Matrix4x4) \
F(Vector2D) \
F(Vector3D) \
F(Vector4D) \
F(Quaternion) \
F(PolygonF) \
#define CONSTRUCT(TYPE) \
{ \
Q##TYPE t; \
v = t; \
QVERIFY(true); \
}
FOR_EACH_GUI_CLASS(CONSTRUCT)
#undef CONSTRUCT
#undef FOR_EACH_GUI_CLASS
}
void tst_QGuiVariant::guiVariantAtExit() void tst_QGuiVariant::guiVariantAtExit()
{ {
// crash test, it should not crash at QGuiApplication exit // crash test, it should not crash at QGuiApplication exit

View File

@ -69,6 +69,8 @@ private slots:
void debugStream_data(); void debugStream_data();
void debugStream(); void debugStream();
void implicitConstruction();
void widgetsVariantAtExit(); void widgetsVariantAtExit();
}; };
@ -254,5 +256,27 @@ void tst_QWidgetsVariant::widgetsVariantAtExit()
QVERIFY(true); QVERIFY(true);
} }
void tst_QWidgetsVariant::implicitConstruction()
{
// This is a compile-time test
QVariant v;
#define FOR_EACH_WIDGETS_CLASS(F) \
F(SizePolicy) \
#define CONSTRUCT(TYPE) \
{ \
Q##TYPE t; \
v = t; \
QVERIFY(true); \
}
FOR_EACH_WIDGETS_CLASS(CONSTRUCT)
#undef CONSTRUCT
#undef FOR_EACH_WIDGETS_CLASS
}
QTEST_MAIN(tst_QWidgetsVariant) QTEST_MAIN(tst_QWidgetsVariant)
#include "tst_qwidgetsvariant.moc" #include "tst_qwidgetsvariant.moc"