opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
scope_exit.h
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <type_traits>
5#include <utility>
6
7namespace osc
8{
14 template<std::invocable EF>
16 public:
18 template<typename Fn>
19 requires (
20 std::destructible<EF> and
21 std::is_constructible_v<EF, Fn> and
22 not std::same_as<std::remove_cvref_t<Fn>, ScopeExit>
23 )
24 explicit ScopeExit(Fn&& fn) : exit_function_{std::forward<Fn>(fn)} {}
25
26 ScopeExit(const ScopeExit&) = delete;
27
33 requires (std::is_nothrow_move_constructible_v<EF>) :
34 exit_function_{std::move(tmp.exit_function_)},
35 is_active_{std::exchange(tmp.is_active_, false)}
36 {}
37
39 ScopeExit(ScopeExit&& tmp) noexcept(std::is_nothrow_copy_constructible_v<EF>)
40 requires (not std::is_nothrow_move_constructible_v<EF> and std::is_copy_constructible_v<EF>) :
41 exit_function_{tmp.exit_function_}, // NOLINT(performance-move-constructor-init)
42 is_active_{std::exchange(tmp.is_active_, false)}
43 {}
44
45 ScopeExit& operator=(const ScopeExit&) = delete;
47
49 {
50 if (is_active_) {
51 exit_function_();
52 }
53 }
54
59 void release() noexcept { is_active_ = false; }
60
61 private:
62 EF exit_function_;
63 bool is_active_ = true;
64 };
65
71 template<typename EF>
73}
Definition scope_exit.h:15
void release() noexcept
Definition scope_exit.h:59
ScopeExit(const ScopeExit &)=delete
ScopeExit(ScopeExit &&tmp) noexcept
Definition scope_exit.h:32
ScopeExit & operator=(const ScopeExit &)=delete
ScopeExit & operator=(ScopeExit &&) noexcept=delete
ScopeExit(ScopeExit &&tmp) noexcept(std::is_nothrow_copy_constructible_v< EF >)
Definition scope_exit.h:39
ScopeExit(Fn &&fn)
Constructs a ScopeExit from a function or function object.
Definition scope_exit.h:24
Definition custom_decoration_generator.h:5
constexpr U to(T &&value)
Definition conversion.h:56
and
Definition algorithms.h:158