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