src/scripttools/debugging/qscriptdebuggervalue.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    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.
       
    23 **
       
    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.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    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 
       
    49 QT_BEGIN_NAMESPACE
       
    50 
       
    51 /*!
       
    52   \since 4.5
       
    53   \class QScriptDebuggerValue
       
    54   \internal
       
    55 
       
    56   \brief The QScriptDebuggerValue class represents a script value.
       
    57 */
       
    58 
       
    59 class QScriptDebuggerValuePrivate
       
    60 {
       
    61 public:
       
    62     QScriptDebuggerValuePrivate();
       
    63     ~QScriptDebuggerValuePrivate();
       
    64 
       
    65     QScriptDebuggerValue::ValueType type;
       
    66     union {
       
    67         bool booleanValue;
       
    68         QString *stringValue;
       
    69         double numberValue;
       
    70         qint64 objectId;
       
    71     };
       
    72 
       
    73     QBasicAtomicInt ref;
       
    74 };
       
    75 
       
    76 QScriptDebuggerValuePrivate::QScriptDebuggerValuePrivate()
       
    77     : type(QScriptDebuggerValue::NoValue)
       
    78 {
       
    79     ref = 0;
       
    80 }
       
    81 
       
    82 QScriptDebuggerValuePrivate::~QScriptDebuggerValuePrivate()
       
    83 {
       
    84     if (type == QScriptDebuggerValue::StringValue)
       
    85         delete stringValue;
       
    86 }
       
    87 
       
    88 QScriptDebuggerValue::QScriptDebuggerValue()
       
    89     : d_ptr(0)
       
    90 {
       
    91 }
       
    92 
       
    93 QScriptDebuggerValue::QScriptDebuggerValue(const QScriptValue &value)
       
    94     : d_ptr(0)
       
    95 {
       
    96     if (value.isValid()) {
       
    97         d_ptr.reset(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();
       
   115         }
       
   116         d_ptr->ref.ref();
       
   117     }
       
   118 }
       
   119 
       
   120 QScriptDebuggerValue::QScriptDebuggerValue(double value)
       
   121     : d_ptr(new QScriptDebuggerValuePrivate)
       
   122 {
       
   123     d_ptr->type = NumberValue;
       
   124     d_ptr->numberValue = value;
       
   125     d_ptr->ref.ref();
       
   126 }
       
   127 
       
   128 QScriptDebuggerValue::QScriptDebuggerValue(bool value)
       
   129     : d_ptr(new QScriptDebuggerValuePrivate)
       
   130 {
       
   131     d_ptr->type = BooleanValue;
       
   132     d_ptr->booleanValue = value;
       
   133     d_ptr->ref.ref();
       
   134 }
       
   135 
       
   136 QScriptDebuggerValue::QScriptDebuggerValue(const QString &value)
       
   137     : d_ptr(new QScriptDebuggerValuePrivate)
       
   138 {
       
   139     d_ptr->type = StringValue;
       
   140     d_ptr->stringValue = new QString(value);
       
   141     d_ptr->ref.ref();
       
   142 }
       
   143 
       
   144 QScriptDebuggerValue::QScriptDebuggerValue(qint64 objectId)
       
   145     : d_ptr(new QScriptDebuggerValuePrivate)
       
   146 {
       
   147     d_ptr->type = ObjectValue;
       
   148     d_ptr->objectId = objectId;
       
   149     d_ptr->ref.ref();
       
   150 }
       
   151 
       
   152 QScriptDebuggerValue::QScriptDebuggerValue(ValueType type)
       
   153     : d_ptr(new QScriptDebuggerValuePrivate)
       
   154 {
       
   155     d_ptr->type = type;
       
   156     d_ptr->ref.ref();
       
   157 }
       
   158 
       
   159 QScriptDebuggerValue::QScriptDebuggerValue(const QScriptDebuggerValue &other)
       
   160     : d_ptr(other.d_ptr.data())
       
   161 {
       
   162     if (d_ptr)
       
   163         d_ptr->ref.ref();
       
   164 }
       
   165 
       
   166 QScriptDebuggerValue::~QScriptDebuggerValue()
       
   167 {
       
   168 }
       
   169 
       
   170 QScriptDebuggerValue &QScriptDebuggerValue::operator=(const QScriptDebuggerValue &other)
       
   171 {
       
   172     d_ptr.assign(other.d_ptr.data());
       
   173     return *this;
       
   174 }
       
   175 
       
   176 /*!
       
   177   Returns the type of this value.
       
   178 */
       
   179 QScriptDebuggerValue::ValueType QScriptDebuggerValue::type() const
       
   180 {
       
   181     Q_D(const QScriptDebuggerValue);
       
   182     if (!d)
       
   183         return NoValue;
       
   184     return d->type;
       
   185 }
       
   186 
       
   187 /*!
       
   188   Returns this value as a number.
       
   189 */
       
   190 double QScriptDebuggerValue::numberValue() const
       
   191 {
       
   192     Q_D(const QScriptDebuggerValue);
       
   193     if (!d)
       
   194         return 0;
       
   195     Q_ASSERT(d->type == NumberValue);
       
   196     return d->numberValue;
       
   197 }
       
   198 
       
   199 /*!
       
   200   Returns this value as a boolean.
       
   201 */
       
   202 bool QScriptDebuggerValue::booleanValue() const
       
   203 {
       
   204     Q_D(const QScriptDebuggerValue);
       
   205     if (!d)
       
   206         return false;
       
   207     Q_ASSERT(d->type == BooleanValue);
       
   208     return d->booleanValue;
       
   209 }
       
   210 
       
   211 /*!
       
   212   Returns this value as a string.
       
   213 */
       
   214 QString QScriptDebuggerValue::stringValue() const
       
   215 {
       
   216     Q_D(const QScriptDebuggerValue);
       
   217     if (!d)
       
   218         return QString();
       
   219     Q_ASSERT(d->type == StringValue);
       
   220     return *d->stringValue;
       
   221 }
       
   222 
       
   223 /*!
       
   224   Returns this value as an object ID.
       
   225 */
       
   226 qint64 QScriptDebuggerValue::objectId() const
       
   227 {
       
   228     Q_D(const QScriptDebuggerValue);
       
   229     if (!d)
       
   230         return -1;
       
   231     Q_ASSERT(d->type == ObjectValue);
       
   232     return d->objectId;
       
   233 }
       
   234 
       
   235 /*!
       
   236   Converts this QScriptDebuggerValue to a QScriptValue in the
       
   237   given \a engine and returns the resulting value.
       
   238 */
       
   239 QScriptValue QScriptDebuggerValue::toScriptValue(QScriptEngine *engine) const
       
   240 {
       
   241     Q_D(const QScriptDebuggerValue);
       
   242     if (!d)
       
   243         return QScriptValue();
       
   244     switch (d->type) {
       
   245     case NoValue:
       
   246         return QScriptValue();
       
   247     case UndefinedValue:
       
   248         return engine->undefinedValue();
       
   249     case NullValue:
       
   250         return engine->nullValue();
       
   251     case BooleanValue:
       
   252         return QScriptValue(engine, d->booleanValue);
       
   253     case StringValue:
       
   254         return QScriptValue(engine, *d->stringValue);
       
   255     case NumberValue:
       
   256         return QScriptValue(engine, d->numberValue);
       
   257     case ObjectValue:
       
   258         return engine->objectById(d->objectId);
       
   259     }
       
   260     return QScriptValue();
       
   261 }
       
   262 
       
   263 /*!
       
   264   Returns a string representation of this value.
       
   265 */
       
   266 QString QScriptDebuggerValue::toString() const
       
   267 {
       
   268     Q_D(const QScriptDebuggerValue);
       
   269     if (!d)
       
   270         return QString();
       
   271     switch (d->type) {
       
   272     case NoValue:
       
   273         return QString();
       
   274     case UndefinedValue:
       
   275         return QString::fromLatin1("undefined");
       
   276     case NullValue:
       
   277         return QString::fromLatin1("null");
       
   278     case BooleanValue:
       
   279         if (d->booleanValue)
       
   280             return QString::fromLatin1("true");
       
   281         else
       
   282             return QString::fromLatin1("false");
       
   283     case StringValue:
       
   284         return *d->stringValue;
       
   285     case NumberValue:
       
   286         return QString::number(d->numberValue); // ### qScriptNumberToString()
       
   287     case ObjectValue:
       
   288         return QString::fromLatin1("[object Object]");
       
   289     }
       
   290     return QString();
       
   291 }
       
   292 
       
   293 /*!
       
   294   Returns true if this QScriptDebuggerValue is equal to the \a other
       
   295   value, otherwise returns false.
       
   296 */
       
   297 bool QScriptDebuggerValue::operator==(const QScriptDebuggerValue &other) const
       
   298 {
       
   299     Q_D(const QScriptDebuggerValue);
       
   300     const QScriptDebuggerValuePrivate *od = other.d_func();
       
   301     if (d == od)
       
   302         return true;
       
   303     if (!d || !od)
       
   304         return false;
       
   305     if (d->type != od->type)
       
   306         return false;
       
   307     switch (d->type) {
       
   308     case NoValue:
       
   309     case UndefinedValue:
       
   310     case NullValue:
       
   311         return true;
       
   312     case BooleanValue:
       
   313         return d->booleanValue == od->booleanValue;
       
   314     case StringValue:
       
   315         return *d->stringValue == *od->stringValue;
       
   316     case NumberValue:
       
   317         return d->numberValue == od->numberValue;
       
   318     case ObjectValue:
       
   319         return d->objectId == od->objectId;
       
   320     }
       
   321     return false;
       
   322 }
       
   323 
       
   324 /*!
       
   325   Returns true if this QScriptDebuggerValue is not equal to the \a
       
   326   other value, otherwise returns false.
       
   327 */
       
   328 bool QScriptDebuggerValue::operator!=(const QScriptDebuggerValue &other) const
       
   329 {
       
   330     return !(*this == other);
       
   331 }
       
   332 
       
   333 /*!
       
   334   \fn QDataStream &operator<<(QDataStream &stream, const QScriptDebuggerValue &value)
       
   335   \relates QScriptDebuggerValue
       
   336 
       
   337   Writes the given \a value to the specified \a stream.
       
   338 */
       
   339 QDataStream &operator<<(QDataStream &out, const QScriptDebuggerValue &value)
       
   340 {
       
   341     out << (quint32)value.type();
       
   342     switch (value.type()) {
       
   343     case QScriptDebuggerValue::NoValue:
       
   344     case QScriptDebuggerValue::UndefinedValue:
       
   345     case QScriptDebuggerValue::NullValue:
       
   346         break;
       
   347     case QScriptDebuggerValue::BooleanValue:
       
   348         out << value.booleanValue();
       
   349         break;
       
   350     case QScriptDebuggerValue::StringValue:
       
   351         out << value.stringValue();
       
   352         break;
       
   353     case QScriptDebuggerValue::NumberValue:
       
   354         out << value.numberValue();
       
   355         break;
       
   356     case QScriptDebuggerValue::ObjectValue:
       
   357         out << value.objectId();
       
   358         break;
       
   359     }
       
   360     return out;
       
   361 }
       
   362 
       
   363 /*!
       
   364   \fn QDataStream &operator>>(QDataStream &stream, QScriptDebuggerValue &value)
       
   365   \relates QScriptDebuggerValue
       
   366 
       
   367   Reads a QScriptDebuggerValue from the specified \a stream into the
       
   368   given \a value.
       
   369 */
       
   370 QDataStream &operator>>(QDataStream &in, QScriptDebuggerValue &value)
       
   371 {
       
   372     quint32 type;
       
   373     in >> type;
       
   374     switch (QScriptDebuggerValue::ValueType(type)) {
       
   375     case QScriptDebuggerValue::UndefinedValue:
       
   376     case QScriptDebuggerValue::NullValue:
       
   377         value = QScriptDebuggerValue(QScriptDebuggerValue::ValueType(type));
       
   378         break;
       
   379     case QScriptDebuggerValue::BooleanValue: {
       
   380         bool b;
       
   381         in >> b;
       
   382         value = QScriptDebuggerValue(b);
       
   383     }   break;
       
   384     case QScriptDebuggerValue::StringValue: {
       
   385         QString s;
       
   386         in >> s;
       
   387         value = QScriptDebuggerValue(s);
       
   388     }   break;
       
   389     case QScriptDebuggerValue::NumberValue: {
       
   390         double d;
       
   391         in >> d;
       
   392         value = QScriptDebuggerValue(d);
       
   393     }   break;
       
   394     case QScriptDebuggerValue::ObjectValue: {
       
   395         qint64 id;
       
   396         in >> id;
       
   397         value = QScriptDebuggerValue(id);
       
   398     }   break;
       
   399     case QScriptDebuggerValue::NoValue:
       
   400     default:
       
   401         value = QScriptDebuggerValue();
       
   402         break;
       
   403     }
       
   404     return in;
       
   405 }
       
   406 
       
   407 QT_END_NAMESPACE