Introduce QMetaType::UnknownType.
[profile/ivi/qtbase.git] / src / corelib / kernel / qvariant_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QVARIANT_P_H
43 #define QVARIANT_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include <QtCore/qglobal.h>
57 #include <QtCore/qvariant.h>
58 #include <QtCore/private/qmetatype_p.h>
59 #include <QtCore/qdebug.h>
60
61 #include "qmetatypeswitcher_p.h"
62
63 QT_BEGIN_NAMESPACE
64
65 namespace {
66 template<typename T>
67 struct QVariantIntegrator
68 {
69     static const bool CanUseInternalSpace = sizeof(T) <= sizeof(QVariant::Private::Data)
70                                             && (!QTypeInfo<T>::isStatic);
71 };
72 Q_STATIC_ASSERT(QVariantIntegrator<double>::CanUseInternalSpace);
73 Q_STATIC_ASSERT(QVariantIntegrator<long int>::CanUseInternalSpace);
74 Q_STATIC_ASSERT(QVariantIntegrator<qulonglong>::CanUseInternalSpace);
75 } // namespace
76
77 #ifdef Q_CC_SUN // Sun CC picks the wrong overload, so introduce awful hack
78
79 // takes a type, returns the internal void* pointer cast
80 // to a pointer of the input type
81 template <typename T>
82 inline T *v_cast(const QVariant::Private *nd, T * = 0)
83 {
84     QVariant::Private *d = const_cast<QVariant::Private *>(nd);
85     return !QVariantIntegrator<T>::CanUseInternalSpace
86             ? static_cast<T *>(d->data.shared->ptr)
87             : static_cast<T *>(static_cast<void *>(&d->data.c));
88 }
89
90 #else // every other compiler in this world
91
92 template <typename T>
93 inline const T *v_cast(const QVariant::Private *d, T * = 0)
94 {
95     return !QVariantIntegrator<T>::CanUseInternalSpace
96             ? static_cast<const T *>(d->data.shared->ptr)
97             : static_cast<const T *>(static_cast<const void *>(&d->data.c));
98 }
99
100 template <typename T>
101 inline T *v_cast(QVariant::Private *d, T * = 0)
102 {
103     return !QVariantIntegrator<T>::CanUseInternalSpace
104             ? static_cast<T *>(d->data.shared->ptr)
105             : static_cast<T *>(static_cast<void *>(&d->data.c));
106 }
107
108 #endif
109
110
111 //a simple template that avoids to allocate 2 memory chunks when creating a QVariant
112 template <class T> class QVariantPrivateSharedEx : public QVariant::PrivateShared
113 {
114 public:
115     QVariantPrivateSharedEx() : QVariant::PrivateShared(&m_t) { }
116     QVariantPrivateSharedEx(const T&t) : QVariant::PrivateShared(&m_t), m_t(t) { }
117
118 private:
119     T m_t;
120 };
121
122 // constructs a new variant if copy is 0, otherwise copy-constructs
123 template <class T>
124 inline void v_construct(QVariant::Private *x, const void *copy, T * = 0)
125 {
126     if (!QVariantIntegrator<T>::CanUseInternalSpace) {
127         x->data.shared = copy ? new QVariantPrivateSharedEx<T>(*static_cast<const T *>(copy))
128                               : new QVariantPrivateSharedEx<T>;
129         x->is_shared = true;
130     } else {
131         if (copy)
132             new (&x->data.ptr) T(*static_cast<const T *>(copy));
133         else
134             new (&x->data.ptr) T;
135     }
136 }
137
138 template <class T>
139 inline void v_construct(QVariant::Private *x, const T &t)
140 {
141     if (!QVariantIntegrator<T>::CanUseInternalSpace) {
142         x->data.shared = new QVariantPrivateSharedEx<T>(t);
143         x->is_shared = true;
144     } else {
145         new (&x->data.ptr) T(t);
146     }
147 }
148
149 // deletes the internal structures
150 template <class T>
151 inline void v_clear(QVariant::Private *d, T* = 0)
152 {
153     
154     if (!QVariantIntegrator<T>::CanUseInternalSpace) {
155         //now we need to cast
156         //because QVariant::PrivateShared doesn't have a virtual destructor
157         delete static_cast<QVariantPrivateSharedEx<T>*>(d->data.shared);
158     } else {
159         v_cast<T>(d)->~T();
160     }
161
162 }
163
164 template<class Filter>
165 class QVariantComparator {
166     template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
167     struct FilteredComparator {
168         static bool compare(const QVariant::Private *a, const QVariant::Private *b)
169         {
170             return *v_cast<T>(a) == *v_cast<T>(b);
171         }
172     };
173     template<typename T>
174     struct FilteredComparator<T, /* IsAcceptedType = */ false> {
175         static bool compare(const QVariant::Private *, const QVariant::Private *) { return false; }
176     };
177 public:
178     QVariantComparator(const QVariant::Private *a, const QVariant::Private *b)
179         : m_a(a), m_b(b)
180     {
181         Q_ASSERT(a->type == b->type);
182     }
183
184     template<typename T>
185     bool delegate(const T*)
186     {
187         return FilteredComparator<T>::compare(m_a, m_b);
188     }
189
190     bool delegate(const void*) { Q_ASSERT(false); return true; }
191     bool delegate(const QMetaTypeSwitcher::UnknownType*)
192     {
193         return true; // for historical reason invalid variant == invalid variant
194     }
195     bool delegate(const QMetaTypeSwitcher::NotBuiltinType*) { return false; }
196 protected:
197     const QVariant::Private *m_a;
198     const QVariant::Private *m_b;
199 };
200
201
202 Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler();
203
204 template<class Filter>
205 class QVariantIsNull
206 {
207     /// \internal
208     /// This class checks if a type T has method called isNull. Result is kept in the Value property
209     /// TODO Can we somehow generalize it? A macro version?
210     template<typename T, bool IsClass = QTypeInfo<T>::isComplex>
211     class HasIsNullMethod
212     {
213         struct Yes { char unused[1]; };
214         struct No { char unused[2]; };
215         Q_STATIC_ASSERT(sizeof(Yes) != sizeof(No));
216
217         struct FallbackMixin { bool isNull() const; };
218         struct Derived : public T, public FallbackMixin {};
219         template<class C, C> struct TypeCheck {};
220
221         template<class C> static Yes test(...);
222         template<class C> static No test(TypeCheck<bool (FallbackMixin::*)() const, &C::isNull> *);
223     public:
224         static const bool Value = (sizeof(test<Derived>(0)) == sizeof(Yes));
225     };
226
227     // We need to exclude primitive types as they won't compile with HasIsNullMethod::Check classes
228     // anyway it is not a problem as the types do not have isNull method.
229     template<typename T>
230     class HasIsNullMethod<T, /* IsClass = */ false> {
231     public:
232         static const bool Value = false;
233     };
234
235     // TODO This part should go to autotests during HasIsNullMethod generalization.
236     Q_STATIC_ASSERT(!HasIsNullMethod<bool>::Value);
237     struct SelfTest1 { bool isNull() const; };
238     Q_STATIC_ASSERT(HasIsNullMethod<SelfTest1>::Value);
239     struct SelfTest2 {};
240     Q_STATIC_ASSERT(!HasIsNullMethod<SelfTest2>::Value);
241     struct SelfTest3 : public SelfTest1 {};
242     Q_STATIC_ASSERT(HasIsNullMethod<SelfTest3>::Value);
243
244     template<typename T, bool HasIsNull = HasIsNullMethod<T>::Value>
245     struct CallFilteredIsNull
246     {
247         static bool isNull(const QVariant::Private *d)
248         {
249             return v_cast<T>(d)->isNull();
250         }
251     };
252     template<typename T>
253     struct CallFilteredIsNull<T, /* HasIsNull = */ false>
254     {
255         static bool isNull(const QVariant::Private *d)
256         {
257             return d->is_null;
258         }
259     };
260
261     template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
262     struct CallIsNull
263     {
264         static bool isNull(const QVariant::Private *d)
265         {
266             return CallFilteredIsNull<T>::isNull(d);
267         }
268     };
269     template<typename T>
270     struct CallIsNull<T, /* IsAcceptedType = */ false>
271     {
272         static bool isNull(const QVariant::Private *d)
273         {
274             return CallFilteredIsNull<T, false>::isNull(d);
275         }
276     };
277
278 public:
279     QVariantIsNull(const QVariant::Private *d)
280         : m_d(d)
281     {}
282     template<typename T>
283     bool delegate(const T*)
284     {
285         return CallIsNull<T>::isNull(m_d);
286     }
287     // we need that as sizof(void) is undefined and it is needed in HasIsNullMethod
288     bool delegate(const void *) { Q_ASSERT(false); return m_d->is_null; }
289     bool delegate(const QMetaTypeSwitcher::UnknownType *) { return m_d->is_null; }
290     bool delegate(const QMetaTypeSwitcher::NotBuiltinType *) { return m_d->is_null; }
291 protected:
292     const QVariant::Private *m_d;
293 };
294
295 template<class Filter>
296 class QVariantConstructor
297 {
298     template<typename T, bool CanUseInternalSpace = QVariantIntegrator<T>::CanUseInternalSpace>
299     struct CallConstructor {};
300
301     template<typename T>
302     struct CallConstructor<T, /* CanUseInternalSpace = */ true>
303     {
304         CallConstructor(const QVariantConstructor &tc)
305         {
306             if (tc.m_copy)
307                 new (&tc.m_x->data.ptr) T(*static_cast<const T*>(tc.m_copy));
308             else
309                 new (&tc.m_x->data.ptr) T();
310             tc.m_x->is_shared = false;
311         }
312     };
313
314     template<typename T>
315     struct CallConstructor<T, /* CanUseInternalSpace = */ false>
316     {
317         CallConstructor(const QVariantConstructor &tc)
318         {
319             Q_STATIC_ASSERT(QTypeInfo<T>::isComplex || sizeof(T) > sizeof(QVariant::Private::Data));
320             tc.m_x->data.shared = tc.m_copy ? new QVariantPrivateSharedEx<T>(*static_cast<const T*>(tc.m_copy))
321                                       : new QVariantPrivateSharedEx<T>;
322             tc.m_x->is_shared = true;
323         }
324     };
325
326     template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
327     struct FilteredConstructor {
328         FilteredConstructor(const QVariantConstructor &tc)
329         {
330             CallConstructor<T> tmp(tc);
331             tc.m_x->is_null = !tc.m_copy;
332         }
333     };
334     template<typename T>
335     struct FilteredConstructor<T, /* IsAcceptedType = */ false> {
336         FilteredConstructor(const QVariantConstructor &tc)
337         {
338             // ignore types that lives outside of the current library
339             tc.m_x->type = QVariant::Invalid;
340         }
341     };
342 public:
343     QVariantConstructor(QVariant::Private *x, const void *copy)
344         : m_x(x)
345         , m_copy(copy)
346     {}
347
348     template<typename T>
349     void delegate(const T*)
350     {
351         FilteredConstructor<T>(*this);
352     }
353
354     void delegate(const QMetaTypeSwitcher::NotBuiltinType*)
355     {
356         qWarning("Trying to construct an instance of an invalid type, type id: %i", m_x->type);
357         m_x->type = QVariant::Invalid;
358     }
359
360     void delegate(const void*)
361     {
362         qWarning("Trying to create a QVariant instance of QMetaType::Void type, an invalid QVariant will be constructed instead");
363         m_x->type = QMetaType::UnknownType;
364         m_x->is_shared = false;
365         m_x->is_null = !m_copy;
366     }
367
368     void delegate(const QMetaTypeSwitcher::UnknownType*)
369     {
370         if (m_x->type != QMetaType::UnknownType) {
371             qWarning("Trying to construct an instance of an invalid type, type id: %i", m_x->type);
372             m_x->type = QMetaType::UnknownType;
373         }
374         m_x->is_shared = false;
375         m_x->is_null = !m_copy;
376     }
377 private:
378     QVariant::Private *m_x;
379     const void *m_copy;
380 };
381
382 template<class Filter>
383 class QVariantDestructor
384 {
385     template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
386     struct FilteredDestructor {
387         FilteredDestructor(QVariant::Private *d)
388         {
389             v_clear<T>(d);
390         }
391     };
392     template<typename T>
393     struct FilteredDestructor<T, /* IsAcceptedType = */ false> {
394         FilteredDestructor(QVariant::Private *) {} // ignore non accessible types
395     };
396
397 public:
398     QVariantDestructor(QVariant::Private *d)
399         : m_d(d)
400     {}
401     ~QVariantDestructor()
402     {
403         m_d->type = QVariant::Invalid;
404         m_d->is_null = true;
405         m_d->is_shared = false;
406     }
407
408     template<typename T>
409     void delegate(const T*)
410     {
411         FilteredDestructor<T> cleaner(m_d);
412     }
413
414     void delegate(const QMetaTypeSwitcher::NotBuiltinType*)
415     {
416         qWarning("Trying to destruct an instance of an invalid type, type id: %i", m_d->type);
417     }
418     // Ignore nonconstructible type
419     void delegate(const QMetaTypeSwitcher::UnknownType*) {}
420     void delegate(const void*) { Q_ASSERT(false); }
421 private:
422     QVariant::Private *m_d;
423 };
424
425 namespace QVariantPrivate {
426 Q_CORE_EXPORT void registerHandler(const int /* Modules::Names */ name, const QVariant::Handler *handler);
427 Q_CORE_EXPORT void unregisterHandler(const int /* Modules::Names */ name);
428 }
429
430 #if !defined(QT_NO_DEBUG_STREAM)
431 template<class Filter>
432 class QVariantDebugStream
433 {
434     template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
435     struct Filtered {
436         Filtered(QDebug dbg, QVariant::Private *d)
437         {
438             dbg.nospace() << *v_cast<T>(d);
439         }
440     };
441     template<typename T>
442     struct Filtered<T, /* IsAcceptedType = */ false> {
443         Filtered(QDebug dbg, QVariant::Private *d)
444         {
445             dbg.nospace() << "QVariant::Type(" << d->type << ")";
446         }
447     };
448
449 public:
450     QVariantDebugStream(QDebug dbg, QVariant::Private *d)
451         : m_debugStream(dbg)
452         , m_d(d)
453     {}
454
455     template<typename T>
456     void delegate(const T*)
457     {
458         Filtered<T> streamIt(m_debugStream, m_d);
459     }
460
461     void delegate(const QMetaTypeSwitcher::NotBuiltinType*)
462     {
463         qWarning("Trying to stream an instance of an invalid type, type id: %i", m_d->type);
464     }
465     void delegate(const QMetaTypeSwitcher::UnknownType*)
466     {
467         m_debugStream.nospace() << "QVariant::Invalid";
468     }
469     void delegate(const void*) { Q_ASSERT(false); }
470 private:
471     QDebug m_debugStream;
472     QVariant::Private *m_d;
473 };
474 #endif
475
476 QT_END_NAMESPACE
477
478 #endif // QVARIANT_P_H