Iros
 
Loading...
Searching...
No Matches
scope_exit.h
Go to the documentation of this file.
1#pragma once
2
5#include "di/meta/util.h"
6#include "di/util/exchange.h"
7#include "di/util/forward.h"
8#include "di/util/move.h"
9
10namespace di::util {
11template<concepts::InvocableTo<void> F>
12class [[nodiscard]] ScopeExit {
13public:
14 template<typename G>
16 constexpr explicit ScopeExit(G&& function) : m_function(util::forward<F>(function)) {}
17
18 constexpr ScopeExit(ScopeExit&& other)
20 : m_function(util::move(other.m_function)), m_released(util::exchange(other.m_released, true)) {}
21
22 constexpr ~ScopeExit() {
23 bool released = util::exchange(m_released, true);
24 if (!released) {
25 function::invoke(util::move(m_function));
26 }
27 }
28
29 auto operator=(ScopeExit&&) -> ScopeExit& = delete;
30
31 constexpr void release() { m_released = true; }
32
33private:
34 F m_function;
35 bool m_released { false };
36};
37
38template<typename F>
40}
41
42namespace di {
43using util::ScopeExit;
44}
Definition scope_exit.h:12
constexpr ScopeExit(G &&function)
Definition scope_exit.h:16
constexpr ScopeExit(ScopeExit &&other)
Definition scope_exit.h:18
constexpr void release()
Definition scope_exit.h:31
auto operator=(ScopeExit &&) -> ScopeExit &=delete
constexpr ~ScopeExit()
Definition scope_exit.h:22
Definition operations.h:11
Definition operations.h:43
Definition as_bool.h:8
constexpr auto invoke
Definition invoke.h:100
Definition vocab.h:96
constexpr auto exchange(T &object, U &&new_value) -> T
Definition exchange.h:8
ScopeExit(F) -> ScopeExit< F >
Definition zstring_parser.h:9