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_ACTIONS_IACTIONSDISPATCHER_H
23 : #define MUSE_ACTIONS_IACTIONSDISPATCHER_H
24 :
25 : #include <functional>
26 :
27 : #include "modularity/imoduleinterface.h"
28 : #include "actiontypes.h"
29 : #include "global/async/channel.h"
30 :
31 : namespace muse::actions {
32 : class Actionable;
33 : class IActionsDispatcher : MODULE_CONTEXT_INTERFACE
34 : {
35 2 : INTERFACE_ID(IActionsDispatcher)
36 : public:
37 32 : ~IActionsDispatcher() = default;
38 :
39 : using ActionCallBack = std::function<void ()>;
40 : using ActionCallBackWithName = std::function<void (const ActionCode&)>;
41 : using ActionCallBackWithData = std::function<void (const ActionData& data)>;
42 : using ActionCallBackWithNameAndData = std::function<void (const ActionCode&, const ActionData& data)>;
43 : using ActionCallBackWithQuery = std::function<void (const ActionQuery&)>;
44 :
45 : virtual void dispatch(const ActionCode& actionCode) = 0;
46 : virtual void dispatch(const ActionCode& actionCode, const ActionData& data) = 0;
47 : virtual void dispatch(const ActionQuery& actionQuery) = 0;
48 :
49 : virtual async::Channel<ActionCode> preDispatch() const = 0;
50 : virtual async::Channel<ActionCode> postDispatch() const = 0;
51 : virtual async::Channel<ActionCode> notRegistered() const = 0;
52 :
53 : virtual void unReg(Actionable* client) = 0;
54 : virtual void reg(Actionable* client, const ActionCode& actionCode, const ActionCallBackWithNameAndData& call) = 0;
55 : virtual void reg(Actionable* client, const ActionQuery& actionQuery, const ActionCallBackWithQuery& call) = 0;
56 : virtual bool isReg(Actionable* client) const = 0;
57 : virtual ActionCodeList actionList() const = 0;
58 :
59 512 : void reg(Actionable* client, const ActionCode& action, const ActionCallBack& call)
60 : {
61 512 : reg(client, action, [call](const ActionCode&, const ActionData&) { call(); });
62 512 : }
63 :
64 : void reg(Actionable* client, const ActionCode& action, const ActionCallBackWithName& call)
65 : {
66 : reg(client, action, [call](const ActionCode& action, const ActionData&) { call(action); });
67 : }
68 :
69 0 : void reg(Actionable* client, const ActionCode& action, const ActionCallBackWithData& call)
70 : {
71 0 : reg(client, action, [call](const ActionCode&, const ActionData& data) { call(data); });
72 0 : }
73 :
74 : template<typename T>
75 : void reg(Actionable* client, const ActionCode& action, T* caller, void (T::* func)(const ActionCode& action))
76 : {
77 : reg(client, action, [caller, func](const ActionCode& action) { (caller->*func)(action); });
78 : }
79 :
80 : template<typename T>
81 : void reg(Actionable* client, const ActionCode& action, T* caller, void (T::* func)())
82 : {
83 : reg(client, action, [caller, func](const ActionCode&) { (caller->*func)(); });
84 : }
85 :
86 : template<typename T>
87 : void reg(Actionable* client, const ActionCode& action, T* caller, void (T::* func)(const ActionCode& action,
88 : const ActionData& data))
89 : {
90 : reg(client, action, [caller, func](const ActionCode& a, const ActionData& data) { (caller->*func)(a, data); });
91 : }
92 :
93 : template<typename T>
94 : void reg(Actionable* client, const ActionCode& action, T* caller, void (T::* func)(const ActionData& data))
95 : {
96 : reg(client, action, [caller, func](const ActionCode&, const ActionData& data) { (caller->*func)(data); });
97 : }
98 :
99 : // as uri query
100 : void reg(Actionable* client, const ActionQuery& query, const ActionCallBack& call)
101 : {
102 : reg(client, query, [call](const ActionQuery&) { call(); });
103 : }
104 :
105 : template<typename T>
106 : void reg(Actionable* client, const ActionQuery& query, T* caller, void (T::* func)(const ActionQuery& query))
107 : {
108 : reg(client, query, [caller, func](const ActionQuery& query) { (caller->*func)(query); });
109 : }
110 :
111 : template<typename T>
112 : void reg(Actionable* client, const ActionQuery& query, T* caller, void (T::* func)())
113 : {
114 : reg(client, query, [caller, func](const ActionQuery&) { (caller->*func)(); });
115 : }
116 : };
117 : }
118 :
119 : #endif // MUSE_ACTIONS_IACTIONSDISPATCHER_H
|