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