1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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.
56 #include <QtCore/qglobal.h>
57 #include <QtCore/qvariant.h>
58 #include <QtCore/private/qmetatype_p.h>
59 #include <QtCore/qdebug.h>
61 #include "qmetatypeswitcher_p.h"
67 struct QVariantIntegrator
69 static const bool CanUseInternalSpace = sizeof(T) <= sizeof(QVariant::Private::Data)
70 && (!QTypeInfo<T>::isStatic);
72 Q_STATIC_ASSERT(QVariantIntegrator<double>::CanUseInternalSpace);
73 Q_STATIC_ASSERT(QVariantIntegrator<long int>::CanUseInternalSpace);
74 Q_STATIC_ASSERT(QVariantIntegrator<qulonglong>::CanUseInternalSpace);
77 #ifdef Q_CC_SUN // Sun CC picks the wrong overload, so introduce awful hack
79 // takes a type, returns the internal void* pointer cast
80 // to a pointer of the input type
82 inline T *v_cast(const QVariant::Private *nd, T * = 0)
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));
90 #else // every other compiler in this world
93 inline const T *v_cast(const QVariant::Private *d, T * = 0)
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));
100 template <typename T>
101 inline T *v_cast(QVariant::Private *d, T * = 0)
103 return !QVariantIntegrator<T>::CanUseInternalSpace
104 ? static_cast<T *>(d->data.shared->ptr)
105 : static_cast<T *>(static_cast<void *>(&d->data.c));
111 //a simple template that avoids to allocate 2 memory chunks when creating a QVariant
112 template <class T> class QVariantPrivateSharedEx : public QVariant::PrivateShared
115 QVariantPrivateSharedEx() : QVariant::PrivateShared(&m_t) { }
116 QVariantPrivateSharedEx(const T&t) : QVariant::PrivateShared(&m_t), m_t(t) { }
122 // constructs a new variant if copy is 0, otherwise copy-constructs
124 inline void v_construct(QVariant::Private *x, const void *copy, T * = 0)
126 if (!QVariantIntegrator<T>::CanUseInternalSpace) {
127 x->data.shared = copy ? new QVariantPrivateSharedEx<T>(*static_cast<const T *>(copy))
128 : new QVariantPrivateSharedEx<T>;
132 new (&x->data.ptr) T(*static_cast<const T *>(copy));
134 new (&x->data.ptr) T;
139 inline void v_construct(QVariant::Private *x, const T &t)
141 if (!QVariantIntegrator<T>::CanUseInternalSpace) {
142 x->data.shared = new QVariantPrivateSharedEx<T>(t);
145 new (&x->data.ptr) T(t);
149 // deletes the internal structures
151 inline void v_clear(QVariant::Private *d, T* = 0)
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);
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)
170 return *v_cast<T>(a) == *v_cast<T>(b);
174 struct FilteredComparator<T, /* IsAcceptedType = */ false> {
175 static bool compare(const QVariant::Private *, const QVariant::Private *) { return false; }
178 QVariantComparator(const QVariant::Private *a, const QVariant::Private *b)
181 Q_ASSERT(a->type == b->type);
185 bool delegate(const T*)
187 return FilteredComparator<T>::compare(m_a, m_b);
190 bool delegate(const void*) { Q_ASSERT(false); return true; }
191 bool delegate(const QMetaTypeSwitcher::UnknownType*)
193 return true; // for historical reason invalid variant == invalid variant
195 bool delegate(const QMetaTypeSwitcher::NotBuiltinType*) { return false; }
197 const QVariant::Private *m_a;
198 const QVariant::Private *m_b;
202 Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler();
204 template<class Filter>
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
213 struct Yes { char unused[1]; };
214 struct No { char unused[2]; };
215 Q_STATIC_ASSERT(sizeof(Yes) != sizeof(No));
217 struct FallbackMixin { bool isNull() const; };
218 struct Derived : public T, public FallbackMixin {};
219 template<class C, C> struct TypeCheck {};
221 template<class C> static Yes test(...);
222 template<class C> static No test(TypeCheck<bool (FallbackMixin::*)() const, &C::isNull> *);
224 static const bool Value = (sizeof(test<Derived>(0)) == sizeof(Yes));
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.
230 class HasIsNullMethod<T, /* IsClass = */ false> {
232 static const bool Value = false;
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);
240 Q_STATIC_ASSERT(!HasIsNullMethod<SelfTest2>::Value);
241 struct SelfTest3 : public SelfTest1 {};
242 Q_STATIC_ASSERT(HasIsNullMethod<SelfTest3>::Value);
244 template<typename T, bool HasIsNull = HasIsNullMethod<T>::Value>
245 struct CallFilteredIsNull
247 static bool isNull(const QVariant::Private *d)
249 return v_cast<T>(d)->isNull();
253 struct CallFilteredIsNull<T, /* HasIsNull = */ false>
255 static bool isNull(const QVariant::Private *d)
261 template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
264 static bool isNull(const QVariant::Private *d)
266 return CallFilteredIsNull<T>::isNull(d);
270 struct CallIsNull<T, /* IsAcceptedType = */ false>
272 static bool isNull(const QVariant::Private *d)
274 return CallFilteredIsNull<T, false>::isNull(d);
279 QVariantIsNull(const QVariant::Private *d)
283 bool delegate(const T*)
285 return CallIsNull<T>::isNull(m_d);
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; }
292 const QVariant::Private *m_d;
295 template<class Filter>
296 class QVariantConstructor
298 template<typename T, bool CanUseInternalSpace = QVariantIntegrator<T>::CanUseInternalSpace>
299 struct CallConstructor {};
302 struct CallConstructor<T, /* CanUseInternalSpace = */ true>
304 CallConstructor(const QVariantConstructor &tc)
307 new (&tc.m_x->data.ptr) T(*static_cast<const T*>(tc.m_copy));
309 new (&tc.m_x->data.ptr) T();
310 tc.m_x->is_shared = false;
315 struct CallConstructor<T, /* CanUseInternalSpace = */ false>
317 CallConstructor(const QVariantConstructor &tc)
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;
326 template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
327 struct FilteredConstructor {
328 FilteredConstructor(const QVariantConstructor &tc)
330 CallConstructor<T> tmp(tc);
331 tc.m_x->is_null = !tc.m_copy;
335 struct FilteredConstructor<T, /* IsAcceptedType = */ false> {
336 FilteredConstructor(const QVariantConstructor &tc)
338 // ignore types that lives outside of the current library
339 tc.m_x->type = QVariant::Invalid;
343 QVariantConstructor(QVariant::Private *x, const void *copy)
349 void delegate(const T*)
351 FilteredConstructor<T>(*this);
354 void delegate(const QMetaTypeSwitcher::NotBuiltinType*)
356 qWarning("Trying to construct an instance of an invalid type, type id: %i", m_x->type);
357 m_x->type = QVariant::Invalid;
360 void delegate(const void*)
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;
368 void delegate(const QMetaTypeSwitcher::UnknownType*)
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;
374 m_x->is_shared = false;
375 m_x->is_null = !m_copy;
378 QVariant::Private *m_x;
382 template<class Filter>
383 class QVariantDestructor
385 template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
386 struct FilteredDestructor {
387 FilteredDestructor(QVariant::Private *d)
393 struct FilteredDestructor<T, /* IsAcceptedType = */ false> {
394 FilteredDestructor(QVariant::Private *) {} // ignore non accessible types
398 QVariantDestructor(QVariant::Private *d)
401 ~QVariantDestructor()
403 m_d->type = QVariant::Invalid;
405 m_d->is_shared = false;
409 void delegate(const T*)
411 FilteredDestructor<T> cleaner(m_d);
414 void delegate(const QMetaTypeSwitcher::NotBuiltinType*)
416 qWarning("Trying to destruct an instance of an invalid type, type id: %i", m_d->type);
418 // Ignore nonconstructible type
419 void delegate(const QMetaTypeSwitcher::UnknownType*) {}
420 void delegate(const void*) { Q_ASSERT(false); }
422 QVariant::Private *m_d;
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);
430 #if !defined(QT_NO_DEBUG_STREAM)
431 template<class Filter>
432 class QVariantDebugStream
434 template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
436 Filtered(QDebug dbg, QVariant::Private *d)
438 dbg.nospace() << *v_cast<T>(d);
442 struct Filtered<T, /* IsAcceptedType = */ false> {
443 Filtered(QDebug dbg, QVariant::Private *d)
445 dbg.nospace() << "QVariant::Type(" << d->type << ")";
450 QVariantDebugStream(QDebug dbg, QVariant::Private *d)
456 void delegate(const T*)
458 Filtered<T> streamIt(m_debugStream, m_d);
461 void delegate(const QMetaTypeSwitcher::NotBuiltinType*)
463 qWarning("Trying to stream an instance of an invalid type, type id: %i", m_d->type);
465 void delegate(const QMetaTypeSwitcher::UnknownType*)
467 m_debugStream.nospace() << "QVariant::Invalid";
469 void delegate(const void*) { Q_ASSERT(false); }
471 QDebug m_debugStream;
472 QVariant::Private *m_d;
478 #endif // QVARIANT_P_H