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_MI_RESOURCELOCKGUARD_H
23 : #define MUSE_MI_RESOURCELOCKGUARD_H
24 :
25 : #include <string>
26 : #include <memory>
27 :
28 : #include "imultiwindowsprovider.h"
29 :
30 : namespace muse::mi {
31 : class ReadResourceLockGuard
32 : {
33 : public:
34 395 : ReadResourceLockGuard(std::shared_ptr<IMultiWindowsProvider> provider, const std::string& name)
35 395 : : m_provider(provider), m_name(name)
36 : {
37 395 : if (m_provider) {
38 188 : m_provider->lockResource(m_name);
39 : }
40 395 : }
41 :
42 395 : ~ReadResourceLockGuard()
43 : {
44 395 : if (m_provider) {
45 188 : m_provider->unlockResource(m_name);
46 : }
47 395 : }
48 :
49 : protected:
50 : std::shared_ptr<IMultiWindowsProvider> m_provider = nullptr;
51 : std::string m_name;
52 : };
53 :
54 : class WriteResourceLockGuard : ReadResourceLockGuard
55 : {
56 : public:
57 254 : WriteResourceLockGuard(std::shared_ptr<IMultiWindowsProvider> provider, const std::string& name)
58 254 : : ReadResourceLockGuard(provider, name)
59 : {
60 254 : }
61 :
62 254 : ~WriteResourceLockGuard()
63 : {
64 254 : if (m_provider) {
65 93 : m_provider->notifyAboutResourceChanged(m_name);
66 : }
67 254 : }
68 : };
69 : }
70 :
71 : #endif // MUSE_MI_RESOURCELOCKGUARD_H
|