Qt 4.8
qscriptdebuggervalue.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSCriptTools 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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qscriptdebuggervalue_p.h"
43 
44 #include <QtScript/qscriptvalue.h>
45 #include <QtScript/qscriptengine.h>
46 #include <QtCore/qdatastream.h>
47 #include <QtCore/qdebug.h>
48 
50 
63 {
64 public:
67 
69  union {
72  double numberValue;
74  };
75 
77 };
78 
81 {
82  ref = 0;
83 }
84 
86 {
88  delete stringValue;
89 }
90 
92  : d_ptr(0)
93 {
94 }
95 
97  : d_ptr(0)
98 {
99  if (value.isValid()) {
101  if (value.isUndefined())
102  d_ptr->type = UndefinedValue;
103  else if (value.isNull())
104  d_ptr->type = NullValue;
105  else if (value.isNumber()) {
106  d_ptr->type = NumberValue;
107  d_ptr->numberValue = value.toNumber();
108  } else if (value.isBoolean()) {
109  d_ptr->type = BooleanValue;
110  d_ptr->booleanValue = value.toBoolean();
111  } else if (value.isString()) {
112  d_ptr->type = StringValue;
113  d_ptr->stringValue = new QString(value.toString());
114  } else {
115  Q_ASSERT(value.isObject());
116  d_ptr->type = ObjectValue;
117  d_ptr->objectId = value.objectId();
118  }
119  d_ptr->ref.ref();
120  }
121 }
122 
125 {
126  d_ptr->type = NumberValue;
127  d_ptr->numberValue = value;
128  d_ptr->ref.ref();
129 }
130 
133 {
134  d_ptr->type = BooleanValue;
135  d_ptr->booleanValue = value;
136  d_ptr->ref.ref();
137 }
138 
141 {
142  d_ptr->type = StringValue;
143  d_ptr->stringValue = new QString(value);
144  d_ptr->ref.ref();
145 }
146 
149 {
150  d_ptr->type = ObjectValue;
151  d_ptr->objectId = objectId;
152  d_ptr->ref.ref();
153 }
154 
157 {
158  d_ptr->type = type;
159  d_ptr->ref.ref();
160 }
161 
163  : d_ptr(other.d_ptr.data())
164 {
165  if (d_ptr)
166  d_ptr->ref.ref();
167 }
168 
170 {
171 }
172 
174 {
175  d_ptr.assign(other.d_ptr.data());
176  return *this;
177 }
178 
183 {
184  Q_D(const QScriptDebuggerValue);
185  if (!d)
186  return NoValue;
187  return d->type;
188 }
189 
194 {
195  Q_D(const QScriptDebuggerValue);
196  if (!d)
197  return 0;
198  Q_ASSERT(d->type == NumberValue);
199  return d->numberValue;
200 }
201 
206 {
207  Q_D(const QScriptDebuggerValue);
208  if (!d)
209  return false;
210  Q_ASSERT(d->type == BooleanValue);
211  return d->booleanValue;
212 }
213 
218 {
219  Q_D(const QScriptDebuggerValue);
220  if (!d)
221  return QString();
222  Q_ASSERT(d->type == StringValue);
223  return *d->stringValue;
224 }
225 
230 {
231  Q_D(const QScriptDebuggerValue);
232  if (!d)
233  return -1;
234  Q_ASSERT(d->type == ObjectValue);
235  return d->objectId;
236 }
237 
243 {
244  Q_D(const QScriptDebuggerValue);
245  if (!d)
246  return QScriptValue();
247  switch (d->type) {
248  case NoValue:
249  return QScriptValue();
250  case UndefinedValue:
251  return engine->undefinedValue();
252  case NullValue:
253  return engine->nullValue();
254  case BooleanValue:
255  return QScriptValue(engine, d->booleanValue);
256  case StringValue:
257  return QScriptValue(engine, *d->stringValue);
258  case NumberValue:
259  return QScriptValue(engine, d->numberValue);
260  case ObjectValue:
261  return engine->objectById(d->objectId);
262  }
263  return QScriptValue();
264 }
265 
270 {
271  Q_D(const QScriptDebuggerValue);
272  if (!d)
273  return QString();
274  switch (d->type) {
275  case NoValue:
276  return QString();
277  case UndefinedValue:
278  return QString::fromLatin1("undefined");
279  case NullValue:
280  return QString::fromLatin1("null");
281  case BooleanValue:
282  if (d->booleanValue)
283  return QString::fromLatin1("true");
284  else
285  return QString::fromLatin1("false");
286  case StringValue:
287  return *d->stringValue;
288  case NumberValue:
289  return QString::number(d->numberValue); // ### qScriptNumberToString()
290  case ObjectValue:
291  return QString::fromLatin1("[object Object]");
292  }
293  return QString();
294 }
295 
301 {
302  Q_D(const QScriptDebuggerValue);
303  const QScriptDebuggerValuePrivate *od = other.d_func();
304  if (d == od)
305  return true;
306  if (!d || !od)
307  return false;
308  if (d->type != od->type)
309  return false;
310  switch (d->type) {
311  case NoValue:
312  case UndefinedValue:
313  case NullValue:
314  return true;
315  case BooleanValue:
316  return d->booleanValue == od->booleanValue;
317  case StringValue:
318  return *d->stringValue == *od->stringValue;
319  case NumberValue:
320  return d->numberValue == od->numberValue;
321  case ObjectValue:
322  return d->objectId == od->objectId;
323  }
324  return false;
325 }
326 
332 {
333  return !(*this == other);
334 }
335 
346 {
347  out << (quint32)value.type();
348  switch (value.type()) {
352  break;
354  out << value.booleanValue();
355  break;
357  out << value.stringValue();
358  break;
360  out << value.numberValue();
361  break;
363  out << value.objectId();
364  break;
365  }
366  return out;
367 }
368 
380 {
381  quint32 type;
382  in >> type;
383  switch (QScriptDebuggerValue::ValueType(type)) {
387  break;
389  bool b;
390  in >> b;
391  value = QScriptDebuggerValue(b);
392  } break;
394  QString s;
395  in >> s;
396  value = QScriptDebuggerValue(s);
397  } break;
399  double d;
400  in >> d;
401  value = QScriptDebuggerValue(d);
402  } break;
404  qint64 id;
405  in >> id;
406  value = QScriptDebuggerValue(id);
407  } break;
409  default:
410  value = QScriptDebuggerValue();
411  break;
412  }
413  return in;
414 }
415 
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
double d
Definition: qnumeric_p.h:62
bool isUndefined() const
Returns true if this QScriptValue is of the primitive type Undefined; otherwise returns false...
QScriptDebuggerValue & operator=(const QScriptDebuggerValue &other)
bool isNull() const
Returns true if this QScriptValue is of the primitive type Null; otherwise returns false...
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QScopedSharedPointer< QScriptDebuggerValuePrivate > d_ptr
T * data() const
Returns the value of the pointer referenced by this object.
QScriptValue toScriptValue(QScriptEngine *engine) const
Converts this QScriptDebuggerValue to a QScriptValue in the given engine and returns the resulting va...
QString toString() const
Returns the string value of this QScriptValue, as defined in ECMA-262 section 9.8, "ToString".
QScriptValue objectById(qint64 id) const
Returns the object with the given id, or an invalid QScriptValue if there is no object with that id...
bool isBoolean() const
Use isBool() instead.
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
QDataStream & operator>>(QDataStream &stream, QScriptDebuggerValue &value)
Reads a QScriptDebuggerValue from the specified stream into the given value.
QDataStream & operator<<(QDataStream &stream, const QScriptDebuggerValue &value)
Writes the given value to the specified stream.
qint64 objectId() const
Returns the ID of this object, or -1 if this QScriptValue is not an object.
The QScriptEngine class provides an environment for evaluating Qt Script code.
QString stringValue() const
Returns this value as a string.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool operator==(const QScriptDebuggerValue &other) const
Returns true if this QScriptDebuggerValue is equal to the other value, otherwise returns false...
bool operator!=(const QScriptDebuggerValue &other) const
Returns true if this QScriptDebuggerValue is not equal to the other value, otherwise returns false...
bool isString() const
Returns true if this QScriptValue is of the primitive type String; otherwise returns false...
static const char * data(const QByteArray &arr)
QString toString() const
Returns a string representation of this value.
__int64 qint64
Definition: qglobal.h:942
bool toBoolean() const
Use toBool() instead.
void reset(T *other=0)
Deletes the existing object it is pointing to if any, and sets its pointer to other.
qsreal toNumber() const
Returns the number value of this QScriptValue, as defined in ECMA-262 section 9.3, "ToNumber".
ValueType type() const
Returns the type of this value.
The QScriptDebuggerValue class represents a script value.
bool isNumber() const
Returns true if this QScriptValue is of the primitive type Number; otherwise returns false...
double numberValue() const
Returns this value as a number.
bool booleanValue() const
Returns this value as a boolean.
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
#define NoValue
unsigned int quint32
Definition: qglobal.h:938
if(void) toggleToolbarShown
QScriptDebuggerValue::ValueType type
QScriptValue undefinedValue()
Returns a QScriptValue of the primitive type Undefined.
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
bool isValid() const
Returns true if this QScriptValue is valid; otherwise returns false.
QScriptValue nullValue()
Returns a QScriptValue of the primitive type Null.
bool isObject() const
Returns true if this QScriptValue is of the Object type; otherwise returns false. ...
qint64 objectId() const
Returns this value as an object ID.