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 QPOINTERVALUEPAIR_P_H
43 #define QPOINTERVALUEPAIR_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>
57 #include <private/qflagpointer_p.h>
61 // QPointerValuePair is intended to help reduce the memory consumption of a class.
62 // In the common case, QPointerValuePair behaves like a pointer. In this mode, it
63 // consumes the same memory as a regular pointer.
64 // Additionally, QPointerValuePair can store an arbitrary value type in *addition*
65 // to the pointer. In this case, it uses slightly more memory than the pointer and
66 // value type combined.
67 // Consequently, this class is most useful in cases where a pointer is always stored
68 // and a value type is rarely stored.
69 template<typename P, typename V>
70 class QPointerValuePair {
72 inline QPointerValuePair();
73 inline QPointerValuePair(P *);
74 inline ~QPointerValuePair();
76 inline bool isNull() const;
78 inline bool flag() const;
79 inline void setFlag();
80 inline void clearFlag();
81 inline void setFlagValue(bool);
83 inline QPointerValuePair<P, V> &operator=(P *);
85 inline P *operator->() const;
86 inline P *operator*() const;
88 inline bool hasValue() const;
90 inline const V *constValue() const;
93 struct Value { P *pointer; V value; };
94 QBiPointer<P, Value> d;
97 template<typename P, typename V>
98 QPointerValuePair<P, V>::QPointerValuePair()
102 template<typename P, typename V>
103 QPointerValuePair<P, V>::QPointerValuePair(P *p)
108 template<typename P, typename V>
109 QPointerValuePair<P, V>::~QPointerValuePair()
111 if (d.isT2()) delete d.asT2();
114 template<typename P, typename V>
115 bool QPointerValuePair<P, V>::isNull() const
117 if (d.isT1()) return 0 == d.asT1();
118 else return d.asT2()->pointer == 0;
121 template<typename P, typename V>
122 bool QPointerValuePair<P, V>::flag() const
127 template<typename P, typename V>
128 void QPointerValuePair<P, V>::setFlag()
133 template<typename P, typename V>
134 void QPointerValuePair<P, V>::clearFlag()
139 template<typename P, typename V>
140 void QPointerValuePair<P, V>::setFlagValue(bool v)
145 template<typename P, typename V>
146 QPointerValuePair<P, V> &QPointerValuePair<P, V>::operator=(P *o)
149 else d.asT2()->pointer = o;
153 template<typename P, typename V>
154 P *QPointerValuePair<P, V>::operator->() const
156 if (d.isT1()) return d.asT1();
157 else return d.asT2()->pointer;
160 template<typename P, typename V>
161 P *QPointerValuePair<P, V>::operator*() const
163 if (d.isT1()) return d.asT1();
164 else return d.asT2()->pointer;
167 template<typename P, typename V>
168 bool QPointerValuePair<P, V>::hasValue() const
173 template<typename P, typename V>
174 V &QPointerValuePair<P, V>::value()
178 Value *value = new Value;
183 return d.asT2()->value;
186 // Will return null if hasValue() == false
187 template<typename P, typename V>
188 const V *QPointerValuePair<P, V>::constValue() const
190 if (d.isT2()) return &d.asT2()->value;
196 #endif // QPOINTERVALUEPAIR_P_H