Fixes a crash in QDoubleSpinBox
[qt-netbsd.git] / src / scripttools / debugging / qscriptdebuggervalue.cpp
blobad79a7cd9ae8ecf25ec65619e1045efbcd6895b6
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtSCriptTools module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial Usage
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** GNU General Public License Usage
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file. Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
36 ** If you have questions regarding the use of this file, please contact
37 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #include "qscriptdebuggervalue_p.h"
44 #include <QtScript/qscriptvalue.h>
45 #include <QtScript/qscriptengine.h>
46 #include <QtCore/qdatastream.h>
47 #include <QtCore/qdebug.h>
49 QT_BEGIN_NAMESPACE
51 /*!
52 \since 4.5
53 \class QScriptDebuggerValue
54 \internal
56 \brief The QScriptDebuggerValue class represents a script value.
59 class QScriptDebuggerValuePrivate
61 public:
62 QScriptDebuggerValuePrivate();
63 ~QScriptDebuggerValuePrivate();
65 QScriptDebuggerValue::ValueType type;
66 union {
67 bool booleanValue;
68 QString *stringValue;
69 double numberValue;
70 qint64 objectId;
73 QBasicAtomicInt ref;
76 QScriptDebuggerValuePrivate::QScriptDebuggerValuePrivate()
77 : type(QScriptDebuggerValue::NoValue)
79 ref = 0;
82 QScriptDebuggerValuePrivate::~QScriptDebuggerValuePrivate()
84 if (type == QScriptDebuggerValue::StringValue)
85 delete stringValue;
88 QScriptDebuggerValue::QScriptDebuggerValue()
89 : d_ptr(0)
93 QScriptDebuggerValue::QScriptDebuggerValue(const QScriptValue &value)
94 : d_ptr(0)
96 if (value.isValid()) {
97 d_ptr = new QScriptDebuggerValuePrivate;
98 if (value.isUndefined())
99 d_ptr->type = UndefinedValue;
100 else if (value.isNull())
101 d_ptr->type = NullValue;
102 else if (value.isNumber()) {
103 d_ptr->type = NumberValue;
104 d_ptr->numberValue = value.toNumber();
105 } else if (value.isBoolean()) {
106 d_ptr->type = BooleanValue;
107 d_ptr->booleanValue = value.toBoolean();
108 } else if (value.isString()) {
109 d_ptr->type = StringValue;
110 d_ptr->stringValue = new QString(value.toString());
111 } else {
112 Q_ASSERT(value.isObject());
113 d_ptr->type = ObjectValue;
114 d_ptr->objectId = value.objectId();
116 d_ptr->ref.ref();
120 QScriptDebuggerValue::QScriptDebuggerValue(double value)
121 : d_ptr(new QScriptDebuggerValuePrivate)
123 d_ptr->type = NumberValue;
124 d_ptr->numberValue = value;
125 d_ptr->ref.ref();
128 QScriptDebuggerValue::QScriptDebuggerValue(bool value)
129 : d_ptr(new QScriptDebuggerValuePrivate)
131 d_ptr->type = BooleanValue;
132 d_ptr->booleanValue = value;
133 d_ptr->ref.ref();
136 QScriptDebuggerValue::QScriptDebuggerValue(const QString &value)
137 : d_ptr(new QScriptDebuggerValuePrivate)
139 d_ptr->type = StringValue;
140 d_ptr->stringValue = new QString(value);
141 d_ptr->ref.ref();
144 QScriptDebuggerValue::QScriptDebuggerValue(qint64 objectId)
145 : d_ptr(new QScriptDebuggerValuePrivate)
147 d_ptr->type = ObjectValue;
148 d_ptr->objectId = objectId;
149 d_ptr->ref.ref();
152 QScriptDebuggerValue::QScriptDebuggerValue(ValueType type)
153 : d_ptr(new QScriptDebuggerValuePrivate)
155 d_ptr->type = type;
156 d_ptr->ref.ref();
159 QScriptDebuggerValue::QScriptDebuggerValue(const QScriptDebuggerValue &other)
160 : d_ptr(other.d_ptr)
162 if (d_ptr)
163 d_ptr->ref.ref();
166 QScriptDebuggerValue::~QScriptDebuggerValue()
168 if (d_ptr && !d_ptr->ref.deref()) {
169 delete d_ptr;
170 d_ptr = 0;
174 QScriptDebuggerValue &QScriptDebuggerValue::operator=(const QScriptDebuggerValue &other)
176 if (d_ptr == other.d_ptr)
177 return *this;
178 if (d_ptr && !d_ptr->ref.deref())
179 delete d_ptr;
180 d_ptr = other.d_ptr;
181 if (d_ptr)
182 d_ptr->ref.ref();
183 return *this;
187 Returns the type of this value.
189 QScriptDebuggerValue::ValueType QScriptDebuggerValue::type() const
191 Q_D(const QScriptDebuggerValue);
192 if (!d)
193 return NoValue;
194 return d->type;
198 Returns this value as a number.
200 double QScriptDebuggerValue::numberValue() const
202 Q_D(const QScriptDebuggerValue);
203 if (!d)
204 return 0;
205 Q_ASSERT(d->type == NumberValue);
206 return d->numberValue;
210 Returns this value as a boolean.
212 bool QScriptDebuggerValue::booleanValue() const
214 Q_D(const QScriptDebuggerValue);
215 if (!d)
216 return false;
217 Q_ASSERT(d->type == BooleanValue);
218 return d->booleanValue;
222 Returns this value as a string.
224 QString QScriptDebuggerValue::stringValue() const
226 Q_D(const QScriptDebuggerValue);
227 if (!d)
228 return QString();
229 Q_ASSERT(d->type == StringValue);
230 return *d->stringValue;
234 Returns this value as an object ID.
236 qint64 QScriptDebuggerValue::objectId() const
238 Q_D(const QScriptDebuggerValue);
239 if (!d)
240 return -1;
241 Q_ASSERT(d->type == ObjectValue);
242 return d->objectId;
246 Converts this QScriptDebuggerValue to a QScriptValue in the
247 given \a engine and returns the resulting value.
249 QScriptValue QScriptDebuggerValue::toScriptValue(QScriptEngine *engine) const
251 Q_D(const QScriptDebuggerValue);
252 if (!d)
253 return QScriptValue();
254 switch (d->type) {
255 case NoValue:
256 return QScriptValue();
257 case UndefinedValue:
258 return engine->undefinedValue();
259 case NullValue:
260 return engine->nullValue();
261 case BooleanValue:
262 return QScriptValue(engine, d->booleanValue);
263 case StringValue:
264 return QScriptValue(engine, *d->stringValue);
265 case NumberValue:
266 return QScriptValue(engine, d->numberValue);
267 case ObjectValue:
268 return engine->objectById(d->objectId);
270 return QScriptValue();
274 Returns a string representation of this value.
276 QString QScriptDebuggerValue::toString() const
278 Q_D(const QScriptDebuggerValue);
279 if (!d)
280 return QString();
281 switch (d->type) {
282 case NoValue:
283 return QString();
284 case UndefinedValue:
285 return QString::fromLatin1("undefined");
286 case NullValue:
287 return QString::fromLatin1("null");
288 case BooleanValue:
289 if (d->booleanValue)
290 return QString::fromLatin1("true");
291 else
292 return QString::fromLatin1("false");
293 case StringValue:
294 return *d->stringValue;
295 case NumberValue:
296 return QString::number(d->numberValue); // ### qScriptNumberToString()
297 case ObjectValue:
298 return QString::fromLatin1("[object Object]");
300 return QString();
304 Returns true if this QScriptDebuggerValue is equal to the \a other
305 value, otherwise returns false.
307 bool QScriptDebuggerValue::operator==(const QScriptDebuggerValue &other) const
309 Q_D(const QScriptDebuggerValue);
310 const QScriptDebuggerValuePrivate *od = other.d_func();
311 if (d == od)
312 return true;
313 if (!d || !od)
314 return false;
315 if (d->type != od->type)
316 return false;
317 switch (d->type) {
318 case NoValue:
319 case UndefinedValue:
320 case NullValue:
321 return true;
322 case BooleanValue:
323 return d->booleanValue == od->booleanValue;
324 case StringValue:
325 return *d->stringValue == *od->stringValue;
326 case NumberValue:
327 return d->numberValue == od->numberValue;
328 case ObjectValue:
329 return d->objectId == od->objectId;
331 return false;
335 Returns true if this QScriptDebuggerValue is not equal to the \a
336 other value, otherwise returns false.
338 bool QScriptDebuggerValue::operator!=(const QScriptDebuggerValue &other) const
340 return !(*this == other);
344 \fn QDataStream &operator<<(QDataStream &stream, const QScriptDebuggerValue &value)
345 \relates QScriptDebuggerValue
347 Writes the given \a value to the specified \a stream.
349 QDataStream &operator<<(QDataStream &out, const QScriptDebuggerValue &value)
351 out << (quint32)value.type();
352 switch (value.type()) {
353 case QScriptDebuggerValue::NoValue:
354 case QScriptDebuggerValue::UndefinedValue:
355 case QScriptDebuggerValue::NullValue:
356 break;
357 case QScriptDebuggerValue::BooleanValue:
358 out << value.booleanValue();
359 break;
360 case QScriptDebuggerValue::StringValue:
361 out << value.stringValue();
362 break;
363 case QScriptDebuggerValue::NumberValue:
364 out << value.numberValue();
365 break;
366 case QScriptDebuggerValue::ObjectValue:
367 out << value.objectId();
368 break;
370 return out;
374 \fn QDataStream &operator>>(QDataStream &stream, QScriptDebuggerValue &value)
375 \relates QScriptDebuggerValue
377 Reads a QScriptDebuggerValue from the specified \a stream into the
378 given \a value.
380 QDataStream &operator>>(QDataStream &in, QScriptDebuggerValue &value)
382 quint32 type;
383 in >> type;
384 switch (QScriptDebuggerValue::ValueType(type)) {
385 case QScriptDebuggerValue::UndefinedValue:
386 case QScriptDebuggerValue::NullValue:
387 value = QScriptDebuggerValue(QScriptDebuggerValue::ValueType(type));
388 break;
389 case QScriptDebuggerValue::BooleanValue: {
390 bool b;
391 in >> b;
392 value = QScriptDebuggerValue(b);
393 } break;
394 case QScriptDebuggerValue::StringValue: {
395 QString s;
396 in >> s;
397 value = QScriptDebuggerValue(s);
398 } break;
399 case QScriptDebuggerValue::NumberValue: {
400 double d;
401 in >> d;
402 value = QScriptDebuggerValue(d);
403 } break;
404 case QScriptDebuggerValue::ObjectValue: {
405 qint64 id;
406 in >> id;
407 value = QScriptDebuggerValue(id);
408 } break;
409 case QScriptDebuggerValue::NoValue:
410 default:
411 value = QScriptDebuggerValue();
412 break;
414 return in;
417 QT_END_NAMESPACE