1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 BlackBerry Limited. All rights reserved.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qppsattribute_p.h"
41 #include "qppsattributeprivate_p.h"
42
43 #include <QDebug>
44 #include <QVariant>
45
46 QT_BEGIN_NAMESPACE
47
48 ///////////////////////////
49 //
50 // QPpsAttributePrivate
51 //
52 ///////////////////////////
53
QPpsAttributePrivate()54 QPpsAttributePrivate::QPpsAttributePrivate() : type(QPpsAttribute::None)
55 {
56 }
57
createPpsAttribute(int value,QPpsAttribute::Flags flags)58 QPpsAttribute QPpsAttributePrivate::createPpsAttribute(int value, QPpsAttribute::Flags flags)
59 {
60 QPpsAttribute attribute;
61 attribute.d->type = QPpsAttribute::Number;
62 attribute.d->data = value;
63 attribute.d->flags = flags;
64 return attribute;
65 }
66
createPpsAttribute(long long value,QPpsAttribute::Flags flags)67 QPpsAttribute QPpsAttributePrivate::createPpsAttribute(long long value, QPpsAttribute::Flags flags)
68 {
69 QPpsAttribute attribute;
70 attribute.d->type = QPpsAttribute::Number;
71 attribute.d->data = value;
72 attribute.d->flags = flags;
73 return attribute;
74 }
75
createPpsAttribute(double value,QPpsAttribute::Flags flags)76 QPpsAttribute QPpsAttributePrivate::createPpsAttribute(double value, QPpsAttribute::Flags flags)
77 {
78 QPpsAttribute attribute;
79 attribute.d->type = QPpsAttribute::Number;
80 attribute.d->data = value;
81 attribute.d->flags = flags;
82 return attribute;
83 }
84
createPpsAttribute(bool value,QPpsAttribute::Flags flags)85 QPpsAttribute QPpsAttributePrivate::createPpsAttribute(bool value, QPpsAttribute::Flags flags)
86 {
87 QPpsAttribute attribute;
88 attribute.d->type = QPpsAttribute::Bool;
89 attribute.d->data = value;
90 attribute.d->flags = flags;
91 return attribute;
92 }
93
createPpsAttribute(const QString & value,QPpsAttribute::Flags flags)94 QPpsAttribute QPpsAttributePrivate::createPpsAttribute(const QString &value,
95 QPpsAttribute::Flags flags)
96 {
97 QPpsAttribute attribute;
98 attribute.d->type = QPpsAttribute::String;
99 attribute.d->data = value;
100 attribute.d->flags = flags;
101 return attribute;
102 }
103
createPpsAttribute(const QPpsAttributeList & value,QPpsAttribute::Flags flags)104 QPpsAttribute QPpsAttributePrivate::createPpsAttribute(const QPpsAttributeList &value,
105 QPpsAttribute::Flags flags)
106 {
107 QPpsAttribute attribute;
108 attribute.d->type = QPpsAttribute::Array;
109 attribute.d->data = QVariant::fromValue(value);
110 attribute.d->flags = flags;
111 return attribute;
112 }
113
createPpsAttribute(const QPpsAttributeMap & value,QPpsAttribute::Flags flags)114 QPpsAttribute QPpsAttributePrivate::createPpsAttribute(const QPpsAttributeMap &value,
115 QPpsAttribute::Flags flags)
116 {
117 QPpsAttribute attribute;
118 attribute.d->type = QPpsAttribute::Object;
119 attribute.d->data = QVariant::fromValue(value);
120 attribute.d->flags = flags;
121 return attribute;
122 }
123
124 ///////////////////////////
125 //
126 // QPpsAttribute
127 //
128 ///////////////////////////
129
QPpsAttribute()130 QPpsAttribute::QPpsAttribute() : d(new QPpsAttributePrivate())
131 {
132 }
133
~QPpsAttribute()134 QPpsAttribute::~QPpsAttribute()
135 {
136 }
137
QPpsAttribute(const QPpsAttribute & other)138 QPpsAttribute::QPpsAttribute(const QPpsAttribute &other) : d(other.d)
139 {
140 }
141
operator =(const QPpsAttribute & other)142 QPpsAttribute &QPpsAttribute::operator=(const QPpsAttribute &other)
143 {
144 d = other.d;
145 return *this;
146 }
147
QPpsAttribute(QPpsAttribute && other)148 QPpsAttribute::QPpsAttribute(QPpsAttribute &&other) : d(other.d)
149 {
150 other.d->type = QPpsAttribute::None;
151 }
152
operator =(QPpsAttribute && other)153 QPpsAttribute &QPpsAttribute::operator=(QPpsAttribute &&other)
154 {
155 qSwap(d, other.d);
156 return *this;
157 }
158
operator ==(const QPpsAttribute & other) const159 bool QPpsAttribute::operator==(const QPpsAttribute &other) const
160 {
161 if (type() != other.type())
162 return false;
163 if (flags() != other.flags())
164 return false;
165
166 switch (type()) {
167 case QPpsAttribute::Number:
168 case QPpsAttribute::Bool:
169 case QPpsAttribute::String:
170 // QVariant can compare double, int, longlong, bool, and QString for us.
171 return d->data == other.d->data;
172 case QPpsAttribute::Array:
173 // QVariant can't compare custom types (like QPpsAttributeList), always returning false.
174 // So we pull the lists out manually and compare them.
175 return toList() == other.toList();
176 case QPpsAttribute::Object:
177 // QVariant can't compare custom types (like QPpsAttributeMap), always returning false.
178 // So we pull the maps out manually and compare them.
179 return toMap() == other.toMap();
180 case QPpsAttribute::None:
181 // Both are "None" type, so the actual content doesn't matter.
182 return true;
183 }
184 return d->data == other.d->data;
185 }
186
isValid() const187 bool QPpsAttribute::isValid() const
188 {
189 return d->type != QPpsAttribute::None;
190 }
191
type() const192 QPpsAttribute::Type QPpsAttribute::type() const
193 {
194 return d->type;
195 }
196
isNumber() const197 bool QPpsAttribute::isNumber() const
198 {
199 return type() == QPpsAttribute::Number;
200 }
201
isBool() const202 bool QPpsAttribute::isBool() const
203 {
204 return type() == QPpsAttribute::Bool;
205 }
206
isString() const207 bool QPpsAttribute::isString() const
208 {
209 return type() == QPpsAttribute::String;
210 }
211
isArray() const212 bool QPpsAttribute::isArray() const
213 {
214 return type() == QPpsAttribute::Array;
215 }
216
isObject() const217 bool QPpsAttribute::isObject() const
218 {
219 return type() == QPpsAttribute::Object;
220 }
221
toDouble() const222 double QPpsAttribute::toDouble() const
223 {
224 return d->data.toDouble();
225 }
226
toLongLong() const227 qlonglong QPpsAttribute::toLongLong() const
228 {
229 return d->data.toLongLong();
230 }
231
toInt() const232 int QPpsAttribute::toInt() const
233 {
234 return d->data.toInt();
235 }
236
toBool() const237 bool QPpsAttribute::toBool() const
238 {
239 return d->data.toBool();
240 }
241
toString() const242 QString QPpsAttribute::toString() const
243 {
244 return d->data.toString();
245 }
246
toList() const247 QPpsAttributeList QPpsAttribute::toList() const
248 {
249 return d->data.value< QPpsAttributeList >();
250 }
251
toMap() const252 QPpsAttributeMap QPpsAttribute::toMap() const
253 {
254 return d->data.value< QPpsAttributeMap >();
255 }
256
flags() const257 QPpsAttribute::Flags QPpsAttribute::flags() const
258 {
259 return d->flags;
260 }
261
toVariant() const262 QVariant QPpsAttribute::toVariant() const
263 {
264 return d->data;
265 }
266
operator <<(QDebug dbg,const QPpsAttribute & attribute)267 QDebug operator<<(QDebug dbg, const QPpsAttribute &attribute)
268 {
269 QDebugStateSaver saver(dbg);
270 dbg.nospace() << "QPpsAttribute(";
271
272 switch (attribute.type()) {
273 case QPpsAttribute::Number:
274 switch (attribute.toVariant().type()) {
275 case QVariant::Int:
276 dbg << "Number, " << attribute.flags() << ", " << attribute.toInt();
277 break;
278 case QVariant::LongLong:
279 dbg << "Number, " << attribute.flags() << ", " << attribute.toLongLong();
280 break;
281 default:
282 dbg << "Number, " << attribute.flags() << ", " << attribute.toDouble();
283 break;
284 }
285 break;
286 case QPpsAttribute::Bool:
287 dbg << "Bool, " << attribute.flags() << ", " << attribute.toBool();
288 break;
289 case QPpsAttribute::String:
290 dbg << "String, " << attribute.flags() << ", " << attribute.toString();
291 break;
292 case QPpsAttribute::Array:
293 dbg << "Array, " << attribute.flags() << ", " << attribute.toList();
294 break;
295 case QPpsAttribute::Object:
296 dbg << "Object, " << attribute.flags() << ", " << attribute.toMap();
297 break;
298 case QPpsAttribute::None:
299 dbg << "None";
300 break;
301 }
302
303 dbg << ')';
304
305 return dbg;
306 }
307
308 QT_END_NAMESPACE
309