| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include <QtCore> |
| 52 | #include <QtScript>cxx . . $ `<file>/include/QtScript/QtScript`/ |
| 53 | |
| 54 | template <class Container> |
| 55 | QScriptValuecxx . . $ QScriptValue# toScriptValuecxx . . $ toScriptValue(5b5c5dfa4ae5dcca).(QScriptEnginecxx . . $ QScriptEngine# *eng, const Container &cont) |
| 56 | { |
| 57 | QScriptValuecxx . . $ QScriptValue# a = eng->newArraycxx . . $ QScriptEngine#newArray(3c7fa2828cb9e5af).(); |
| 58 | typename Container::const_iterator begin = cont.begin(); |
| 59 | typename Container::const_iterator end = cont.end(); |
| 60 | typename Container::const_iterator it; |
| 61 | for (it = begin; it !=cxx . . $ `operator!=`(e8aee2ed4e0f1f82). end; ++it) |
| 62 | a.setPropertycxx . . $ QScriptValue#setProperty(47a99ff2df11e869).(quint32cxx . . $ quint32#(it -cxx . . $ operator-(275e3cffd3c96684). begin), eng->toScriptValuecxx . . $ QScriptEngine#toScriptValue(3f6667d060280b89).(*cxx . . $ `operator*`(9a6be20836af9fc).it)); |
| 63 | return acxx . . $ QScriptValue#QScriptValue(6b35c8bde2804237).; |
| 64 | } |
| 65 | |
| 66 | template <class Container> |
| 67 | void fromScriptValuecxx . . $ fromScriptValue(8d64b74b32184bb1).(const QScriptValuecxx . . $ QScriptValue# &value, Container &cont) |
| 68 | { |
| 69 | quint32cxx . . $ quint32# len = value.propertycxx . . $ QScriptValue#property(cc0ed881c2909ea3).("length"cxx . . $ QString#QString(85c52e162fed56f9).).toUInt32cxx . . $ QScriptValue#toUInt32(ddd8539a2fcdd4ce).(); |
| 70 | for (quint32cxx . . $ quint32# i = 0; i < len; ++i) { |
| 71 | QScriptValuecxx . . $ QScriptValue# item = value.propertycxx . . $ QScriptValue#property(84c547af90749cd4).(i); |
| 72 | typedef typename Container::value_type ContainerValuecxx . . $ fromScriptValue(8d64b74b32184bb1).ContainerValue#; |
| 73 | cont.push_back(qscriptvalue_castcxx . . $ qscriptvalue_cast(11c954240a7b88c6).<ContainerValuecxx . . $ fromScriptValue(8d64b74b32184bb1).ContainerValue#>(item)); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | typedef QVectorcxx . . $ QVector#<int> IntVectorcxx . . $ IntVector#; |
| 78 | typedef QVectorcxx . . $ QVector#<QStringcxx . . $ QString#> StringVectorcxx . . $ StringVector#; |
| 79 | |
| 80 | Q_DECLARE_METATYPEcxx . . $ `<external>/17491093664533580526/qmetatype.h:2056:9`!(IntVector) |
| 81 | Q_DECLARE_METATYPEcxx . . $ `<external>/17491093664533580526/qmetatype.h:2056:9`!(StringVector) |
| 82 | |
| 83 | int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[]) |
| 84 | { |
| 85 | QCoreApplicationcxx . . $ QCoreApplication# appcxx . . $ QCoreApplication#QCoreApplication(6e9b1c7df49e191d).(argc, argv); |
| 86 | |
| 87 | QScriptEnginecxx . . $ QScriptEngine# engcxx . . $ QScriptEngine#QScriptEngine(49f6e7a06ebc5aa8).; |
| 88 | // register our custom types |
| 89 | qScriptRegisterMetaTypecxx . . $ qScriptRegisterMetaType(b910593bb212e9de).<IntVectorcxx . . $ IntVector#>(&eng, toScriptValuecxx . . $ toScriptValue(5b5c5dfa4ae5dcca)., fromScriptValuecxx . . $ fromScriptValue(8d64b74b32184bb1).); |
| 90 | qScriptRegisterMetaTypecxx . . $ qScriptRegisterMetaType(b910593bb212e9de).<StringVectorcxx . . $ StringVector#>(&eng, toScriptValuecxx . . $ toScriptValue(5b5c5dfa4ae5dcca)., fromScriptValuecxx . . $ fromScriptValue(8d64b74b32184bb1).); |
| 91 | |
| 92 | QScriptValuecxx . . $ QScriptValue# val = eng.evaluatecxx . . $ QScriptEngine#evaluate(bbfdac3a7d53e517).("[1, 4, 7, 11, 50, 3, 19, 60]"cxx . . $ QString#QString(85c52e162fed56f9).); |
| 93 | |
| 94 | fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "Script array: %s\n", qPrintablecxx . . $ `<external>/15045578679651060896/qglobal.h:831:11`!(val.toString())); |
| 95 | |
| 96 | IntVectorcxx . . $ IntVector# iv = qscriptvalue_castcxx . . $ qscriptvalue_cast(11c954240a7b88c6).<IntVectorcxx . . $ IntVector#>(val); |
| 97 | |
| 98 | fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "qscriptvalue_cast to QVector<int>: "); |
| 99 | for (int i = 0; i < iv.sizecxx . . $ QVector#size(455f465bc33b4cdf).(); ++i) |
| 100 | fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "%s%d", (i > 0) ? "," : "", iv.atcxx . . $ QVector#at(e2681f9bb6b2f357).(i)); |
| 101 | fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "\n"); |
| 102 | |
| 103 | val =cxx . . $ QScriptValue#`operator=`(1a503f9e5bba28ab). eng.evaluatecxx . . $ QScriptEngine#evaluate(bbfdac3a7d53e517).("[9, 'foo', 46.5, 'bar', 'Qt', 555, 'hello']"cxx . . $ QString#QString(85c52e162fed56f9).); |
| 104 | |
| 105 | fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "Script array: %s\n", qPrintablecxx . . $ `<external>/15045578679651060896/qglobal.h:831:11`!(val.toString())); |
| 106 | |
| 107 | StringVectorcxx . . $ StringVector# sv = qscriptvalue_castcxx . . $ qscriptvalue_cast(11c954240a7b88c6).<StringVectorcxx . . $ StringVector#>(val); |
| 108 | |
| 109 | fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "qscriptvalue_cast to QVector<QString>: "); |
| 110 | for (int i = 0; i < sv.sizecxx . . $ QVector#size(455f465bc33b4cdf).(); ++i) |
| 111 | fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "%s%s", (i > 0) ? "," : "", qPrintablecxx . . $ `<external>/15045578679651060896/qglobal.h:831:11`!(sv.at(i))); |
| 112 | fprintf(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "\n"); |
| 113 | |
| 114 | return 0; |
| 115 | } |