diff options
author | Kent Hansen <kent.hansen@nokia.com> | 2010-03-25 14:50:38 (GMT) |
---|---|---|
committer | Kent Hansen <kent.hansen@nokia.com> | 2010-03-25 14:53:00 (GMT) |
commit | f4da9f9b347d296bc23e44e47e0a6883d5ad41ea (patch) | |
tree | 3a2fa3ab32ac986e74763bba01ef2dde643d1266 | |
parent | 2fdfb3e0285ac535b63548d208da6dcae71105cc (diff) | |
download | Qt-f4da9f9b347d296bc23e44e47e0a6883d5ad41ea.zip Qt-f4da9f9b347d296bc23e44e47e0a6883d5ad41ea.tar.gz Qt-f4da9f9b347d296bc23e44e47e0a6883d5ad41ea.tar.bz2 |
Use the new memory cost reporting API in QtScript Custom Class example
Also describe its purpose in the example doc.
-rw-r--r-- | doc/src/examples/qtscriptcustomclass.qdoc | 13 | ||||
-rw-r--r-- | examples/script/customclass/bytearrayclass.cpp | 17 | ||||
-rw-r--r-- | examples/script/customclass/bytearrayclass.h | 2 |
3 files changed, 30 insertions, 2 deletions
diff --git a/doc/src/examples/qtscriptcustomclass.qdoc b/doc/src/examples/qtscriptcustomclass.qdoc index f825ad9..028c7a6 100644 --- a/doc/src/examples/qtscriptcustomclass.qdoc +++ b/doc/src/examples/qtscriptcustomclass.qdoc @@ -120,6 +120,8 @@ this ByteArrayClass object, so that the constructor, when it is invoked, can extract the pointer and use it to create a new \c{ByteArray} object. + \snippet examples/script/customclass/bytearrayclass.cpp 10 + \snippet examples/script/customclass/bytearrayclass.cpp 1 The newInstance() function isn't part of the QScriptClass API; its purpose is to offer @@ -128,6 +130,11 @@ QScriptEngine::newObject() will call the prototype() function of our class, ensuring that the prototype of the new object will be the standard \c{ByteArray} prototype. + QScriptEngine::reportAdditionalMemoryCost() is called to inform the script engine of the + memory occupied by the QByteArray. This gives the garbage collector a hint that it should + perhaps trigger more frequently, possibly freeing up memory associated with large ByteArray + objects that are no longer in use. + \snippet examples/script/customclass/bytearrayclass.cpp 2 construct() is the native function that will act as a constructor for \c{ByteArray} @@ -159,6 +166,12 @@ array index that was calculated in the queryProperty() function, enlarge the array if necessary, and write the given value to the array. + \snippet examples/script/customclass/bytearrayclass.cpp 9 + + The resize() function is a helper function that resizes the QByteArray to a new size, and, + if the new size is greater than the old, reports the additional memory cost to the script + engine. + \snippet examples/script/customclass/bytearrayclass.cpp 6 The propertyFlags() reimplementation specifies that the \c{length} property can't be deleted, diff --git a/examples/script/customclass/bytearrayclass.cpp b/examples/script/customclass/bytearrayclass.cpp index 1dbf2d4..239b4b4 100644 --- a/examples/script/customclass/bytearrayclass.cpp +++ b/examples/script/customclass/bytearrayclass.cpp @@ -148,13 +148,13 @@ void ByteArrayClass::setProperty(QScriptValue &object, if (!ba) return; if (name == length) { - ba->resize(value.toInt32()); + resize(*ba, value.toInt32()); } else { qint32 pos = id; if (pos < 0) return; if (ba->size() <= pos) - ba->resize(pos + 1); + resize(*ba, pos + 1); (*ba)[pos] = char(value.toInt32()); } } @@ -194,10 +194,13 @@ QScriptValue ByteArrayClass::constructor() return ctor; } +//! [10] QScriptValue ByteArrayClass::newInstance(int size) { + engine()->reportAdditionalMemoryCost(size); return newInstance(QByteArray(size, /*ch=*/0)); } +//! [10] //! [1] QScriptValue ByteArrayClass::newInstance(const QByteArray &ba) @@ -235,6 +238,16 @@ void ByteArrayClass::fromScriptValue(const QScriptValue &obj, QByteArray &ba) ba = qvariant_cast<QByteArray>(obj.data().toVariant()); } +//! [9] +void ByteArrayClass::resize(QByteArray &ba, int newSize) +{ + int oldSize = ba.size(); + ba.resize(newSize); + if (newSize > oldSize) + engine()->reportAdditionalMemoryCost(newSize - oldSize); +} +//! [9] + ByteArrayClassPropertyIterator::ByteArrayClassPropertyIterator(const QScriptValue &object) diff --git a/examples/script/customclass/bytearrayclass.h b/examples/script/customclass/bytearrayclass.h index f185754..3f7ce22 100644 --- a/examples/script/customclass/bytearrayclass.h +++ b/examples/script/customclass/bytearrayclass.h @@ -82,6 +82,8 @@ private: static QScriptValue toScriptValue(QScriptEngine *eng, const QByteArray &ba); static void fromScriptValue(const QScriptValue &obj, QByteArray &ba); + void resize(QByteArray &ba, int newSize); + QScriptString length; QScriptValue proto; QScriptValue ctor; |