LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
scopeguardlist.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_SCOPEGUARDLIST_H
21 #define LIBREPCB_SCOPEGUARDLIST_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 
27 #include "scopeguard.h"
28 
29 #include <algorithm>
30 #include <functional>
31 #include <vector>
32 
33 /*******************************************************************************
34  * Namespace / Forward Declarations
35  ******************************************************************************/
36 namespace librepcb {
37 
38 /*******************************************************************************
39  * Class ScopeGuardList
40  ******************************************************************************/
46 class ScopeGuardList final : public ScopeGuardBase {
47 public:
48  ScopeGuardList() = default;
49 
50  ScopeGuardList(size_t size) noexcept : ScopeGuardBase(), mScopeGuards() {
51  mScopeGuards.reserve(size);
52  }
53 
55  : ScopeGuardBase(std::move(rhs)),
56  mScopeGuards(std::move(rhs.mScopeGuards)) {}
57 
58  ScopeGuardList(const ScopeGuardList&) = delete;
59  ScopeGuardList& operator=(const ScopeGuardList&) = delete;
60 
64  ~ScopeGuardList() noexcept {
65  if (mActive) {
66  for (auto scopeGuard = mScopeGuards.rbegin();
67  scopeGuard != mScopeGuards.rend(); ++scopeGuard) {
68  // skip empty functions
69  if (!*scopeGuard) continue;
70  try {
71  (*scopeGuard)();
72  } catch (const std::exception& e) {
73  qFatal("Cleanup function threw an exception: %s", e.what());
74  }
75  }
76  }
77  }
78 
82  template <class Fun>
83  void add(Fun f) {
84  mScopeGuards.emplace_back(std::move(f));
85  }
86 
87 private:
88  std::vector<std::function<void()>> mScopeGuards;
89 };
90 
91 } // namespace librepcb
92 
93 #endif // LIBREPCB_SCOPEGUARDLIST_H
ScopeGuard< Fun > scopeGuard(Fun f)
Definition: scopeguard.h:104
Keeps a list of functions to call.
Definition: scopeguardlist.h:46
Definition: scopeguard.h:41
ScopeGuardBase() noexcept
Definition: scopeguard.h:43
ScopeGuardList(size_t size) noexcept
Definition: scopeguardlist.h:50
ScopeGuardList & operator=(const ScopeGuardList &)=delete
ScopeGuardList(ScopeGuardList &&rhs) noexcept
Definition: scopeguardlist.h:54
std::vector< std::function< void()> > mScopeGuards
Definition: scopeguardlist.h:88
void add(Fun f)
Definition: scopeguardlist.h:83
~ScopeGuardList() noexcept
Definition: scopeguardlist.h:64
bool mActive
Definition: scopeguard.h:60