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*) { return true; }
191 bool delegate(const QMetaTypeSwitcher::NotBuiltinType*) { return false; }
193 const QVariant::Private *m_a;
194 const QVariant::Private *m_b;
198 Q_CORE_EXPORT const QVariant::Handler *qcoreVariantHandler();
200 template<class Filter>
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
209 struct Yes { char unused[1]; };
210 struct No { char unused[2]; };
211 Q_STATIC_ASSERT(sizeof(Yes) != sizeof(No));
213 struct FallbackMixin { bool isNull() const; };
214 struct Derived : public T, public FallbackMixin {};
215 template<class C, C> struct TypeCheck {};
217 template<class C> static Yes test(...);
218 template<class C> static No test(TypeCheck<bool (FallbackMixin::*)() const, &C::isNull> *);
220 static const bool Value = (sizeof(test<Derived>(0)) == sizeof(Yes));
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.
226 class HasIsNullMethod<T, /* IsClass = */ false> {
228 static const bool Value = false;
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);
236 Q_STATIC_ASSERT(!HasIsNullMethod<SelfTest2>::Value);
237 struct SelfTest3 : public SelfTest1 {};
238 Q_STATIC_ASSERT(HasIsNullMethod<SelfTest3>::Value);
240 template<typename T, bool HasIsNull = HasIsNullMethod<T>::Value>
241 struct CallFilteredIsNull
243 static bool isNull(const QVariant::Private *d)
245 return v_cast<T>(d)->isNull();
249 struct CallFilteredIsNull<T, /* HasIsNull = */ false>
251 static bool isNull(const QVariant::Private *d)
257 template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
260 static bool isNull(const QVariant::Private *d)
262 return CallFilteredIsNull<T>::isNull(d);
266 struct CallIsNull<T, /* IsAcceptedType = */ false>
268 static bool isNull(const QVariant::Private *d)
270 return CallFilteredIsNull<T, false>::isNull(d);
275 QVariantIsNull(const QVariant::Private *d)
279 bool delegate(const T*)
281 return CallIsNull<T>::isNull(m_d);
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; }
287 const QVariant::Private *m_d;
290 template<class Filter>
291 class QVariantConstructor
293 template<typename T, bool CanUseInternalSpace = QVariantIntegrator<T>::CanUseInternalSpace>
294 struct CallConstructor {};
297 struct CallConstructor<T, /* CanUseInternalSpace = */ true>
299 CallConstructor(const QVariantConstructor &tc)
302 new (&tc.m_x->data.ptr) T(*static_cast<const T*>(tc.m_copy));
304 new (&tc.m_x->data.ptr) T();
305 tc.m_x->is_shared = false;
310 struct CallConstructor<T, /* CanUseInternalSpace = */ false>
312 CallConstructor(const QVariantConstructor &tc)
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;
321 template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
322 struct FilteredConstructor {
323 FilteredConstructor(const QVariantConstructor &tc)
325 CallConstructor<T> tmp(tc);
326 tc.m_x->is_null = !tc.m_copy;
330 struct FilteredConstructor<T, /* IsAcceptedType = */ false> {
331 FilteredConstructor(const QVariantConstructor &tc)
333 // ignore types that lives outside of the current library
334 tc.m_x->type = QVariant::Invalid;
338 QVariantConstructor(QVariant::Private *x, const void *copy)
344 void delegate(const T*)
346 FilteredConstructor<T>(*this);
349 void delegate(const QMetaTypeSwitcher::NotBuiltinType*)
351 qWarning("Trying to construct an instance of an invalid type, type id: %i", m_x->type);
352 m_x->type = QVariant::Invalid;
355 void delegate(const void*)
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;
363 QVariant::Private *m_x;
367 template<class Filter>
368 class QVariantDestructor
370 template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
371 struct FilteredDestructor {
372 FilteredDestructor(QVariant::Private *d)
378 struct FilteredDestructor<T, /* IsAcceptedType = */ false> {
379 FilteredDestructor(QVariant::Private *) {} // ignore non accessible types
383 QVariantDestructor(QVariant::Private *d)
386 ~QVariantDestructor()
388 m_d->type = QVariant::Invalid;
390 m_d->is_shared = false;
394 void delegate(const T*)
396 FilteredDestructor<T> cleaner(m_d);
399 void delegate(const QMetaTypeSwitcher::NotBuiltinType*)
401 qWarning("Trying to destruct an instance of an invalid type, type id: %i", m_d->type);
403 // Ignore nonconstructible type
404 void delegate(const void*) {}
406 QVariant::Private *m_d;
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);
414 #if !defined(QT_NO_DEBUG_STREAM)
415 template<class Filter>
416 class QVariantDebugStream
418 template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
420 Filtered(QDebug dbg, QVariant::Private *d)
422 dbg.nospace() << *v_cast<T>(d);
426 struct Filtered<T, /* IsAcceptedType = */ false> {
427 Filtered(QDebug dbg, QVariant::Private *d)
429 dbg.nospace() << "QVariant::Type(" << d->type << ")";
434 QVariantDebugStream(QDebug dbg, QVariant::Private *d)
440 void delegate(const T*)
442 Filtered<T> streamIt(m_debugStream, m_d);
445 void delegate(const QMetaTypeSwitcher::NotBuiltinType*)
447 qWarning("Trying to stream an instance of an invalid type, type id: %i", m_d->type);
449 void delegate(const void*)
451 m_debugStream.nospace() << "QVariant::Invalid";
454 QDebug m_debugStream;
455 QVariant::Private *m_d;
461 #endif // QVARIANT_P_H