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