userver: userver/utils/fast_scope_guard.hpp Source File
Loading...
Searching...
No Matches
fast_scope_guard.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <type_traits>
4#include <utility>
5
6/// @file userver/utils/fast_scope_guard.hpp
7/// @brief @copybrief utils::FastScopeGuard
8
9USERVER_NAMESPACE_BEGIN
10
11namespace utils {
12
13/// @ingroup userver_universal userver_containers
14///
15/// @brief a helper class to perform actions on scope exit
16///
17/// The lambda argument must be explicitly marked as `noexcept`. Only use
18/// FastScopeGuard if it's proven that the lambda body is `noexcept`, otherwise
19/// use ScopeGuard.
20///
21/// Usage example:
22/// @snippet universal/src/utils/fast_scope_guard_test.cpp FastScopeGuard
23///
24/// @see ScopeGuard for type-erasure and throwing functor support
25template <typename Callback>
26class FastScopeGuard final {
27public:
28 static_assert(std::is_nothrow_move_constructible_v<Callback>);
29
30 static_assert(
31 std::is_nothrow_invocable_v<Callback&&>,
32 "If the functions called in the body of the lambda are all 'noexcept', "
33 "please mark the lambda itself as 'noexcept'. If however, the contents "
34 "are not 'noexcept', use 'ScopeGuard' instead of 'FastScopeGuard'."
35 );
36
37 static_assert(std::is_void_v<std::invoke_result_t<Callback&&>>, "Return type of Callback function should be void");
38
39 static_assert(std::is_nothrow_destructible_v<Callback>, "Callback function destructor should be noexcept");
40
41 constexpr explicit FastScopeGuard(Callback callback) noexcept : callback_(std::move(callback)) {}
42
43 constexpr FastScopeGuard(FastScopeGuard&& other) noexcept
44 : callback_(std::move(other.callback_)), is_active_(std::exchange(other.is_active_, false)) {}
45
46 constexpr FastScopeGuard& operator=(FastScopeGuard&& other) noexcept {
47 if (this != &other) {
48 callback_ = std::move(other.callback_);
49 is_active_ = std::exchange(other.is_active_, false);
50 }
51 return *this;
52 }
53
54 ~FastScopeGuard() {
55 if (is_active_) std::move(callback_)();
56 }
57
58 constexpr void Release() noexcept { is_active_ = false; }
59
60private:
61 Callback callback_;
62
63 // should be optimized out if 'Release' is not called
64 bool is_active_{true};
65};
66
67} // namespace utils
68
69USERVER_NAMESPACE_END