[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
[2] | 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
[846] | 13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
| 14 | ** written agreement between you and Nokia.
|
---|
[2] | 15 | **
|
---|
[846] | 16 | ** GNU Free Documentation License
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
| 18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
| 20 | ** file.
|
---|
[2] | 21 | **
|
---|
[561] | 22 | ** If you have questions regarding the use of this file, please contact
|
---|
| 23 | ** Nokia at qt-info@nokia.com.
|
---|
[2] | 24 | ** $QT_END_LICENSE$
|
---|
| 25 | **
|
---|
| 26 | ****************************************************************************/
|
---|
| 27 |
|
---|
| 28 | /*!
|
---|
| 29 | \example script/customclass
|
---|
| 30 | \title Custom Script Class Example
|
---|
| 31 |
|
---|
| 32 | The Custom Script Class example shows how to use QScriptClass and QScriptClassPropertyIterator
|
---|
| 33 | to implement a custom script class.
|
---|
| 34 |
|
---|
| 35 | The script class we are going to implement is called \c{ByteArray}. It provides a wrapper around
|
---|
| 36 | the QByteArray class in Qt, with a simplified API. Why do we need such a class? Well, neither the
|
---|
| 37 | ECMAScript \c{Array} class or \c{String} class is appropriate to use when working with arrays of
|
---|
| 38 | bytes. Our \c{ByteArray} class will have the right semantics; objects will use only the amount of
|
---|
| 39 | memory that is really needed (a byte is stored as a byte, not as a floating-point number or a
|
---|
| 40 | Unicode character) and can be passed directly to C++ slots taking QByteArray arguments (no costly
|
---|
| 41 | conversion necessary).
|
---|
| 42 |
|
---|
| 43 | \section1 ByteArray Class In Use
|
---|
| 44 |
|
---|
| 45 | When the \c{ByteArray} class has been made available to the
|
---|
| 46 | scripting environment, \c{ByteArray} objects can be constructed like
|
---|
| 47 | so:
|
---|
| 48 |
|
---|
| 49 | \snippet doc/src/snippets/code/doc_src_examples_qtscriptcustomclass.qdoc 0
|
---|
| 50 |
|
---|
| 51 | \c{ByteArray} objects behave similar to normal \c{Array} objects. Every \c{ByteArray} object has
|
---|
| 52 | a \c{length} property, that holds the length of the array. If a new value is assigned to the \c{length}
|
---|
| 53 | property, the array is resized. If the array is enlarged, the new bytes are initialized to 0.
|
---|
| 54 | (This is a difference from normal \c{Array} objects; \c{ByteArray} objects are always dense arrays.)
|
---|
| 55 | Use normal array operations to read or write bytes in the array. The following code sets all the
|
---|
| 56 | bytes of an array to a certain value:
|
---|
| 57 |
|
---|
| 58 | \snippet doc/src/snippets/code/doc_src_examples_qtscriptcustomclass.qdoc 1
|
---|
| 59 |
|
---|
| 60 | When assigning a value to an array element, the value is truncated to eight bits:
|
---|
| 61 |
|
---|
| 62 | \snippet doc/src/snippets/code/doc_src_examples_qtscriptcustomclass.qdoc 2
|
---|
| 63 |
|
---|
| 64 | Like normal \c{Array} objects, if the array index is greater than the current length
|
---|
| 65 | of the array, the array is resized accordingly:
|
---|
| 66 |
|
---|
| 67 | \snippet doc/src/snippets/code/doc_src_examples_qtscriptcustomclass.qdoc 3
|
---|
| 68 |
|
---|
| 69 | Property names that aren't valid array indexes are treated
|
---|
| 70 | like normal object properties (again, the same is the case for normal \c{Array} objects);
|
---|
| 71 | in other words, it's perfectly fine to do something like this:
|
---|
| 72 |
|
---|
| 73 | \snippet doc/src/snippets/code/doc_src_examples_qtscriptcustomclass.qdoc 4
|
---|
| 74 |
|
---|
| 75 | The above assignment won't affect the contents of the array, but will rather assign a value
|
---|
| 76 | to the object property named "foo".
|
---|
| 77 |
|
---|
| 78 | \c{ByteArray} objects have a set of methods: chop(), equals(), left(), mid(), toBase64() and so on.
|
---|
| 79 | These map directly onto the corresponding methods in QByteArray.
|
---|
| 80 |
|
---|
| 81 | \snippet doc/src/snippets/code/doc_src_examples_qtscriptcustomclass.qdoc 5
|
---|
| 82 |
|
---|
| 83 | \section1 ByteArray Class Implementation
|
---|
| 84 |
|
---|
| 85 | To implement the \c{ByteArray} script class in C++, we create a subclass of QScriptClass,
|
---|
| 86 | called ByteArrayClass, and reimplement the virtual functions from QScriptClass. We also provide
|
---|
| 87 | a Qt Script constructor function suitable for being added to a QScriptEngine's environment.
|
---|
| 88 |
|
---|
| 89 | The ByteArrayClass constructor prepares the script class:
|
---|
| 90 |
|
---|
| 91 | \snippet examples/script/customclass/bytearrayclass.cpp 0
|
---|
| 92 |
|
---|
| 93 | First, the constructor registers a pair of conversion functions, so that C++ QByteArray objects
|
---|
| 94 | and Qt Script \c{ByteArray} objects can move seamlessly between the C++ side and the script side.
|
---|
| 95 | For example, if a \c{ByteArray} object is passed to a C++ slot that takes a QByteArray
|
---|
| 96 | argument, the actual QByteArray that the \c{ByteArray} object wraps will be passed correctly.
|
---|
| 97 |
|
---|
| 98 | Second, we store a handle to the string "length", so that we can quickly compare a given property name
|
---|
| 99 | to "length" later on.
|
---|
| 100 |
|
---|
| 101 | Third, we initialize the standard \c{ByteArray} prototype, to be returned by our prototype()
|
---|
| 102 | reimplementation later on. (The implementation of the prototype is discussed later.)
|
---|
| 103 |
|
---|
| 104 | Fourth, we initialize a constructor function for \c{ByteArray}, to be returned by the
|
---|
| 105 | constructor() function. We set the internal data of the constructor to be a pointer to
|
---|
| 106 | this ByteArrayClass object, so that the constructor, when it is invoked, can extract the
|
---|
| 107 | pointer and use it to create a new \c{ByteArray} object.
|
---|
| 108 |
|
---|
[846] | 109 | \snippet examples/script/customclass/bytearrayclass.cpp 10
|
---|
| 110 |
|
---|
[2] | 111 | \snippet examples/script/customclass/bytearrayclass.cpp 1
|
---|
| 112 |
|
---|
| 113 | The newInstance() function isn't part of the QScriptClass API; its purpose is to offer
|
---|
| 114 | a convenient way to construct a \c{ByteArray} object from an existing QByteArray. We store the
|
---|
| 115 | QByteArray as the internal data of the new object, and return the new object.
|
---|
| 116 | QScriptEngine::newObject() will call the prototype() function of our class, ensuring that
|
---|
| 117 | the prototype of the new object will be the standard \c{ByteArray} prototype.
|
---|
| 118 |
|
---|
[846] | 119 | QScriptEngine::reportAdditionalMemoryCost() is called to inform the script engine of the
|
---|
| 120 | memory occupied by the QByteArray. This gives the garbage collector a hint that it should
|
---|
| 121 | perhaps trigger more frequently, possibly freeing up memory associated with large ByteArray
|
---|
| 122 | objects that are no longer in use.
|
---|
| 123 |
|
---|
[2] | 124 | \snippet examples/script/customclass/bytearrayclass.cpp 2
|
---|
| 125 |
|
---|
| 126 | construct() is the native function that will act as a constructor for \c{ByteArray}
|
---|
| 127 | in scripts. We extract the pointer to the class, then call a newInstance() overload
|
---|
| 128 | that takes an initial size as argument, and return the new script object.
|
---|
| 129 |
|
---|
| 130 | \snippet examples/script/customclass/bytearrayclass.cpp 3
|
---|
| 131 |
|
---|
| 132 | queryProperty() is the function that Qt Script will call whenever someone tries to access
|
---|
| 133 | a property of a \c{ByteArray} object. We first get a pointer to the underlying QByteArray.
|
---|
| 134 | We check if the property being accessed is the special \c{length} property; if so, we
|
---|
| 135 | return, indicating that we will handle every kind of access to this property (e.g. both
|
---|
| 136 | read and write). Otherwise, we attempt to convert the property name to an array index. If
|
---|
| 137 | this fails, we return, indicating that we don't want to handle this property. Otherwise, we
|
---|
| 138 | have a valid array index, and store it in the \c{id} argument, so that we don't have to
|
---|
| 139 | recompute it in e.g. property() or setProperty(). If the index is greater than or equal to
|
---|
| 140 | the QByteArray's size, we indicate that we don't want to handle read access (but we still want
|
---|
| 141 | to handle writes, if requested).
|
---|
| 142 |
|
---|
| 143 | \snippet examples/script/customclass/bytearrayclass.cpp 4
|
---|
| 144 |
|
---|
| 145 | In the property() reimplementation, we do similar checks as in queryProperty() to find out
|
---|
| 146 | which property is being requested, and then return the value of that property.
|
---|
| 147 |
|
---|
| 148 | \snippet examples/script/customclass/bytearrayclass.cpp 5
|
---|
| 149 |
|
---|
| 150 | The setProperty() reimplementation has a structure that is similar to property(). If the \c{length} property
|
---|
| 151 | is being set, we resize the underlying QByteArray to the given length. Otherwise, we grab the
|
---|
| 152 | array index that was calculated in the queryProperty() function, enlarge the array if necessary,
|
---|
| 153 | and write the given value to the array.
|
---|
| 154 |
|
---|
[846] | 155 | \snippet examples/script/customclass/bytearrayclass.cpp 9
|
---|
| 156 |
|
---|
| 157 | The resize() function is a helper function that resizes the QByteArray to a new size, and,
|
---|
| 158 | if the new size is greater than the old, reports the additional memory cost to the script
|
---|
| 159 | engine.
|
---|
| 160 |
|
---|
[2] | 161 | \snippet examples/script/customclass/bytearrayclass.cpp 6
|
---|
| 162 |
|
---|
| 163 | The propertyFlags() reimplementation specifies that the \c{length} property can't be deleted,
|
---|
| 164 | and that it is not enumerable. Array elements can't be deleted.
|
---|
| 165 |
|
---|
| 166 | \snippet examples/script/customclass/bytearrayclass.cpp 7
|
---|
| 167 |
|
---|
| 168 | We want the array elements to show up when a \c{ByteArray} object is used in for-in
|
---|
| 169 | statements and together with QScriptValueIterator. Therefore, we reimplement the
|
---|
| 170 | newIterator() function and have it return a new iterator for a given \c{ByteArray}.
|
---|
| 171 |
|
---|
| 172 | \section1 ByteArray Iterator Implementation
|
---|
| 173 |
|
---|
| 174 | \snippet examples/script/customclass/bytearrayclass.cpp 8
|
---|
| 175 |
|
---|
| 176 | The \c{ByteArrayClassPropertyIterator} class is simple. It maintains an index into the
|
---|
| 177 | underlying QByteArray, and checks and updates the index in hasNext(), next() and so on.
|
---|
| 178 |
|
---|
| 179 | \section1 ByteArray Prototype Implementation
|
---|
| 180 |
|
---|
| 181 | The prototype class, ByteArrayPrototype, implements the \c{ByteArray} functions as slots.
|
---|
| 182 |
|
---|
| 183 | \snippet examples/script/customclass/bytearrayprototype.h 0
|
---|
| 184 |
|
---|
| 185 | There is a small helper function, thisByteArray(), that returns a pointer to the QByteArray
|
---|
| 186 | being operated upon:
|
---|
| 187 |
|
---|
| 188 | \snippet examples/script/customclass/bytearrayprototype.cpp 0
|
---|
| 189 |
|
---|
| 190 | The slots simply forward the calls to the QByteArray. Examples:
|
---|
| 191 |
|
---|
| 192 | \snippet examples/script/customclass/bytearrayprototype.cpp 1
|
---|
| 193 |
|
---|
| 194 | The remove() function is noteworthy; if we look at QByteArray::remove(), we see that it
|
---|
| 195 | should return a reference to the QByteArray itself (i.e. not a copy). To get the same
|
---|
| 196 | behavior in scripts, we return the script object (thisObject()).
|
---|
| 197 | */
|
---|