9#include <userver/logging/log.hpp>
10#include <userver/utils/assert.hpp>
12USERVER_NAMESPACE_BEGIN
28class ScopeGuard
final {
30 using Callback = std::function<
void()>;
32 explicit ScopeGuard(Callback callback)
33 : callback_(std::move(callback)),
34 exceptions_on_enter_(std::uncaught_exceptions()) {}
36 ScopeGuard(
const ScopeGuard&) =
delete;
37 ScopeGuard(ScopeGuard&&) =
delete;
39 ScopeGuard& operator=(
const ScopeGuard&) =
delete;
40 ScopeGuard& operator=(ScopeGuard&&) =
delete;
42 ~ScopeGuard()
noexcept(
false) {
43 if (!callback_)
return;
45 if (std::uncaught_exceptions() != exceptions_on_enter_) {
49 }
catch (
const std::exception& e) {
50 UASSERT_MSG(
false,
"exception is thrown during stack unwinding");
51 LOG_ERROR() <<
"Exception is thrown during stack unwinding - ignoring: "
60 void Release()
noexcept { callback_ = {}; }
64 const int exceptions_on_enter_;