ChimeraTK-cppext 01.05.02
All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
finally.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3#pragma once
4
5#include <utility>
6
7// Taken from GSL library, https://github.com/Microsoft/GSL/blob/master/include/gsl/gsl_util
8// Original Licence: MIT
9
10namespace cppext {
11
12 template<class F>
13 class final_act {
14 public:
15 explicit final_act(F f) noexcept : f_(std::move(f)), invoke_(true) {}
16
17 final_act(final_act&& other) noexcept : f_(std::move(other.f_)), invoke_(other.invoke_) { other.invoke_ = false; }
18
19 final_act(const final_act&) = delete;
20 final_act& operator=(const final_act&) = delete;
21
22 ~final_act() noexcept {
23 if(invoke_) f_();
24 }
25
26 private:
27 F f_;
28 bool invoke_;
29 };
30
31 template<class F>
32 inline final_act<F> finally(const F& f) noexcept {
33 return final_act<F>(f);
34 }
35
36 template<class F>
37 inline final_act<F> finally(F&& f) noexcept {
38 return final_act<F>(std::forward<F>(f));
39 }
40
41} // namespace cppext
final_act & operator=(const final_act &)=delete
~final_act() noexcept
Definition finally.hpp:22
final_act(final_act &&other) noexcept
Definition finally.hpp:17
final_act(const final_act &)=delete
final_act(F f) noexcept
Definition finally.hpp:15