| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 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:BSD$ | 
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows: | 
|---|
| 11 | ** | 
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without | 
|---|
| 13 | ** modification, are permitted provided that the following conditions are | 
|---|
| 14 | ** met: | 
|---|
| 15 | **   * Redistributions of source code must retain the above copyright | 
|---|
| 16 | **     notice, this list of conditions and the following disclaimer. | 
|---|
| 17 | **   * Redistributions in binary form must reproduce the above copyright | 
|---|
| 18 | **     notice, this list of conditions and the following disclaimer in | 
|---|
| 19 | **     the documentation and/or other materials provided with the | 
|---|
| 20 | **     distribution. | 
|---|
| 21 | **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor | 
|---|
| 22 | **     the names of its contributors may be used to endorse or promote | 
|---|
| 23 | **     products derived from this software without specific prior written | 
|---|
| 24 | **     permission. | 
|---|
| 25 | ** | 
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." | 
|---|
| 37 | ** $QT_END_LICENSE$ | 
|---|
| 38 | ** | 
|---|
| 39 | ****************************************************************************/ | 
|---|
| 40 |  | 
|---|
| 41 | #include <QtCore> | 
|---|
| 42 | #include <QtScript> | 
|---|
| 43 |  | 
|---|
| 44 | template <class Container> | 
|---|
| 45 | QScriptValue toScriptValue(QScriptEngine *eng, const Container &cont) | 
|---|
| 46 | { | 
|---|
| 47 | QScriptValue a = eng->newArray(); | 
|---|
| 48 | typename Container::const_iterator begin = cont.begin(); | 
|---|
| 49 | typename Container::const_iterator end = cont.end(); | 
|---|
| 50 | typename Container::const_iterator it; | 
|---|
| 51 | for (it = begin; it != end; ++it) | 
|---|
| 52 | a.setProperty(quint32(it - begin), qScriptValueFromValue(eng, *it)); | 
|---|
| 53 | return a; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | template <class Container> | 
|---|
| 57 | void fromScriptValue(const QScriptValue &value, Container &cont) | 
|---|
| 58 | { | 
|---|
| 59 | quint32 len = value.property("length").toUInt32(); | 
|---|
| 60 | for (quint32 i = 0; i < len; ++i) { | 
|---|
| 61 | QScriptValue item = value.property(i); | 
|---|
| 62 | typedef typename Container::value_type ContainerValue; | 
|---|
| 63 | cont.push_back(qscriptvalue_cast<ContainerValue>(item)); | 
|---|
| 64 | } | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | typedef QVector<int> IntVector; | 
|---|
| 68 | typedef QVector<QString> StringVector; | 
|---|
| 69 |  | 
|---|
| 70 | Q_DECLARE_METATYPE(IntVector) | 
|---|
| 71 | Q_DECLARE_METATYPE(StringVector) | 
|---|
| 72 |  | 
|---|
| 73 | int main(int argc, char *argv[]) | 
|---|
| 74 | { | 
|---|
| 75 | QCoreApplication app(argc, argv); | 
|---|
| 76 |  | 
|---|
| 77 | QScriptEngine eng; | 
|---|
| 78 | // register our custom types | 
|---|
| 79 | qScriptRegisterMetaType<IntVector>(&eng, toScriptValue, fromScriptValue); | 
|---|
| 80 | qScriptRegisterMetaType<StringVector>(&eng, toScriptValue, fromScriptValue); | 
|---|
| 81 |  | 
|---|
| 82 | QScriptValue val = eng.evaluate("[1, 4, 7, 11, 50, 3, 19, 60]"); | 
|---|
| 83 |  | 
|---|
| 84 | fprintf(stdout, "Script array: %s\n", qPrintable(val.toString())); | 
|---|
| 85 |  | 
|---|
| 86 | IntVector iv = qscriptvalue_cast<IntVector>(val); | 
|---|
| 87 |  | 
|---|
| 88 | fprintf(stdout, "qscriptvalue_cast to QVector<int>: "); | 
|---|
| 89 | for (int i = 0; i < iv.size(); ++i) | 
|---|
| 90 | fprintf(stdout, "%s%d", (i > 0) ? "," : "", iv.at(i)); | 
|---|
| 91 | fprintf(stdout, "\n"); | 
|---|
| 92 |  | 
|---|
| 93 | val = eng.evaluate("[9, 'foo', 46.5, 'bar', 'Qt', 555, 'hello']"); | 
|---|
| 94 |  | 
|---|
| 95 | fprintf(stdout, "Script array: %s\n", qPrintable(val.toString())); | 
|---|
| 96 |  | 
|---|
| 97 | StringVector sv = qscriptvalue_cast<StringVector>(val); | 
|---|
| 98 |  | 
|---|
| 99 | fprintf(stdout, "qscriptvalue_cast to QVector<QString>: "); | 
|---|
| 100 | for (int i = 0; i < sv.size(); ++i) | 
|---|
| 101 | fprintf(stdout, "%s%s", (i > 0) ? "," : "", qPrintable(sv.at(i))); | 
|---|
| 102 | fprintf(stdout, "\n"); | 
|---|
| 103 |  | 
|---|
| 104 | return 0; | 
|---|
| 105 | } | 
|---|