TTKMusicPlayer  3.7.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 - 2024 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)
36  : m_fun(std::forward<Function>(f))
37  {
38 
39  }
40 
42  : m_fun(std::move(rhs.m_fun))
43  {
44 
45  }
46 
51  {
52  if(m_fun)
53  {
54  m_fun();
55  }
56  }
57 
59 
60 private:
61  Function m_fun;
62 
63 };
64 
65 
69 namespace TTK
70 {
71 enum class ScopeGuardOnExit { };
72 }
73 
74 
75 template <typename Function>
77 {
78  return _ScopeGuard<Function>(std::forward<Function>(fn));
79 }
80 
81 // Helper macro
82 #define defer const auto TTK_CAT(ext_exitBlock_, __LINE__) = TTK::ScopeGuardOnExit() + [&]()
83 
84 #endif // TTKDEFER_H
The class of the ttk defer module.
Definition: ttkdefer.h:29
Definition: ttkcompat.h:39
Function m_fun
Definition: ttkdefer.h:61
_ScopeGuard(Function &&f)
Definition: ttkdefer.h:35
_ScopeGuard< Function > operator+(TTK::ScopeGuardOnExit, Function &&fn)
Definition: ttkdefer.h:76
~_ScopeGuard()
Definition: ttkdefer.h:50
_ScopeGuard(_ScopeGuard &&rhs)
Definition: ttkdefer.h:41
ScopeGuardOnExit
Definition: ttkdefer.h:71
The namespace of the process utils.
Definition: ttkcompat.h:24
#define TTK_DISABLE_INIT_COPY(Class)
Definition: ttkqtglobal.h:147