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