source: trunk/src/corelib/kernel/qvariant.h@ 209

Last change on this file since 209 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 17.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QVARIANT_H
43#define QVARIANT_H
44
45#include <QtCore/qatomic.h>
46#include <QtCore/qbytearray.h>
47#include <QtCore/qlist.h>
48#include <QtCore/qmetatype.h>
49#include <QtCore/qmap.h>
50#include <QtCore/qhash.h>
51#include <QtCore/qstring.h>
52
53QT_BEGIN_HEADER
54
55QT_BEGIN_NAMESPACE
56
57QT_MODULE(Core)
58
59class QBitArray;
60class QDataStream;
61class QDate;
62class QDateTime;
63class QLine;
64class QLineF;
65class QLocale;
66class QMatrix;
67class QTransform;
68class QStringList;
69class QTime;
70class QPoint;
71class QPointF;
72class QSize;
73class QSizeF;
74class QRect;
75class QRectF;
76#ifndef QT_NO_REGEXP
77class QRegExp;
78#endif
79class QTextFormat;
80class QTextLength;
81class QUrl;
82class QVariant;
83class QVariantComparisonHelper;
84
85#ifndef QT_NO_MEMBER_TEMPLATES
86template <typename T>
87inline QVariant qVariantFromValue(const T &);
88
89template <typename T>
90inline void qVariantSetValue(QVariant &, const T &);
91
92template<typename T>
93inline T qVariantValue(const QVariant &);
94
95template<typename T>
96inline bool qVariantCanConvert(const QVariant &);
97#endif
98
99class Q_CORE_EXPORT QVariant
100{
101 public:
102 enum Type {
103 Invalid = 0,
104
105 Bool = 1,
106 Int = 2,
107 UInt = 3,
108 LongLong = 4,
109 ULongLong = 5,
110 Double = 6,
111 Char = 7,
112 Map = 8,
113 List = 9,
114 String = 10,
115 StringList = 11,
116 ByteArray = 12,
117 BitArray = 13,
118 Date = 14,
119 Time = 15,
120 DateTime = 16,
121 Url = 17,
122 Locale = 18,
123 Rect = 19,
124 RectF = 20,
125 Size = 21,
126 SizeF = 22,
127 Line = 23,
128 LineF = 24,
129 Point = 25,
130 PointF = 26,
131 RegExp = 27,
132 Hash = 28,
133 LastCoreType = Hash,
134
135 // value 62 is internally reserved
136#ifdef QT3_SUPPORT
137 ColorGroup = 63,
138#endif
139 Font = 64,
140 Pixmap = 65,
141 Brush = 66,
142 Color = 67,
143 Palette = 68,
144 Icon = 69,
145 Image = 70,
146 Polygon = 71,
147 Region = 72,
148 Bitmap = 73,
149 Cursor = 74,
150 SizePolicy = 75,
151 KeySequence = 76,
152 Pen = 77,
153 TextLength = 78,
154 TextFormat = 79,
155 Matrix = 80,
156 Transform = 81,
157 LastGuiType = Transform,
158
159 UserType = 127,
160#ifdef QT3_SUPPORT
161 IconSet = Icon,
162 CString = ByteArray,
163 PointArray = Polygon,
164#endif
165 LastType = 0xffffffff // need this so that gcc >= 3.4 allocates 32 bits for Type
166 };
167
168 inline QVariant();
169 ~QVariant();
170 QVariant(Type type);
171 QVariant(int typeOrUserType, const void *copy);
172 QVariant(const QVariant &other);
173
174#ifndef QT_NO_DATASTREAM
175 QVariant(QDataStream &s);
176#endif
177
178 QVariant(int i);
179 QVariant(uint ui);
180 QVariant(qlonglong ll);
181 QVariant(qulonglong ull);
182 QVariant(bool b);
183 QVariant(double d);
184#ifndef QT_NO_CAST_FROM_ASCII
185 QT_ASCII_CAST_WARN_CONSTRUCTOR QVariant(const char *str);
186#endif
187
188 QVariant(const QByteArray &bytearray);
189 QVariant(const QBitArray &bitarray);
190 QVariant(const QString &string);
191 QVariant(const QLatin1String &string);
192 QVariant(const QStringList &stringlist);
193 QVariant(const QChar &qchar);
194 QVariant(const QDate &date);
195 QVariant(const QTime &time);
196 QVariant(const QDateTime &datetime);
197 QVariant(const QList<QVariant> &list);
198 QVariant(const QMap<QString,QVariant> &map);
199 QVariant(const QHash<QString,QVariant> &hash);
200#ifndef QT_NO_GEOM_VARIANT
201 QVariant(const QSize &size);
202 QVariant(const QSizeF &size);
203 QVariant(const QPoint &pt);
204 QVariant(const QPointF &pt);
205 QVariant(const QLine &line);
206 QVariant(const QLineF &line);
207 QVariant(const QRect &rect);
208 QVariant(const QRectF &rect);
209#endif
210 QVariant(const QUrl &url);
211 QVariant(const QLocale &locale);
212#ifndef QT_NO_REGEXP
213 QVariant(const QRegExp &regExp);
214#endif
215 QVariant(Qt::GlobalColor color);
216
217 QVariant& operator=(const QVariant &other);
218
219 Type type() const;
220 int userType() const;
221 const char *typeName() const;
222
223 bool canConvert(Type t) const;
224 bool convert(Type t);
225
226#ifdef QT3_SUPPORT
227 inline QT3_SUPPORT bool canCast(Type t) const
228 { return canConvert(t); }
229 inline QT3_SUPPORT bool cast(Type t)
230 { return convert(t); }
231#endif
232
233 inline bool isValid() const;
234 bool isNull() const;
235
236 void clear();
237
238 void detach();
239 inline bool isDetached() const;
240
241 int toInt(bool *ok = 0) const;
242 uint toUInt(bool *ok = 0) const;
243 qlonglong toLongLong(bool *ok = 0) const;
244 qulonglong toULongLong(bool *ok = 0) const;
245 bool toBool() const;
246 double toDouble(bool *ok = 0) const;
247 QByteArray toByteArray() const;
248 QBitArray toBitArray() const;
249 QString toString() const;
250 QStringList toStringList() const;
251 QChar toChar() const;
252 QDate toDate() const;
253 QTime toTime() const;
254 QDateTime toDateTime() const;
255 QList<QVariant> toList() const;
256 QMap<QString, QVariant> toMap() const;
257 QHash<QString, QVariant> toHash() const;
258
259#ifndef QT_NO_GEOM_VARIANT
260 QPoint toPoint() const;
261 QPointF toPointF() const;
262 QRect toRect() const;
263 QSize toSize() const;
264 QSizeF toSizeF() const;
265 QLine toLine() const;
266 QLineF toLineF() const;
267 QRectF toRectF() const;
268#endif
269 QUrl toUrl() const;
270 QLocale toLocale() const;
271#ifndef QT_NO_REGEXP
272 QRegExp toRegExp() const;
273#endif
274
275#ifdef QT3_SUPPORT
276 inline QT3_SUPPORT int &asInt();
277 inline QT3_SUPPORT uint &asUInt();
278 inline QT3_SUPPORT qlonglong &asLongLong();
279 inline QT3_SUPPORT qulonglong &asULongLong();
280 inline QT3_SUPPORT bool &asBool();
281 inline QT3_SUPPORT double &asDouble();
282 inline QT3_SUPPORT QByteArray &asByteArray();
283 inline QT3_SUPPORT QBitArray &asBitArray();
284 inline QT3_SUPPORT QString &asString();
285 inline QT3_SUPPORT QStringList &asStringList();
286 inline QT3_SUPPORT QDate &asDate();
287 inline QT3_SUPPORT QTime &asTime();
288 inline QT3_SUPPORT QDateTime &asDateTime();
289 inline QT3_SUPPORT QList<QVariant> &asList();
290 inline QT3_SUPPORT QMap<QString,QVariant> &asMap();
291 inline QT3_SUPPORT QPoint &asPoint();
292 inline QT3_SUPPORT QRect &asRect();
293 inline QT3_SUPPORT QSize &asSize();
294#endif //QT3_SUPPORT
295
296#ifndef QT_NO_DATASTREAM
297 void load(QDataStream &ds);
298 void save(QDataStream &ds) const;
299#endif
300 static const char *typeToName(Type type);
301 static Type nameToType(const char *name);
302
303#ifdef QT3_SUPPORT
304 inline QT3_SUPPORT_CONSTRUCTOR QVariant(bool val, int) { create(Bool, &val); }
305 inline QT3_SUPPORT const QByteArray toCString() const { return toByteArray(); }
306 inline QT3_SUPPORT QByteArray &asCString() { return *reinterpret_cast<QByteArray *>(castOrDetach(ByteArray)); }
307#endif
308
309 void *data();
310 const void *constData() const;
311 inline const void *data() const { return constData(); }
312
313#ifndef QT_NO_MEMBER_TEMPLATES
314 template<typename T>
315 inline void setValue(const T &value);
316
317 template<typename T>
318 inline T value() const
319 { return qVariantValue<T>(*this); }
320
321 template<typename T>
322 static inline QVariant fromValue(const T &value)
323 { return qVariantFromValue(value); }
324
325 template<typename T>
326 bool canConvert() const
327 { return qVariantCanConvert<T>(*this); }
328#endif
329
330 public:
331#ifndef qdoc
332 struct PrivateShared
333 {
334 inline PrivateShared(void *v) : ptr(v), ref(1) { }
335 void *ptr;
336 QAtomicInt ref;
337 };
338 struct Private
339 {
340 inline Private(): type(Invalid), is_shared(false), is_null(true) { data.ptr = 0; }
341 inline Private(const Private &other)
342 : data(other.data), type(other.type),
343 is_shared(other.is_shared), is_null(other.is_null)
344 {}
345 union Data
346 {
347 char c;
348 int i;
349 uint u;
350 bool b;
351 double d;
352 qlonglong ll;
353 qulonglong ull;
354 void *ptr;
355 PrivateShared *shared;
356 } data;
357 uint type : 30;
358 uint is_shared : 1;
359 uint is_null : 1;
360 };
361 public:
362 typedef void (*f_construct)(Private *, const void *);
363 typedef void (*f_clear)(Private *);
364 typedef bool (*f_null)(const Private *);
365#ifndef QT_NO_DATASTREAM
366 typedef void (*f_load)(Private *, QDataStream &);
367 typedef void (*f_save)(const Private *, QDataStream &);
368#endif
369 typedef bool (*f_compare)(const Private *, const Private *);
370 typedef bool (*f_convert)(const QVariant::Private *d, Type t, void *, bool *);
371 typedef bool (*f_canConvert)(const QVariant::Private *d, Type t);
372 typedef void (*f_debugStream)(QDebug, const QVariant &);
373 struct Handler {
374 f_construct construct;
375 f_clear clear;
376 f_null isNull;
377#ifndef QT_NO_DATASTREAM
378 f_load load;
379 f_save save;
380#endif
381 f_compare compare;
382 f_convert convert;
383 f_canConvert canConvert;
384 f_debugStream debugStream;
385 };
386#endif
387
388 inline bool operator==(const QVariant &v) const
389 { return cmp(v); }
390 inline bool operator!=(const QVariant &v) const
391 { return !cmp(v); }
392
393protected:
394 friend inline bool qvariant_cast_helper(const QVariant &, QVariant::Type, void *);
395 friend int qRegisterGuiVariant();
396 friend int qUnregisterGuiVariant();
397 friend inline bool operator==(const QVariant &, const QVariantComparisonHelper &);
398#ifndef QT_NO_DEBUG_STREAM
399 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QVariant &);
400#endif
401 Private d;
402
403 static const Handler *handler;
404
405 void create(int type, const void *copy);
406#ifdef QT3_SUPPORT
407 void *castOrDetach(Type t);
408#endif
409 bool cmp(const QVariant &other) const;
410
411private:
412 // force compile error, prevent QVariant(bool) to be called
413 inline QVariant(void *) { Q_ASSERT(false); }
414#ifdef QT_NO_CAST_FROM_ASCII
415 // force compile error when implicit conversion is not wanted
416 inline QVariant(const char *) { Q_ASSERT(false); }
417#endif
418#ifndef QT3_SUPPORT
419 // force compile error, prevent QVariant(QVariant::Type, int) to be called
420 inline QVariant(bool, int) { Q_ASSERT(false); }
421#endif
422public:
423 typedef Private DataPtr;
424 inline DataPtr &data_ptr() { return d; }
425};
426
427typedef QList<QVariant> QVariantList;
428typedef QMap<QString, QVariant> QVariantMap;
429typedef QHash<QString, QVariant> QVariantHash;
430
431inline bool qvariant_cast_helper(const QVariant &v, QVariant::Type tp, void *ptr)
432{ return QVariant::handler->convert(&v.d, tp, ptr, 0); }
433
434template <typename T>
435inline QVariant qVariantFromValue(const T &t)
436{
437 return QVariant(qMetaTypeId<T>(reinterpret_cast<T *>(0)), &t);
438}
439
440template <>
441inline QVariant qVariantFromValue(const QVariant &t) { return t; }
442
443template <typename T>
444inline void qVariantSetValue(QVariant &v, const T &t)
445{
446 v = QVariant(qMetaTypeId<T>(reinterpret_cast<T *>(0)), &t);
447}
448
449inline QVariant::QVariant() {}
450inline bool QVariant::isValid() const { return d.type != Invalid; }
451
452#ifdef QT3_SUPPORT
453inline int &QVariant::asInt()
454{ return *reinterpret_cast<int *>(castOrDetach(Int)); }
455inline uint &QVariant::asUInt()
456{ return *reinterpret_cast<uint *>(castOrDetach(UInt)); }
457inline qlonglong &QVariant::asLongLong()
458{ return *reinterpret_cast<qlonglong *>(castOrDetach(LongLong)); }
459inline qulonglong &QVariant::asULongLong()
460{ return *reinterpret_cast<qulonglong *>(castOrDetach(ULongLong)); }
461inline bool &QVariant::asBool()
462{ return *reinterpret_cast<bool *>(castOrDetach(Bool)); }
463inline double &QVariant::asDouble()
464{ return *reinterpret_cast<double *>(castOrDetach(Double)); }
465inline QByteArray& QVariant::asByteArray()
466{ return *reinterpret_cast<QByteArray *>(castOrDetach(ByteArray)); }
467inline QBitArray& QVariant::asBitArray()
468{ return *reinterpret_cast<QBitArray *>(castOrDetach(BitArray)); }
469inline QString& QVariant::asString()
470{ return *reinterpret_cast<QString *>(castOrDetach(String)); }
471inline QStringList& QVariant::asStringList()
472{ return *reinterpret_cast<QStringList *>(castOrDetach(StringList)); }
473inline QDate& QVariant::asDate()
474{ return *reinterpret_cast<QDate *>(castOrDetach(Date)); }
475inline QTime& QVariant::asTime()
476{ return *reinterpret_cast<QTime *>(castOrDetach(Time)); }
477inline QDateTime& QVariant::asDateTime()
478{ return *reinterpret_cast<QDateTime *>(castOrDetach(DateTime)); }
479inline QList<QVariant>& QVariant::asList()
480{ return *reinterpret_cast<QList<QVariant> *>(castOrDetach(List)); }
481inline QMap<QString, QVariant>& QVariant::asMap()
482{ return *reinterpret_cast<QMap<QString, QVariant> *>(castOrDetach(Map)); }
483inline QPoint &QVariant::asPoint()
484{ return *reinterpret_cast<QPoint *>(castOrDetach(Point)); }
485inline QRect &QVariant::asRect()
486{ return *reinterpret_cast<QRect *>(castOrDetach(Rect)); }
487inline QSize &QVariant::asSize()
488{ return *reinterpret_cast<QSize *>(castOrDetach(Size)); }
489#endif //QT3_SUPPORT
490
491#ifndef QT_NO_MEMBER_TEMPLATES
492template<typename T>
493inline void QVariant::setValue(const T &avalue)
494{ qVariantSetValue(*this, avalue); }
495#endif
496
497#ifndef QT_NO_DATASTREAM
498Q_CORE_EXPORT QDataStream& operator>> (QDataStream& s, QVariant& p);
499Q_CORE_EXPORT QDataStream& operator<< (QDataStream& s, const QVariant& p);
500Q_CORE_EXPORT QDataStream& operator>> (QDataStream& s, QVariant::Type& p);
501Q_CORE_EXPORT QDataStream& operator<< (QDataStream& s, const QVariant::Type p);
502#endif
503
504inline bool QVariant::isDetached() const
505{ return !d.is_shared || d.data.shared->ref == 1; }
506
507
508#ifdef qdoc
509 inline bool operator==(const QVariant &v1, const QVariant &v2);
510 inline bool operator!=(const QVariant &v1, const QVariant &v2);
511#else
512
513/* Helper class to add one more level of indirection to prevent
514 implicit casts.
515*/
516class QVariantComparisonHelper
517{
518public:
519 inline QVariantComparisonHelper(const QVariant &var)
520 : v(&var) {}
521private:
522 friend inline bool operator==(const QVariant &, const QVariantComparisonHelper &);
523 const QVariant *v;
524};
525
526inline bool operator==(const QVariant &v1, const QVariantComparisonHelper &v2)
527{
528 return v1.cmp(*v2.v);
529}
530
531inline bool operator!=(const QVariant &v1, const QVariantComparisonHelper &v2)
532{
533 return !operator==(v1, v2);
534}
535#endif
536
537#ifndef QT_MOC
538#if !defined qdoc && defined Q_CC_MSVC && _MSC_VER < 1300
539
540template<typename T> T qvariant_cast(const QVariant &v, T * = 0)
541{
542 const int vid = qMetaTypeId<T>(static_cast<T *>(0));
543 if (vid == v.userType())
544 return *reinterpret_cast<const T *>(v.constData());
545 if (vid < int(QMetaType::User)) {
546 T t;
547 if (qvariant_cast_helper(v, QVariant::Type(vid), &t))
548 return t;
549 }
550 return T();
551}
552
553template<typename T>
554inline T qVariantValue(const QVariant &variant, T *t = 0)
555{ return qvariant_cast<T>(variant, t); }
556
557template<typename T>
558inline bool qVariantCanConvert(const QVariant &variant, T *t = 0)
559{
560 return variant.canConvert(static_cast<QVariant::Type>(qMetaTypeId<T>(t)));
561}
562#else
563
564template<typename T> T qvariant_cast(const QVariant &v)
565{
566 const int vid = qMetaTypeId<T>(static_cast<T *>(0));
567 if (vid == v.userType())
568 return *reinterpret_cast<const T *>(v.constData());
569 if (vid < int(QMetaType::User)) {
570 T t;
571 if (qvariant_cast_helper(v, QVariant::Type(vid), &t))
572 return t;
573 }
574 return T();
575}
576
577template<typename T>
578inline T qVariantValue(const QVariant &variant)
579{ return qvariant_cast<T>(variant); }
580
581template<typename T>
582inline bool qVariantCanConvert(const QVariant &variant)
583{
584 return variant.canConvert(static_cast<QVariant::Type>(
585 qMetaTypeId<T>(static_cast<T *>(0))));
586}
587#endif
588#endif
589Q_DECLARE_SHARED(QVariant)
590Q_DECLARE_TYPEINFO(QVariant, Q_MOVABLE_TYPE);
591
592#ifndef QT_NO_DEBUG_STREAM
593Q_CORE_EXPORT QDebug operator<<(QDebug, const QVariant &);
594Q_CORE_EXPORT QDebug operator<<(QDebug, const QVariant::Type);
595#endif
596
597QT_END_NAMESPACE
598
599Q_DECLARE_BUILTIN_METATYPE(QVariantList, QVariantList)
600Q_DECLARE_BUILTIN_METATYPE(QVariantMap, QVariantMap)
601Q_DECLARE_BUILTIN_METATYPE(QVariantHash, QVariantHash)
602
603QT_END_HEADER
604
605#endif // QVARIANT_H
Note: See TracBrowser for help on using the repository browser.