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_GLOBAL_DEFER_H
23 : #define MUSE_GLOBAL_DEFER_H
24 :
25 : #include <functional>
26 :
27 : namespace muse {
28 : struct Defer
29 : {
30 : template<class F>
31 7967 : Defer(F f)
32 7967 : : m_f(f) {}
33 :
34 7967 : ~Defer()
35 : {
36 7967 : if (m_f) {
37 7967 : m_f();
38 : }
39 7967 : }
40 :
41 : std::function<void()> m_f;
42 : };
43 : }
44 :
45 : #define CONCAT_IMPL(x, y) x##y
46 : #define MACRO_CONCAT(x, y) CONCAT_IMPL(x, y)
47 :
48 : #define DEFER const ::muse::Defer MACRO_CONCAT(defer_, __LINE__) = [&]()
49 :
50 : #endif // MUSE_GLOBAL_DEFER_H
|