GM2Calc 2.3.0
Loading...
Searching...
No Matches
gm2_raii.hpp
Go to the documentation of this file.
1// ====================================================================
2// This file is part of GM2Calc.
3//
4// GM2Calc 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// GM2Calc 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 GM2Calc. If not, see
16// <http://www.gnu.org/licenses/>.
17// ====================================================================
18
19#ifndef GM2_RAII_HPP
20#define GM2_RAII_HPP
21
22namespace gm2calc {
23
24/**
25 * @class RAII_save
26 * @brief Saves value of variable and restores it at destruction
27 */
28template <typename T>
29class RAII_save {
30public:
31 explicit RAII_save(T& var_) noexcept : var(var_), value(var_) {}
32 RAII_save(const RAII_save&) = delete;
34 ~RAII_save() { var = value; }
35 RAII_save& operator=(const RAII_save&) = delete;
36 RAII_save& operator=(RAII_save&& other) noexcept = default;
37
38private:
39 T& var;
40 T value{};
41};
42
43template <typename T>
45{
46 return RAII_save<T>(var);
47}
48
49} // namespace gm2calc
50
51#endif
Saves value of variable and restores it at destruction.
Definition gm2_raii.hpp:29
RAII_save & operator=(const RAII_save &)=delete
RAII_save(T &var_) noexcept
Definition gm2_raii.hpp:31
RAII_save(RAII_save &&) noexcept=default
RAII_save(const RAII_save &)=delete
RAII_save & operator=(RAII_save &&other) noexcept=default
void svd_eigen(const Eigen::Matrix< Scalar, M, N > &m, Eigen::Array< Real,(((M)<(N)) ?(M) :(N)), 1 > &s, Eigen::Matrix< Scalar, M, M > *u, Eigen::Matrix< Scalar, N, N > *vh)
constexpr RAII_save< T > make_raii_save(T &var)
Definition gm2_raii.hpp:44