Update spec to build Qt 5.0
[profile/ivi/qtbase.git] / src / corelib / kernel / qvariant.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qvariant.h"
43 #include "qbitarray.h"
44 #include "qbytearray.h"
45 #include "qdatastream.h"
46 #include "qdebug.h"
47 #include "qmap.h"
48 #include "qdatetime.h"
49 #include "qeasingcurve.h"
50 #include "qlist.h"
51 #include "qregularexpression.h"
52 #include "qstring.h"
53 #include "qstringlist.h"
54 #include "qurl.h"
55 #include "qlocale.h"
56 #include "quuid.h"
57 #ifndef QT_BOOTSTRAPPED
58 #include "qabstractitemmodel.h"
59 #include "qjsonvalue.h"
60 #include "qjsonobject.h"
61 #include "qjsonarray.h"
62 #include "qjsondocument.h"
63 #endif
64 #include "private/qvariant_p.h"
65 #include "qmetatype_p.h"
66
67 #ifndef QT_NO_GEOM_VARIANT
68 #include "qsize.h"
69 #include "qpoint.h"
70 #include "qrect.h"
71 #include "qline.h"
72 #endif
73
74 #include <float.h>
75
76 QT_BEGIN_NAMESPACE
77
78 #ifndef DBL_DIG
79 #  define DBL_DIG 10
80 #endif
81 #ifndef FLT_DIG
82 #  define FLT_DIG 6
83 #endif
84
85 namespace {
86 class HandlersManager
87 {
88     static const QVariant::Handler *Handlers[QModulesPrivate::ModulesCount];
89 public:
90     const QVariant::Handler *operator[] (const uint typeId) const
91     {
92         return Handlers[QModulesPrivate::moduleForType(typeId)];
93     }
94
95     void registerHandler(const QModulesPrivate::Names name, const QVariant::Handler *handler)
96     {
97         Handlers[name] = handler;
98     }
99 };
100 }  // namespace
101
102 namespace {
103 struct CoreTypesFilter {
104     template<typename T>
105     struct Acceptor {
106         static const bool IsAccepted = QModulesPrivate::QTypeModuleInfo<T>::IsCore && QtMetaTypePrivate::TypeDefinition<T>::IsAvailable;
107     };
108 };
109 } // annonymous
110
111 namespace { // annonymous used to hide QVariant handlers
112
113 static void construct(QVariant::Private *x, const void *copy)
114 {
115     QVariantConstructor<CoreTypesFilter> constructor(x, copy);
116     QMetaTypeSwitcher::switcher<void>(constructor, x->type, 0);
117 }
118
119 static void clear(QVariant::Private *d)
120 {
121     QVariantDestructor<CoreTypesFilter> cleaner(d);
122     QMetaTypeSwitcher::switcher<void>(cleaner, d->type, 0);
123 }
124
125 static bool isNull(const QVariant::Private *d)
126 {
127     QVariantIsNull<CoreTypesFilter> isNull(d);
128     return QMetaTypeSwitcher::switcher<bool>(isNull, d->type, 0);
129 }
130
131 /*!
132   \internal
133
134   Compares \a a to \a b. The caller guarantees that \a a and \a b
135   are of the same type.
136  */
137 static bool compare(const QVariant::Private *a, const QVariant::Private *b)
138 {
139     QVariantComparator<CoreTypesFilter> comparator(a, b);
140     return QMetaTypeSwitcher::switcher<bool>(comparator, a->type, 0);
141 }
142
143 /*!
144   \internal
145  */
146 static qlonglong qMetaTypeNumber(const QVariant::Private *d)
147 {
148     switch (d->type) {
149     case QMetaType::Int:
150         return d->data.i;
151     case QMetaType::LongLong:
152         return d->data.ll;
153     case QMetaType::Char:
154         return qlonglong(d->data.c);
155     case QMetaType::SChar:
156         return qlonglong(d->data.sc);
157     case QMetaType::Short:
158         return qlonglong(d->data.s);
159     case QMetaType::Long:
160         return qlonglong(d->data.l);
161     case QMetaType::Float:
162         return qRound64(d->data.f);
163     case QVariant::Double:
164         return qRound64(d->data.d);
165     }
166     Q_ASSERT(false);
167     return 0;
168 }
169
170 static qulonglong qMetaTypeUNumber(const QVariant::Private *d)
171 {
172     switch (d->type) {
173     case QVariant::UInt:
174         return d->data.u;
175     case QVariant::ULongLong:
176         return d->data.ull;
177     case QMetaType::UChar:
178         return d->data.uc;
179     case QMetaType::UShort:
180         return d->data.us;
181     case QMetaType::ULong:
182         return d->data.ul;
183     }
184     Q_ASSERT(false);
185     return 0;
186 }
187
188 static qlonglong qConvertToNumber(const QVariant::Private *d, bool *ok)
189 {
190     *ok = true;
191
192     switch (uint(d->type)) {
193     case QVariant::String:
194         return v_cast<QString>(d)->toLongLong(ok);
195     case QVariant::Char:
196         return v_cast<QChar>(d)->unicode();
197     case QVariant::ByteArray:
198         return v_cast<QByteArray>(d)->toLongLong(ok);
199     case QVariant::Bool:
200         return qlonglong(d->data.b);
201     case QVariant::Double:
202     case QVariant::Int:
203     case QMetaType::Char:
204     case QMetaType::SChar:
205     case QMetaType::Short:
206     case QMetaType::Long:
207     case QMetaType::Float:
208     case QMetaType::LongLong:
209         return qMetaTypeNumber(d);
210     case QVariant::ULongLong:
211     case QVariant::UInt:
212     case QMetaType::UChar:
213     case QMetaType::UShort:
214     case QMetaType::ULong:
215         return qlonglong(qMetaTypeUNumber(d));
216     }
217
218     *ok = false;
219     return Q_INT64_C(0);
220 }
221
222 static qulonglong qConvertToUnsignedNumber(const QVariant::Private *d, bool *ok)
223 {
224     *ok = true;
225
226     switch (uint(d->type)) {
227     case QVariant::String:
228         return v_cast<QString>(d)->toULongLong(ok);
229     case QVariant::Char:
230         return v_cast<QChar>(d)->unicode();
231     case QVariant::ByteArray:
232         return v_cast<QByteArray>(d)->toULongLong(ok);
233     case QVariant::Bool:
234         return qulonglong(d->data.b);
235     case QVariant::Double:
236     case QVariant::Int:
237     case QMetaType::Char:
238     case QMetaType::SChar:
239     case QMetaType::Short:
240     case QMetaType::Long:
241     case QMetaType::Float:
242     case QMetaType::LongLong:
243         return qulonglong(qMetaTypeNumber(d));
244     case QVariant::ULongLong:
245     case QVariant::UInt:
246     case QMetaType::UChar:
247     case QMetaType::UShort:
248     case QMetaType::ULong:
249         return qMetaTypeUNumber(d);
250     }
251
252     *ok = false;
253     return Q_UINT64_C(0);
254 }
255
256 template<typename TInput, typename LiteralWrapper>
257 inline bool qt_convertToBool(const QVariant::Private *const d)
258 {
259     TInput str = v_cast<TInput>(d)->toLower();
260     return !(str == LiteralWrapper("0") || str == LiteralWrapper("false") || str.isEmpty());
261 }
262
263 /*!
264  \internal
265
266  Converts \a d to type \a t, which is placed in \a result.
267  */
268 static bool convert(const QVariant::Private *d, int t, void *result, bool *ok)
269 {
270     Q_ASSERT(d->type != uint(t));
271     Q_ASSERT(result);
272
273     bool dummy;
274     if (!ok)
275         ok = &dummy;
276
277     switch (uint(t)) {
278 #ifndef QT_BOOTSTRAPPED
279     case QVariant::Url:
280         switch (d->type) {
281         case QVariant::String:
282             *static_cast<QUrl *>(result) = QUrl(*v_cast<QString>(d));
283             break;
284         default:
285             return false;
286         }
287         break;
288 #endif
289     case QVariant::String: {
290         QString *str = static_cast<QString *>(result);
291         switch (d->type) {
292         case QVariant::Char:
293             *str = QString(*v_cast<QChar>(d));
294             break;
295         case QMetaType::Char:
296         case QMetaType::SChar:
297         case QMetaType::UChar:
298             *str = QChar::fromLatin1(d->data.c);
299             break;
300         case QMetaType::Short:
301         case QMetaType::Long:
302         case QVariant::Int:
303         case QVariant::LongLong:
304             *str = QString::number(qMetaTypeNumber(d));
305             break;
306         case QVariant::UInt:
307         case QVariant::ULongLong:
308         case QMetaType::UShort:
309         case QMetaType::ULong:
310             *str = QString::number(qMetaTypeUNumber(d));
311             break;
312         case QMetaType::Float:
313             *str = QString::number(d->data.f, 'g', FLT_DIG);
314             break;
315         case QVariant::Double:
316             *str = QString::number(d->data.d, 'g', DBL_DIG);
317             break;
318 #if !defined(QT_NO_DATESTRING)
319         case QVariant::Date:
320             *str = v_cast<QDate>(d)->toString(Qt::ISODate);
321             break;
322         case QVariant::Time:
323             *str = v_cast<QTime>(d)->toString(Qt::ISODate);
324             break;
325         case QVariant::DateTime:
326             *str = v_cast<QDateTime>(d)->toString(Qt::ISODate);
327             break;
328 #endif
329         case QVariant::Bool:
330             *str = QLatin1String(d->data.b ? "true" : "false");
331             break;
332         case QVariant::ByteArray:
333             *str = QString::fromUtf8(v_cast<QByteArray>(d)->constData());
334             break;
335         case QVariant::StringList:
336             if (v_cast<QStringList>(d)->count() == 1)
337                 *str = v_cast<QStringList>(d)->at(0);
338             break;
339 #ifndef QT_BOOTSTRAPPED
340         case QVariant::Url:
341             *str = v_cast<QUrl>(d)->toString();
342             break;
343 #endif
344         case QVariant::Uuid:
345             *str = v_cast<QUuid>(d)->toString();
346             break;
347         default:
348             return false;
349         }
350         break;
351     }
352     case QVariant::Char: {
353         QChar *c = static_cast<QChar *>(result);
354         switch (d->type) {
355         case QVariant::Int:
356         case QVariant::LongLong:
357         case QMetaType::Char:
358         case QMetaType::SChar:
359         case QMetaType::Short:
360         case QMetaType::Long:
361         case QMetaType::Float:
362             *c = QChar(ushort(qMetaTypeNumber(d)));
363             break;
364         case QVariant::UInt:
365         case QVariant::ULongLong:
366         case QMetaType::UChar:
367         case QMetaType::UShort:
368         case QMetaType::ULong:
369             *c = QChar(ushort(qMetaTypeUNumber(d)));
370             break;
371         default:
372             return false;
373         }
374         break;
375     }
376 #ifndef QT_NO_GEOM_VARIANT
377     case QVariant::Size: {
378         QSize *s = static_cast<QSize *>(result);
379         switch (d->type) {
380         case QVariant::SizeF:
381             *s = v_cast<QSizeF>(d)->toSize();
382             break;
383         default:
384             return false;
385         }
386         break;
387     }
388
389     case QVariant::SizeF: {
390         QSizeF *s = static_cast<QSizeF *>(result);
391         switch (d->type) {
392         case QVariant::Size:
393             *s = QSizeF(*(v_cast<QSize>(d)));
394             break;
395         default:
396             return false;
397         }
398         break;
399     }
400
401     case QVariant::Line: {
402         QLine *s = static_cast<QLine *>(result);
403         switch (d->type) {
404         case QVariant::LineF:
405             *s = v_cast<QLineF>(d)->toLine();
406             break;
407         default:
408             return false;
409         }
410         break;
411     }
412
413     case QVariant::LineF: {
414         QLineF *s = static_cast<QLineF *>(result);
415         switch (d->type) {
416         case QVariant::Line:
417             *s = QLineF(*(v_cast<QLine>(d)));
418             break;
419         default:
420             return false;
421         }
422         break;
423     }
424 #endif
425     case QVariant::StringList:
426         if (d->type == QVariant::List) {
427             QStringList *slst = static_cast<QStringList *>(result);
428             const QVariantList *list = v_cast<QVariantList >(d);
429             for (int i = 0; i < list->size(); ++i)
430                 slst->append(list->at(i).toString());
431         } else if (d->type == QVariant::String) {
432             QStringList *slst = static_cast<QStringList *>(result);
433             *slst = QStringList(*v_cast<QString>(d));
434         } else {
435             return false;
436         }
437         break;
438     case QVariant::Date: {
439         QDate *dt = static_cast<QDate *>(result);
440         if (d->type == QVariant::DateTime)
441             *dt = v_cast<QDateTime>(d)->date();
442 #ifndef QT_NO_DATESTRING
443         else if (d->type == QVariant::String)
444             *dt = QDate::fromString(*v_cast<QString>(d), Qt::ISODate);
445 #endif
446         else
447             return false;
448
449         return dt->isValid();
450     }
451     case QVariant::Time: {
452         QTime *t = static_cast<QTime *>(result);
453         switch (d->type) {
454         case QVariant::DateTime:
455             *t = v_cast<QDateTime>(d)->time();
456             break;
457 #ifndef QT_NO_DATESTRING
458         case QVariant::String:
459             *t = QTime::fromString(*v_cast<QString>(d), Qt::ISODate);
460             break;
461 #endif
462         default:
463             return false;
464         }
465         return t->isValid();
466     }
467     case QVariant::DateTime: {
468         QDateTime *dt = static_cast<QDateTime *>(result);
469         switch (d->type) {
470 #ifndef QT_NO_DATESTRING
471         case QVariant::String:
472             *dt = QDateTime::fromString(*v_cast<QString>(d), Qt::ISODate);
473             break;
474 #endif
475         case QVariant::Date:
476             *dt = QDateTime(*v_cast<QDate>(d));
477             break;
478         default:
479             return false;
480         }
481         return dt->isValid();
482     }
483     case QVariant::ByteArray: {
484         QByteArray *ba = static_cast<QByteArray *>(result);
485         switch (d->type) {
486         case QVariant::String:
487             *ba = v_cast<QString>(d)->toUtf8();
488             break;
489         case QVariant::Double:
490             *ba = QByteArray::number(d->data.d, 'g', DBL_DIG);
491             break;
492         case QMetaType::Float:
493             *ba = QByteArray::number(d->data.f, 'g', FLT_DIG);
494             break;
495         case QMetaType::Char:
496         case QMetaType::SChar:
497         case QMetaType::UChar:
498             *ba = QByteArray(1, d->data.c);
499             break;
500         case QVariant::Int:
501         case QVariant::LongLong:
502         case QMetaType::Short:
503         case QMetaType::Long:
504             *ba = QByteArray::number(qMetaTypeNumber(d));
505             break;
506         case QVariant::UInt:
507         case QVariant::ULongLong:
508         case QMetaType::UShort:
509         case QMetaType::ULong:
510             *ba = QByteArray::number(qMetaTypeUNumber(d));
511             break;
512         case QVariant::Bool:
513             *ba = QByteArray(d->data.b ? "true" : "false");
514             break;
515         default:
516             return false;
517         }
518     }
519     break;
520     case QMetaType::Short:
521         *static_cast<short *>(result) = short(qConvertToNumber(d, ok));
522         return *ok;
523     case QMetaType::Long:
524         *static_cast<long *>(result) = long(qConvertToNumber(d, ok));
525         return *ok;
526     case QMetaType::UShort:
527         *static_cast<ushort *>(result) = ushort(qConvertToUnsignedNumber(d, ok));
528         return *ok;
529     case QMetaType::ULong:
530         *static_cast<ulong *>(result) = ulong(qConvertToUnsignedNumber(d, ok));
531         return *ok;
532     case QVariant::Int:
533         *static_cast<int *>(result) = int(qConvertToNumber(d, ok));
534         return *ok;
535     case QVariant::UInt:
536         *static_cast<uint *>(result) = uint(qConvertToUnsignedNumber(d, ok));
537         return *ok;
538     case QVariant::LongLong:
539         *static_cast<qlonglong *>(result) = qConvertToNumber(d, ok);
540         return *ok;
541     case QVariant::ULongLong: {
542         *static_cast<qulonglong *>(result) = qConvertToUnsignedNumber(d, ok);
543         return *ok;
544     }
545     case QMetaType::SChar: {
546         signed char s = qConvertToNumber(d, ok);
547         *static_cast<signed char*>(result) = s;
548         return *ok;
549     }
550     case QMetaType::UChar: {
551         *static_cast<uchar *>(result) = qConvertToUnsignedNumber(d, ok);
552         return *ok;
553     }
554     case QVariant::Bool: {
555         bool *b = static_cast<bool *>(result);
556         switch(d->type) {
557         case QVariant::ByteArray:
558             *b = qt_convertToBool<QByteArray, QByteArray>(d);
559             break;
560         case QVariant::String:
561             *b = qt_convertToBool<QString, QLatin1String>(d);
562             break;
563         case QVariant::Char:
564             *b = !v_cast<QChar>(d)->isNull();
565             break;
566         case QVariant::Double:
567         case QVariant::Int:
568         case QVariant::LongLong:
569         case QMetaType::Char:
570         case QMetaType::SChar:
571         case QMetaType::Short:
572         case QMetaType::Long:
573         case QMetaType::Float:
574             *b = qMetaTypeNumber(d) != Q_INT64_C(0);
575             break;
576         case QVariant::UInt:
577         case QVariant::ULongLong:
578         case QMetaType::UChar:
579         case QMetaType::UShort:
580         case QMetaType::ULong:
581             *b = qMetaTypeUNumber(d) != Q_UINT64_C(0);
582             break;
583         default:
584             *b = false;
585             return false;
586         }
587         break;
588     }
589     case QVariant::Double: {
590         double *f = static_cast<double *>(result);
591         switch (d->type) {
592         case QVariant::String:
593             *f = v_cast<QString>(d)->toDouble(ok);
594             break;
595         case QVariant::ByteArray:
596             *f = v_cast<QByteArray>(d)->toDouble(ok);
597             break;
598         case QVariant::Bool:
599             *f = double(d->data.b);
600             break;
601         case QMetaType::Float:
602             *f = double(d->data.f);
603             break;
604         case QVariant::LongLong:
605         case QVariant::Int:
606         case QMetaType::Char:
607         case QMetaType::SChar:
608         case QMetaType::Short:
609         case QMetaType::Long:
610             *f = double(qMetaTypeNumber(d));
611             break;
612         case QVariant::UInt:
613         case QVariant::ULongLong:
614         case QMetaType::UChar:
615         case QMetaType::UShort:
616         case QMetaType::ULong:
617             *f = double(qMetaTypeUNumber(d));
618             break;
619         default:
620             *f = 0.0;
621             return false;
622         }
623         break;
624     }
625     case QMetaType::Float: {
626         float *f = static_cast<float *>(result);
627         switch (d->type) {
628         case QVariant::String:
629             *f = v_cast<QString>(d)->toFloat(ok);
630             break;
631         case QVariant::ByteArray:
632             *f = v_cast<QByteArray>(d)->toFloat(ok);
633             break;
634         case QVariant::Bool:
635             *f = float(d->data.b);
636             break;
637         case QVariant::Double:
638             *f = float(d->data.d);
639             break;
640         case QVariant::LongLong:
641         case QVariant::Int:
642         case QMetaType::Char:
643         case QMetaType::SChar:
644         case QMetaType::Short:
645         case QMetaType::Long:
646             *f = float(qMetaTypeNumber(d));
647             break;
648         case QVariant::UInt:
649         case QVariant::ULongLong:
650         case QMetaType::UChar:
651         case QMetaType::UShort:
652         case QMetaType::ULong:
653             *f = float(qMetaTypeUNumber(d));
654             break;
655         default:
656             *f = 0.0f;
657             return false;
658         }
659         break;
660     }
661     case QVariant::List:
662         if (d->type == QVariant::StringList) {
663             QVariantList *lst = static_cast<QVariantList *>(result);
664             const QStringList *slist = v_cast<QStringList>(d);
665             for (int i = 0; i < slist->size(); ++i)
666                 lst->append(QVariant(slist->at(i)));
667         } else if (qstrcmp(QMetaType::typeName(d->type), "QList<QVariant>") == 0) {
668             *static_cast<QVariantList *>(result) =
669                 *static_cast<QList<QVariant> *>(d->data.shared->ptr);
670         } else {
671             return false;
672         }
673         break;
674     case QVariant::Map:
675         if (qstrcmp(QMetaType::typeName(d->type), "QMap<QString, QVariant>") == 0) {
676             *static_cast<QVariantMap *>(result) =
677                 *static_cast<QMap<QString, QVariant> *>(d->data.shared->ptr);
678         } else {
679             return false;
680         }
681         break;
682     case QVariant::Hash:
683         if (qstrcmp(QMetaType::typeName(d->type), "QHash<QString, QVariant>") == 0) {
684             *static_cast<QVariantHash *>(result) =
685                 *static_cast<QHash<QString, QVariant> *>(d->data.shared->ptr);
686         } else {
687             return false;
688         }
689         break;
690 #ifndef QT_NO_GEOM_VARIANT
691     case QVariant::Rect:
692         if (d->type == QVariant::RectF)
693             *static_cast<QRect *>(result) = (v_cast<QRectF>(d))->toRect();
694         else
695             return false;
696         break;
697     case QVariant::RectF:
698         if (d->type == QVariant::Rect)
699             *static_cast<QRectF *>(result) = *v_cast<QRect>(d);
700         else
701             return false;
702         break;
703     case QVariant::PointF:
704         if (d->type == QVariant::Point)
705             *static_cast<QPointF *>(result) = *v_cast<QPoint>(d);
706         else
707             return false;
708         break;
709     case QVariant::Point:
710         if (d->type == QVariant::PointF)
711             *static_cast<QPoint *>(result) = (v_cast<QPointF>(d))->toPoint();
712         else
713             return false;
714         break;
715     case QMetaType::Char:
716     {
717         *static_cast<qint8 *>(result) = qint8(qConvertToNumber(d, ok));
718         return *ok;
719     }
720 #endif
721     case QVariant::Uuid:
722         switch (d->type) {
723         case QVariant::String:
724             *static_cast<QUuid *>(result) = QUuid(*v_cast<QString>(d));
725             break;
726         default:
727             return false;
728         }
729         break;
730     default:
731         return false;
732     }
733     return true;
734 }
735
736 #if !defined(QT_NO_DEBUG_STREAM)
737 static void streamDebug(QDebug dbg, const QVariant &v)
738 {
739     QVariant::Private *d = const_cast<QVariant::Private *>(&v.data_ptr());
740     QVariantDebugStream<CoreTypesFilter> stream(dbg, d);
741     QMetaTypeSwitcher::switcher<void>(stream, d->type, 0);
742 }
743 #endif
744
745 const QVariant::Handler qt_kernel_variant_handler = {
746     construct,
747     clear,
748     isNull,
749 #ifndef QT_NO_DATASTREAM
750     0,
751     0,
752 #endif
753     compare,
754     convert,
755     0,
756 #if !defined(QT_NO_DEBUG_STREAM)
757     streamDebug
758 #else
759     0
760 #endif
761 };
762
763 static void dummyConstruct(QVariant::Private *, const void *) { Q_ASSERT_X(false, "QVariant", "Trying to construct an unknown type"); }
764 static void dummyClear(QVariant::Private *) { Q_ASSERT_X(false, "QVariant", "Trying to clear an unknown type"); }
765 static bool dummyIsNull(const QVariant::Private *d) { Q_ASSERT_X(false, "QVariant::isNull", "Trying to call isNull on an unknown type"); return d->is_null; }
766 static bool dummyCompare(const QVariant::Private *, const QVariant::Private *) { Q_ASSERT_X(false, "QVariant", "Trying to compare an unknown types"); return false; }
767 static bool dummyConvert(const QVariant::Private *, int, void *, bool *) { Q_ASSERT_X(false, "QVariant", "Trying to convert an unknown type"); return false; }
768 #if !defined(QT_NO_DEBUG_STREAM)
769 static void dummyStreamDebug(QDebug, const QVariant &) { Q_ASSERT_X(false, "QVariant", "Trying to convert an unknown type"); }
770 #endif
771 const QVariant::Handler qt_dummy_variant_handler = {
772     dummyConstruct,
773     dummyClear,
774     dummyIsNull,
775 #ifndef QT_NO_DATASTREAM
776     0,
777     0,
778 #endif
779     dummyCompare,
780     dummyConvert,
781     0,
782 #if !defined(QT_NO_DEBUG_STREAM)
783     dummyStreamDebug
784 #else
785     0
786 #endif
787 };
788
789 static void customConstruct(QVariant::Private *d, const void *copy)
790 {
791     const QMetaType type(d->type);
792     const uint size = type.sizeOf();
793     if (!size) {
794         qWarning("Trying to construct an instance of an invalid type, type id: %i", d->type);
795         d->type = QVariant::Invalid;
796         return;
797     }
798
799     // this logic should match with QVariantIntegrator::CanUseInternalSpace
800     if (size <= sizeof(QVariant::Private::Data)
801             && (type.flags() & QMetaType::MovableType)) {
802         type.construct(&d->data.ptr, copy);
803         d->is_shared = false;
804     } else {
805         void *ptr = type.create(copy);
806         d->is_shared = true;
807         d->data.shared = new QVariant::PrivateShared(ptr);
808     }
809 }
810
811 static void customClear(QVariant::Private *d)
812 {
813     if (!d->is_shared) {
814         QMetaType::destruct(d->type, &d->data.ptr);
815     } else {
816         QMetaType::destroy(d->type, d->data.shared->ptr);
817         delete d->data.shared;
818     }
819 }
820
821 static bool customIsNull(const QVariant::Private *d)
822 {
823     return d->is_null;
824 }
825
826 static bool customCompare(const QVariant::Private *a, const QVariant::Private *b)
827 {
828     const char *const typeName = QMetaType::typeName(a->type);
829     if (Q_UNLIKELY(!typeName) && Q_LIKELY(!QMetaType::isRegistered(a->type)))
830         qFatal("QVariant::compare: type %d unknown to QVariant.", a->type);
831
832     const void *a_ptr = a->is_shared ? a->data.shared->ptr : &(a->data.ptr);
833     const void *b_ptr = b->is_shared ? b->data.shared->ptr : &(b->data.ptr);
834
835     uint typeNameLen = qstrlen(typeName);
836     if (typeNameLen > 0 && typeName[typeNameLen - 1] == '*')
837         return *static_cast<void *const *>(a_ptr) == *static_cast<void *const *>(b_ptr);
838
839     if (a->is_null && b->is_null)
840         return true;
841
842     return !memcmp(a_ptr, b_ptr, QMetaType::sizeOf(a->type));
843 }
844
845 static bool customConvert(const QVariant::Private *, int, void *, bool *ok)
846 {
847     if (ok)
848         *ok = false;
849     return false;
850 }
851
852 #if !defined(QT_NO_DEBUG_STREAM)
853 static void customStreamDebug(QDebug dbg, const QVariant &variant) {
854 #ifndef QT_BOOTSTRAPPED
855     QMetaType::TypeFlags flags = QMetaType::typeFlags(variant.userType());
856     if (flags & QMetaType::PointerToQObject)
857         dbg.nospace() << variant.value<QObject*>();
858 #else
859     Q_UNUSED(dbg);
860     Q_UNUSED(variant);
861 #endif
862 }
863 #endif
864
865 const QVariant::Handler qt_custom_variant_handler = {
866     customConstruct,
867     customClear,
868     customIsNull,
869 #ifndef QT_NO_DATASTREAM
870     0,
871     0,
872 #endif
873     customCompare,
874     customConvert,
875     0,
876 #if !defined(QT_NO_DEBUG_STREAM)
877     customStreamDebug
878 #else
879     0
880 #endif
881 };
882
883 } // annonymous used to hide QVariant handlers
884
885 static HandlersManager handlerManager;
886 Q_STATIC_ASSERT_X(!QModulesPrivate::Core, "Initialization assumes that ModulesNames::Core is 0");
887 const QVariant::Handler *HandlersManager::Handlers[QModulesPrivate::ModulesCount]
888                                         = { &qt_kernel_variant_handler, &qt_dummy_variant_handler,
889                                             &qt_dummy_variant_handler, &qt_custom_variant_handler };
890
891 Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler()
892 {
893     return &qt_kernel_variant_handler;
894 }
895
896 Q_CORE_EXPORT void QVariantPrivate::registerHandler(const int /* Modules::Names */name, const QVariant::Handler *handler)
897 {
898     handlerManager.registerHandler(static_cast<QModulesPrivate::Names>(name), handler);
899 }
900
901 /*!
902     \class QVariant
903     \inmodule QtCore
904     \brief The QVariant class acts like a union for the most common Qt data types.
905
906     \ingroup objectmodel
907     \ingroup shared
908
909
910     Because C++ forbids unions from including types that have
911     non-default constructors or destructors, most interesting Qt
912     classes cannot be used in unions. Without QVariant, this would be
913     a problem for QObject::property() and for database work, etc.
914
915     A QVariant object holds a single value of a single type() at a
916     time. (Some type()s are multi-valued, for example a string list.)
917     You can find out what type, T, the variant holds, convert it to a
918     different type using convert(), get its value using one of the
919     toT() functions (e.g., toSize()) and check whether the type can
920     be converted to a particular type using canConvert().
921
922     The methods named toT() (e.g., toInt(), toString()) are const. If
923     you ask for the stored type, they return a copy of the stored
924     object. If you ask for a type that can be generated from the
925     stored type, toT() copies and converts and leaves the object
926     itself unchanged. If you ask for a type that cannot be generated
927     from the stored type, the result depends on the type; see the
928     function documentation for details.
929
930     Here is some example code to demonstrate the use of QVariant:
931
932     \snippet code/src_corelib_kernel_qvariant.cpp 0
933
934     You can even store QList<QVariant> and QMap<QString, QVariant>
935     values in a variant, so you can easily construct arbitrarily
936     complex data structures of arbitrary types. This is very powerful
937     and versatile, but may prove less memory and speed efficient than
938     storing specific types in standard data structures.
939
940     QVariant also supports the notion of null values, where you can
941     have a defined type with no value set. However, note that QVariant
942     types can only be cast when they have had a value set.
943
944     \snippet code/src_corelib_kernel_qvariant.cpp 1
945
946     QVariant can be extended to support other types than those
947     mentioned in the \l Type enum. See the \l QMetaType documentation
948     for details.
949
950     \section1 A Note on GUI Types
951
952     Because QVariant is part of the QtCore library, it cannot provide
953     conversion functions to data types defined in QtGui, such as
954     QColor, QImage, and QPixmap. In other words, there is no \c
955     toColor() function. Instead, you can use the QVariant::value() or
956     the qvariant_cast() template function. For example:
957
958     \snippet code/src_corelib_kernel_qvariant.cpp 2
959
960     The inverse conversion (e.g., from QColor to QVariant) is
961     automatic for all data types supported by QVariant, including
962     GUI-related types:
963
964     \snippet code/src_corelib_kernel_qvariant.cpp 3
965
966     \section1 Using canConvert() and convert() Consecutively
967
968     When using canConvert() and convert() consecutively, it is possible for
969     canConvert() to return true, but convert() to return false. This
970     is typically because canConvert() only reports the general ability of
971     QVariant to convert between types given suitable data; it is still
972     possible to supply data which cannot actually be converted.
973
974     For example, canConvert() would return true when called on a variant
975     containing a string because, in principle, QVariant is able to convert
976     strings of numbers to integers.
977     However, if the string contains non-numeric characters, it cannot be
978     converted to an integer, and any attempt to convert it will fail.
979     Hence, it is important to have both functions return true for a
980     successful conversion.
981
982     \sa QMetaType
983 */
984
985 /*!
986     \obsolete Use QMetaType::Type instead
987     \enum QVariant::Type
988
989     This enum type defines the types of variable that a QVariant can
990     contain.
991
992     \value Invalid  no type
993     \value BitArray  a QBitArray
994     \value Bitmap  a QBitmap
995     \value Bool  a bool
996     \value Brush  a QBrush
997     \value ByteArray  a QByteArray
998     \value Char  a QChar
999     \value Color  a QColor
1000     \value Cursor  a QCursor
1001     \value Date  a QDate
1002     \value DateTime  a QDateTime
1003     \value Double  a double
1004     \value EasingCurve a QEasingCurve
1005     \value Uuid a QUuid
1006     \value ModelIndex a QModelIndex
1007     \value Font  a QFont
1008     \value Hash a QVariantHash
1009     \value Icon  a QIcon
1010     \value Image  a QImage
1011     \value Int  an int
1012     \value KeySequence  a QKeySequence
1013     \value Line  a QLine
1014     \value LineF  a QLineF
1015     \value List  a QVariantList
1016     \value Locale  a QLocale
1017     \value LongLong a \l qlonglong
1018     \value Map  a QVariantMap
1019     \value Matrix  a QMatrix
1020     \value Transform  a QTransform
1021     \value Matrix4x4  a QMatrix4x4
1022     \value Palette  a QPalette
1023     \value Pen  a QPen
1024     \value Pixmap  a QPixmap
1025     \value Point  a QPoint
1026     \value PointF  a QPointF
1027     \value Polygon a QPolygon
1028     \value PolygonF a QPolygonF
1029     \value Quaternion  a QQuaternion
1030     \value Rect  a QRect
1031     \value RectF  a QRectF
1032     \value RegExp  a QRegExp
1033     \value RegularExpression  a QRegularExpression
1034     \value Region  a QRegion
1035     \value Size  a QSize
1036     \value SizeF  a QSizeF
1037     \value SizePolicy  a QSizePolicy
1038     \value String  a QString
1039     \value StringList  a QStringList
1040     \value TextFormat  a QTextFormat
1041     \value TextLength  a QTextLength
1042     \value Time  a QTime
1043     \value UInt  a \l uint
1044     \value ULongLong a \l qulonglong
1045     \value Url  a QUrl
1046     \value Vector2D  a QVector2D
1047     \value Vector3D  a QVector3D
1048     \value Vector4D  a QVector4D
1049
1050     \value UserType Base value for user-defined types.
1051
1052     \omitvalue LastGuiType
1053     \omitvalue LastCoreType
1054     \omitvalue LastType
1055 */
1056
1057 /*!
1058     \fn QVariant::QVariant()
1059
1060     Constructs an invalid variant.
1061 */
1062
1063
1064 /*!
1065     \fn QVariant::QVariant(int typeId, const void *copy)
1066
1067     Constructs variant of type \a typeId, and initializes with
1068     \a copy if \a copy is not 0.
1069
1070     Note that you have to pass the address of the variable you want stored.
1071
1072     Usually, you never have to use this constructor, use QVariant::fromValue()
1073     instead to construct variants from the pointer types represented by
1074     \c QMetaType::VoidStar, and \c QMetaType::QObjectStar.
1075
1076     \sa QVariant::fromValue(), Type
1077 */
1078
1079 /*!
1080     \fn QVariant::QVariant(Type type)
1081
1082     Constructs a null variant of type \a type.
1083 */
1084
1085
1086
1087 /*!
1088     \fn QVariant::create(int type, const void *copy)
1089
1090     \internal
1091
1092     Constructs a variant private of type \a type, and initializes with \a copy if
1093     \a copy is not 0.
1094 */
1095
1096 void QVariant::create(int type, const void *copy)
1097 {
1098     d.type = type;
1099     handlerManager[type]->construct(&d, copy);
1100 }
1101
1102 /*!
1103     \fn QVariant::~QVariant()
1104
1105     Destroys the QVariant and the contained object.
1106
1107     Note that subclasses that reimplement clear() should reimplement
1108     the destructor to call clear(). This destructor calls clear(), but
1109     because it is the destructor, QVariant::clear() is called rather
1110     than a subclass's clear().
1111 */
1112
1113 QVariant::~QVariant()
1114 {
1115     if ((d.is_shared && !d.data.shared->ref.deref()) || (!d.is_shared && d.type > Char))
1116         handlerManager[d.type]->clear(&d);
1117 }
1118
1119 /*!
1120   \fn QVariant::QVariant(const QVariant &p)
1121
1122     Constructs a copy of the variant, \a p, passed as the argument to
1123     this constructor.
1124 */
1125
1126 QVariant::QVariant(const QVariant &p)
1127     : d(p.d)
1128 {
1129     if (d.is_shared) {
1130         d.data.shared->ref.ref();
1131     } else if (p.d.type > Char) {
1132         handlerManager[d.type]->construct(&d, p.constData());
1133         d.is_null = p.d.is_null;
1134     }
1135 }
1136
1137 #ifndef QT_NO_DATASTREAM
1138 /*!
1139     Reads the variant from the data stream, \a s.
1140 */
1141 QVariant::QVariant(QDataStream &s)
1142 {
1143     d.is_null = true;
1144     s >> *this;
1145 }
1146 #endif //QT_NO_DATASTREAM
1147
1148 /*!
1149   \fn QVariant::QVariant(const QString &val)
1150
1151     Constructs a new variant with a string value, \a val.
1152 */
1153
1154 /*!
1155   \fn QVariant::QVariant(QLatin1String val)
1156
1157     Constructs a new variant with a string value, \a val.
1158 */
1159
1160 /*!
1161   \fn QVariant::QVariant(const char *val)
1162
1163     Constructs a new variant with a string value of \a val.
1164     The variant creates a deep copy of \a val into a QString assuming
1165     UTF-8 encoding on the input \a val.
1166
1167     Note that \a val is converted to a QString for storing in the
1168     variant and QVariant::userType() will return QMetaType::QString for
1169     the variant.
1170
1171     You can disable this operator by defining \c
1172     QT_NO_CAST_FROM_ASCII when you compile your applications.
1173 */
1174
1175 #ifndef QT_NO_CAST_FROM_ASCII
1176 QVariant::QVariant(const char *val)
1177 {
1178     QString s = QString::fromUtf8(val);
1179     create(String, &s);
1180 }
1181 #endif
1182
1183 /*!
1184   \fn QVariant::QVariant(const QStringList &val)
1185
1186     Constructs a new variant with a string list value, \a val.
1187 */
1188
1189 /*!
1190   \fn QVariant::QVariant(const QMap<QString, QVariant> &val)
1191
1192     Constructs a new variant with a map of QVariants, \a val.
1193 */
1194
1195 /*!
1196   \fn QVariant::QVariant(const QHash<QString, QVariant> &val)
1197
1198     Constructs a new variant with a hash of QVariants, \a val.
1199 */
1200
1201 /*!
1202   \fn QVariant::QVariant(const QDate &val)
1203
1204     Constructs a new variant with a date value, \a val.
1205 */
1206
1207 /*!
1208   \fn QVariant::QVariant(const QTime &val)
1209
1210     Constructs a new variant with a time value, \a val.
1211 */
1212
1213 /*!
1214   \fn QVariant::QVariant(const QDateTime &val)
1215
1216     Constructs a new variant with a date/time value, \a val.
1217 */
1218
1219 /*!
1220     \since 4.7
1221   \fn QVariant::QVariant(const QEasingCurve &val)
1222
1223     Constructs a new variant with an easing curve value, \a val.
1224 */
1225
1226 /*!
1227     \since 5.0
1228     \fn QVariant::QVariant(const QUuid &val)
1229
1230     Constructs a new variant with an uuid value, \a val.
1231 */
1232
1233 /*!
1234     \since 5.0
1235     \fn QVariant::QVariant(const QModelIndex &val)
1236
1237     Constructs a new variant with an modelIndex value, \a val.
1238 */
1239
1240 /*!
1241     \since 5.0
1242     \fn QVariant::QVariant(const QJsonValue &val)
1243
1244     Constructs a new variant with a json value, \a val.
1245 */
1246
1247 /*!
1248     \since 5.0
1249     \fn QVariant::QVariant(const QJsonObject &val)
1250
1251     Constructs a new variant with a json object value, \a val.
1252 */
1253
1254 /*!
1255     \since 5.0
1256     \fn QVariant::QVariant(const QJsonArray &val)
1257
1258     Constructs a new variant with a json array value, \a val.
1259 */
1260
1261 /*!
1262     \since 5.0
1263     \fn QVariant::QVariant(const QJsonDocument &val)
1264
1265     Constructs a new variant with a json document value, \a val.
1266 */
1267
1268 /*!
1269   \fn QVariant::QVariant(const QByteArray &val)
1270
1271     Constructs a new variant with a bytearray value, \a val.
1272 */
1273
1274 /*!
1275   \fn QVariant::QVariant(const QBitArray &val)
1276
1277     Constructs a new variant with a bitarray value, \a val.
1278 */
1279
1280 /*!
1281   \fn QVariant::QVariant(const QPoint &val)
1282
1283   Constructs a new variant with a point value of \a val.
1284  */
1285
1286 /*!
1287   \fn QVariant::QVariant(const QPointF &val)
1288
1289   Constructs a new variant with a point value of \a val.
1290  */
1291
1292 /*!
1293   \fn QVariant::QVariant(const QRectF &val)
1294
1295   Constructs a new variant with a rect value of \a val.
1296  */
1297
1298 /*!
1299   \fn QVariant::QVariant(const QLineF &val)
1300
1301   Constructs a new variant with a line value of \a val.
1302  */
1303
1304 /*!
1305   \fn QVariant::QVariant(const QLine &val)
1306
1307   Constructs a new variant with a line value of \a val.
1308  */
1309
1310 /*!
1311   \fn QVariant::QVariant(const QRect &val)
1312
1313   Constructs a new variant with a rect value of \a val.
1314  */
1315
1316 /*!
1317   \fn QVariant::QVariant(const QSize &val)
1318
1319   Constructs a new variant with a size value of \a val.
1320  */
1321
1322 /*!
1323   \fn QVariant::QVariant(const QSizeF &val)
1324
1325   Constructs a new variant with a size value of \a val.
1326  */
1327
1328 /*!
1329   \fn QVariant::QVariant(const QUrl &val)
1330
1331   Constructs a new variant with a url value of \a val.
1332  */
1333
1334 /*!
1335   \fn QVariant::QVariant(int val)
1336
1337     Constructs a new variant with an integer value, \a val.
1338 */
1339
1340 /*!
1341   \fn QVariant::QVariant(uint val)
1342
1343     Constructs a new variant with an unsigned integer value, \a val.
1344 */
1345
1346 /*!
1347   \fn QVariant::QVariant(qlonglong val)
1348
1349     Constructs a new variant with a long long integer value, \a val.
1350 */
1351
1352 /*!
1353   \fn QVariant::QVariant(qulonglong val)
1354
1355     Constructs a new variant with an unsigned long long integer value, \a val.
1356 */
1357
1358
1359 /*!
1360   \fn QVariant::QVariant(bool val)
1361
1362     Constructs a new variant with a boolean value, \a val.
1363 */
1364
1365 /*!
1366   \fn QVariant::QVariant(double val)
1367
1368     Constructs a new variant with a floating point value, \a val.
1369 */
1370
1371 /*!
1372   \fn QVariant::QVariant(float val)
1373
1374     Constructs a new variant with a floating point value, \a val.
1375     \since 4.6
1376 */
1377
1378 /*!
1379     \fn QVariant::QVariant(const QList<QVariant> &val)
1380
1381     Constructs a new variant with a list value, \a val.
1382 */
1383
1384 /*!
1385   \fn QVariant::QVariant(QChar c)
1386
1387   Constructs a new variant with a char value, \a c.
1388 */
1389
1390 /*!
1391   \fn QVariant::QVariant(const QLocale &l)
1392
1393   Constructs a new variant with a locale value, \a l.
1394 */
1395
1396 /*!
1397   \fn QVariant::QVariant(const QRegExp &regExp)
1398
1399   Constructs a new variant with the regexp value \a regExp.
1400 */
1401
1402 /*!
1403   \fn QVariant::QVariant(const QRegularExpression &re)
1404
1405   \since 5.0
1406
1407   Constructs a new variant with the regular expression value \a re.
1408 */
1409
1410 QVariant::QVariant(Type type)
1411 { create(type, 0); }
1412 QVariant::QVariant(int typeId, const void *copy)
1413 { create(typeId, copy); d.is_null = false; }
1414
1415 /*!
1416     \internal
1417     flags is true if it is a pointer type
1418  */
1419 QVariant::QVariant(int typeId, const void *copy, uint flags)
1420 {
1421     if (flags) { //type is a pointer type
1422         d.type = typeId;
1423         d.data.ptr = *reinterpret_cast<void *const*>(copy);
1424     } else {
1425         create(typeId, copy);
1426     }
1427     d.is_null = false;
1428 }
1429
1430 QVariant::QVariant(int val)
1431     : d(Int)
1432 { d.data.i = val; }
1433 QVariant::QVariant(uint val)
1434     : d(UInt)
1435 { d.data.u = val; }
1436 QVariant::QVariant(qlonglong val)
1437     : d(LongLong)
1438 { d.data.ll = val; }
1439 QVariant::QVariant(qulonglong val)
1440     : d(ULongLong)
1441 { d.data.ull = val; }
1442 QVariant::QVariant(bool val)
1443     : d(Bool)
1444 { d.data.b = val; }
1445 QVariant::QVariant(double val)
1446     : d(Double)
1447 { d.data.d = val; }
1448 QVariant::QVariant(float val)
1449     : d(QMetaType::Float)
1450 { d.data.f = val; }
1451
1452 QVariant::QVariant(const QByteArray &val)
1453     : d(ByteArray)
1454 { v_construct<QByteArray>(&d, val); }
1455 QVariant::QVariant(const QBitArray &val)
1456     : d(BitArray)
1457 { v_construct<QBitArray>(&d, val);  }
1458 QVariant::QVariant(const QString &val)
1459     : d(String)
1460 { v_construct<QString>(&d, val);  }
1461 QVariant::QVariant(QChar val)
1462     : d(Char)
1463 { v_construct<QChar>(&d, val);  }
1464 QVariant::QVariant(QLatin1String val)
1465     : d(String)
1466 { v_construct<QString>(&d, val); }
1467 QVariant::QVariant(const QStringList &val)
1468     : d(StringList)
1469 { v_construct<QStringList>(&d, val); }
1470
1471 QVariant::QVariant(const QDate &val)
1472     : d(Date)
1473 { v_construct<QDate>(&d, val); }
1474 QVariant::QVariant(const QTime &val)
1475     : d(Time)
1476 { v_construct<QTime>(&d, val); }
1477 QVariant::QVariant(const QDateTime &val)
1478     : d(DateTime)
1479 { v_construct<QDateTime>(&d, val); }
1480 #ifndef QT_BOOTSTRAPPED
1481 QVariant::QVariant(const QEasingCurve &val)
1482     : d(EasingCurve)
1483 { v_construct<QEasingCurve>(&d, val); }
1484 #endif
1485 QVariant::QVariant(const QList<QVariant> &list)
1486     : d(List)
1487 { v_construct<QVariantList>(&d, list); }
1488 QVariant::QVariant(const QMap<QString, QVariant> &map)
1489     : d(Map)
1490 { v_construct<QVariantMap>(&d, map); }
1491 QVariant::QVariant(const QHash<QString, QVariant> &hash)
1492     : d(Hash)
1493 { v_construct<QVariantHash>(&d, hash); }
1494 #ifndef QT_NO_GEOM_VARIANT
1495 QVariant::QVariant(const QPoint &pt)
1496     : d(Point)
1497 { v_construct<QPoint>(&d, pt); }
1498 QVariant::QVariant(const QPointF &pt)
1499     : d(PointF)
1500 { v_construct<QPointF>(&d, pt); }
1501 QVariant::QVariant(const QRectF &r)
1502     : d(RectF)
1503 { v_construct<QRectF>(&d, r); }
1504 QVariant::QVariant(const QLineF &l)
1505     : d(LineF)
1506 { v_construct<QLineF>(&d, l); }
1507 QVariant::QVariant(const QLine &l)
1508     : d(Line)
1509 { v_construct<QLine>(&d, l); }
1510 QVariant::QVariant(const QRect &r)
1511     : d(Rect)
1512 { v_construct<QRect>(&d, r); }
1513 QVariant::QVariant(const QSize &s)
1514     : d(Size)
1515 { v_construct<QSize>(&d, s); }
1516 QVariant::QVariant(const QSizeF &s)
1517     : d(SizeF)
1518 { v_construct<QSizeF>(&d, s); }
1519 #endif
1520 #ifndef QT_BOOTSTRAPPED
1521 QVariant::QVariant(const QUrl &u)
1522     : d(Url)
1523 { v_construct<QUrl>(&d, u); }
1524 #endif
1525 QVariant::QVariant(const QLocale &l)
1526     : d(Locale)
1527 { v_construct<QLocale>(&d, l); }
1528 #ifndef QT_NO_REGEXP
1529 QVariant::QVariant(const QRegExp &regExp)
1530     : d(RegExp)
1531 { v_construct<QRegExp>(&d, regExp); }
1532 #ifndef QT_BOOTSTRAPPED
1533 QVariant::QVariant(const QRegularExpression &re)
1534     : d(RegularExpression)
1535 { v_construct<QRegularExpression>(&d, re); }
1536 QVariant::QVariant(const QUuid &uuid)
1537     : d(Uuid)
1538 { v_construct<QUuid>(&d, uuid); }
1539 QVariant::QVariant(const QModelIndex &modelIndex)
1540     : d(ModelIndex)
1541 { v_construct<QModelIndex>(&d, modelIndex); }
1542 QVariant::QVariant(const QJsonValue &jsonValue)
1543     : d(QMetaType::QJsonValue)
1544 { v_construct<QJsonValue>(&d, jsonValue); }
1545 QVariant::QVariant(const QJsonObject &jsonObject)
1546     : d(QMetaType::QJsonObject)
1547 { v_construct<QJsonObject>(&d, jsonObject); }
1548 QVariant::QVariant(const QJsonArray &jsonArray)
1549     : d(QMetaType::QJsonArray)
1550 { v_construct<QJsonArray>(&d, jsonArray); }
1551 QVariant::QVariant(const QJsonDocument &jsonDocument)
1552     : d(QMetaType::QJsonDocument)
1553 { v_construct<QJsonDocument>(&d, jsonDocument); }
1554 #endif // QT_BOOTSTRAPPED
1555 #endif // QT_NO_REGEXP
1556
1557 /*!
1558     Returns the storage type of the value stored in the variant.
1559     Although this function is declared as returning QVariant::Type,
1560     the return value should be interpreted as QMetaType::Type. In
1561     particular, QVariant::UserType is returned here only if the value
1562     is equal or greater than QMetaType::User.
1563
1564     Note that return values in the ranges QVariant::Char through
1565     QVariant::RegExp and QVariant::Font through QVariant::Transform
1566     correspond to the values in the ranges QMetaType::QChar through
1567     QMetaType::QRegExp and QMetaType::QFont through QMetaType::QQuaternion.
1568
1569     Pay particular attention when working with char and QChar
1570     variants.  Note that there is no QVariant constructor specifically
1571     for type char, but there is one for QChar. For a variant of type
1572     QChar, this function returns QVariant::Char, which is the same as
1573     QMetaType::QChar, but for a variant of type \c char, this function
1574     returns QMetaType::Char, which is \e not the same as
1575     QVariant::Char.
1576
1577     Also note that the types \c void*, \c long, \c short, \c unsigned
1578     \c long, \c unsigned \c short, \c unsigned \c char, \c float, \c
1579     QObject*, and \c QWidget* are represented in QMetaType::Type but
1580     not in QVariant::Type, and they can be returned by this function.
1581     However, they are considered to be user defined types when tested
1582     against QVariant::Type.
1583
1584     To test whether an instance of QVariant contains a data type that
1585     is compatible with the data type you are interested in, use
1586     canConvert().
1587 */
1588
1589 QVariant::Type QVariant::type() const
1590 {
1591     return d.type >= QMetaType::User ? UserType : static_cast<Type>(d.type);
1592 }
1593
1594 /*!
1595     Returns the storage type of the value stored in the variant. For
1596     non-user types, this is the same as type().
1597
1598     \sa type()
1599 */
1600
1601 int QVariant::userType() const
1602 {
1603     return d.type;
1604 }
1605
1606 /*!
1607     Assigns the value of the variant \a variant to this variant.
1608 */
1609 QVariant& QVariant::operator=(const QVariant &variant)
1610 {
1611     if (this == &variant)
1612         return *this;
1613
1614     clear();
1615     if (variant.d.is_shared) {
1616         variant.d.data.shared->ref.ref();
1617         d = variant.d;
1618     } else if (variant.d.type > Char) {
1619         d.type = variant.d.type;
1620         handlerManager[d.type]->construct(&d, variant.constData());
1621         d.is_null = variant.d.is_null;
1622     } else {
1623         d = variant.d;
1624     }
1625
1626     return *this;
1627 }
1628
1629 /*!
1630     \fn void QVariant::swap(QVariant &other)
1631     \since 4.8
1632
1633     Swaps variant \a other with this variant. This operation is very
1634     fast and never fails.
1635 */
1636
1637 /*!
1638     \fn void QVariant::detach()
1639
1640     \internal
1641 */
1642
1643 void QVariant::detach()
1644 {
1645     if (!d.is_shared || d.data.shared->ref.load() == 1)
1646         return;
1647
1648     Private dd;
1649     dd.type = d.type;
1650     handlerManager[d.type]->construct(&dd, constData());
1651     if (!d.data.shared->ref.deref())
1652         handlerManager[d.type]->clear(&d);
1653     d.data.shared = dd.data.shared;
1654 }
1655
1656 /*!
1657     \fn bool QVariant::isDetached() const
1658
1659     \internal
1660 */
1661
1662 /*!
1663     Returns the name of the type stored in the variant. The returned
1664     strings describe the C++ datatype used to store the data: for
1665     example, "QFont", "QString", or "QVariantList". An Invalid
1666     variant returns 0.
1667 */
1668 const char *QVariant::typeName() const
1669 {
1670     return QMetaType::typeName(d.type);
1671 }
1672
1673 /*!
1674     Convert this variant to type Invalid and free up any resources
1675     used.
1676 */
1677 void QVariant::clear()
1678 {
1679     if ((d.is_shared && !d.data.shared->ref.deref()) || (!d.is_shared && d.type > Char))
1680         handlerManager[d.type]->clear(&d);
1681     d.type = Invalid;
1682     d.is_null = true;
1683     d.is_shared = false;
1684 }
1685
1686 /*!
1687     Converts the int representation of the storage type, \a typeId, to
1688     its string representation.
1689
1690     Returns a null pointer if the type is QVariant::Invalid or doesn't exist.
1691 */
1692 const char *QVariant::typeToName(int typeId)
1693 {
1694     return QMetaType::typeName(typeId);
1695 }
1696
1697
1698 /*!
1699     Converts the string representation of the storage type given in \a
1700     name, to its enum representation.
1701
1702     If the string representation cannot be converted to any enum
1703     representation, the variant is set to \c Invalid.
1704 */
1705 QVariant::Type QVariant::nameToType(const char *name)
1706 {
1707     int metaType = QMetaType::type(name);
1708     return metaType <= int(UserType) ? QVariant::Type(metaType) : UserType;
1709 }
1710
1711 #ifndef QT_NO_DATASTREAM
1712 enum { MapFromThreeCount = 36 };
1713 static const ushort mapIdFromQt3ToCurrent[MapFromThreeCount] =
1714 {
1715     QVariant::Invalid,
1716     QVariant::Map,
1717     QVariant::List,
1718     QVariant::String,
1719     QVariant::StringList,
1720     QVariant::Font,
1721     QVariant::Pixmap,
1722     QVariant::Brush,
1723     QVariant::Rect,
1724     QVariant::Size,
1725     QVariant::Color,
1726     QVariant::Palette,
1727     0, // ColorGroup
1728     QVariant::Icon,
1729     QVariant::Point,
1730     QVariant::Image,
1731     QVariant::Int,
1732     QVariant::UInt,
1733     QVariant::Bool,
1734     QVariant::Double,
1735     0, // Buggy ByteArray, QByteArray never had id == 20
1736     QVariant::Polygon,
1737     QVariant::Region,
1738     QVariant::Bitmap,
1739     QVariant::Cursor,
1740     QVariant::SizePolicy,
1741     QVariant::Date,
1742     QVariant::Time,
1743     QVariant::DateTime,
1744     QVariant::ByteArray,
1745     QVariant::BitArray,
1746     QVariant::KeySequence,
1747     QVariant::Pen,
1748     QVariant::LongLong,
1749     QVariant::ULongLong,
1750     QVariant::EasingCurve
1751 };
1752
1753 /*!
1754     Internal function for loading a variant from stream \a s. Use the
1755     stream operators instead.
1756
1757     \internal
1758 */
1759 void QVariant::load(QDataStream &s)
1760 {
1761     clear();
1762
1763     quint32 typeId;
1764     s >> typeId;
1765     if (s.version() < QDataStream::Qt_4_0) {
1766         if (typeId >= MapFromThreeCount)
1767             return;
1768         typeId = mapIdFromQt3ToCurrent[typeId];
1769     } else if (s.version() < QDataStream::Qt_5_0) {
1770         if (typeId == 127 /* QVariant::UserType */) {
1771             typeId = QMetaType::User;
1772         } else if (typeId >= 128 && typeId != QVariant::UserType) {
1773             // In Qt4 id == 128 was FirstExtCoreType. In Qt5 ExtCoreTypes set was merged to CoreTypes
1774             // by moving all ids down by 97.
1775             typeId -= 97;
1776         } else if (typeId == 75 /* QSizePolicy */) {
1777             typeId = QMetaType::QSizePolicy;
1778         } else if (typeId > 75 && typeId <= 86) {
1779             // and as a result these types received lower ids too
1780             // QKeySequence QPen QTextLength QTextFormat QMatrix QTransform QMatrix4x4 QVector2D QVector3D QVector4D QQuaternion
1781             typeId -=1;
1782         }
1783     }
1784
1785     qint8 is_null = false;
1786     if (s.version() >= QDataStream::Qt_4_2)
1787         s >> is_null;
1788     if (typeId == QVariant::UserType) {
1789         QByteArray name;
1790         s >> name;
1791         typeId = QMetaType::type(name.constData());
1792         if (typeId == QMetaType::UnknownType) {
1793             s.setStatus(QDataStream::ReadCorruptData);
1794             return;
1795         }
1796     }
1797     create(typeId, 0);
1798     d.is_null = is_null;
1799
1800     if (!isValid()) {
1801         if (s.version() < QDataStream::Qt_5_0) {
1802         // Since we wrote something, we should read something
1803             QString x;
1804             s >> x;
1805         }
1806         d.is_null = true;
1807         return;
1808     }
1809
1810     // const cast is safe since we operate on a newly constructed variant
1811     if (!QMetaType::load(s, d.type, const_cast<void *>(constData()))) {
1812         s.setStatus(QDataStream::ReadCorruptData);
1813         qWarning("QVariant::load: unable to load type %d.", d.type);
1814     }
1815 }
1816
1817 /*!
1818     Internal function for saving a variant to the stream \a s. Use the
1819     stream operators instead.
1820
1821     \internal
1822 */
1823 void QVariant::save(QDataStream &s) const
1824 {
1825     quint32 typeId = type();
1826     if (s.version() < QDataStream::Qt_4_0) {
1827         int i;
1828         for (i = 0; i <= MapFromThreeCount - 1; ++i) {
1829             if (mapIdFromQt3ToCurrent[i] == typeId) {
1830                 typeId = i;
1831                 break;
1832             }
1833         }
1834         if (i >= MapFromThreeCount) {
1835             s << QVariant();
1836             return;
1837         }
1838     } else if (s.version() < QDataStream::Qt_5_0) {
1839         if (typeId == QMetaType::User) {
1840             typeId = 127; // QVariant::UserType had this value in Qt4
1841         } else if (typeId >= 128 - 97 && typeId <= LastCoreType) {
1842             // In Qt4 id == 128 was FirstExtCoreType. In Qt5 ExtCoreTypes set was merged to CoreTypes
1843             // by moving all ids down by 97.
1844             typeId += 97;
1845         } else if (typeId == QMetaType::QSizePolicy) {
1846             typeId = 75;
1847         } else if (typeId >= QMetaType::QKeySequence && typeId <= QMetaType::QQuaternion) {
1848             // and as a result these types received lower ids too
1849             typeId +=1;
1850         }
1851     }
1852     s << typeId;
1853     if (s.version() >= QDataStream::Qt_4_2)
1854         s << qint8(d.is_null);
1855     if (d.type >= QVariant::UserType) {
1856         s << QMetaType::typeName(userType());
1857     }
1858
1859     if (!isValid()) {
1860         if (s.version() < QDataStream::Qt_5_0)
1861             s << QString();
1862         return;
1863     }
1864
1865     if (!QMetaType::save(s, d.type, constData())) {
1866         qWarning("QVariant::save: unable to save type '%s' (type id: %d).\n", QMetaType::typeName(d.type), d.type);
1867         Q_ASSERT_X(false, "QVariant::save", "Invalid type to save");
1868     }
1869 }
1870
1871 /*!
1872     \since 4.4
1873
1874     Reads a variant \a p from the stream \a s.
1875
1876     \sa{Serializing Qt Data Types}{Format of the QDataStream operators}
1877 */
1878 QDataStream& operator>>(QDataStream &s, QVariant &p)
1879 {
1880     p.load(s);
1881     return s;
1882 }
1883
1884 /*!
1885     Writes a variant \a p to the stream \a s.
1886
1887     \sa{Serializing Qt Data Types}{Format of the QDataStream operators}
1888 */
1889 QDataStream& operator<<(QDataStream &s, const QVariant &p)
1890 {
1891     p.save(s);
1892     return s;
1893 }
1894
1895 /*!
1896     Reads a variant type \a p in enum representation from the stream \a s.
1897 */
1898 QDataStream& operator>>(QDataStream &s, QVariant::Type &p)
1899 {
1900     quint32 u;
1901     s >> u;
1902     p = (QVariant::Type)u;
1903
1904     return s;
1905 }
1906
1907 /*!
1908     Writes a variant type \a p to the stream \a s.
1909 */
1910 QDataStream& operator<<(QDataStream &s, const QVariant::Type p)
1911 {
1912     s << static_cast<quint32>(p);
1913
1914     return s;
1915 }
1916
1917 #endif //QT_NO_DATASTREAM
1918
1919 /*!
1920     \fn bool QVariant::isValid() const
1921
1922     Returns true if the storage type of this variant is not
1923     QVariant::Invalid; otherwise returns false.
1924 */
1925
1926 template <typename T>
1927 inline T qVariantToHelper(const QVariant::Private &d, const HandlersManager &handlerManager)
1928 {
1929     const uint targetType = qMetaTypeId<T>();
1930     if (d.type == targetType)
1931         return *v_cast<T>(&d);
1932
1933     T ret;
1934     handlerManager[d.type]->convert(&d, targetType, &ret, 0);
1935     return ret;
1936 }
1937
1938 /*!
1939     \fn QStringList QVariant::toStringList() const
1940
1941     Returns the variant as a QStringList if the variant has type()
1942     StringList, \l String, or \l List of a type that can be converted
1943     to QString; otherwise returns an empty list.
1944
1945     \sa canConvert(), convert()
1946 */
1947 QStringList QVariant::toStringList() const
1948 {
1949     return qVariantToHelper<QStringList>(d, handlerManager);
1950 }
1951
1952 /*!
1953     Returns the variant as a QString if the variant has type() \l
1954     String, \l Bool, \l ByteArray, \l Char, \l Date, \l DateTime, \l
1955     Double, \l Int, \l LongLong, \l StringList, \l Time, \l UInt, or
1956     \l ULongLong; otherwise returns an empty string.
1957
1958     \sa canConvert(), convert()
1959 */
1960 QString QVariant::toString() const
1961 {
1962     return qVariantToHelper<QString>(d, handlerManager);
1963 }
1964
1965 /*!
1966     Returns the variant as a QMap<QString, QVariant> if the variant
1967     has type() \l Map; otherwise returns an empty map.
1968
1969     \sa canConvert(), convert()
1970 */
1971 QVariantMap QVariant::toMap() const
1972 {
1973     return qVariantToHelper<QVariantMap>(d, handlerManager);
1974 }
1975
1976 /*!
1977     Returns the variant as a QHash<QString, QVariant> if the variant
1978     has type() \l Hash; otherwise returns an empty map.
1979
1980     \sa canConvert(), convert()
1981 */
1982 QVariantHash QVariant::toHash() const
1983 {
1984     return qVariantToHelper<QVariantHash>(d, handlerManager);
1985 }
1986
1987 /*!
1988     \fn QDate QVariant::toDate() const
1989
1990     Returns the variant as a QDate if the variant has type() \l Date,
1991     \l DateTime, or \l String; otherwise returns an invalid date.
1992
1993     If the type() is \l String, an invalid date will be returned if the
1994     string cannot be parsed as a Qt::ISODate format date.
1995
1996     \sa canConvert(), convert()
1997 */
1998 QDate QVariant::toDate() const
1999 {
2000     return qVariantToHelper<QDate>(d, handlerManager);
2001 }
2002
2003 /*!
2004     \fn QTime QVariant::toTime() const
2005
2006     Returns the variant as a QTime if the variant has type() \l Time,
2007     \l DateTime, or \l String; otherwise returns an invalid time.
2008
2009     If the type() is \l String, an invalid time will be returned if
2010     the string cannot be parsed as a Qt::ISODate format time.
2011
2012     \sa canConvert(), convert()
2013 */
2014 QTime QVariant::toTime() const
2015 {
2016     return qVariantToHelper<QTime>(d, handlerManager);
2017 }
2018
2019 /*!
2020     \fn QDateTime QVariant::toDateTime() const
2021
2022     Returns the variant as a QDateTime if the variant has type() \l
2023     DateTime, \l Date, or \l String; otherwise returns an invalid
2024     date/time.
2025
2026     If the type() is \l String, an invalid date/time will be returned
2027     if the string cannot be parsed as a Qt::ISODate format date/time.
2028
2029     \sa canConvert(), convert()
2030 */
2031 QDateTime QVariant::toDateTime() const
2032 {
2033     return qVariantToHelper<QDateTime>(d, handlerManager);
2034 }
2035
2036 /*!
2037     \since 4.7
2038     \fn QEasingCurve QVariant::toEasingCurve() const
2039
2040     Returns the variant as a QEasingCurve if the variant has type() \l
2041     EasingCurve; otherwise returns a default easing curve.
2042
2043     \sa canConvert(), convert()
2044 */
2045 #ifndef QT_BOOTSTRAPPED
2046 QEasingCurve QVariant::toEasingCurve() const
2047 {
2048     return qVariantToHelper<QEasingCurve>(d, handlerManager);
2049 }
2050 #endif
2051
2052 /*!
2053     \fn QByteArray QVariant::toByteArray() const
2054
2055     Returns the variant as a QByteArray if the variant has type() \l
2056     ByteArray or \l String (converted using QString::fromUtf8());
2057     otherwise returns an empty byte array.
2058
2059     \sa canConvert(), convert()
2060 */
2061 QByteArray QVariant::toByteArray() const
2062 {
2063     return qVariantToHelper<QByteArray>(d, handlerManager);
2064 }
2065
2066 #ifndef QT_NO_GEOM_VARIANT
2067 /*!
2068     \fn QPoint QVariant::toPoint() const
2069
2070     Returns the variant as a QPoint if the variant has type()
2071     \l Point or \l PointF; otherwise returns a null QPoint.
2072
2073     \sa canConvert(), convert()
2074 */
2075 QPoint QVariant::toPoint() const
2076 {
2077     return qVariantToHelper<QPoint>(d, handlerManager);
2078 }
2079
2080 /*!
2081     \fn QRect QVariant::toRect() const
2082
2083     Returns the variant as a QRect if the variant has type() \l Rect;
2084     otherwise returns an invalid QRect.
2085
2086     \sa canConvert(), convert()
2087 */
2088 QRect QVariant::toRect() const
2089 {
2090     return qVariantToHelper<QRect>(d, handlerManager);
2091 }
2092
2093 /*!
2094     \fn QSize QVariant::toSize() const
2095
2096     Returns the variant as a QSize if the variant has type() \l Size;
2097     otherwise returns an invalid QSize.
2098
2099     \sa canConvert(), convert()
2100 */
2101 QSize QVariant::toSize() const
2102 {
2103     return qVariantToHelper<QSize>(d, handlerManager);
2104 }
2105
2106 /*!
2107     \fn QSizeF QVariant::toSizeF() const
2108
2109     Returns the variant as a QSizeF if the variant has type() \l
2110     SizeF; otherwise returns an invalid QSizeF.
2111
2112     \sa canConvert(), convert()
2113 */
2114 QSizeF QVariant::toSizeF() const
2115 {
2116     return qVariantToHelper<QSizeF>(d, handlerManager);
2117 }
2118
2119 /*!
2120     \fn QRectF QVariant::toRectF() const
2121
2122     Returns the variant as a QRectF if the variant has type() \l Rect
2123     or \l RectF; otherwise returns an invalid QRectF.
2124
2125     \sa canConvert(), convert()
2126 */
2127 QRectF QVariant::toRectF() const
2128 {
2129     return qVariantToHelper<QRectF>(d, handlerManager);
2130 }
2131
2132 /*!
2133     \fn QLineF QVariant::toLineF() const
2134
2135     Returns the variant as a QLineF if the variant has type() \l
2136     LineF; otherwise returns an invalid QLineF.
2137
2138     \sa canConvert(), convert()
2139 */
2140 QLineF QVariant::toLineF() const
2141 {
2142     return qVariantToHelper<QLineF>(d, handlerManager);
2143 }
2144
2145 /*!
2146     \fn QLine QVariant::toLine() const
2147
2148     Returns the variant as a QLine if the variant has type() \l Line;
2149     otherwise returns an invalid QLine.
2150
2151     \sa canConvert(), convert()
2152 */
2153 QLine QVariant::toLine() const
2154 {
2155     return qVariantToHelper<QLine>(d, handlerManager);
2156 }
2157
2158 /*!
2159     \fn QPointF QVariant::toPointF() const
2160
2161     Returns the variant as a QPointF if the variant has type() \l
2162     Point or \l PointF; otherwise returns a null QPointF.
2163
2164     \sa canConvert(), convert()
2165 */
2166 QPointF QVariant::toPointF() const
2167 {
2168     return qVariantToHelper<QPointF>(d, handlerManager);
2169 }
2170
2171 #endif // QT_NO_GEOM_VARIANT
2172
2173 #ifndef QT_BOOTSTRAPPED
2174 /*!
2175     \fn QUrl QVariant::toUrl() const
2176
2177     Returns the variant as a QUrl if the variant has type()
2178     \l Url; otherwise returns an invalid QUrl.
2179
2180     \sa canConvert(), convert()
2181 */
2182 QUrl QVariant::toUrl() const
2183 {
2184     return qVariantToHelper<QUrl>(d, handlerManager);
2185 }
2186 #endif
2187
2188 /*!
2189     \fn QLocale QVariant::toLocale() const
2190
2191     Returns the variant as a QLocale if the variant has type()
2192     \l Locale; otherwise returns an invalid QLocale.
2193
2194     \sa canConvert(), convert()
2195 */
2196 QLocale QVariant::toLocale() const
2197 {
2198     return qVariantToHelper<QLocale>(d, handlerManager);
2199 }
2200
2201 /*!
2202     \fn QRegExp QVariant::toRegExp() const
2203     \since 4.1
2204
2205     Returns the variant as a QRegExp if the variant has type() \l
2206     RegExp; otherwise returns an empty QRegExp.
2207
2208     \sa canConvert(), convert()
2209 */
2210 #ifndef QT_NO_REGEXP
2211 QRegExp QVariant::toRegExp() const
2212 {
2213     return qVariantToHelper<QRegExp>(d, handlerManager);
2214 }
2215 #endif
2216
2217 /*!
2218     \fn QRegularExpression QVariant::toRegularExpression() const
2219     \since 5.0
2220
2221     Returns the variant as a QRegularExpression if the variant has type() \l
2222     QRegularExpression; otherwise returns an empty QRegularExpression.
2223
2224     \sa canConvert(), convert()
2225 */
2226 #ifndef QT_BOOTSTRAPPED
2227 #ifndef QT_NO_REGEXP
2228 QRegularExpression QVariant::toRegularExpression() const
2229 {
2230     return qVariantToHelper<QRegularExpression>(d, handlerManager);
2231 }
2232 #endif
2233
2234 /*!
2235     \since 5.0
2236
2237     Returns the variant as a QUuid if the variant has type() \l
2238     QUuid; otherwise returns a default constructed QUuid.
2239
2240     \sa canConvert(), convert()
2241 */
2242 QUuid QVariant::toUuid() const
2243 {
2244     return qVariantToHelper<QUuid>(d, handlerManager);
2245 }
2246
2247 /*!
2248     \since 5.0
2249
2250     Returns the variant as a QModelIndex if the variant has type() \l
2251     QModelIndex; otherwise returns a default constructed QModelIndex.
2252
2253     \sa canConvert(), convert()
2254 */
2255 QModelIndex QVariant::toModelIndex() const
2256 {
2257     return qVariantToHelper<QModelIndex>(d, handlerManager);
2258 }
2259
2260 /*!
2261     \since 5.0
2262
2263     Returns the variant as a QJsonValue if the variant has type() \l
2264     QJsonValue; otherwise returns a default constructed QJsonValue.
2265
2266     \sa canConvert(), convert()
2267 */
2268 QJsonValue QVariant::toJsonValue() const
2269 {
2270     return qVariantToHelper<QJsonValue>(d, handlerManager);
2271 }
2272
2273 /*!
2274     \since 5.0
2275
2276     Returns the variant as a QJsonObject if the variant has type() \l
2277     QJsonObject; otherwise returns a default constructed QJsonObject.
2278
2279     \sa canConvert(), convert()
2280 */
2281 QJsonObject QVariant::toJsonObject() const
2282 {
2283     return qVariantToHelper<QJsonObject>(d, handlerManager);
2284 }
2285
2286 /*!
2287     \since 5.0
2288
2289     Returns the variant as a QJsonArray if the variant has type() \l
2290     QJsonArray; otherwise returns a default constructed QJsonArray.
2291
2292     \sa canConvert(), convert()
2293 */
2294 QJsonArray QVariant::toJsonArray() const
2295 {
2296     return qVariantToHelper<QJsonArray>(d, handlerManager);
2297 }
2298
2299 /*!
2300     \since 5.0
2301
2302     Returns the variant as a QJsonDocument if the variant has type() \l
2303     QJsonDocument; otherwise returns a default constructed QJsonDocument.
2304
2305     \sa canConvert(), convert()
2306 */
2307 QJsonDocument QVariant::toJsonDocument() const
2308 {
2309     return qVariantToHelper<QJsonDocument>(d, handlerManager);
2310 }
2311 #endif
2312
2313 /*!
2314     \fn QChar QVariant::toChar() const
2315
2316     Returns the variant as a QChar if the variant has type() \l Char,
2317     \l Int, or \l UInt; otherwise returns an invalid QChar.
2318
2319     \sa canConvert(), convert()
2320 */
2321 QChar QVariant::toChar() const
2322 {
2323     return qVariantToHelper<QChar>(d, handlerManager);
2324 }
2325
2326 /*!
2327     Returns the variant as a QBitArray if the variant has type()
2328     \l BitArray; otherwise returns an empty bit array.
2329
2330     \sa canConvert(), convert()
2331 */
2332 QBitArray QVariant::toBitArray() const
2333 {
2334     return qVariantToHelper<QBitArray>(d, handlerManager);
2335 }
2336
2337 template <typename T>
2338 inline T qNumVariantToHelper(const QVariant::Private &d,
2339                              const HandlersManager &handlerManager, bool *ok, const T& val)
2340 {
2341     uint t = qMetaTypeId<T>();
2342     if (ok)
2343         *ok = true;
2344     if (d.type == t)
2345         return val;
2346
2347     T ret = 0;
2348     if (!handlerManager[d.type]->convert(&d, t, &ret, ok) && ok)
2349         *ok = false;
2350     return ret;
2351 }
2352
2353 /*!
2354     Returns the variant as an int if the variant has type() \l Int,
2355     \l Bool, \l ByteArray, \l Char, \l Double, \l LongLong, \l
2356     String, \l UInt, or \l ULongLong; otherwise returns 0.
2357
2358     If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2359     converted to an int; otherwise \c{*}\a{ok} is set to false.
2360
2361     \b{Warning:} If the value is convertible to a \l LongLong but is too
2362     large to be represented in an int, the resulting arithmetic overflow will
2363     not be reflected in \a ok. A simple workaround is to use QString::toInt().
2364
2365     \sa canConvert(), convert()
2366 */
2367 int QVariant::toInt(bool *ok) const
2368 {
2369     return qNumVariantToHelper<int>(d, handlerManager, ok, d.data.i);
2370 }
2371
2372 /*!
2373     Returns the variant as an unsigned int if the variant has type()
2374     \l UInt,  \l Bool, \l ByteArray, \l Char, \l Double, \l Int, \l
2375     LongLong, \l String, or \l ULongLong; otherwise returns 0.
2376
2377     If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2378     converted to an unsigned int; otherwise \c{*}\a{ok} is set to false.
2379
2380     \b{Warning:} If the value is convertible to a \l ULongLong but is too
2381     large to be represented in an unsigned int, the resulting arithmetic overflow will
2382     not be reflected in \a ok. A simple workaround is to use QString::toUInt().
2383
2384     \sa canConvert(), convert()
2385 */
2386 uint QVariant::toUInt(bool *ok) const
2387 {
2388     return qNumVariantToHelper<uint>(d, handlerManager, ok, d.data.u);
2389 }
2390
2391 /*!
2392     Returns the variant as a long long int if the variant has type()
2393     \l LongLong, \l Bool, \l ByteArray, \l Char, \l Double, \l Int,
2394     \l String, \l UInt, or \l ULongLong; otherwise returns 0.
2395
2396     If \a ok is non-null: \c{*}\c{ok} is set to true if the value could be
2397     converted to an int; otherwise \c{*}\c{ok} is set to false.
2398
2399     \sa canConvert(), convert()
2400 */
2401 qlonglong QVariant::toLongLong(bool *ok) const
2402 {
2403     return qNumVariantToHelper<qlonglong>(d, handlerManager, ok, d.data.ll);
2404 }
2405
2406 /*!
2407     Returns the variant as as an unsigned long long int if the
2408     variant has type() \l ULongLong, \l Bool, \l ByteArray, \l Char,
2409     \l Double, \l Int, \l LongLong, \l String, or \l UInt; otherwise
2410     returns 0.
2411
2412     If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2413     converted to an int; otherwise \c{*}\a{ok} is set to false.
2414
2415     \sa canConvert(), convert()
2416 */
2417 qulonglong QVariant::toULongLong(bool *ok) const
2418 {
2419     return qNumVariantToHelper<qulonglong>(d, handlerManager, ok, d.data.ull);
2420 }
2421
2422 /*!
2423     Returns the variant as a bool if the variant has type() Bool.
2424
2425     Returns true if the variant has type() \l Bool, \l Char, \l Double,
2426     \l Int, \l LongLong, \l UInt, or \l ULongLong and the value is
2427     non-zero, or if the variant has type \l String or \l ByteArray and
2428     its lower-case content is not one of the following: empty, "0"
2429     or "false"; otherwise returns false.
2430
2431     \sa canConvert(), convert()
2432 */
2433 bool QVariant::toBool() const
2434 {
2435     if (d.type == Bool)
2436         return d.data.b;
2437
2438     bool res = false;
2439     handlerManager[d.type]->convert(&d, Bool, &res, 0);
2440
2441     return res;
2442 }
2443
2444 /*!
2445     Returns the variant as a double if the variant has type() \l
2446     Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2447     UInt, or \l ULongLong; otherwise returns 0.0.
2448
2449     If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2450     converted to a double; otherwise \c{*}\a{ok} is set to false.
2451
2452     \sa canConvert(), convert()
2453 */
2454 double QVariant::toDouble(bool *ok) const
2455 {
2456     return qNumVariantToHelper<double>(d, handlerManager, ok, d.data.d);
2457 }
2458
2459 /*!
2460     Returns the variant as a float if the variant has type() \l
2461     Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2462     UInt, or \l ULongLong; otherwise returns 0.0.
2463
2464     \since 4.6
2465
2466     If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2467     converted to a double; otherwise \c{*}\a{ok} is set to false.
2468
2469     \sa canConvert(), convert()
2470 */
2471 float QVariant::toFloat(bool *ok) const
2472 {
2473     return qNumVariantToHelper<float>(d, handlerManager, ok, d.data.f);
2474 }
2475
2476 /*!
2477     Returns the variant as a qreal if the variant has type() \l
2478     Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2479     UInt, or \l ULongLong; otherwise returns 0.0.
2480
2481     \since 4.6
2482
2483     If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2484     converted to a double; otherwise \c{*}\a{ok} is set to false.
2485
2486     \sa canConvert(), convert()
2487 */
2488 qreal QVariant::toReal(bool *ok) const
2489 {
2490     return qNumVariantToHelper<qreal>(d, handlerManager, ok, d.data.real);
2491 }
2492
2493 /*!
2494     Returns the variant as a QVariantList if the variant has type()
2495     \l List or \l StringList; otherwise returns an empty list.
2496
2497     \sa canConvert(), convert()
2498 */
2499 QVariantList QVariant::toList() const
2500 {
2501     return qVariantToHelper<QVariantList>(d, handlerManager);
2502 }
2503
2504
2505 static const quint32 qCanConvertMatrix[QVariant::LastCoreType + 1] =
2506 {
2507 /*Invalid*/     0,
2508
2509 /*Bool*/          1 << QVariant::Double     | 1 << QVariant::Int        | 1 << QVariant::UInt
2510                 | 1 << QVariant::LongLong   | 1 << QVariant::ULongLong  | 1 << QVariant::ByteArray
2511                 | 1 << QVariant::String     | 1 << QVariant::Char,
2512
2513 /*Int*/           1 << QVariant::UInt       | 1 << QVariant::String     | 1 << QVariant::Double
2514                 | 1 << QVariant::Bool       | 1 << QVariant::LongLong   | 1 << QVariant::ULongLong
2515                 | 1 << QVariant::Char       | 1 << QVariant::ByteArray,
2516
2517 /*UInt*/          1 << QVariant::Int        | 1 << QVariant::String     | 1 << QVariant::Double
2518                 | 1 << QVariant::Bool       | 1 << QVariant::LongLong   | 1 << QVariant::ULongLong
2519                 | 1 << QVariant::Char       | 1 << QVariant::ByteArray,
2520
2521 /*LLong*/         1 << QVariant::Int        | 1 << QVariant::String     | 1 << QVariant::Double
2522                 | 1 << QVariant::Bool       | 1 << QVariant::UInt       | 1 << QVariant::ULongLong
2523                 | 1 << QVariant::Char       | 1 << QVariant::ByteArray,
2524
2525 /*ULlong*/        1 << QVariant::Int        | 1 << QVariant::String     | 1 << QVariant::Double
2526                 | 1 << QVariant::Bool       | 1 << QVariant::UInt       | 1 << QVariant::LongLong
2527                 | 1 << QVariant::Char       | 1 << QVariant::ByteArray,
2528
2529 /*double*/        1 << QVariant::Int        | 1 << QVariant::String     | 1 << QVariant::ULongLong
2530                 | 1 << QVariant::Bool       | 1 << QVariant::UInt       | 1 << QVariant::LongLong
2531                 | 1 << QVariant::ByteArray,
2532
2533 /*QChar*/         1 << QVariant::Int        | 1 << QVariant::UInt       | 1 << QVariant::LongLong
2534                 | 1 << QVariant::ULongLong,
2535
2536 /*QMap*/          0,
2537
2538 /*QList*/         1 << QVariant::StringList,
2539
2540 /*QString*/       1 << QVariant::StringList | 1 << QVariant::ByteArray  | 1 << QVariant::Int
2541                 | 1 << QVariant::UInt       | 1 << QVariant::Bool       | 1 << QVariant::Double
2542                 | 1 << QVariant::Date       | 1 << QVariant::Time       | 1 << QVariant::DateTime
2543                 | 1 << QVariant::LongLong   | 1 << QVariant::ULongLong  | 1 << QVariant::Char
2544                 | 1 << QVariant::Url        | 1 << QVariant::Uuid,
2545
2546 /*QStringList*/   1 << QVariant::List       | 1 << QVariant::String,
2547
2548 /*QByteArray*/    1 << QVariant::String     | 1 << QVariant::Int        | 1 << QVariant::UInt | 1 << QVariant::Bool
2549                 | 1 << QVariant::Double     | 1 << QVariant::LongLong   | 1 << QVariant::ULongLong,
2550
2551 /*QBitArray*/     0,
2552
2553 /*QDate*/         1 << QVariant::String     | 1 << QVariant::DateTime,
2554
2555 /*QTime*/         1 << QVariant::String     | 1 << QVariant::DateTime,
2556
2557 /*QDateTime*/     1 << QVariant::String     | 1 << QVariant::Date,
2558
2559 /*QUrl*/          1 << QVariant::String,
2560
2561 /*QLocale*/       0,
2562
2563 /*QRect*/         1 << QVariant::RectF,
2564
2565 /*QRectF*/        1 << QVariant::Rect,
2566
2567 /*QSize*/         1 << QVariant::SizeF,
2568
2569 /*QSizeF*/        1 << QVariant::Size,
2570
2571 /*QLine*/         1 << QVariant::LineF,
2572
2573 /*QLineF*/        1 << QVariant::Line,
2574
2575 /*QPoint*/        1 << QVariant::PointF,
2576
2577 /*QPointF*/       1 << QVariant::Point,
2578
2579 /*QRegExp*/       0,
2580
2581 /*QHash*/         0,
2582
2583 /*QEasingCurve*/  0,
2584
2585 /*QUuid*/         1 << QVariant::String
2586 };
2587
2588 #ifndef QT_BOOTSTRAPPED
2589 /*!
2590     Returns true if from inherits to.
2591 */
2592 static bool canConvertMetaObject(const QMetaObject *from, const QMetaObject *to)
2593 {
2594     if (from && to == &QObject::staticMetaObject)
2595         return true;
2596
2597     while (from) {
2598         if (from == to)
2599             return true;
2600         from = from->superClass();
2601     }
2602
2603     return false;
2604 }
2605 #endif
2606
2607 static bool canConvertMetaObject(int fromId, int toId, QObject *fromObject)
2608 {
2609 #ifndef QT_BOOTSTRAPPED
2610     QMetaType toType(toId);
2611     if ((QMetaType::typeFlags(fromId) & QMetaType::PointerToQObject) && (toType.flags() & QMetaType::PointerToQObject)) {
2612         if (!fromObject)
2613             return true;
2614         return canConvertMetaObject(fromObject->metaObject(), toType.metaObject());
2615     }
2616 #else
2617     Q_UNUSED(fromId);
2618     Q_UNUSED(toId);
2619     Q_UNUSED(fromObject);
2620 #endif
2621     return false;
2622 }
2623
2624
2625 /*!
2626     Returns true if the variant's type can be cast to the requested
2627     type, \a targetTypeId. Such casting is done automatically when calling the
2628     toInt(), toBool(), ... methods.
2629
2630     The following casts are done automatically:
2631
2632     \table
2633     \header \li Type \li Automatically Cast To
2634     \row \li \l Bool \li \l Char, \l Double, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2635     \row \li \l ByteArray \li \l Double, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2636     \row \li \l Char \li \l Bool, \l Int, \l UInt, \l LongLong, \l ULongLong
2637     \row \li \l Color \li \l String
2638     \row \li \l Date \li \l DateTime, \l String
2639     \row \li \l DateTime \li \l Date, \l String, \l Time
2640     \row \li \l Double \li \l Bool, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2641     \row \li \l Font \li \l String
2642     \row \li \l Int \li \l Bool, \l Char, \l Double, \l LongLong, \l String, \l UInt, \l ULongLong
2643     \row \li \l KeySequence \li \l Int, \l String
2644     \row \li \l List \li \l StringList (if the list's items can be converted to strings)
2645     \row \li \l LongLong \li \l Bool, \l ByteArray, \l Char, \l Double, \l Int, \l String, \l UInt, \l ULongLong
2646     \row \li \l Point \li PointF
2647     \row \li \l Rect \li RectF
2648     \row \li \l String \li \l Bool, \l ByteArray, \l Char, \l Color, \l Date, \l DateTime, \l Double,
2649                          \l Font, \l Int, \l KeySequence, \l LongLong, \l StringList, \l Time, \l UInt,
2650                          \l ULongLong
2651     \row \li \l StringList \li \l List, \l String (if the list contains exactly one item)
2652     \row \li \l Time \li \l String
2653     \row \li \l UInt \li \l Bool, \l Char, \l Double, \l Int, \l LongLong, \l String, \l ULongLong
2654     \row \li \l ULongLong \li \l Bool, \l Char, \l Double, \l Int, \l LongLong, \l String, \l UInt
2655     \endtable
2656
2657     A QVariant containing a pointer to a type derived from QObject will also return true for this
2658     function if a qobject_cast to the type described by \a targetTypeId would succeed. Note that
2659     this only works for QObject subclasses which use the Q_OBJECT macro.
2660
2661     \sa convert()
2662 */
2663 bool QVariant::canConvert(int targetTypeId) const
2664 {
2665     // TODO Reimplement this function, currently it works but it is a historical mess.
2666     uint currentType = ((d.type == QMetaType::Float) ? QVariant::Double : d.type);
2667     if (currentType == QMetaType::SChar || currentType == QMetaType::Char)
2668         currentType = QMetaType::UInt;
2669     if (targetTypeId == QMetaType::SChar || currentType == QMetaType::Char)
2670         targetTypeId = QMetaType::UInt;
2671     if (uint(targetTypeId) == uint(QMetaType::Float)) targetTypeId = QVariant::Double;
2672
2673
2674     if (currentType == uint(targetTypeId))
2675         return true;
2676
2677     if (targetTypeId < 0)
2678         return false;
2679     if (targetTypeId >= QMetaType::User)
2680         return canConvertMetaObject(currentType, targetTypeId, d.data.o);
2681
2682     // FIXME It should be LastCoreType intead of Uuid
2683     if (currentType > int(QMetaType::QUuid) || targetTypeId > int(QMetaType::QUuid)) {
2684         switch (uint(targetTypeId)) {
2685         case QVariant::Int:
2686             if (currentType == QVariant::KeySequence)
2687                 return true;
2688             // fall through
2689         case QVariant::UInt:
2690                return currentType == QMetaType::ULong
2691                    || currentType == QMetaType::Long
2692                    || currentType == QMetaType::UShort
2693                    || currentType == QMetaType::UChar
2694                    || currentType == QMetaType::Char
2695                    || currentType == QMetaType::SChar
2696                    || currentType == QMetaType::Short;
2697         case QVariant::Image:
2698             return currentType == QVariant::Pixmap || currentType == QVariant::Bitmap;
2699         case QVariant::Pixmap:
2700             return currentType == QVariant::Image || currentType == QVariant::Bitmap
2701                               || currentType == QVariant::Brush;
2702         case QVariant::Bitmap:
2703             return currentType == QVariant::Pixmap || currentType == QVariant::Image;
2704         case QVariant::ByteArray:
2705             return currentType == QVariant::Color;
2706         case QVariant::String:
2707             return currentType == QVariant::KeySequence || currentType == QVariant::Font
2708                               || currentType == QVariant::Color;
2709         case QVariant::KeySequence:
2710             return currentType == QVariant::String || currentType == QVariant::Int;
2711         case QVariant::Font:
2712             return currentType == QVariant::String;
2713         case QVariant::Color:
2714             return currentType == QVariant::String || currentType == QVariant::ByteArray
2715                               || currentType == QVariant::Brush;
2716         case QVariant::Brush:
2717             return currentType == QVariant::Color || currentType == QVariant::Pixmap;
2718         case QMetaType::Long:
2719         case QMetaType::Char:
2720         case QMetaType::SChar:
2721         case QMetaType::UChar:
2722         case QMetaType::ULong:
2723         case QMetaType::Short:
2724         case QMetaType::UShort:
2725             return qCanConvertMatrix[QVariant::Int] & (1 << currentType) || currentType == QVariant::Int;
2726         case QMetaType::QObjectStar:
2727             return canConvertMetaObject(currentType, targetTypeId, d.data.o);
2728         default:
2729             return false;
2730         }
2731     }
2732
2733     if (targetTypeId == String && currentType == StringList)
2734         return v_cast<QStringList>(&d)->count() == 1;
2735     return qCanConvertMatrix[targetTypeId] & (1 << currentType);
2736 }
2737
2738 /*!
2739     Casts the variant to the requested type, \a targetTypeId. If the cast cannot be
2740     done, the variant is cleared. Returns true if the current type of
2741     the variant was successfully cast; otherwise returns false.
2742
2743     A QVariant containing a pointer to a type derived from QObject will also convert
2744     and return true for this function if a qobject_cast to the type described
2745     by \a targetTypeId would succeed. Note that this only works for QObject subclasses
2746     which use the Q_OBJECT macro.
2747
2748     \warning For historical reasons, converting a null QVariant results
2749     in a null value of the desired type (e.g., an empty string for
2750     QString) and a result of false.
2751
2752     \sa canConvert(), clear()
2753 */
2754
2755 bool QVariant::convert(int targetTypeId)
2756 {
2757     if (d.type == uint(targetTypeId))
2758         return true;
2759
2760     QVariant oldValue = *this;
2761
2762     clear();
2763     if (!oldValue.canConvert(targetTypeId))
2764         return false;
2765
2766     create(targetTypeId, 0);
2767     if (oldValue.isNull())
2768         return false;
2769
2770     if ((QMetaType::typeFlags(d.type) & QMetaType::PointerToQObject) && (QMetaType::typeFlags(targetTypeId) & QMetaType::PointerToQObject)) {
2771         create(targetTypeId, &oldValue.d.data.o);
2772         return true;
2773     }
2774
2775     bool isOk = true;
2776     if (!handlerManager[d.type]->convert(&oldValue.d, targetTypeId, data(), &isOk))
2777         isOk = false;
2778     d.is_null = !isOk;
2779     return isOk;
2780 }
2781
2782 /*!
2783   \fn bool QVariant::convert(const int type, void *ptr) const
2784   \internal
2785   Created for qvariant_cast() usage
2786 */
2787 bool QVariant::convert(const int type, void *ptr) const
2788 {
2789     Q_ASSERT(type < int(QMetaType::User));
2790     return handlerManager[type]->convert(&d, type, ptr, 0);
2791 }
2792
2793
2794 /*!
2795     \fn bool operator==(const QVariant &v1, const QVariant &v2)
2796
2797     \relates QVariant
2798
2799     Returns true if \a v1 and \a v2 are equal; otherwise returns false.
2800
2801     If \a v1 and \a v2 have the same \l{QVariant::}{type()}, the
2802     type's equality operator is used for comparison. If not, it is
2803     attempted to \l{QVariant::}{convert()} \a v2 to the same type as
2804     \a v1. See \l{QVariant::}{canConvert()} for a list of possible
2805     conversions.
2806
2807     The result of the function is not affected by the result of QVariant::isNull,
2808     which means that two values can be equal even if one of them is null and
2809     another is not.
2810
2811     \warning This function doesn't support custom types registered
2812     with qRegisterMetaType().
2813 */
2814 /*!
2815     \fn bool operator!=(const QVariant &v1, const QVariant &v2)
2816
2817     \relates QVariant
2818
2819     Returns false if \a v1 and \a v2 are equal; otherwise returns true.
2820
2821     \warning This function doesn't support custom types registered
2822     with qRegisterMetaType().
2823 */
2824
2825 /*! \fn bool QVariant::operator==(const QVariant &v) const
2826
2827     Compares this QVariant with \a v and returns true if they are
2828     equal; otherwise returns false.
2829
2830     QVariant uses the equality operator of the type() it contains to
2831     check for equality. QVariant will try to convert() \a v if its
2832     type is not the same as this variant's type. See canConvert() for
2833     a list of possible conversions.
2834
2835     \warning This function doesn't support custom types registered
2836     with qRegisterMetaType().
2837 */
2838
2839 /*!
2840     \fn bool QVariant::operator!=(const QVariant &v) const
2841
2842     Compares this QVariant with \a v and returns true if they are not
2843     equal; otherwise returns false.
2844
2845     \warning This function doesn't support custom types registered
2846     with qRegisterMetaType().
2847 */
2848
2849 static bool qIsNumericType(uint tp)
2850 {
2851     return (tp >= QVariant::Bool && tp <= QVariant::Double)
2852            || (tp >= QMetaType::Long && tp <= QMetaType::Float);
2853 }
2854
2855 static bool qIsFloatingPoint(uint tp)
2856 {
2857     return tp == QVariant::Double || tp == QMetaType::Float;
2858 }
2859
2860 /*!
2861     \internal
2862  */
2863 bool QVariant::cmp(const QVariant &v) const
2864 {
2865     QVariant v2 = v;
2866     if (d.type != v2.d.type) {
2867         if (qIsNumericType(d.type) && qIsNumericType(v.d.type)) {
2868             if (qIsFloatingPoint(d.type) || qIsFloatingPoint(v.d.type))
2869                 return qFuzzyCompare(toReal(), v.toReal());
2870             else
2871                 return toLongLong() == v.toLongLong();
2872         }
2873         if (!v2.canConvert(d.type) || !v2.convert(d.type))
2874             return false;
2875     }
2876     return handlerManager[d.type]->compare(&d, &v2.d);
2877 }
2878
2879 /*!
2880     \internal
2881  */
2882
2883 const void *QVariant::constData() const
2884 {
2885     return d.is_shared ? d.data.shared->ptr : reinterpret_cast<const void *>(&d.data.ptr);
2886 }
2887
2888 /*!
2889     \fn const void* QVariant::data() const
2890
2891     \internal
2892 */
2893
2894 /*!
2895     \internal
2896 */
2897 void* QVariant::data()
2898 {
2899     detach();
2900     return const_cast<void *>(constData());
2901 }
2902
2903
2904 /*!
2905     Returns true if this is a null variant, false otherwise. A variant is
2906     considered null if it contains a default constructed value or a built-in
2907     type instance that has an isNull method, in which case the result
2908     would be the same as calling isNull on the wrapped object.
2909
2910     \warning The result of the function doesn't affect == operator, which means
2911     that two values can be equal even if one of them is null and another is not.
2912 */
2913 bool QVariant::isNull() const
2914 {
2915     return handlerManager[d.type]->isNull(&d);
2916 }
2917
2918 #ifndef QT_NO_DEBUG_STREAM
2919 QDebug operator<<(QDebug dbg, const QVariant &v)
2920 {
2921     const uint typeId = v.d.type;
2922     dbg.nospace() << "QVariant(";
2923     if (typeId != QMetaType::UnknownType) {
2924         dbg.nospace() << QMetaType::typeName(typeId) << ", ";
2925         handlerManager[typeId]->debugStream(dbg, v);
2926     } else {
2927         dbg.nospace() << "Invalid";
2928     }
2929     dbg.nospace() << ')';
2930     return dbg.space();
2931 }
2932
2933 QDebug operator<<(QDebug dbg, const QVariant::Type p)
2934 {
2935     dbg.nospace() << "QVariant::"
2936                   << (int(p) != int(QMetaType::UnknownType)
2937                      ? QMetaType::typeName(p)
2938                      : "Invalid");
2939     return dbg.space();
2940 }
2941 #endif
2942
2943
2944 /*! \fn void QVariant::setValue(const T &value)
2945
2946     Stores a copy of \a value. If \c{T} is a type that QVariant
2947     doesn't support, QMetaType is used to store the value. A compile
2948     error will occur if QMetaType doesn't handle the type.
2949
2950     Example:
2951
2952     \snippet code/src_corelib_kernel_qvariant.cpp 4
2953
2954     \sa value(), fromValue(), canConvert()
2955  */
2956
2957 /*! \fn T QVariant::value() const
2958
2959     Returns the stored value converted to the template type \c{T}.
2960     Call canConvert() to find out whether a type can be converted.
2961     If the value cannot be converted, \l{default-constructed value}
2962     will be returned.
2963
2964     If the type \c{T} is supported by QVariant, this function behaves
2965     exactly as toString(), toInt() etc.
2966
2967     Example:
2968
2969     \snippet code/src_corelib_kernel_qvariant.cpp 5
2970
2971     If the QVariant contains a pointer to a type derived from QObject then
2972     \c{T} may be any QObject type. If the pointer stored in the QVariant can be
2973     qobject_cast to T, then that result is returned. Otherwise a null pointer is
2974     returned. Note that this only works for QObject subclasses which use the
2975     Q_OBJECT macro.
2976
2977     \sa setValue(), fromValue(), canConvert()
2978 */
2979
2980 /*! \fn bool QVariant::canConvert() const
2981
2982     Returns true if the variant can be converted to the template type \c{T},
2983     otherwise false.
2984
2985     Example:
2986
2987     \snippet code/src_corelib_kernel_qvariant.cpp 6
2988
2989     A QVariant containing a pointer to a type derived from QObject will also return true for this
2990     function if a qobject_cast to the template type \c{T} would succeed. Note that this only works
2991     for QObject subclasses which use the Q_OBJECT macro.
2992
2993     \sa convert()
2994 */
2995
2996 /*! \fn static QVariant QVariant::fromValue(const T &value)
2997
2998     Returns a QVariant containing a copy of \a value. Behaves
2999     exactly like setValue() otherwise.
3000
3001     Example:
3002
3003     \snippet code/src_corelib_kernel_qvariant.cpp 7
3004
3005     \note If you are working with custom types, you should use
3006     the Q_DECLARE_METATYPE() macro to register your custom type.
3007
3008     \sa setValue(), value()
3009 */
3010
3011 /*!
3012     \fn QVariant qVariantFromValue(const T &value)
3013     \relates QVariant
3014     \obsolete
3015
3016     Returns a variant containing a copy of the given \a value
3017     with template type \c{T}.
3018
3019     This function is equivalent to QVariant::fromValue(\a value).
3020
3021     \note This function was provided as a workaround for MSVC 6
3022     which did not support member template functions. It is advised
3023     to use the other form in new code.
3024
3025     For example, a QObject pointer can be stored in a variant with the
3026     following code:
3027
3028     \snippet code/src_corelib_kernel_qvariant.cpp 8
3029
3030     \sa QVariant::fromValue()
3031 */
3032
3033 /*! \fn void qVariantSetValue(QVariant &variant, const T &value)
3034     \relates QVariant
3035     \obsolete
3036
3037     Sets the contents of the given \a variant to a copy of the
3038     \a value with the specified template type \c{T}.
3039
3040     This function is equivalent to QVariant::setValue(\a value).
3041
3042     \note This function was provided as a workaround for MSVC 6
3043     which did not support member template functions. It is advised
3044     to use the other form in new code.
3045
3046     \sa QVariant::setValue()
3047 */
3048
3049 /*!
3050     \fn T qvariant_cast(const QVariant &value)
3051     \relates QVariant
3052
3053     Returns the given \a value converted to the template type \c{T}.
3054
3055     This function is equivalent to QVariant::value().
3056
3057     \sa QVariant::value()
3058 */
3059
3060 /*! \fn T qVariantValue(const QVariant &value)
3061     \relates QVariant
3062     \obsolete
3063
3064     Returns the given \a value converted to the template type \c{T}.
3065
3066     This function is equivalent to
3067     \l{QVariant::value()}{QVariant::value}<T>(\a value).
3068
3069     \note This function was provided as a workaround for MSVC 6
3070     which did not support member template functions. It is advised
3071     to use the other form in new code.
3072
3073     \sa QVariant::value(), qvariant_cast()
3074 */
3075
3076 /*! \fn bool qVariantCanConvert(const QVariant &value)
3077     \relates QVariant
3078     \obsolete
3079
3080     Returns true if the given \a value can be converted to the
3081     template type specified; otherwise returns false.
3082
3083     This function is equivalent to QVariant::canConvert(\a value).
3084
3085     \note This function was provided as a workaround for MSVC 6
3086     which did not support member template functions. It is advised
3087     to use the other form in new code.
3088
3089     \sa QVariant::canConvert()
3090 */
3091
3092 /*!
3093     \typedef QVariantList
3094     \relates QVariant
3095
3096     Synonym for QList<QVariant>.
3097 */
3098
3099 /*!
3100     \typedef QVariantMap
3101     \relates QVariant
3102
3103     Synonym for QMap<QString, QVariant>.
3104 */
3105
3106 /*!
3107     \typedef QVariantHash
3108     \relates QVariant
3109     \since 4.5
3110
3111     Synonym for QHash<QString, QVariant>.
3112 */
3113
3114 /*!
3115     \typedef QVariant::DataPtr
3116     \internal
3117 */
3118
3119 /*!
3120     \fn DataPtr &QVariant::data_ptr()
3121     \internal
3122 */
3123
3124 /*!
3125     \fn const DataPtr &QVariant::data_ptr() const
3126     \internal
3127 */
3128
3129 QT_END_NAMESPACE