Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #ifndef MUSE_API_JSRETVAL_H
23 : #define MUSE_API_JSRETVAL_H
24 :
25 : #include <QVariant>
26 :
27 : #include "types/retval.h"
28 : #include "io/path.h"
29 :
30 : namespace muse::api {
31 : #define JSVal QVariant
32 : #define JSRet QVariantMap
33 : #define JSRetVal QVariantMap
34 :
35 0 : inline JSRet retToJs(const Ret& r)
36 : {
37 0 : QVariantMap o;
38 0 : o["errcode"] = r.code();
39 0 : o["success"] = r.success();
40 0 : o["text"] = QString::fromStdString(r.text());
41 0 : return o;
42 0 : }
43 :
44 : inline Ret retFromJs(const JSRet& r)
45 : {
46 : return Ret(r.value("errcode", 0).toInt(), r.value("text").toString().toStdString());
47 : }
48 :
49 0 : inline QVariant toQVariant(const QString& v)
50 : {
51 0 : return QVariant::fromValue(v);
52 : }
53 :
54 : inline QVariant toQVariant(const io::path_t& v)
55 : {
56 : return QVariant(v.toQString());
57 : }
58 :
59 : inline io::path_t fromQVariant(const QVariant& v)
60 : {
61 : return v.toString();
62 : }
63 :
64 0 : inline QVariant toQVariant(const io::paths_t& v)
65 : {
66 0 : QStringList l;
67 0 : l.reserve(int(v.size()));
68 0 : for (const io::path_t& p : v) {
69 0 : l << p.toQString();
70 : }
71 0 : return QVariant(l);
72 0 : }
73 :
74 : template<typename T>
75 0 : inline JSRet retValToJs(const RetVal<T>& r)
76 : {
77 0 : QVariantMap o = retToJs(r.ret);
78 0 : o["value"] = toQVariant(r.val);
79 0 : return o;
80 0 : }
81 :
82 : template<typename T>
83 : inline RetVal<T> retValFromJs(const JSRet& r)
84 : {
85 : RetVal<T> rv;
86 : rv.ret = retFromJs(r);
87 : rv.val = fromQVariant(r.value("value"));
88 : return rv;
89 : }
90 : }
91 :
92 : #endif // MUSE_API_JSRETVAL_H
|