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