7b0caf49bcf1ee5dccac4156c80a7050d344d274
[profile/ivi/qtdeclarative.git] / src / qml / qml / ftw / qpointervaluepair_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 QPOINTERVALUEPAIR_P_H
43 #define QPOINTERVALUEPAIR_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 #include <private/qflagpointer_p.h>
58
59 QT_BEGIN_NAMESPACE
60
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 {
71 public:
72     inline QPointerValuePair();
73     inline QPointerValuePair(P *);
74     inline ~QPointerValuePair();
75
76     inline bool isNull() const;
77
78     inline bool flag() const;
79     inline void setFlag();
80     inline void clearFlag();
81     inline void setFlagValue(bool);
82
83     inline QPointerValuePair<P, V> &operator=(P *);
84
85     inline P *operator->() const;
86     inline P *operator*() const;
87
88     inline bool hasValue() const;
89     inline V &value();
90     inline const V *constValue() const;
91
92 private:
93     struct Value { P *pointer; V value; };
94     QBiPointer<P, Value> d;
95 };
96
97 template<typename P, typename V>
98 QPointerValuePair<P, V>::QPointerValuePair()
99 {
100 }
101
102 template<typename P, typename V>
103 QPointerValuePair<P, V>::QPointerValuePair(P *p)
104 : d(p)
105 {
106 }
107
108 template<typename P, typename V>
109 QPointerValuePair<P, V>::~QPointerValuePair()
110 {
111     if (d.isT2()) delete d.asT2();
112 }
113
114 template<typename P, typename V>
115 bool QPointerValuePair<P, V>::isNull() const
116 {
117     if (d.isT1()) return 0 == d.asT1();
118     else return d.asT2()->pointer == 0;
119 }
120
121 template<typename P, typename V>
122 bool QPointerValuePair<P, V>::flag() const
123 {
124     return d.flag();
125 }
126
127 template<typename P, typename V>
128 void QPointerValuePair<P, V>::setFlag()
129 {
130     d.setFlag();
131 }
132
133 template<typename P, typename V>
134 void QPointerValuePair<P, V>::clearFlag()
135 {
136     d.clearFlag();
137 }
138
139 template<typename P, typename V>
140 void QPointerValuePair<P, V>::setFlagValue(bool v)
141 {
142     d.setFlagValue(v);
143 }
144
145 template<typename P, typename V>
146 QPointerValuePair<P, V> &QPointerValuePair<P, V>::operator=(P *o)
147 {
148     if (d.isT1()) d = o;
149     else d.asT2()->pointer = o;
150     return *this;
151 }
152
153 template<typename P, typename V>
154 P *QPointerValuePair<P, V>::operator->() const
155 {
156     if (d.isT1()) return d.asT1();
157     else return d.asT2()->pointer;
158 }
159
160 template<typename P, typename V>
161 P *QPointerValuePair<P, V>::operator*() const
162 {
163     if (d.isT1()) return d.asT1();
164     else return d.asT2()->pointer;
165 }
166
167 template<typename P, typename V>
168 bool QPointerValuePair<P, V>::hasValue() const
169 {
170     return d.isT2();
171 }
172
173 template<typename P, typename V>
174 V &QPointerValuePair<P, V>::value()
175 {
176     if (d.isT1()) {
177         P *p = d.asT1();
178         Value *value = new Value;
179         value->pointer = p;
180         d = value;
181     }
182
183     return d.asT2()->value;
184 }
185
186 // Will return null if hasValue() == false
187 template<typename P, typename V>
188 const V *QPointerValuePair<P, V>::constValue() const
189 {
190     if (d.isT2()) return &d.asT2()->value;
191     else return 0;
192 }
193
194 QT_END_NAMESPACE
195
196 #endif // QPOINTERVALUEPAIR_P_H