LibrePCB Developers Documentation
scopeguard.h
Go to the documentation of this file.
1/*
2 * LibrePCB - Professional EDA for everyone!
3 * Copyright (C) 2016 The LibrePCB developers
4 * https://librepcb.org/
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef LIBREPCB_CORE_SCOPEGUARD_H
21#define LIBREPCB_CORE_SCOPEGUARD_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include <QtCore>
27
28#include <utility>
29
30/*******************************************************************************
31 * Namespace / Forward Declarations
32 ******************************************************************************/
33namespace librepcb {
34
35/*******************************************************************************
36 * Class ScopeGuardBase
37 ******************************************************************************/
42public:
43 ScopeGuardBase() noexcept : mActive(true) {}
44
45 ScopeGuardBase(ScopeGuardBase&& rhs) noexcept : mActive(rhs.mActive) {
46 rhs.dismiss();
47 }
48
52 void dismiss() noexcept { mActive = false; }
53
54protected:
59 ~ScopeGuardBase() = default;
60 bool mActive;
61};
62
63/*******************************************************************************
64 * Class ScopeGuard
65 ******************************************************************************/
70template <class Fun>
71class ScopeGuard final : public ScopeGuardBase {
72public:
73 ScopeGuard() = delete;
74 ScopeGuard(const ScopeGuard&) = delete;
75
76 ScopeGuard(Fun f) noexcept : mF(std::move(f)) {}
77
78 ScopeGuard(ScopeGuard&& rhs) noexcept
79 : ScopeGuardBase(std::move(rhs)), mF(std::move(rhs.mF)) {}
80
84 ~ScopeGuard() noexcept {
85 if (mActive) {
86 try {
87 mF();
88 } catch (const std::exception& e) {
89 qFatal("Cleanup function threw an exception: %s", e.what());
90 }
91 }
92 }
93
94 ScopeGuard& operator=(const ScopeGuard&) = delete;
95
96private:
97 Fun mF;
98};
99
103template <class Fun>
105 return ScopeGuard<Fun>(std::move(f));
106}
107
108/*******************************************************************************
109 * End of File
110 ******************************************************************************/
111
112} // namespace librepcb
113
114#endif
Definition: scopeguard.h:41
ScopeGuardBase() noexcept
Definition: scopeguard.h:43
bool mActive
Definition: scopeguard.h:60
void dismiss() noexcept
Definition: scopeguard.h:52
ScopeGuardBase(ScopeGuardBase &&rhs) noexcept
Definition: scopeguard.h:45
Definition: scopeguard.h:71
ScopeGuard(const ScopeGuard &)=delete
ScopeGuard(Fun f) noexcept
Definition: scopeguard.h:76
ScopeGuard(ScopeGuard &&rhs) noexcept
Definition: scopeguard.h:78
Fun mF
Definition: scopeguard.h:97
~ScopeGuard() noexcept
Definition: scopeguard.h:84
ScopeGuard & operator=(const ScopeGuard &)=delete
Definition: occmodel.cpp:77
ScopeGuard< Fun > scopeGuard(Fun f)
Definition: scopeguard.h:104