userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
scope_guard.hpp
Go to the documentation of this file.
1
#pragma once
2
5
6
#include <exception>
7
#include <functional>
8
9
#include <
userver/logging/log.hpp
>
10
#include <
userver/utils/assert.hpp
>
11
12
USERVER_NAMESPACE_BEGIN
13
14
namespace
utils
{
15
28
class
ScopeGuard final {
29
public
:
30
using
Callback = std::function<void()>;
31
32
explicit
ScopeGuard(Callback callback)
33
: callback_(std::move(callback)),
34
exceptions_on_enter_(std::uncaught_exceptions())
35
{}
36
37
ScopeGuard(
const
ScopeGuard&) =
delete
;
38
ScopeGuard(ScopeGuard&&) =
delete
;
39
40
ScopeGuard& operator=(
const
ScopeGuard&) =
delete
;
41
ScopeGuard& operator=(ScopeGuard&&) =
delete
;
42
43
~ScopeGuard()
noexcept
(
false
) {
44
if
(!callback_) {
45
return
;
46
}
47
48
if
(std::uncaught_exceptions() != exceptions_on_enter_) {
49
// keep all exceptions inside the destructor to avoid std::terminate
50
try
{
51
callback_();
52
}
catch
(
const
std::exception& e) {
53
UASSERT_MSG
(
false
,
"exception is thrown during stack unwinding"
);
54
LOG_ERROR
() <<
"Exception is thrown during stack unwinding - ignoring: "
<< e;
55
}
56
}
else
{
57
// safe to throw
58
callback_();
59
}
60
}
61
62
void
Release()
noexcept
{ callback_ = {}; }
63
64
private
:
65
Callback callback_;
66
const
int
exceptions_on_enter_;
67
};
68
69
}
// namespace utils
70
71
USERVER_NAMESPACE_END
userver
utils
scope_guard.hpp
Generated on
for userver by
Doxygen
1.17.0