1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtSCriptTools module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
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.
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>
53 \class QScriptDebuggerValue
56 \brief The QScriptDebuggerValue class represents a script value.
59 class QScriptDebuggerValuePrivate
62 QScriptDebuggerValuePrivate();
63 ~QScriptDebuggerValuePrivate();
65 QScriptDebuggerValue::ValueType type
;
76 QScriptDebuggerValuePrivate::QScriptDebuggerValuePrivate()
77 : type(QScriptDebuggerValue::NoValue
)
82 QScriptDebuggerValuePrivate::~QScriptDebuggerValuePrivate()
84 if (type
== QScriptDebuggerValue::StringValue
)
88 QScriptDebuggerValue::QScriptDebuggerValue()
93 QScriptDebuggerValue::QScriptDebuggerValue(const QScriptValue
&value
)
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());
112 Q_ASSERT(value
.isObject());
113 d_ptr
->type
= ObjectValue
;
114 d_ptr
->objectId
= value
.objectId();
120 QScriptDebuggerValue::QScriptDebuggerValue(double value
)
121 : d_ptr(new QScriptDebuggerValuePrivate
)
123 d_ptr
->type
= NumberValue
;
124 d_ptr
->numberValue
= value
;
128 QScriptDebuggerValue::QScriptDebuggerValue(bool value
)
129 : d_ptr(new QScriptDebuggerValuePrivate
)
131 d_ptr
->type
= BooleanValue
;
132 d_ptr
->booleanValue
= value
;
136 QScriptDebuggerValue::QScriptDebuggerValue(const QString
&value
)
137 : d_ptr(new QScriptDebuggerValuePrivate
)
139 d_ptr
->type
= StringValue
;
140 d_ptr
->stringValue
= new QString(value
);
144 QScriptDebuggerValue::QScriptDebuggerValue(qint64 objectId
)
145 : d_ptr(new QScriptDebuggerValuePrivate
)
147 d_ptr
->type
= ObjectValue
;
148 d_ptr
->objectId
= objectId
;
152 QScriptDebuggerValue::QScriptDebuggerValue(ValueType type
)
153 : d_ptr(new QScriptDebuggerValuePrivate
)
159 QScriptDebuggerValue::QScriptDebuggerValue(const QScriptDebuggerValue
&other
)
166 QScriptDebuggerValue::~QScriptDebuggerValue()
168 if (d_ptr
&& !d_ptr
->ref
.deref()) {
174 QScriptDebuggerValue
&QScriptDebuggerValue::operator=(const QScriptDebuggerValue
&other
)
176 if (d_ptr
== other
.d_ptr
)
178 if (d_ptr
&& !d_ptr
->ref
.deref())
187 Returns the type of this value.
189 QScriptDebuggerValue::ValueType
QScriptDebuggerValue::type() const
191 Q_D(const QScriptDebuggerValue
);
198 Returns this value as a number.
200 double QScriptDebuggerValue::numberValue() const
202 Q_D(const QScriptDebuggerValue
);
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
);
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
);
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
);
241 Q_ASSERT(d
->type
== ObjectValue
);
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
);
253 return QScriptValue();
256 return QScriptValue();
258 return engine
->undefinedValue();
260 return engine
->nullValue();
262 return QScriptValue(engine
, d
->booleanValue
);
264 return QScriptValue(engine
, *d
->stringValue
);
266 return QScriptValue(engine
, d
->numberValue
);
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
);
285 return QString::fromLatin1("undefined");
287 return QString::fromLatin1("null");
290 return QString::fromLatin1("true");
292 return QString::fromLatin1("false");
294 return *d
->stringValue
;
296 return QString::number(d
->numberValue
); // ### qScriptNumberToString()
298 return QString::fromLatin1("[object Object]");
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();
315 if (d
->type
!= od
->type
)
323 return d
->booleanValue
== od
->booleanValue
;
325 return *d
->stringValue
== *od
->stringValue
;
327 return d
->numberValue
== od
->numberValue
;
329 return d
->objectId
== od
->objectId
;
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
:
357 case QScriptDebuggerValue::BooleanValue
:
358 out
<< value
.booleanValue();
360 case QScriptDebuggerValue::StringValue
:
361 out
<< value
.stringValue();
363 case QScriptDebuggerValue::NumberValue
:
364 out
<< value
.numberValue();
366 case QScriptDebuggerValue::ObjectValue
:
367 out
<< value
.objectId();
374 \fn QDataStream &operator>>(QDataStream &stream, QScriptDebuggerValue &value)
375 \relates QScriptDebuggerValue
377 Reads a QScriptDebuggerValue from the specified \a stream into the
380 QDataStream
&operator>>(QDataStream
&in
, QScriptDebuggerValue
&value
)
384 switch (QScriptDebuggerValue::ValueType(type
)) {
385 case QScriptDebuggerValue::UndefinedValue
:
386 case QScriptDebuggerValue::NullValue
:
387 value
= QScriptDebuggerValue(QScriptDebuggerValue::ValueType(type
));
389 case QScriptDebuggerValue::BooleanValue
: {
392 value
= QScriptDebuggerValue(b
);
394 case QScriptDebuggerValue::StringValue
: {
397 value
= QScriptDebuggerValue(s
);
399 case QScriptDebuggerValue::NumberValue
: {
402 value
= QScriptDebuggerValue(d
);
404 case QScriptDebuggerValue::ObjectValue
: {
407 value
= QScriptDebuggerValue(id
);
409 case QScriptDebuggerValue::NoValue
:
411 value
= QScriptDebuggerValue();