ThreadSchedule 3.0.0
Modern C++ thread management library
Loading...
Searching...
No Matches
scope_exit.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <type_traits>
4#include <utility>
5
7{
8
9template <typename F>
11{
12public:
13 explicit scope_exit(F&& function) noexcept(std::is_nothrow_move_constructible_v<F>) : function_(std::move(function))
14 {
15 }
16
17 scope_exit(scope_exit const&) = delete;
18 auto operator=(scope_exit const&) -> scope_exit& = delete;
20 auto operator=(scope_exit&&) -> scope_exit& = delete;
21
22 ~scope_exit() noexcept
23 {
24 if (active_)
25 function_();
26 }
27
28 void
29 release() noexcept
30 {
31 active_ = false;
32 }
33
34private:
35 F function_;
36 bool active_{ true };
37};
38
39template <typename F>
40[[nodiscard]] auto
42{
43 return scope_exit<std::decay_t<F>>(std::forward<F>(function));
44}
45
46} // namespace threadschedule::detail
scope_exit(scope_exit const &)=delete
scope_exit(scope_exit &&)=delete
scope_exit(F &&function) noexcept(std::is_nothrow_move_constructible_v< F >)
auto operator=(scope_exit &&) -> scope_exit &=delete
auto operator=(scope_exit const &) -> scope_exit &=delete
Aggregates multiple thread_registry_backend instances into a single queryable view.
auto make_scope_exit(F &&function) -> scope_exit< std::decay_t< F > >