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)), exceptions_on_enter_(std::uncaught_exceptions()) {}
35 ScopeGuard(
const ScopeGuard&) =
delete;
36 ScopeGuard(ScopeGuard&&) =
delete;
38 ScopeGuard& operator=(
const ScopeGuard&) =
delete;
39 ScopeGuard& operator=(ScopeGuard&&) =
delete;
41 ~ScopeGuard()
noexcept(
false) {
42 if (!callback_)
return;
44 if (std::uncaught_exceptions() != exceptions_on_enter_) {
48 }
catch (
const std::exception& e) {
49 UASSERT_MSG(
false,
"exception is thrown during stack unwinding");
50 LOG_ERROR() <<
"Exception is thrown during stack unwinding - ignoring: " << e;
58 void Release()
noexcept { callback_ = {}; }
62 const int exceptions_on_enter_;