Commit | Line | Data |
---|---|---|
b6bbb1d6 JG |
1 | /* |
2 | * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: LGPL-2.1-only | |
5 | * | |
6 | */ | |
7 | ||
8 | #ifndef LTTNG_SCOPE_EXIT_H | |
9 | #define LTTNG_SCOPE_EXIT_H | |
10 | ||
11 | #include <utility> | |
12 | ||
13 | namespace lttng { | |
14 | ||
15 | namespace details { | |
16 | /* Is operator() of InvocableType is marked as noexcept? */ | |
17 | template <typename InvocableType> | |
18 | struct is_invocation_noexcept | |
19 | : std::integral_constant<bool, noexcept((std::declval<InvocableType>())())> { | |
20 | }; | |
21 | } /* namespace details. */ | |
22 | ||
23 | /* | |
24 | * Generic utility to run a lambda (or any other invocable object) when leaving | |
25 | * a scope. | |
26 | * | |
27 | * Notably, this makes it easy to specify an action (e.g. restore a context) | |
28 | * that must occur at the end of a function or roll-back operations in an | |
29 | * exception-safe way. | |
30 | */ | |
31 | template <typename ScopeExitInvocableType> | |
32 | class scope_exit { | |
33 | public: | |
34 | /* | |
35 | * Since ScopeExitInvocableType will be invoked in the destructor, it | |
36 | * must be `noexcept` lest we anger the undefined behaviour gods. | |
37 | */ | |
38 | static_assert(details::is_invocation_noexcept<ScopeExitInvocableType>::value, | |
cd9adb8b | 39 | "scope_exit requires a noexcept invocable type"); |
b6bbb1d6 JG |
40 | |
41 | explicit scope_exit(ScopeExitInvocableType&& scope_exit_callable) : | |
cd9adb8b | 42 | _on_scope_exit{ std::forward<ScopeExitInvocableType>(scope_exit_callable) } |
b6bbb1d6 JG |
43 | { |
44 | } | |
45 | ||
cd9adb8b JG |
46 | scope_exit(scope_exit&& rhs) noexcept : |
47 | _on_scope_exit{ std::move(rhs._on_scope_exit) }, _armed{ rhs._armed } | |
b6bbb1d6 JG |
48 | { |
49 | /* Don't invoke ScopeExitInvocableType for the moved-from copy. */ | |
50 | rhs.disarm(); | |
51 | } | |
52 | ||
53 | /* | |
54 | * The copy constructor is disabled to prevent the action from being | |
55 | * executed twice should a copy be performed accidentaly. | |
56 | * | |
57 | * The move-constructor is present to enable make_scope_exit() but to | |
58 | * also propagate the scope_exit to another scope, should it be needed. | |
59 | */ | |
60 | scope_exit(const scope_exit&) = delete; | |
9d89db29 JG |
61 | scope_exit& operator=(const scope_exit&) = delete; |
62 | scope_exit& operator=(scope_exit&&) = delete; | |
b6bbb1d6 JG |
63 | scope_exit() = delete; |
64 | ||
65 | void disarm() noexcept | |
66 | { | |
67 | _armed = false; | |
68 | } | |
69 | ||
70 | ~scope_exit() | |
71 | { | |
72 | if (_armed) { | |
73 | _on_scope_exit(); | |
74 | } | |
75 | } | |
76 | ||
77 | private: | |
78 | ScopeExitInvocableType _on_scope_exit; | |
79 | bool _armed = true; | |
80 | }; | |
81 | ||
82 | template <typename ScopeExitInvocableType> | |
83 | scope_exit<ScopeExitInvocableType> make_scope_exit(ScopeExitInvocableType&& scope_exit_callable) | |
84 | { | |
85 | return scope_exit<ScopeExitInvocableType>( | |
cd9adb8b | 86 | std::forward<ScopeExitInvocableType>(scope_exit_callable)); |
b6bbb1d6 JG |
87 | } |
88 | ||
89 | } /* namespace lttng */ | |
90 | ||
91 | #endif /* LTTNG_SCOPE_EXIT_H */ |