a754bc436389446bd7091d410e4cfbe05d035364
[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*) { return true; }
191     bool delegate(const QMetaTypeSwitcher::NotBuiltinType*) { return false; }
192 protected:
193     const QVariant::Private *m_a;
194     const QVariant::Private *m_b;
195 };
196
197
198 Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler();
199
200 template<class Filter>
201 class QVariantIsNull
202 {
203     /// \internal
204     /// This class checks if a type T has method called isNull. Result is kept in the Value property
205     /// TODO Can we somehow generalize it? A macro version?
206     template<typename T, bool IsClass = QTypeInfo<T>::isComplex>
207     class HasIsNullMethod
208     {
209         struct Yes { char unused[1]; };
210         struct No { char unused[2]; };
211         Q_STATIC_ASSERT(sizeof(Yes) != sizeof(No));
212
213         struct FallbackMixin { bool isNull() const; };
214         struct Derived : public T, public FallbackMixin {};
215         template<class C, C> struct TypeCheck {};
216
217         template<class C> static Yes test(...);
218         template<class C> static No test(TypeCheck<bool (FallbackMixin::*)() const, &C::isNull> *);
219     public:
220         static const bool Value = (sizeof(test<Derived>(0)) == sizeof(Yes));
221     };
222
223     // We need to exclude primitive types as they won't compile with HasIsNullMethod::Check classes
224     // anyway it is not a problem as the types do not have isNull method.
225     template<typename T>
226     class HasIsNullMethod<T, /* IsClass = */ false> {
227     public:
228         static const bool Value = false;
229     };
230
231     // TODO This part should go to autotests during HasIsNullMethod generalization.
232     Q_STATIC_ASSERT(!HasIsNullMethod<bool>::Value);
233     struct SelfTest1 { bool isNull() const; };
234     Q_STATIC_ASSERT(HasIsNullMethod<SelfTest1>::Value);
235     struct SelfTest2 {};
236     Q_STATIC_ASSERT(!HasIsNullMethod<SelfTest2>::Value);
237     struct SelfTest3 : public SelfTest1 {};
238     Q_STATIC_ASSERT(HasIsNullMethod<SelfTest3>::Value);
239
240     template<typename T, bool HasIsNull = HasIsNullMethod<T>::Value>
241     struct CallFilteredIsNull
242     {
243         static bool isNull(const QVariant::Private *d)
244         {
245             return v_cast<T>(d)->isNull();
246         }
247     };
248     template<typename T>
249     struct CallFilteredIsNull<T, /* HasIsNull = */ false>
250     {
251         static bool isNull(const QVariant::Private *d)
252         {
253             return d->is_null;
254         }
255     };
256
257     template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
258     struct CallIsNull
259     {
260         static bool isNull(const QVariant::Private *d)
261         {
262             return CallFilteredIsNull<T>::isNull(d);
263         }
264     };
265     template<typename T>
266     struct CallIsNull<T, /* IsAcceptedType = */ false>
267     {
268         static bool isNull(const QVariant::Private *d)
269         {
270             return CallFilteredIsNull<T, false>::isNull(d);
271         }
272     };
273
274 public:
275     QVariantIsNull(const QVariant::Private *d)
276         : m_d(d)
277     {}
278     template<typename T>
279     bool delegate(const T*)
280     {
281         return CallIsNull<T>::isNull(m_d);
282     }
283     // we need that as sizof(void) is undefined and it is needed in HasIsNullMethod
284     bool delegate(const void *) { return m_d->is_null; }
285     bool delegate(const QMetaTypeSwitcher::NotBuiltinType *) { return m_d->is_null; }
286 protected:
287     const QVariant::Private *m_d;
288 };
289
290 template<class Filter>
291 class QVariantConstructor
292 {
293     template<typename T, bool CanUseInternalSpace = QVariantIntegrator<T>::CanUseInternalSpace>
294     struct CallConstructor {};
295
296     template<typename T>
297     struct CallConstructor<T, /* CanUseInternalSpace = */ true>
298     {
299         CallConstructor(const QVariantConstructor &tc)
300         {
301             if (tc.m_copy)
302                 new (&tc.m_x->data.ptr) T(*static_cast<const T*>(tc.m_copy));
303             else
304                 new (&tc.m_x->data.ptr) T();
305             tc.m_x->is_shared = false;
306         }
307     };
308
309     template<typename T>
310     struct CallConstructor<T, /* CanUseInternalSpace = */ false>
311     {
312         CallConstructor(const QVariantConstructor &tc)
313         {
314             Q_STATIC_ASSERT(QTypeInfo<T>::isComplex || sizeof(T) > sizeof(QVariant::Private::Data));
315             tc.m_x->data.shared = tc.m_copy ? new QVariantPrivateSharedEx<T>(*static_cast<const T*>(tc.m_copy))
316                                       : new QVariantPrivateSharedEx<T>;
317             tc.m_x->is_shared = true;
318         }
319     };
320
321     template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
322     struct FilteredConstructor {
323         FilteredConstructor(const QVariantConstructor &tc)
324         {
325             CallConstructor<T> tmp(tc);
326             tc.m_x->is_null = !tc.m_copy;
327         }
328     };
329     template<typename T>
330     struct FilteredConstructor<T, /* IsAcceptedType = */ false> {
331         FilteredConstructor(const QVariantConstructor &tc)
332         {
333             // ignore types that lives outside of the current library
334             tc.m_x->type = QVariant::Invalid;
335         }
336     };
337 public:
338     QVariantConstructor(QVariant::Private *x, const void *copy)
339         : m_x(x)
340         , m_copy(copy)
341     {}
342
343     template<typename T>
344     void delegate(const T*)
345     {
346         FilteredConstructor<T>(*this);
347     }
348
349     void delegate(const QMetaTypeSwitcher::NotBuiltinType*)
350     {
351         qWarning("Trying to construct an instance of an invalid type, type id: %i", m_x->type);
352         m_x->type = QVariant::Invalid;
353     }
354
355     void delegate(const void*)
356     {
357         // QMetaType::Void == QVariant::Invalid, creating an invalid value creates invalid QVariant
358         // TODO it might go away, check is needed
359         m_x->is_shared = false;
360         m_x->is_null = !m_copy;
361     }
362 private:
363     QVariant::Private *m_x;
364     const void *m_copy;
365 };
366
367 template<class Filter>
368 class QVariantDestructor
369 {
370     template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
371     struct FilteredDestructor {
372         FilteredDestructor(QVariant::Private *d)
373         {
374             v_clear<T>(d);
375         }
376     };
377     template<typename T>
378     struct FilteredDestructor<T, /* IsAcceptedType = */ false> {
379         FilteredDestructor(QVariant::Private *) {} // ignore non accessible types
380     };
381
382 public:
383     QVariantDestructor(QVariant::Private *d)
384         : m_d(d)
385     {}
386     ~QVariantDestructor()
387     {
388         m_d->type = QVariant::Invalid;
389         m_d->is_null = true;
390         m_d->is_shared = false;
391     }
392
393     template<typename T>
394     void delegate(const T*)
395     {
396         FilteredDestructor<T> cleaner(m_d);
397     }
398
399     void delegate(const QMetaTypeSwitcher::NotBuiltinType*)
400     {
401         qWarning("Trying to destruct an instance of an invalid type, type id: %i", m_d->type);
402     }
403     // Ignore nonconstructible type
404     void delegate(const void*) {}
405 private:
406     QVariant::Private *m_d;
407 };
408
409 namespace QVariantPrivate {
410 Q_CORE_EXPORT void registerHandler(const int /* Modules::Names */ name, const QVariant::Handler *handler);
411 Q_CORE_EXPORT void unregisterHandler(const int /* Modules::Names */ name);
412 }
413
414 #if !defined(QT_NO_DEBUG_STREAM)
415 template<class Filter>
416 class QVariantDebugStream
417 {
418     template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
419     struct Filtered {
420         Filtered(QDebug dbg, QVariant::Private *d)
421         {
422             dbg.nospace() << *v_cast<T>(d);
423         }
424     };
425     template<typename T>
426     struct Filtered<T, /* IsAcceptedType = */ false> {
427         Filtered(QDebug dbg, QVariant::Private *d)
428         {
429             dbg.nospace() << "QVariant::Type(" << d->type << ")";
430         }
431     };
432
433 public:
434     QVariantDebugStream(QDebug dbg, QVariant::Private *d)
435         : m_debugStream(dbg)
436         , m_d(d)
437     {}
438
439     template<typename T>
440     void delegate(const T*)
441     {
442         Filtered<T> streamIt(m_debugStream, m_d);
443     }
444
445     void delegate(const QMetaTypeSwitcher::NotBuiltinType*)
446     {
447         qWarning("Trying to stream an instance of an invalid type, type id: %i", m_d->type);
448     }
449     void delegate(const void*)
450     {
451         m_debugStream.nospace() << "QVariant::Invalid";
452     }
453 private:
454     QDebug m_debugStream;
455     QVariant::Private *m_d;
456 };
457 #endif
458
459 QT_END_NAMESPACE
460
461 #endif // QVARIANT_P_H