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 :
23 : #pragma once
24 :
25 : #include <cassert>
26 : #include <vector>
27 :
28 : #include "changednotify.h"
29 :
30 : #include "asyncable.h"
31 :
32 : namespace muse::async {
33 : template<typename T>
34 : class NotifyList : public std::vector<T>
35 : {
36 : public:
37 0 : NotifyList() {}
38 : NotifyList(const NotifyList&) = default;
39 : NotifyList(std::shared_ptr<ChangedNotify<T> > n)
40 : : m_notify(n) {}
41 : NotifyList(const std::vector<T>& l, std::shared_ptr<ChangedNotify<T> > n)
42 : : std::vector<T>(l), m_notify(n) {}
43 :
44 0 : void setNotify(std::shared_ptr<ChangedNotify<T> > n)
45 : {
46 0 : m_notify = n;
47 0 : }
48 :
49 : NotifyList<T>& operator =(const NotifyList<T>& nl)
50 : {
51 : std::vector<T>::operator=(nl);
52 : m_notify = nl.m_notify;
53 : return *this;
54 : }
55 :
56 : template<typename Call>
57 0 : void onChanged(Asyncable* caller, Call f, Asyncable::Mode mode = Asyncable::Mode::SetOnce)
58 : {
59 0 : assert(m_notify);
60 0 : if (!m_notify) {
61 0 : return;
62 : }
63 0 : m_notify->onChanged(caller, f, mode);
64 : }
65 :
66 : template<typename Call>
67 0 : void onItemChanged(Asyncable* caller, Call f, Asyncable::Mode mode = Asyncable::Mode::SetOnce)
68 : {
69 0 : assert(m_notify);
70 0 : if (!m_notify) {
71 0 : return;
72 : }
73 0 : m_notify->onItemChanged(caller, f, mode);
74 : }
75 :
76 : template<typename Call>
77 0 : void onItemAdded(Asyncable* caller, Call f, Asyncable::Mode mode = Asyncable::Mode::SetOnce)
78 : {
79 0 : assert(m_notify);
80 0 : if (!m_notify) {
81 0 : return;
82 : }
83 0 : m_notify->onItemAdded(caller, f, mode);
84 : }
85 :
86 : template<typename Call>
87 0 : void onItemRemoved(Asyncable* caller, Call f, Asyncable::Mode mode = Asyncable::Mode::SetOnce)
88 : {
89 0 : assert(m_notify);
90 0 : if (!m_notify) {
91 0 : return;
92 : }
93 0 : m_notify->onItemRemoved(caller, f, mode);
94 : }
95 :
96 : template<typename Call>
97 : void onItemReplaced(Asyncable* caller, Call f, Asyncable::Mode mode = Asyncable::Mode::SetOnce)
98 : {
99 : assert(m_notify);
100 : if (!m_notify) {
101 : return;
102 : }
103 : m_notify->onItemReplaced(caller, f, mode);
104 : }
105 :
106 0 : void disconnect(Asyncable* caller)
107 : {
108 0 : assert(m_notify);
109 0 : if (!m_notify) {
110 0 : return;
111 : }
112 0 : m_notify->disconnect(caller);
113 : }
114 :
115 : private:
116 : std::shared_ptr<ChangedNotify<T> > m_notify = nullptr;
117 : };
118 : }
|