tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
scope_guard.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2#ifndef TETL_SCOPE_SCOPE_GUARD_HPP
3#define TETL_SCOPE_SCOPE_GUARD_HPP
4
8
9namespace etl::detail {
10template <typename FuncT, typename PolicyT>
11struct scope_guard {
12public:
13 template <typename F>
14 explicit constexpr scope_guard(F&& f)
15 : _func{etl::forward<F>(f)}
16 {
17 }
18
19 constexpr scope_guard(scope_guard&& rhs) noexcept
20 : _func{etl::move(rhs._func)}
21 , _policy{etl::move(rhs._policy)}
22 {
23 }
24
25 constexpr ~scope_guard()
26 {
27 if (_policy) {
28 _func();
29 }
30 }
31
32 constexpr auto release() noexcept -> void { _policy.release(); }
33
34 scope_guard(scope_guard const&) = delete;
35 auto operator=(scope_guard const&) -> scope_guard& = delete;
36 auto operator=(scope_guard&&) -> scope_guard& = delete;
37
38private:
39 FuncT _func;
40 PolicyT _policy{};
41};
42
43struct scope_exit_impl {
44 constexpr scope_exit_impl() = default;
45
46 constexpr scope_exit_impl(scope_exit_impl&& rhs) noexcept
47 : should_execute{rhs.should_execute}
48 {
49 rhs.release();
50 }
51
52 constexpr auto release() noexcept -> void { should_execute = false; }
53
54 explicit constexpr operator bool() const noexcept { return should_execute; }
55
56 bool should_execute = true;
57};
58} // namespace etl::detail
59
60#endif // TETL_SCOPE_SCOPE_GUARD_HPP
constexpr auto move(InputIt first, InputIt last, OutputIt destination) -> OutputIt
Moves the elements in the range [first, last), to another range beginning at destination,...
Definition move.hpp:26
constexpr auto forward(remove_reference_t< T > &param) noexcept -> T &&
Forwards lvalues as either lvalues or as rvalues, depending on T. When t is a forwarding reference (a...
Definition forward.hpp:18