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 QtQml 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 ****************************************************************************/
42 #ifndef QFLAGPOINTER_P_H
43 #define QFLAGPOINTER_P_H
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>
63 inline QFlagPointer();
64 inline QFlagPointer(T *);
65 inline QFlagPointer(const QFlagPointer<T> &o);
67 inline bool isNull() const;
69 inline bool flag() const;
70 inline void setFlag();
71 inline void clearFlag();
72 inline void setFlagValue(bool);
74 inline bool flag2() const;
75 inline void setFlag2();
76 inline void clearFlag2();
77 inline void setFlag2Value(bool);
79 inline QFlagPointer<T> &operator=(const QFlagPointer &o);
80 inline QFlagPointer<T> &operator=(T *);
82 inline T *operator->() const;
83 inline T *operator*() const;
85 inline T *data() const;
90 static const quintptr FlagBit = 0x1;
91 static const quintptr Flag2Bit = 0x2;
92 static const quintptr FlagsMask = FlagBit | Flag2Bit;
95 template<typename T, typename T2>
99 inline QBiPointer(T *);
100 inline QBiPointer(T2 *);
101 inline QBiPointer(const QBiPointer<T, T2> &o);
103 inline bool isNull() const;
104 inline bool isT1() const;
105 inline bool isT2() const;
107 inline bool flag() const;
108 inline void setFlag();
109 inline void clearFlag();
110 inline void setFlagValue(bool);
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 *);
116 inline T *asT1() const;
117 inline T2 *asT2() const;
122 static const quintptr FlagBit = 0x1;
123 static const quintptr Flag2Bit = 0x2;
124 static const quintptr FlagsMask = FlagBit | Flag2Bit;
128 QFlagPointer<T>::QFlagPointer()
134 QFlagPointer<T>::QFlagPointer(T *v)
135 : ptr_value(quintptr(v))
137 Q_ASSERT((ptr_value & FlagsMask) == 0);
141 QFlagPointer<T>::QFlagPointer(const QFlagPointer<T> &o)
142 : ptr_value(o.ptr_value)
147 bool QFlagPointer<T>::isNull() const
149 return 0 == (ptr_value & (~FlagsMask));
153 bool QFlagPointer<T>::flag() const
155 return ptr_value & FlagBit;
159 void QFlagPointer<T>::setFlag()
161 ptr_value |= FlagBit;
165 void QFlagPointer<T>::clearFlag()
167 ptr_value &= ~FlagBit;
171 void QFlagPointer<T>::setFlagValue(bool v)
178 bool QFlagPointer<T>::flag2() const
180 return ptr_value & Flag2Bit;
184 void QFlagPointer<T>::setFlag2()
186 ptr_value|= Flag2Bit;
190 void QFlagPointer<T>::clearFlag2()
192 ptr_value &= ~Flag2Bit;
196 void QFlagPointer<T>::setFlag2Value(bool v)
203 QFlagPointer<T> &QFlagPointer<T>::operator=(const QFlagPointer &o)
205 ptr_value = o.ptr_value;
210 QFlagPointer<T> &QFlagPointer<T>::operator=(T *o)
212 Q_ASSERT((quintptr(o) & FlagsMask) == 0);
214 ptr_value = quintptr(o) | (ptr_value & FlagsMask);
219 T *QFlagPointer<T>::operator->() const
221 return (T *)(ptr_value & ~FlagsMask);
225 T *QFlagPointer<T>::operator*() const
227 return (T *)(ptr_value & ~FlagsMask);
231 T *QFlagPointer<T>::data() const
233 return (T *)(ptr_value & ~FlagsMask);
236 template<typename T, typename T2>
237 QBiPointer<T, T2>::QBiPointer()
242 template<typename T, typename T2>
243 QBiPointer<T, T2>::QBiPointer(T *v)
244 : ptr_value(quintptr(v))
246 Q_ASSERT((quintptr(v) & FlagsMask) == 0);
249 template<typename T, typename T2>
250 QBiPointer<T, T2>::QBiPointer(T2 *v)
251 : ptr_value(quintptr(v) | Flag2Bit)
253 Q_ASSERT((quintptr(v) & FlagsMask) == 0);
256 template<typename T, typename T2>
257 QBiPointer<T, T2>::QBiPointer(const QBiPointer<T, T2> &o)
258 : ptr_value(o.ptr_value)
262 template<typename T, typename T2>
263 bool QBiPointer<T, T2>::isNull() const
265 return 0 == (ptr_value & (~FlagsMask));
268 template<typename T, typename T2>
269 bool QBiPointer<T, T2>::isT1() const
271 return !(ptr_value & Flag2Bit);
274 template<typename T, typename T2>
275 bool QBiPointer<T, T2>::isT2() const
277 return ptr_value & Flag2Bit;
280 template<typename T, typename T2>
281 bool QBiPointer<T, T2>::flag() const
283 return ptr_value & FlagBit;
286 template<typename T, typename T2>
287 void QBiPointer<T, T2>::setFlag()
289 ptr_value |= FlagBit;
292 template<typename T, typename T2>
293 void QBiPointer<T, T2>::clearFlag()
295 ptr_value &= ~FlagBit;
298 template<typename T, typename T2>
299 void QBiPointer<T, T2>::setFlagValue(bool v)
305 template<typename T, typename T2>
306 QBiPointer<T, T2> &QBiPointer<T, T2>::operator=(const QBiPointer<T, T2> &o)
308 ptr_value = o.ptr_value;
312 template<typename T, typename T2>
313 QBiPointer<T, T2> &QBiPointer<T, T2>::operator=(T *o)
315 Q_ASSERT((quintptr(o) & FlagsMask) == 0);
317 ptr_value = quintptr(o) | (ptr_value & FlagBit);
321 template<typename T, typename T2>
322 QBiPointer<T, T2> &QBiPointer<T, T2>::operator=(T2 *o)
324 Q_ASSERT((quintptr(o) & FlagsMask) == 0);
326 ptr_value = quintptr(o) | (ptr_value & FlagBit) | Flag2Bit;
330 template<typename T, typename T2>
331 T *QBiPointer<T, T2>::asT1() const
334 return (T *)(ptr_value & ~FlagsMask);
337 template<typename T, typename T2>
338 T2 *QBiPointer<T, T2>::asT2() const
341 return (T2 *)(ptr_value & ~FlagsMask);
346 #endif // QFLAGPOINTER_P_H