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_SCOPEGUARD_H
21 #define LIBREPCB_SCOPEGUARD_H
22 
23 /*******************************************************************************
24  *  Includes
25  ******************************************************************************/
26 #include <QtCore>
27 
28 #include <utility>
29 
30 /*******************************************************************************
31  *  Namespace / Forward Declarations
32  ******************************************************************************/
33 namespace librepcb {
34 
35 /*******************************************************************************
36  *  Class ScopeGuardBase
37  ******************************************************************************/
38 /**
39  * Base class for ScopGuard mainly providing dismiss()
40  */
41 class ScopeGuardBase {
42 public:
ScopeGuardBase()43   ScopeGuardBase() noexcept : mActive(true) {}
44 
ScopeGuardBase(ScopeGuardBase && rhs)45   ScopeGuardBase(ScopeGuardBase&& rhs) noexcept : mActive(rhs.mActive) {
46     rhs.dismiss();
47   }
48 
49   /**
50    * Do not execute cleanup code
51    */
dismiss()52   void dismiss() noexcept { mActive = false; }
53 
54 protected:
55   /**
56    * Do not allow to delete a ScopeGuardBase directly. So we can avoid a
57    * virtual destructor.
58    */
59   ~ScopeGuardBase() = default;
60   bool mActive;
61 };
62 
63 /*******************************************************************************
64  *  Class ScopeGuard
65  ******************************************************************************/
66 /**
67  * Implementation of a ScopeGuard based on
68  * https://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C
69  */
70 template <class Fun>
71 class ScopeGuard final : public ScopeGuardBase {
72 public:
73   ScopeGuard() = delete;
74   ScopeGuard(const ScopeGuard&) = delete;
75 
ScopeGuard(Fun f)76   ScopeGuard(Fun f) noexcept : mF(std::move(f)) {}
77 
ScopeGuard(ScopeGuard && rhs)78   ScopeGuard(ScopeGuard&& rhs) noexcept
79     : ScopeGuardBase(std::move(rhs)), mF(std::move(rhs.mF)) {}
80 
81   /**
82    * Calls the attached cleanup function
83    */
~ScopeGuard()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 
96 private:
97   Fun mF;
98 };
99 
100 /**
101  * Create a ScopeGuard using argument deduction.
102  */
103 template <class Fun>
scopeGuard(Fun f)104 ScopeGuard<Fun> scopeGuard(Fun f) {
105   return ScopeGuard<Fun>(std::move(f));
106 }
107 
108 /*******************************************************************************
109  *  End of File
110  ******************************************************************************/
111 
112 }  // namespace librepcb
113 
114 #endif  // LIBREPCB_SCOPEGUARD_H
115