TTKMusicPlayer  4.2.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttkdefer.h
Go to the documentation of this file.
1 #ifndef TTKDEFER_H
2 #define TTKDEFER_H
3 
4 /***************************************************************************
5  * This file is part of the TTK Library Module project
6  * Copyright (C) 2015 - 2025 Greedysky Studio
7 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17 
18  * You should have received a copy of the GNU Lesser General Public License along
19  * with this program; If not, see <http://www.gnu.org/licenses/>.
20  ***************************************************************************/
21 
22 #include <type_traits>
23 #include "ttkmoduleexport.h"
24 
28 template <typename Function>
30 {
31 public:
35  _ScopeGuard(Function &&f) noexcept
36  : m_fun(std::forward<Function>(f))
37  {
38 
39  }
40 
41  _ScopeGuard(_ScopeGuard &&rhs) noexcept
42  : m_fun(std::move(rhs.m_fun))
43  {
44 
45  }
46 
50  ~_ScopeGuard() noexcept
51  {
52  m_fun();
53  }
54 
56 
57 private:
58  Function m_fun;
59 
60 };
61 
62 
66 namespace TTK
67 {
68 enum class ScopeGuardOnExit { };
69 }
70 
71 
72 template <typename Function>
74 {
75  return _ScopeGuard<Function>(std::forward<Function>(fn));
76 }
77 
78 // Helper macro
79 #define defer const auto TTK_CAT(ext_exitBlock_, __LINE__) = TTK::ScopeGuardOnExit() + [&]()
80 
81 #endif // TTKDEFER_H
_ScopeGuard(Function &&f) noexcept
Definition: ttkdefer.h:35
The class of the ttk defer module.
Definition: ttkdefer.h:29
~_ScopeGuard() noexcept
Definition: ttkdefer.h:50
Definition: ttkcompat.h:39
Function m_fun
Definition: ttkdefer.h:58
_ScopeGuard< Function > operator+(TTK::ScopeGuardOnExit, Function &&fn)
Definition: ttkdefer.h:73
ScopeGuardOnExit
Definition: ttkdefer.h:68
The namespace of the application object.
Definition: ttkcompat.h:24
_ScopeGuard(_ScopeGuard &&rhs) noexcept
Definition: ttkdefer.h:41
#define TTK_DISABLE_INIT_COPY(Class)
Definition: ttkqtglobal.h:159