Make usage of internal QVariant space.
[profile/ivi/qtbase.git] / src / widgets / kernel / qwidgetsvariant.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtGui module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qvariant.h"
43
44 #include "qicon.h"
45 #include "qsizepolicy.h"
46
47 #include "private/qvariant_p.h"
48
49 QT_BEGIN_NAMESPACE
50
51
52 static void construct(QVariant::Private *x, const void *copy)
53 {
54     switch (x->type) {
55 #ifndef QT_NO_ICON
56     case QVariant::Icon:
57         v_construct<QIcon>(x, copy);
58         break;
59 #endif
60     case QVariant::SizePolicy:
61         v_construct<QSizePolicy>(x, copy);
62         break;
63     default:
64         Q_ASSERT(false);
65         return;
66     }
67     x->is_null = !copy;
68 }
69
70 static void clear(QVariant::Private *d)
71 {
72     switch (d->type) {
73 #ifndef QT_NO_ICON
74     case QVariant::Icon:
75         v_clear<QIcon>(d);
76         break;
77 #endif
78     case QVariant::SizePolicy:
79         v_clear<QSizePolicy>(d);
80         break;
81     default:
82         Q_ASSERT(false);
83         return;
84     }
85
86     d->type = QVariant::Invalid;
87     d->is_null = true;
88     d->is_shared = false;
89 }
90
91
92 static bool isNull(const QVariant::Private *d)
93 {
94     switch(d->type) {
95 #ifndef QT_NO_ICON
96     case QVariant::Icon:
97         return v_cast<QIcon>(d)->isNull();
98 #endif
99     default:
100         Q_ASSERT(false);
101     }
102     return true;
103 }
104
105 static bool compare(const QVariant::Private *a, const QVariant::Private *b)
106 {
107     Q_ASSERT(a->type == b->type);
108     switch(a->type) {
109 #ifndef QT_NO_ICON
110     case QVariant::Icon:
111         return false;
112 #endif
113     case QVariant::SizePolicy:
114         return *v_cast<QSizePolicy>(a) == *v_cast<QSizePolicy>(b);
115     default:
116         Q_ASSERT(false);
117     }
118     return false;
119 }
120
121
122 static const QVariant::Handler widgets_handler = {
123     construct,
124     clear,
125     isNull,
126 #ifndef QT_NO_DATASTREAM
127     0,
128     0,
129 #endif
130     compare,
131     0,
132     0,
133 #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM)
134     0
135 #else
136     0
137 #endif
138 };
139
140 struct QMetaTypeGuiHelper
141 {
142     QMetaType::Creator creator;
143     QMetaType::Deleter deleter;
144 #ifndef QT_NO_DATASTREAM
145     QMetaType::SaveOperator saveOp;
146     QMetaType::LoadOperator loadOp;
147 #endif
148     QMetaType::Constructor constructor;
149     QMetaType::Destructor destructor;
150     int size;
151 };
152
153 extern Q_CORE_EXPORT const QMetaTypeGuiHelper *qMetaTypeWidgetsHelper;
154
155
156 #ifdef QT_NO_DATASTREAM
157 #  define Q_DECL_METATYPE_HELPER(TYPE) \
158      typedef void *(*QCreate##TYPE)(const TYPE *); \
159      static const QCreate##TYPE qCreate##TYPE = qMetaTypeCreateHelper<TYPE>; \
160      typedef void (*QDelete##TYPE)(TYPE *); \
161      static const QDelete##TYPE qDelete##TYPE = qMetaTypeDeleteHelper<TYPE>; \
162      typedef void *(*QConstruct##TYPE)(void *, const TYPE *); \
163      static const QConstruct##TYPE qConstruct##TYPE = qMetaTypeConstructHelper<TYPE>; \
164      typedef void (*QDestruct##TYPE)(TYPE *); \
165      static const QDestruct##TYPE qDestruct##TYPE = qMetaTypeDestructHelper<TYPE>;
166 #else
167 #  define Q_DECL_METATYPE_HELPER(TYPE) \
168      typedef void *(*QCreate##TYPE)(const TYPE *); \
169      static const QCreate##TYPE qCreate##TYPE = qMetaTypeCreateHelper<TYPE>; \
170      typedef void (*QDelete##TYPE)(TYPE *); \
171      static const QDelete##TYPE qDelete##TYPE = qMetaTypeDeleteHelper<TYPE>; \
172      typedef void *(*QConstruct##TYPE)(void *, const TYPE *); \
173      static const QConstruct##TYPE qConstruct##TYPE = qMetaTypeConstructHelper<TYPE>; \
174      typedef void (*QDestruct##TYPE)(TYPE *); \
175      static const QDestruct##TYPE qDestruct##TYPE = qMetaTypeDestructHelper<TYPE>; \
176      typedef void (*QSave##TYPE)(QDataStream &, const TYPE *); \
177      static const QSave##TYPE qSave##TYPE = qMetaTypeSaveHelper<TYPE>; \
178      typedef void (*QLoad##TYPE)(QDataStream &, TYPE *); \
179      static const QLoad##TYPE qLoad##TYPE = qMetaTypeLoadHelper<TYPE>;
180 #endif
181
182 #ifndef QT_NO_ICON
183 Q_DECL_METATYPE_HELPER(QIcon)
184 #endif
185 Q_DECL_METATYPE_HELPER(QSizePolicy)
186
187 #ifdef QT_NO_DATASTREAM
188 #  define Q_IMPL_METATYPE_HELPER(TYPE) \
189      { reinterpret_cast<QMetaType::Creator>(qCreate##TYPE), \
190        reinterpret_cast<QMetaType::Deleter>(qDelete##TYPE), \
191        reinterpret_cast<QMetaType::Constructor>(qConstruct##TYPE), \
192        reinterpret_cast<QMetaType::Destructor>(qDestruct##TYPE), \
193        sizeof(TYPE) \
194      }
195 #else
196 #  define Q_IMPL_METATYPE_HELPER(TYPE) \
197      { reinterpret_cast<QMetaType::Creator>(qCreate##TYPE), \
198        reinterpret_cast<QMetaType::Deleter>(qDelete##TYPE), \
199        reinterpret_cast<QMetaType::SaveOperator>(qSave##TYPE), \
200        reinterpret_cast<QMetaType::LoadOperator>(qLoad##TYPE), \
201        reinterpret_cast<QMetaType::Constructor>(qConstruct##TYPE), \
202        reinterpret_cast<QMetaType::Destructor>(qDestruct##TYPE), \
203        sizeof(TYPE) \
204      }
205 #endif
206
207 static const QMetaTypeGuiHelper qVariantWidgetsHelper[] = {
208 #ifdef QT_NO_ICON
209     {0, 0, 0, 0},
210 #else
211     Q_IMPL_METATYPE_HELPER(QIcon),
212 #endif
213     Q_IMPL_METATYPE_HELPER(QSizePolicy),
214 };
215
216 extern Q_GUI_EXPORT const QVariant::Handler *qt_widgets_variant_handler;
217
218 int qRegisterWidgetsVariant()
219 {
220     qt_widgets_variant_handler = &widgets_handler;
221     qMetaTypeWidgetsHelper = qVariantWidgetsHelper;
222     return 1;
223 }
224 Q_CONSTRUCTOR_FUNCTION(qRegisterWidgetsVariant)
225
226 int qUnregisterWidgetsVariant()
227 {
228     qt_widgets_variant_handler = 0;
229     qMetaTypeWidgetsHelper = 0;
230     return 1;
231 }
232 Q_DESTRUCTOR_FUNCTION(qUnregisterWidgetsVariant)
233
234
235 QT_END_NAMESPACE