userver
C++ Async Framework
Toggle main menu visibility
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
9
USERVER_NAMESPACE_BEGIN
10
11
namespace
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
25
template
<
typename
Callback>
26
class
FastScopeGuard final {
27
public
:
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_) {
56
std::move(callback_)();
57
}
58
}
59
60
constexpr
void
Release()
noexcept
{ is_active_ =
false
; }
61
62
private
:
63
[[no_unique_address]] Callback callback_;
64
65
// should be optimized out if 'Release' is not called
66
bool
is_active_{
true
};
67
};
68
69
}
// namespace utils
70
71
USERVER_NAMESPACE_END
userver
utils
fast_scope_guard.hpp
Generated on
for userver by
Doxygen
1.17.0