flexiblesusy is hosted by Hepforge, IPPP Durham
FlexibleSUSY
raii.hpp
Go to the documentation of this file.
1// ====================================================================
2// This file is part of FlexibleSUSY.
3//
4// FlexibleSUSY is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published
6// by the Free Software Foundation, either version 3 of the License,
7// or (at your option) any later version.
8//
9// FlexibleSUSY is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with FlexibleSUSY. If not, see
16// <http://www.gnu.org/licenses/>.
17// ====================================================================
18
19#ifndef RAII_H
20#define RAII_H
21
22#include <utility>
23
24namespace flexiblesusy {
25
30template <typename T>
31class RAII_save {
32public:
33 RAII_save(T& var_) noexcept : var(var_), value(var_) {}
34 RAII_save(const RAII_save&) = delete;
35 RAII_save(RAII_save&&) noexcept = default;
37 RAII_save& operator=(const RAII_save&) = delete;
38 RAII_save& operator=(RAII_save&& other) noexcept = default;
39
40private:
41 T& var;
42 T value{};
43};
44
45template <typename T>
47{
48 return RAII_save<T>(var);
49}
50
55template <typename F>
57public:
58 RAII_guard(F f_) : clean_up(std::move(f_)) {}
59 RAII_guard(const RAII_guard&) = delete;
60 RAII_guard(RAII_guard&&) noexcept = default;
62 RAII_guard& operator=(const RAII_guard&) = delete;
63 RAII_guard& operator=(RAII_guard&&) noexcept = default;
64private:
66};
67
68template <typename F>
70{
71 return RAII_guard<F>(std::move(f));
72}
73
74} // namespace flexiblesusy
75
76#endif
Carries out provided clean-up actions at destruction.
Definition: raii.hpp:56
RAII_guard & operator=(RAII_guard &&) noexcept=default
RAII_guard(const RAII_guard &)=delete
RAII_guard(RAII_guard &&) noexcept=default
RAII_guard & operator=(const RAII_guard &)=delete
Saves value of variable and restores it at destruction.
Definition: raii.hpp:31
RAII_save(T &var_) noexcept
Definition: raii.hpp:33
RAII_save(const RAII_save &)=delete
RAII_save & operator=(const RAII_save &)=delete
RAII_save & operator=(RAII_save &&other) noexcept=default
RAII_save(RAII_save &&) noexcept=default
std::complex< double > f(double tau) noexcept
constexpr RAII_guard< F > make_raii_guard(F f)
Definition: raii.hpp:69
constexpr RAII_save< T > make_raii_save(T &var)
Definition: raii.hpp:46