6200b058789ab4a36d2b7c2cd17811d4409494f1
[profile/ivi/qtdeclarative.git] / src / qml / qml / ftw / qflagpointer_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 QtQml 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 QFLAGPOINTER_P_H
43 #define QFLAGPOINTER_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
58 QT_BEGIN_NAMESPACE
59
60 template<typename T>
61 class QFlagPointer {
62 public:
63     inline QFlagPointer();
64     inline QFlagPointer(T *);
65     inline QFlagPointer(const QFlagPointer<T> &o);
66
67     inline bool isNull() const;
68
69     inline bool flag() const;
70     inline void setFlag();
71     inline void clearFlag();
72     inline void setFlagValue(bool);
73
74     inline bool flag2() const;
75     inline void setFlag2();
76     inline void clearFlag2();
77     inline void setFlag2Value(bool);
78
79     inline QFlagPointer<T> &operator=(const QFlagPointer &o);
80     inline QFlagPointer<T> &operator=(T *);
81
82     inline T *operator->() const;
83     inline T *operator*() const;
84
85     inline T *data() const;
86
87 private:
88     quintptr ptr_value;
89
90     static const quintptr FlagBit = 0x1;
91     static const quintptr Flag2Bit = 0x2;
92     static const quintptr FlagsMask = FlagBit | Flag2Bit;
93 };
94
95 template<typename T, typename T2>
96 class QBiPointer {
97 public:
98     inline QBiPointer();
99     inline QBiPointer(T *);
100     inline QBiPointer(T2 *);
101     inline QBiPointer(const QBiPointer<T, T2> &o);
102
103     inline bool isNull() const;
104     inline bool isT1() const;
105     inline bool isT2() const;
106
107     inline bool flag() const;
108     inline void setFlag();
109     inline void clearFlag();
110     inline void setFlagValue(bool);
111
112     inline QBiPointer<T, T2> &operator=(const QBiPointer<T, T2> &o);
113     inline QBiPointer<T, T2> &operator=(T *);
114     inline QBiPointer<T, T2> &operator=(T2 *);
115
116     inline T *asT1() const;
117     inline T2 *asT2() const;
118
119 private:
120     quintptr ptr_value;
121
122     static const quintptr FlagBit = 0x1;
123     static const quintptr Flag2Bit = 0x2;
124     static const quintptr FlagsMask = FlagBit | Flag2Bit;
125 };
126
127 template<typename T>
128 QFlagPointer<T>::QFlagPointer()
129 : ptr_value(0)
130 {
131 }
132
133 template<typename T>
134 QFlagPointer<T>::QFlagPointer(T *v)
135 : ptr_value(quintptr(v))
136 {
137     Q_ASSERT((ptr_value & FlagsMask) == 0);
138 }
139
140 template<typename T>
141 QFlagPointer<T>::QFlagPointer(const QFlagPointer<T> &o)
142 : ptr_value(o.ptr_value)
143 {
144 }
145
146 template<typename T>
147 bool QFlagPointer<T>::isNull() const
148 {
149     return 0 == (ptr_value & (~FlagsMask));
150 }
151
152 template<typename T>
153 bool QFlagPointer<T>::flag() const
154 {
155     return ptr_value & FlagBit;
156 }
157
158 template<typename T>
159 void QFlagPointer<T>::setFlag()
160 {
161     ptr_value |= FlagBit;
162 }
163
164 template<typename T>
165 void QFlagPointer<T>::clearFlag()
166 {
167     ptr_value &= ~FlagBit;
168 }
169
170 template<typename T>
171 void QFlagPointer<T>::setFlagValue(bool v)
172 {
173     if (v) setFlag();
174     else clearFlag();
175 }
176
177 template<typename T>
178 bool QFlagPointer<T>::flag2() const
179 {
180     return ptr_value & Flag2Bit;
181 }
182
183 template<typename T>
184 void QFlagPointer<T>::setFlag2()
185 {
186     ptr_value|= Flag2Bit;
187 }
188
189 template<typename T>
190 void QFlagPointer<T>::clearFlag2()
191 {
192     ptr_value &= ~Flag2Bit;
193 }
194
195 template<typename T>
196 void QFlagPointer<T>::setFlag2Value(bool v)
197 {
198     if (v) setFlag2();
199     else clearFlag2();
200 }
201
202 template<typename T>
203 QFlagPointer<T> &QFlagPointer<T>::operator=(const QFlagPointer &o)
204 {
205     ptr_value = o.ptr_value;
206     return *this;
207 }
208
209 template<typename T>
210 QFlagPointer<T> &QFlagPointer<T>::operator=(T *o)
211 {
212     Q_ASSERT((quintptr(o) & FlagsMask) == 0);
213
214     ptr_value = quintptr(o) | (ptr_value & FlagsMask);
215     return *this;
216 }
217
218 template<typename T>
219 T *QFlagPointer<T>::operator->() const
220 {
221     return (T *)(ptr_value & ~FlagsMask);
222 }
223
224 template<typename T>
225 T *QFlagPointer<T>::operator*() const
226 {
227     return (T *)(ptr_value & ~FlagsMask);
228 }
229
230 template<typename T>
231 T *QFlagPointer<T>::data() const
232 {
233     return (T *)(ptr_value & ~FlagsMask);
234 }
235
236 template<typename T, typename T2>
237 QBiPointer<T, T2>::QBiPointer()
238 : ptr_value(0)
239 {
240 }
241
242 template<typename T, typename T2>
243 QBiPointer<T, T2>::QBiPointer(T *v)
244 : ptr_value(quintptr(v))
245 {
246     Q_ASSERT((quintptr(v) & FlagsMask) == 0);
247 }
248
249 template<typename T, typename T2>
250 QBiPointer<T, T2>::QBiPointer(T2 *v)
251 : ptr_value(quintptr(v) | Flag2Bit)
252 {
253     Q_ASSERT((quintptr(v) & FlagsMask) == 0);
254 }
255
256 template<typename T, typename T2>
257 QBiPointer<T, T2>::QBiPointer(const QBiPointer<T, T2> &o)
258 : ptr_value(o.ptr_value)
259 {
260 }
261
262 template<typename T, typename T2>
263 bool QBiPointer<T, T2>::isNull() const
264 {
265     return 0 == (ptr_value & (~FlagsMask));
266 }
267
268 template<typename T, typename T2>
269 bool QBiPointer<T, T2>::isT1() const
270 {
271     return !(ptr_value & Flag2Bit);
272 }
273
274 template<typename T, typename T2>
275 bool QBiPointer<T, T2>::isT2() const
276 {
277     return ptr_value & Flag2Bit;
278 }
279
280 template<typename T, typename T2>
281 bool QBiPointer<T, T2>::flag() const
282 {
283     return ptr_value & FlagBit;
284 }
285
286 template<typename T, typename T2>
287 void QBiPointer<T, T2>::setFlag()
288 {
289     ptr_value |= FlagBit;
290 }
291
292 template<typename T, typename T2>
293 void QBiPointer<T, T2>::clearFlag()
294 {
295     ptr_value &= ~FlagBit;
296 }
297
298 template<typename T, typename T2>
299 void QBiPointer<T, T2>::setFlagValue(bool v)
300 {
301     if (v) setFlag();
302     else clearFlag();
303 }
304
305 template<typename T, typename T2>
306 QBiPointer<T, T2> &QBiPointer<T, T2>::operator=(const QBiPointer<T, T2> &o)
307 {
308     ptr_value = o.ptr_value;
309     return *this;
310 }
311
312 template<typename T, typename T2>
313 QBiPointer<T, T2> &QBiPointer<T, T2>::operator=(T *o)
314 {
315     Q_ASSERT((quintptr(o) & FlagsMask) == 0);
316
317     ptr_value = quintptr(o) | (ptr_value & FlagBit);
318     return *this;
319 }
320
321 template<typename T, typename T2>
322 QBiPointer<T, T2> &QBiPointer<T, T2>::operator=(T2 *o)
323 {
324     Q_ASSERT((quintptr(o) & FlagsMask) == 0);
325
326     ptr_value = quintptr(o) | (ptr_value & FlagBit) | Flag2Bit;
327     return *this;
328 }
329
330 template<typename T, typename T2>
331 T *QBiPointer<T, T2>::asT1() const
332 {
333     Q_ASSERT(isT1());
334     return (T *)(ptr_value & ~FlagsMask);
335 }
336
337 template<typename T, typename T2>
338 T2 *QBiPointer<T, T2>::asT2() const
339 {
340     Q_ASSERT(isT2());
341     return (T2 *)(ptr_value & ~FlagsMask);
342 }
343
344 QT_END_NAMESPACE
345
346 #endif // QFLAGPOINTER_P_H