Remove redundant parameter from qVariantToHelper.
[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, const HandlersManager &handlerManager)
1808 {
1809     const QVariant::Type targetType = static_cast<const QVariant::Type>(qMetaTypeId<T>());
1810     if (d.type == targetType)
1811         return *v_cast<T>(&d);
1812
1813     T ret;
1814     handlerManager[d.type]->convert(&d, targetType, &ret, 0);
1815     return ret;
1816 }
1817
1818 /*!
1819     \fn QStringList QVariant::toStringList() const
1820
1821     Returns the variant as a QStringList if the variant has type()
1822     StringList, \l String, or \l List of a type that can be converted
1823     to QString; otherwise returns an empty list.
1824
1825     \sa canConvert(), convert()
1826 */
1827 QStringList QVariant::toStringList() const
1828 {
1829     return qVariantToHelper<QStringList>(d, handlerManager);
1830 }
1831
1832 /*!
1833     Returns the variant as a QString if the variant has type() \l
1834     String, \l Bool, \l ByteArray, \l Char, \l Date, \l DateTime, \l
1835     Double, \l Int, \l LongLong, \l StringList, \l Time, \l UInt, or
1836     \l ULongLong; otherwise returns an empty string.
1837
1838     \sa canConvert(), convert()
1839 */
1840 QString QVariant::toString() const
1841 {
1842     return qVariantToHelper<QString>(d, handlerManager);
1843 }
1844
1845 /*!
1846     Returns the variant as a QMap<QString, QVariant> if the variant
1847     has type() \l Map; otherwise returns an empty map.
1848
1849     \sa canConvert(), convert()
1850 */
1851 QVariantMap QVariant::toMap() const
1852 {
1853     return qVariantToHelper<QVariantMap>(d, handlerManager);
1854 }
1855
1856 /*!
1857     Returns the variant as a QHash<QString, QVariant> if the variant
1858     has type() \l Hash; otherwise returns an empty map.
1859
1860     \sa canConvert(), convert()
1861 */
1862 QVariantHash QVariant::toHash() const
1863 {
1864     return qVariantToHelper<QVariantHash>(d, handlerManager);
1865 }
1866
1867 /*!
1868     \fn QDate QVariant::toDate() const
1869
1870     Returns the variant as a QDate if the variant has type() \l Date,
1871     \l DateTime, or \l String; otherwise returns an invalid date.
1872
1873     If the type() is \l String, an invalid date will be returned if the
1874     string cannot be parsed as a Qt::ISODate format date.
1875
1876     \sa canConvert(), convert()
1877 */
1878 QDate QVariant::toDate() const
1879 {
1880     return qVariantToHelper<QDate>(d, handlerManager);
1881 }
1882
1883 /*!
1884     \fn QTime QVariant::toTime() const
1885
1886     Returns the variant as a QTime if the variant has type() \l Time,
1887     \l DateTime, or \l String; otherwise returns an invalid time.
1888
1889     If the type() is \l String, an invalid time will be returned if
1890     the string cannot be parsed as a Qt::ISODate format time.
1891
1892     \sa canConvert(), convert()
1893 */
1894 QTime QVariant::toTime() const
1895 {
1896     return qVariantToHelper<QTime>(d, handlerManager);
1897 }
1898
1899 /*!
1900     \fn QDateTime QVariant::toDateTime() const
1901
1902     Returns the variant as a QDateTime if the variant has type() \l
1903     DateTime, \l Date, or \l String; otherwise returns an invalid
1904     date/time.
1905
1906     If the type() is \l String, an invalid date/time will be returned
1907     if the string cannot be parsed as a Qt::ISODate format date/time.
1908
1909     \sa canConvert(), convert()
1910 */
1911 QDateTime QVariant::toDateTime() const
1912 {
1913     return qVariantToHelper<QDateTime>(d, handlerManager);
1914 }
1915
1916 /*!
1917     \since 4.7
1918     \fn QEasingCurve QVariant::toEasingCurve() const
1919
1920     Returns the variant as a QEasingCurve if the variant has type() \l
1921     EasingCurve; otherwise returns a default easing curve.
1922
1923     \sa canConvert(), convert()
1924 */
1925 #ifndef QT_BOOTSTRAPPED
1926 QEasingCurve QVariant::toEasingCurve() const
1927 {
1928     return qVariantToHelper<QEasingCurve>(d, handlerManager);
1929 }
1930 #endif
1931
1932 /*!
1933     \fn QByteArray QVariant::toByteArray() const
1934
1935     Returns the variant as a QByteArray if the variant has type() \l
1936     ByteArray or \l String (converted using QString::fromAscii());
1937     otherwise returns an empty byte array.
1938
1939     \sa canConvert(), convert()
1940 */
1941 QByteArray QVariant::toByteArray() const
1942 {
1943     return qVariantToHelper<QByteArray>(d, handlerManager);
1944 }
1945
1946 #ifndef QT_NO_GEOM_VARIANT
1947 /*!
1948     \fn QPoint QVariant::toPoint() const
1949
1950     Returns the variant as a QPoint if the variant has type()
1951     \l Point or \l PointF; otherwise returns a null QPoint.
1952
1953     \sa canConvert(), convert()
1954 */
1955 QPoint QVariant::toPoint() const
1956 {
1957     return qVariantToHelper<QPoint>(d, handlerManager);
1958 }
1959
1960 /*!
1961     \fn QRect QVariant::toRect() const
1962
1963     Returns the variant as a QRect if the variant has type() \l Rect;
1964     otherwise returns an invalid QRect.
1965
1966     \sa canConvert(), convert()
1967 */
1968 QRect QVariant::toRect() const
1969 {
1970     return qVariantToHelper<QRect>(d, handlerManager);
1971 }
1972
1973 /*!
1974     \fn QSize QVariant::toSize() const
1975
1976     Returns the variant as a QSize if the variant has type() \l Size;
1977     otherwise returns an invalid QSize.
1978
1979     \sa canConvert(), convert()
1980 */
1981 QSize QVariant::toSize() const
1982 {
1983     return qVariantToHelper<QSize>(d, handlerManager);
1984 }
1985
1986 /*!
1987     \fn QSizeF QVariant::toSizeF() const
1988
1989     Returns the variant as a QSizeF if the variant has type() \l
1990     SizeF; otherwise returns an invalid QSizeF.
1991
1992     \sa canConvert(), convert()
1993 */
1994 QSizeF QVariant::toSizeF() const
1995 {
1996     return qVariantToHelper<QSizeF>(d, handlerManager);
1997 }
1998
1999 /*!
2000     \fn QRectF QVariant::toRectF() const
2001
2002     Returns the variant as a QRectF if the variant has type() \l Rect
2003     or \l RectF; otherwise returns an invalid QRectF.
2004
2005     \sa canConvert(), convert()
2006 */
2007 QRectF QVariant::toRectF() const
2008 {
2009     return qVariantToHelper<QRectF>(d, handlerManager);
2010 }
2011
2012 /*!
2013     \fn QLineF QVariant::toLineF() const
2014
2015     Returns the variant as a QLineF if the variant has type() \l
2016     LineF; otherwise returns an invalid QLineF.
2017
2018     \sa canConvert(), convert()
2019 */
2020 QLineF QVariant::toLineF() const
2021 {
2022     return qVariantToHelper<QLineF>(d, handlerManager);
2023 }
2024
2025 /*!
2026     \fn QLine QVariant::toLine() const
2027
2028     Returns the variant as a QLine if the variant has type() \l Line;
2029     otherwise returns an invalid QLine.
2030
2031     \sa canConvert(), convert()
2032 */
2033 QLine QVariant::toLine() const
2034 {
2035     return qVariantToHelper<QLine>(d, handlerManager);
2036 }
2037
2038 /*!
2039     \fn QPointF QVariant::toPointF() const
2040
2041     Returns the variant as a QPointF if the variant has type() \l
2042     Point or \l PointF; otherwise returns a null QPointF.
2043
2044     \sa canConvert(), convert()
2045 */
2046 QPointF QVariant::toPointF() const
2047 {
2048     return qVariantToHelper<QPointF>(d, handlerManager);
2049 }
2050
2051 #endif // QT_NO_GEOM_VARIANT
2052
2053 /*!
2054     \fn QUrl QVariant::toUrl() const
2055
2056     Returns the variant as a QUrl if the variant has type()
2057     \l Url; otherwise returns an invalid QUrl.
2058
2059     \sa canConvert(), convert()
2060 */
2061 QUrl QVariant::toUrl() const
2062 {
2063     return qVariantToHelper<QUrl>(d, handlerManager);
2064 }
2065
2066 /*!
2067     \fn QLocale QVariant::toLocale() const
2068
2069     Returns the variant as a QLocale if the variant has type()
2070     \l Locale; otherwise returns an invalid QLocale.
2071
2072     \sa canConvert(), convert()
2073 */
2074 QLocale QVariant::toLocale() const
2075 {
2076     return qVariantToHelper<QLocale>(d, handlerManager);
2077 }
2078
2079 /*!
2080     \fn QRegExp QVariant::toRegExp() const
2081     \since 4.1
2082
2083     Returns the variant as a QRegExp if the variant has type() \l
2084     RegExp; otherwise returns an empty QRegExp.
2085
2086     \sa canConvert(), convert()
2087 */
2088 #ifndef QT_NO_REGEXP
2089 QRegExp QVariant::toRegExp() const
2090 {
2091     return qVariantToHelper<QRegExp>(d, handlerManager);
2092 }
2093 #endif
2094
2095 /*!
2096     \fn QChar QVariant::toChar() const
2097
2098     Returns the variant as a QChar if the variant has type() \l Char,
2099     \l Int, or \l UInt; otherwise returns an invalid QChar.
2100
2101     \sa canConvert(), convert()
2102 */
2103 QChar QVariant::toChar() const
2104 {
2105     return qVariantToHelper<QChar>(d, handlerManager);
2106 }
2107
2108 /*!
2109     Returns the variant as a QBitArray if the variant has type()
2110     \l BitArray; otherwise returns an empty bit array.
2111
2112     \sa canConvert(), convert()
2113 */
2114 QBitArray QVariant::toBitArray() const
2115 {
2116     return qVariantToHelper<QBitArray>(d, handlerManager);
2117 }
2118
2119 template <typename T>
2120 inline T qNumVariantToHelper(const QVariant::Private &d,
2121                              const HandlersManager &handlerManager, bool *ok, const T& val)
2122 {
2123     uint t = qMetaTypeId<T>();
2124     if (ok)
2125         *ok = true;
2126     if (d.type == t)
2127         return val;
2128
2129     T ret = 0;
2130     if (!handlerManager[d.type]->convert(&d, QVariant::Type(t), &ret, ok) && ok)
2131         *ok = false;
2132     return ret;
2133 }
2134
2135 /*!
2136     Returns the variant as an int if the variant has type() \l Int,
2137     \l Bool, \l ByteArray, \l Char, \l Double, \l LongLong, \l
2138     String, \l UInt, or \l ULongLong; otherwise returns 0.
2139
2140     If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2141     converted to an int; otherwise \c{*}\a{ok} is set to false.
2142
2143     \bold{Warning:} If the value is convertible to a \l LongLong but is too
2144     large to be represented in an int, the resulting arithmetic overflow will
2145     not be reflected in \a ok. A simple workaround is to use QString::toInt().
2146     Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.
2147
2148     \sa canConvert(), convert()
2149 */
2150 int QVariant::toInt(bool *ok) const
2151 {
2152     return qNumVariantToHelper<int>(d, handlerManager, ok, d.data.i);
2153 }
2154
2155 /*!
2156     Returns the variant as an unsigned int if the variant has type()
2157     \l UInt,  \l Bool, \l ByteArray, \l Char, \l Double, \l Int, \l
2158     LongLong, \l String, or \l ULongLong; otherwise returns 0.
2159
2160     If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2161     converted to an unsigned int; otherwise \c{*}\a{ok} is set to false.
2162
2163     \bold{Warning:} If the value is convertible to a \l ULongLong but is too
2164     large to be represented in an unsigned int, the resulting arithmetic overflow will
2165     not be reflected in \a ok. A simple workaround is to use QString::toUInt().
2166     Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.
2167
2168     \sa canConvert(), convert()
2169 */
2170 uint QVariant::toUInt(bool *ok) const
2171 {
2172     return qNumVariantToHelper<uint>(d, handlerManager, ok, d.data.u);
2173 }
2174
2175 /*!
2176     Returns the variant as a long long int if the variant has type()
2177     \l LongLong, \l Bool, \l ByteArray, \l Char, \l Double, \l Int,
2178     \l String, \l UInt, or \l ULongLong; otherwise returns 0.
2179
2180     If \a ok is non-null: \c{*}\c{ok} is set to true if the value could be
2181     converted to an int; otherwise \c{*}\c{ok} is set to false.
2182
2183     \sa canConvert(), convert()
2184 */
2185 qlonglong QVariant::toLongLong(bool *ok) const
2186 {
2187     return qNumVariantToHelper<qlonglong>(d, handlerManager, ok, d.data.ll);
2188 }
2189
2190 /*!
2191     Returns the variant as as an unsigned long long int if the
2192     variant has type() \l ULongLong, \l Bool, \l ByteArray, \l Char,
2193     \l Double, \l Int, \l LongLong, \l String, or \l UInt; otherwise
2194     returns 0.
2195
2196     If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2197     converted to an int; otherwise \c{*}\a{ok} is set to false.
2198
2199     \sa canConvert(), convert()
2200 */
2201 qulonglong QVariant::toULongLong(bool *ok) const
2202 {
2203     return qNumVariantToHelper<qulonglong>(d, handlerManager, ok, d.data.ull);
2204 }
2205
2206 /*!
2207     Returns the variant as a bool if the variant has type() Bool.
2208
2209     Returns true if the variant has type() \l Bool, \l Char, \l Double,
2210     \l Int, \l LongLong, \l UInt, or \l ULongLong and the value is
2211     non-zero, or if the variant has type \l String or \l ByteArray and
2212     its lower-case content is not empty, "0" or "false"; otherwise
2213     returns false.
2214
2215     \sa canConvert(), convert()
2216 */
2217 bool QVariant::toBool() const
2218 {
2219     if (d.type == Bool)
2220         return d.data.b;
2221
2222     bool res = false;
2223     handlerManager[d.type]->convert(&d, Bool, &res, 0);
2224
2225     return res;
2226 }
2227
2228 /*!
2229     Returns the variant as a double if the variant has type() \l
2230     Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2231     UInt, or \l ULongLong; otherwise returns 0.0.
2232
2233     If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2234     converted to a double; otherwise \c{*}\a{ok} is set to false.
2235
2236     \sa canConvert(), convert()
2237 */
2238 double QVariant::toDouble(bool *ok) const
2239 {
2240     return qNumVariantToHelper<double>(d, handlerManager, ok, d.data.d);
2241 }
2242
2243 /*!
2244     Returns the variant as a float if the variant has type() \l
2245     Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2246     UInt, or \l ULongLong; otherwise returns 0.0.
2247
2248     \since 4.6
2249
2250     If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2251     converted to a double; otherwise \c{*}\a{ok} is set to false.
2252
2253     \sa canConvert(), convert()
2254 */
2255 float QVariant::toFloat(bool *ok) const
2256 {
2257     return qNumVariantToHelper<float>(d, handlerManager, ok, d.data.f);
2258 }
2259
2260 /*!
2261     Returns the variant as a qreal if the variant has type() \l
2262     Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l
2263     UInt, or \l ULongLong; otherwise returns 0.0.
2264
2265     \since 4.6
2266
2267     If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be
2268     converted to a double; otherwise \c{*}\a{ok} is set to false.
2269
2270     \sa canConvert(), convert()
2271 */
2272 qreal QVariant::toReal(bool *ok) const
2273 {
2274     return qNumVariantToHelper<qreal>(d, handlerManager, ok, d.data.real);
2275 }
2276
2277 /*!
2278     Returns the variant as a QVariantList if the variant has type()
2279     \l List or \l StringList; otherwise returns an empty list.
2280
2281     \sa canConvert(), convert()
2282 */
2283 QVariantList QVariant::toList() const
2284 {
2285     return qVariantToHelper<QVariantList>(d, handlerManager);
2286 }
2287
2288
2289 static const quint32 qCanConvertMatrix[QVariant::LastCoreType + 1] =
2290 {
2291 /*Invalid*/     0,
2292
2293 /*Bool*/          1 << QVariant::Double     | 1 << QVariant::Int        | 1 << QVariant::UInt
2294                 | 1 << QVariant::LongLong   | 1 << QVariant::ULongLong  | 1 << QVariant::ByteArray
2295                 | 1 << QVariant::String     | 1 << QVariant::Char,
2296
2297 /*Int*/           1 << QVariant::UInt       | 1 << QVariant::String     | 1 << QVariant::Double
2298                 | 1 << QVariant::Bool       | 1 << QVariant::LongLong   | 1 << QVariant::ULongLong
2299                 | 1 << QVariant::Char       | 1 << QVariant::ByteArray,
2300
2301 /*UInt*/          1 << QVariant::Int        | 1 << QVariant::String     | 1 << QVariant::Double
2302                 | 1 << QVariant::Bool       | 1 << QVariant::LongLong   | 1 << QVariant::ULongLong
2303                 | 1 << QVariant::Char       | 1 << QVariant::ByteArray,
2304
2305 /*LLong*/         1 << QVariant::Int        | 1 << QVariant::String     | 1 << QVariant::Double
2306                 | 1 << QVariant::Bool       | 1 << QVariant::UInt       | 1 << QVariant::ULongLong
2307                 | 1 << QVariant::Char       | 1 << QVariant::ByteArray,
2308
2309 /*ULlong*/        1 << QVariant::Int        | 1 << QVariant::String     | 1 << QVariant::Double
2310                 | 1 << QVariant::Bool       | 1 << QVariant::UInt       | 1 << QVariant::LongLong
2311                 | 1 << QVariant::Char       | 1 << QVariant::ByteArray,
2312
2313 /*double*/        1 << QVariant::Int        | 1 << QVariant::String     | 1 << QVariant::ULongLong
2314                 | 1 << QVariant::Bool       | 1 << QVariant::UInt       | 1 << QVariant::LongLong
2315                 | 1 << QVariant::ByteArray,
2316
2317 /*QChar*/         1 << QVariant::Int        | 1 << QVariant::UInt       | 1 << QVariant::LongLong
2318                 | 1 << QVariant::ULongLong,
2319
2320 /*QMap*/          0,
2321
2322 /*QList*/         1 << QVariant::StringList,
2323
2324 /*QString*/       1 << QVariant::StringList | 1 << QVariant::ByteArray  | 1 << QVariant::Int
2325                 | 1 << QVariant::UInt       | 1 << QVariant::Bool       | 1 << QVariant::Double
2326                 | 1 << QVariant::Date       | 1 << QVariant::Time       | 1 << QVariant::DateTime
2327                 | 1 << QVariant::LongLong   | 1 << QVariant::ULongLong  | 1 << QVariant::Char
2328                 | 1 << QVariant::Url        | 1 << QVariant::Uuid,
2329
2330 /*QStringList*/   1 << QVariant::List       | 1 << QVariant::String,
2331
2332 /*QByteArray*/    1 << QVariant::String     | 1 << QVariant::Int        | 1 << QVariant::UInt | 1 << QVariant::Bool
2333                 | 1 << QVariant::Double     | 1 << QVariant::LongLong   | 1 << QVariant::ULongLong,
2334
2335 /*QBitArray*/     0,
2336
2337 /*QDate*/         1 << QVariant::String     | 1 << QVariant::DateTime,
2338
2339 /*QTime*/         1 << QVariant::String     | 1 << QVariant::DateTime,
2340
2341 /*QDateTime*/     1 << QVariant::String     | 1 << QVariant::Date,
2342
2343 /*QUrl*/          1 << QVariant::String,
2344
2345 /*QLocale*/       0,
2346
2347 /*QRect*/         1 << QVariant::RectF,
2348
2349 /*QRectF*/        1 << QVariant::Rect,
2350
2351 /*QSize*/         1 << QVariant::SizeF,
2352
2353 /*QSizeF*/        1 << QVariant::Size,
2354
2355 /*QLine*/         1 << QVariant::LineF,
2356
2357 /*QLineF*/        1 << QVariant::Line,
2358
2359 /*QPoint*/        1 << QVariant::PointF,
2360
2361 /*QPointF*/       1 << QVariant::Point,
2362
2363 /*QRegExp*/       0,
2364
2365 /*QHash*/         0,
2366
2367 /*QEasingCurve*/  0,
2368
2369 /*QUuid*/         1 << QVariant::String
2370 };
2371
2372 /*!
2373     Returns true if the variant's type can be cast to the requested
2374     type, \a t. Such casting is done automatically when calling the
2375     toInt(), toBool(), ... methods.
2376
2377     The following casts are done automatically:
2378
2379     \table
2380     \header \o Type \o Automatically Cast To
2381     \row \o \l Bool \o \l Char, \l Double, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2382     \row \o \l ByteArray \o \l Double, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2383     \row \o \l Char \o \l Bool, \l Int, \l UInt, \l LongLong, \l ULongLong
2384     \row \o \l Color \o \l String
2385     \row \o \l Date \o \l DateTime, \l String
2386     \row \o \l DateTime \o \l Date, \l String, \l Time
2387     \row \o \l Double \o \l Bool, \l Int, \l LongLong, \l String, \l UInt, \l ULongLong
2388     \row \o \l Font \o \l String
2389     \row \o \l Int \o \l Bool, \l Char, \l Double, \l LongLong, \l String, \l UInt, \l ULongLong
2390     \row \o \l KeySequence \o \l Int, \l String
2391     \row \o \l List \o \l StringList (if the list's items can be converted to strings)
2392     \row \o \l LongLong \o \l Bool, \l ByteArray, \l Char, \l Double, \l Int, \l String, \l UInt, \l ULongLong
2393     \row \o \l Point \o PointF
2394     \row \o \l Rect \o RectF
2395     \row \o \l String \o \l Bool, \l ByteArray, \l Char, \l Color, \l Date, \l DateTime, \l Double,
2396                          \l Font, \l Int, \l KeySequence, \l LongLong, \l StringList, \l Time, \l UInt,
2397                          \l ULongLong
2398     \row \o \l StringList \o \l List, \l String (if the list contains exactly one item)
2399     \row \o \l Time \o \l String
2400     \row \o \l UInt \o \l Bool, \l Char, \l Double, \l Int, \l LongLong, \l String, \l ULongLong
2401     \row \o \l ULongLong \o \l Bool, \l Char, \l Double, \l Int, \l LongLong, \l String, \l UInt
2402     \endtable
2403
2404     \sa convert()
2405 */
2406 bool QVariant::canConvert(Type t) const
2407 {
2408     //we can treat floats as double
2409     //the reason for not doing it the "proper" way is that QMetaType::Float's value is 135,
2410     //which can't be handled by qCanConvertMatrix
2411     //In addition QVariant::Type doesn't have a Float value, so we're using QMetaType::Float
2412     const uint currentType = ((d.type == QMetaType::Float) ? QVariant::Double : d.type);
2413     if (uint(t) == uint(QMetaType::Float)) t = QVariant::Double;
2414
2415     if (currentType == uint(t))
2416         return true;
2417
2418     if (currentType > QVariant::LastCoreType || t > QVariant::LastCoreType) {
2419         switch (uint(t)) {
2420         case QVariant::Int:
2421             return currentType == QVariant::KeySequence
2422                    || currentType == QMetaType::ULong
2423                    || currentType == QMetaType::Long
2424                    || currentType == QMetaType::UShort
2425                    || currentType == QMetaType::UChar
2426                    || currentType == QMetaType::Char
2427                    || currentType == QMetaType::Short;
2428         case QVariant::Image:
2429             return currentType == QVariant::Pixmap || currentType == QVariant::Bitmap;
2430         case QVariant::Pixmap:
2431             return currentType == QVariant::Image || currentType == QVariant::Bitmap
2432                               || currentType == QVariant::Brush;
2433         case QVariant::Bitmap:
2434             return currentType == QVariant::Pixmap || currentType == QVariant::Image;
2435         case QVariant::ByteArray:
2436             return currentType == QVariant::Color;
2437         case QVariant::String:
2438             return currentType == QVariant::KeySequence || currentType == QVariant::Font
2439                               || currentType == QVariant::Color;
2440         case QVariant::KeySequence:
2441             return currentType == QVariant::String || currentType == QVariant::Int;
2442         case QVariant::Font:
2443             return currentType == QVariant::String;
2444         case QVariant::Color:
2445             return currentType == QVariant::String || currentType == QVariant::ByteArray
2446                               || currentType == QVariant::Brush;
2447         case QVariant::Brush:
2448             return currentType == QVariant::Color || currentType == QVariant::Pixmap;
2449         case QMetaType::Long:
2450         case QMetaType::Char:
2451         case QMetaType::UChar:
2452         case QMetaType::ULong:
2453         case QMetaType::Short:
2454         case QMetaType::UShort:
2455             return qCanConvertMatrix[QVariant::Int] & (1 << currentType) || currentType == QVariant::Int;
2456         default:
2457             return false;
2458         }
2459     }
2460
2461     if(t == String && currentType == StringList)
2462         return v_cast<QStringList>(&d)->count() == 1;
2463     else
2464         return qCanConvertMatrix[t] & (1 << currentType);
2465 }
2466
2467 /*!
2468     Casts the variant to the requested type, \a t. If the cast cannot be
2469     done, the variant is cleared. Returns true if the current type of
2470     the variant was successfully cast; otherwise returns false.
2471
2472     \warning For historical reasons, converting a null QVariant results
2473     in a null value of the desired type (e.g., an empty string for
2474     QString) and a result of false.
2475
2476     \sa canConvert(), clear()
2477 */
2478
2479 bool QVariant::convert(Type t)
2480 {
2481     if (d.type == uint(t))
2482         return true;
2483
2484     QVariant oldValue = *this;
2485
2486     clear();
2487     if (!oldValue.canConvert(t))
2488         return false;
2489
2490     create(t, 0);
2491     if (oldValue.isNull())
2492         return false;
2493
2494     bool isOk = true;
2495     if (!handlerManager[d.type]->convert(&oldValue.d, t, data(), &isOk))
2496         isOk = false;
2497     d.is_null = !isOk;
2498     return isOk;
2499 }
2500
2501 /*!
2502   \fn convert(const int type, void *ptr) const
2503   \internal
2504   Created for qvariant_cast() usage
2505 */
2506 bool QVariant::convert(const int type, void *ptr) const
2507 {
2508     Q_ASSERT(type < int(QMetaType::User));
2509     return handlerManager[type]->convert(&d, QVariant::Type(type), ptr, 0);
2510 }
2511
2512
2513 /*!
2514     \fn bool operator==(const QVariant &v1, const QVariant &v2)
2515
2516     \relates QVariant
2517
2518     Returns true if \a v1 and \a v2 are equal; otherwise returns false.
2519
2520     \warning This function doesn't support custom types registered
2521     with qRegisterMetaType().
2522 */
2523 /*!
2524     \fn bool operator!=(const QVariant &v1, const QVariant &v2)
2525
2526     \relates QVariant
2527
2528     Returns false if \a v1 and \a v2 are equal; otherwise returns true.
2529
2530     \warning This function doesn't support custom types registered
2531     with qRegisterMetaType().
2532 */
2533
2534 /*! \fn bool QVariant::operator==(const QVariant &v) const
2535
2536     Compares this QVariant with \a v and returns true if they are
2537     equal; otherwise returns false.
2538
2539     In the case of custom types, their equalness operators are not called.
2540     Instead the values' addresses are compared.
2541 */
2542
2543 /*!
2544     \fn bool QVariant::operator!=(const QVariant &v) const
2545
2546     Compares this QVariant with \a v and returns true if they are not
2547     equal; otherwise returns false.
2548
2549     \warning This function doesn't support custom types registered
2550     with qRegisterMetaType().
2551 */
2552
2553 static bool qIsNumericType(uint tp)
2554 {
2555     return (tp >= QVariant::Bool && tp <= QVariant::Double)
2556            || (tp >= QMetaType::Long && tp <= QMetaType::Float);
2557 }
2558
2559 static bool qIsFloatingPoint(uint tp)
2560 {
2561     return tp == QVariant::Double || tp == QMetaType::Float;
2562 }
2563
2564 /*! \internal
2565  */
2566 bool QVariant::cmp(const QVariant &v) const
2567 {
2568     QVariant v2 = v;
2569     if (d.type != v2.d.type) {
2570         if (qIsNumericType(d.type) && qIsNumericType(v.d.type)) {
2571             if (qIsFloatingPoint(d.type) || qIsFloatingPoint(v.d.type))
2572                 return qFuzzyCompare(toReal(), v.toReal());
2573             else
2574                 return toLongLong() == v.toLongLong();
2575         }
2576         if (!v2.canConvert(Type(d.type)) || !v2.convert(Type(d.type)))
2577             return false;
2578     }
2579     return handlerManager[d.type]->compare(&d, &v2.d);
2580 }
2581
2582 /*! \internal
2583  */
2584
2585 const void *QVariant::constData() const
2586 {
2587     return d.is_shared ? d.data.shared->ptr : reinterpret_cast<const void *>(&d.data.ptr);
2588 }
2589
2590 /*!
2591     \fn const void* QVariant::data() const
2592
2593     \internal
2594 */
2595
2596 /*! \internal */
2597 void* QVariant::data()
2598 {
2599     detach();
2600     return const_cast<void *>(constData());
2601 }
2602
2603
2604 /*!
2605   Returns true if this is a NULL variant, false otherwise.
2606 */
2607 bool QVariant::isNull() const
2608 {
2609     return handlerManager[d.type]->isNull(&d);
2610 }
2611
2612 #ifndef QT_NO_DEBUG_STREAM
2613 QDebug operator<<(QDebug dbg, const QVariant &v)
2614 {
2615 #ifndef Q_BROKEN_DEBUG_STREAM
2616     dbg.nospace() << "QVariant(" << QMetaType::typeName(v.userType()) << ", ";
2617     handlerManager[v.d.type]->debugStream(dbg, v);
2618     dbg.nospace() << ')';
2619     return dbg.space();
2620 #else
2621     qWarning("This compiler doesn't support streaming QVariant to QDebug");
2622     return dbg;
2623     Q_UNUSED(v);
2624 #endif
2625 }
2626
2627 QDebug operator<<(QDebug dbg, const QVariant::Type p)
2628 {
2629 #ifndef Q_BROKEN_DEBUG_STREAM
2630     dbg.nospace() << "QVariant::" << QMetaType::typeName(p);
2631     return dbg.space();
2632 #else
2633     qWarning("This compiler doesn't support streaming QVariant::Type to QDebug");
2634     return dbg;
2635     Q_UNUSED(p);
2636 #endif
2637 }
2638 #endif
2639
2640
2641 /*! \fn void QVariant::setValue(const T &value)
2642
2643     Stores a copy of \a value. If \c{T} is a type that QVariant
2644     doesn't support, QMetaType is used to store the value. A compile
2645     error will occur if QMetaType doesn't handle the type.
2646
2647     Example:
2648
2649     \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 4
2650
2651     \sa value(), fromValue(), canConvert()
2652  */
2653
2654 /*! \fn T QVariant::value() const
2655
2656     Returns the stored value converted to the template type \c{T}.
2657     Call canConvert() to find out whether a type can be converted.
2658     If the value cannot be converted, \l{default-constructed value}
2659     will be returned.
2660
2661     If the type \c{T} is supported by QVariant, this function behaves
2662     exactly as toString(), toInt() etc.
2663
2664     Example:
2665
2666     \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 5
2667
2668     \sa setValue(), fromValue(), canConvert()
2669 */
2670
2671 /*! \fn bool QVariant::canConvert() const
2672
2673     Returns true if the variant can be converted to the template type \c{T},
2674     otherwise false.
2675
2676     Example:
2677
2678     \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 6
2679
2680     \sa convert()
2681 */
2682
2683 /*! \fn static QVariant QVariant::fromValue(const T &value)
2684
2685     Returns a QVariant containing a copy of \a value. Behaves
2686     exactly like setValue() otherwise.
2687
2688     Example:
2689
2690     \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 7
2691
2692     \note If you are working with custom types, you should use
2693     the Q_DECLARE_METATYPE() macro to register your custom type.
2694
2695     \sa setValue(), value()
2696 */
2697
2698 /*!
2699     \fn QVariant qVariantFromValue(const T &value)
2700     \relates QVariant
2701     \obsolete
2702
2703     Returns a variant containing a copy of the given \a value
2704     with template type \c{T}.
2705
2706     This function is equivalent to QVariant::fromValue(\a value).
2707
2708     \note This function was provided as a workaround for MSVC 6
2709     which did not support member template functions. It is advised
2710     to use the other form in new code.
2711
2712     For example, a QObject pointer can be stored in a variant with the
2713     following code:
2714
2715     \snippet doc/src/snippets/code/src_corelib_kernel_qvariant.cpp 8
2716
2717     \sa QVariant::fromValue()
2718 */
2719
2720 /*! \fn void qVariantSetValue(QVariant &variant, const T &value)
2721     \relates QVariant
2722     \obsolete
2723
2724     Sets the contents of the given \a variant to a copy of the
2725     \a value with the specified template type \c{T}.
2726
2727     This function is equivalent to QVariant::setValue(\a value).
2728
2729     \note This function was provided as a workaround for MSVC 6
2730     which did not support member template functions. It is advised
2731     to use the other form in new code.
2732
2733     \sa QVariant::setValue()
2734 */
2735
2736 /*!
2737     \fn T qvariant_cast(const QVariant &value)
2738     \relates QVariant
2739
2740     Returns the given \a value converted to the template type \c{T}.
2741
2742     This function is equivalent to QVariant::value().
2743
2744     \sa QVariant::value()
2745 */
2746
2747 /*! \fn T qVariantValue(const QVariant &value)
2748     \relates QVariant
2749     \obsolete
2750
2751     Returns the given \a value converted to the template type \c{T}.
2752
2753     This function is equivalent to
2754     \l{QVariant::value()}{QVariant::value}<T>(\a value).
2755
2756     \note This function was provided as a workaround for MSVC 6
2757     which did not support member template functions. It is advised
2758     to use the other form in new code.
2759
2760     \sa QVariant::value(), qvariant_cast()
2761 */
2762
2763 /*! \fn bool qVariantCanConvert(const QVariant &value)
2764     \relates QVariant
2765     \obsolete
2766
2767     Returns true if the given \a value can be converted to the
2768     template type specified; otherwise returns false.
2769
2770     This function is equivalent to QVariant::canConvert(\a value).
2771
2772     \note This function was provided as a workaround for MSVC 6
2773     which did not support member template functions. It is advised
2774     to use the other form in new code.
2775
2776     \sa QVariant::canConvert()
2777 */
2778
2779 /*!
2780     \typedef QVariantList
2781     \relates QVariant
2782
2783     Synonym for QList<QVariant>.
2784 */
2785
2786 /*!
2787     \typedef QVariantMap
2788     \relates QVariant
2789
2790     Synonym for QMap<QString, QVariant>.
2791 */
2792
2793 /*!
2794     \typedef QVariantHash
2795     \relates QVariant
2796     \since 4.5
2797
2798     Synonym for QHash<QString, QVariant>.
2799 */
2800
2801 /*!
2802     \typedef QVariant::DataPtr
2803     \internal
2804 */
2805
2806 /*!
2807     \fn DataPtr &QVariant::data_ptr()
2808     \internal
2809 */
2810
2811 QT_END_NAMESPACE