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