TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
scope_guard.hpp
Go to the documentation of this file.
1// Copyright (c) 2016-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2016-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018 Simons Foundation
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You may obtain a copy of the License at
16// https://www.gnu.org/licenses/gpl-3.0.txt
17//
18// Authors: Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include <utility>
28
29namespace triqs::utility {
30
35
40 template <class F> class scope_guard {
41 F f_;
42 bool active_;
43
44 public:
50 scope_guard(F f) : f_(std::move(f)), active_(true) {}
51
57 if (active_) f_();
58 }
59
63 void dismiss() { active_ = false; }
64
69 bool active() const { return active_; }
70
72 scope_guard() = delete;
73
75 scope_guard(const scope_guard &) = delete;
76
82 scope_guard(scope_guard &&rhs) noexcept : f_(std::move(rhs.f_)), active_(rhs.active_) { rhs.dismiss(); }
83
85 scope_guard &operator=(const scope_guard &) = delete;
86
96 using std::swap;
97 swap(f_, rhs.f_);
98 swap(active_, rhs.active_);
99 return *this;
100 }
101 };
102
112 template <class F> scope_guard<F> exec_at_scope_exit(F f) { return {std::move(f)}; }
113
123 template <class F> scope_guard<F> make_scope_guard(F f) { return {std::move(f)}; }
124
126
127 namespace detail {
128
129 // Tag type used by the SCOPE_EXIT macro to build a scope_guard via operator+.
130 enum class _scope_guard_on_exit {};
131
132 // Build a scope_guard from a callable, used to implement the SCOPE_EXIT macro.
133 template <typename F> scope_guard<F> operator+(_scope_guard_on_exit, F &&f) { return {std::forward<F>(f)}; }
134
135 } // namespace detail
136
141
142// Helper macro for TRIQS_CONCAT.
143#define TRIQS_CONCAT2(X1, X2) X1##X2
144
146#define TRIQS_CONCAT(X1, X2) TRIQS_CONCAT2(X1, X2)
147
148#ifdef __COUNTER__
150// NOLINTNEXTLINE
151#define SCOPE_EXIT auto TRIQS_CONCAT(SCOPE_EXIT_ANONYMOUS_VARIABLE, __COUNTER__) = triqs::utility::detail::_scope_guard_on_exit{} + [&]()
152#else
153#error "__COUNTER__ not defined for this compiler"
154#endif // __COUNTER__
155
157
158} // namespace triqs::utility
RAII wrapper that invokes a callable when it goes out of scope, unless dismissed.
scope_guard & operator=(const scope_guard &)=delete
Copy assignment is disabled: a guard owns a unique cleanup action.
scope_guard(scope_guard &&rhs) noexcept
Move constructor.
scope_guard(const scope_guard &)=delete
Copy construction is disabled: a guard owns a unique cleanup action.
bool active() const
Query whether the guard is still active.
scope_guard()=delete
Default construction is disabled: a guard must always wrap a callable.
scope_guard(F f)
Construct a guard from a callable.
scope_guard & operator=(scope_guard &&rhs) noexcept
Move assignment.
void dismiss()
Disable the guard so the stored callable is not invoked on destruction.
scope_guard< F > make_scope_guard(F f)
Build a scope_guard from a callable.
scope_guard< F > exec_at_scope_exit(F f)
Build a scope_guard from a callable.