util/examples/script/customclass/bytearrayclass.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 examples 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 <QtScript/QScriptClassPropertyIterator>
       
    43 #include <QtScript/QScriptEngine>
       
    44 #include "bytearrayclass.h"
       
    45 #include "bytearrayprototype.h"
       
    46 
       
    47 #include <stdlib.h>
       
    48 
       
    49 Q_DECLARE_METATYPE(QByteArray*)
       
    50 Q_DECLARE_METATYPE(ByteArrayClass*)
       
    51 
       
    52 class ByteArrayClassPropertyIterator : public QScriptClassPropertyIterator
       
    53 {
       
    54 public:
       
    55     ByteArrayClassPropertyIterator(const QScriptValue &object);
       
    56     ~ByteArrayClassPropertyIterator();
       
    57 
       
    58     bool hasNext() const;
       
    59     void next();
       
    60 
       
    61     bool hasPrevious() const;
       
    62     void previous();
       
    63 
       
    64     void toFront();
       
    65     void toBack();
       
    66 
       
    67     QScriptString name() const;
       
    68     uint id() const;
       
    69 
       
    70 private:
       
    71     int m_index;
       
    72     int m_last;
       
    73 };
       
    74 
       
    75 //! [0]
       
    76 ByteArrayClass::ByteArrayClass(QScriptEngine *engine)
       
    77     : QObject(engine), QScriptClass(engine)
       
    78 {
       
    79     qScriptRegisterMetaType<QByteArray>(engine, toScriptValue, fromScriptValue);
       
    80 
       
    81     length = engine->toStringHandle(QLatin1String("length"));
       
    82 
       
    83     proto = engine->newQObject(new ByteArrayPrototype(this),
       
    84                                QScriptEngine::QtOwnership,
       
    85                                QScriptEngine::SkipMethodsInEnumeration
       
    86                                | QScriptEngine::ExcludeSuperClassMethods
       
    87                                | QScriptEngine::ExcludeSuperClassProperties);
       
    88     QScriptValue global = engine->globalObject();
       
    89     proto.setPrototype(global.property("Object").property("prototype"));
       
    90 
       
    91     ctor = engine->newFunction(construct, proto);
       
    92     ctor.setData(qScriptValueFromValue(engine, this));
       
    93 }
       
    94 //! [0]
       
    95 
       
    96 ByteArrayClass::~ByteArrayClass()
       
    97 {
       
    98 }
       
    99 
       
   100 //! [3]
       
   101 QScriptClass::QueryFlags ByteArrayClass::queryProperty(const QScriptValue &object,
       
   102                                                        const QScriptString &name,
       
   103                                                        QueryFlags flags, uint *id)
       
   104 {
       
   105     QByteArray *ba = qscriptvalue_cast<QByteArray*>(object.data());
       
   106     if (!ba)
       
   107         return 0;
       
   108     if (name == length) {
       
   109         return flags;
       
   110     } else {
       
   111         bool isArrayIndex;
       
   112         qint32 pos = name.toArrayIndex(&isArrayIndex);
       
   113         if (!isArrayIndex)
       
   114             return 0;
       
   115         *id = pos;
       
   116         if ((flags & HandlesReadAccess) && (pos >= ba->size()))
       
   117             flags &= ~HandlesReadAccess;
       
   118         return flags;
       
   119     }
       
   120 }
       
   121 //! [3]
       
   122 
       
   123 //! [4]
       
   124 QScriptValue ByteArrayClass::property(const QScriptValue &object,
       
   125                                       const QScriptString &name, uint id)
       
   126 {
       
   127     QByteArray *ba = qscriptvalue_cast<QByteArray*>(object.data());
       
   128     if (!ba)
       
   129         return QScriptValue();
       
   130     if (name == length) {
       
   131         return ba->length();
       
   132     } else {
       
   133         qint32 pos = id;
       
   134         if ((pos < 0) || (pos >= ba->size()))
       
   135             return QScriptValue();
       
   136         return uint(ba->at(pos)) & 255;
       
   137     }
       
   138     return QScriptValue();
       
   139 }
       
   140 //! [4]
       
   141 
       
   142 //! [5]
       
   143 void ByteArrayClass::setProperty(QScriptValue &object,
       
   144                                  const QScriptString &name,
       
   145                                  uint id, const QScriptValue &value)
       
   146 {
       
   147     QByteArray *ba = qscriptvalue_cast<QByteArray*>(object.data());
       
   148     if (!ba)
       
   149         return;
       
   150     if (name == length) {
       
   151         ba->resize(value.toInt32());
       
   152     } else {
       
   153         qint32 pos = id;
       
   154         if (pos < 0)
       
   155             return;
       
   156         if (ba->size() <= pos)
       
   157             ba->resize(pos + 1);
       
   158         (*ba)[pos] = char(value.toInt32());
       
   159     }
       
   160 }
       
   161 //! [5]
       
   162 
       
   163 //! [6]
       
   164 QScriptValue::PropertyFlags ByteArrayClass::propertyFlags(
       
   165     const QScriptValue &/*object*/, const QScriptString &name, uint /*id*/)
       
   166 {
       
   167     if (name == length) {
       
   168         return QScriptValue::Undeletable
       
   169             | QScriptValue::SkipInEnumeration;
       
   170     }
       
   171     return QScriptValue::Undeletable;
       
   172 }
       
   173 //! [6]
       
   174 
       
   175 //! [7]
       
   176 QScriptClassPropertyIterator *ByteArrayClass::newIterator(const QScriptValue &object)
       
   177 {
       
   178     return new ByteArrayClassPropertyIterator(object);
       
   179 }
       
   180 //! [7]
       
   181 
       
   182 QString ByteArrayClass::name() const
       
   183 {
       
   184     return QLatin1String("ByteArray");
       
   185 }
       
   186 
       
   187 QScriptValue ByteArrayClass::prototype() const
       
   188 {
       
   189     return proto;
       
   190 }
       
   191 
       
   192 QScriptValue ByteArrayClass::constructor()
       
   193 {
       
   194     return ctor;
       
   195 }
       
   196 
       
   197 QScriptValue ByteArrayClass::newInstance(int size)
       
   198 {
       
   199     return newInstance(QByteArray(size, /*ch=*/0));
       
   200 }
       
   201 
       
   202 //! [1]
       
   203 QScriptValue ByteArrayClass::newInstance(const QByteArray &ba)
       
   204 {
       
   205     QScriptValue data = engine()->newVariant(qVariantFromValue(ba));
       
   206     return engine()->newObject(this, data);
       
   207 }
       
   208 //! [1]
       
   209 
       
   210 //! [2]
       
   211 QScriptValue ByteArrayClass::construct(QScriptContext *ctx, QScriptEngine *)
       
   212 {
       
   213     ByteArrayClass *cls = qscriptvalue_cast<ByteArrayClass*>(ctx->callee().data());
       
   214     if (!cls)
       
   215         return QScriptValue();
       
   216     QScriptValue arg = ctx->argument(0);
       
   217     if (arg.instanceOf(ctx->callee()))
       
   218         return cls->newInstance(qscriptvalue_cast<QByteArray>(arg));
       
   219     int size = arg.toInt32();
       
   220     return cls->newInstance(size);
       
   221 }
       
   222 //! [2]
       
   223 
       
   224 QScriptValue ByteArrayClass::toScriptValue(QScriptEngine *eng, const QByteArray &ba)
       
   225 {
       
   226     QScriptValue ctor = eng->globalObject().property("ByteArray");
       
   227     ByteArrayClass *cls = qscriptvalue_cast<ByteArrayClass*>(ctor.data());
       
   228     if (!cls)
       
   229         return eng->newVariant(qVariantFromValue(ba));
       
   230     return cls->newInstance(ba);
       
   231 }
       
   232 
       
   233 void ByteArrayClass::fromScriptValue(const QScriptValue &obj, QByteArray &ba)
       
   234 {
       
   235     ba = qvariant_cast<QByteArray>(obj.data().toVariant());
       
   236 }
       
   237 
       
   238 
       
   239 
       
   240 ByteArrayClassPropertyIterator::ByteArrayClassPropertyIterator(const QScriptValue &object)
       
   241     : QScriptClassPropertyIterator(object)
       
   242 {
       
   243     toFront();
       
   244 }
       
   245 
       
   246 ByteArrayClassPropertyIterator::~ByteArrayClassPropertyIterator()
       
   247 {
       
   248 }
       
   249 
       
   250 //! [8]
       
   251 bool ByteArrayClassPropertyIterator::hasNext() const
       
   252 {
       
   253     QByteArray *ba = qscriptvalue_cast<QByteArray*>(object().data());
       
   254     return m_index < ba->size();
       
   255 }
       
   256 
       
   257 void ByteArrayClassPropertyIterator::next()
       
   258 {
       
   259     m_last = m_index;
       
   260     ++m_index;
       
   261 }
       
   262 
       
   263 bool ByteArrayClassPropertyIterator::hasPrevious() const
       
   264 {
       
   265     return (m_index > 0);
       
   266 }
       
   267 
       
   268 void ByteArrayClassPropertyIterator::previous()
       
   269 {
       
   270     --m_index;
       
   271     m_last = m_index;
       
   272 }
       
   273 
       
   274 void ByteArrayClassPropertyIterator::toFront()
       
   275 {
       
   276     m_index = 0;
       
   277     m_last = -1;
       
   278 }
       
   279 
       
   280 void ByteArrayClassPropertyIterator::toBack()
       
   281 {
       
   282     QByteArray *ba = qscriptvalue_cast<QByteArray*>(object().data());
       
   283     m_index = ba->size();
       
   284     m_last = -1;
       
   285 }
       
   286 
       
   287 QScriptString ByteArrayClassPropertyIterator::name() const
       
   288 {
       
   289     return object().engine()->toStringHandle(QString::number(m_last));
       
   290 }
       
   291 
       
   292 uint ByteArrayClassPropertyIterator::id() const
       
   293 {
       
   294     return m_last;
       
   295 }
       
   296 //! [8]