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_IAPIREGISTER_H
23 : #define MUSE_API_IAPIREGISTER_H
24 :
25 : #include <vector>
26 : #include <QMetaEnum>
27 :
28 : #include "modularity/imoduleinterface.h"
29 : #include "iapiengine.h"
30 : #include "apiobject.h"
31 : #include "apitypes.h"
32 :
33 : namespace muse::api {
34 : class IApiRegister : MODULE_GLOBAL_INTERFACE
35 : {
36 46 : INTERFACE_ID(api::IApiRegister)
37 : public:
38 46 : virtual ~IApiRegister() = default;
39 :
40 : struct ICreator {
41 0 : virtual ~ICreator() {}
42 : virtual ApiObject* create(IApiEngine* e) = 0;
43 : virtual bool isNeedDelete() const = 0;
44 : };
45 :
46 : template<class T>
47 : struct ApiCreator : public IApiRegister::ICreator
48 : {
49 0 : ApiObject* create(IApiEngine* e) override { return new T(e); }
50 0 : bool isNeedDelete() const override { return true; }
51 : };
52 :
53 : virtual void regApiCreator(const std::string& module, const std::string& api, ICreator* c) = 0;
54 : virtual void regApiSingltone(const std::string& module, const std::string& api, ApiObject* o) = 0;
55 : virtual std::pair<ApiObject*, bool /*is need delete*/> createApi(const std::string& api, IApiEngine* e) const = 0;
56 :
57 : virtual void regEnum(const char* uri, const char* name, const QMetaEnum& meta, EnumType type) = 0;
58 :
59 : template<typename T>
60 0 : void regApi(const std::string& module, const std::string& api)
61 : {
62 0 : regApiCreator(module, api, new ApiCreator<T>());
63 0 : }
64 :
65 : template<typename E>
66 0 : void regEnum(const char* uri, EnumType type = EnumType::String, const char* name = nullptr)
67 : {
68 0 : const QMetaEnum meta = QMetaEnum::fromType<E>();
69 0 : const char* ename = name ? name : meta.enumName();
70 0 : regEnum(uri, ename, meta, type);
71 0 : }
72 :
73 : struct GlobalEnum {
74 : std::string module;
75 : std::string name;
76 : QMetaEnum meta;
77 : EnumType type = EnumType::String;
78 : };
79 :
80 : virtual void regGlobalEnum(const std::string& module, const QMetaEnum& meta, EnumType type = EnumType::String,
81 : const std::string& name = "") = 0;
82 : virtual const std::vector<GlobalEnum>& globalEnums() const = 0;
83 :
84 : // dev
85 : struct Dump
86 : {
87 : enum class MethodType {
88 : Method = 0,
89 : Property
90 : };
91 :
92 : struct Arg {
93 : QString type;
94 : QString name;
95 : };
96 :
97 : struct Sig {
98 : QString name;
99 : QString retType;
100 : QList<Arg> args;
101 : };
102 :
103 : struct Method {
104 : MethodType type = MethodType::Method;
105 : Sig sig;
106 : QString doc;
107 : };
108 :
109 : struct Api {
110 : QString prefix;
111 : QList<Method> methods;
112 : };
113 :
114 : QList<Api> apis;
115 : };
116 :
117 : virtual Dump dump() const = 0;
118 : };
119 :
120 : template<class T>
121 : using ApiCreator = typename IApiRegister::ApiCreator<T>;
122 : }
123 :
124 : #endif // MUSE_API_IAPIREGISTER_H
|