2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
2018-03-14 15:43:00 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <memory>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
2018-07-22 01:56:40 +02:00
|
|
|
|
2018-03-13 14:03:46 +01:00
|
|
|
class Guard {
|
|
|
|
public:
|
|
|
|
Guard() = default;
|
2023-05-05 12:51:19 +02:00
|
|
|
Guard(const Guard &) = delete;
|
|
|
|
Guard &operator=(const Guard &) = delete;
|
|
|
|
Guard(Guard &&) = default;
|
|
|
|
Guard &operator=(Guard &&) = default;
|
2018-03-13 14:03:46 +01:00
|
|
|
virtual ~Guard() = default;
|
|
|
|
virtual void dismiss() {
|
2018-03-14 15:43:00 +01:00
|
|
|
std::abort();
|
2018-03-13 14:03:46 +01:00
|
|
|
}
|
|
|
|
};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
template <class FunctionT>
|
2021-07-04 04:58:54 +02:00
|
|
|
class LambdaGuard final : public Guard {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2018-03-13 14:03:46 +01:00
|
|
|
explicit LambdaGuard(const FunctionT &func) : func_(func) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-03-13 14:03:46 +01:00
|
|
|
explicit LambdaGuard(FunctionT &&func) : func_(std::move(func)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-05-05 12:51:19 +02:00
|
|
|
LambdaGuard(const LambdaGuard &) = delete;
|
|
|
|
LambdaGuard &operator=(const LambdaGuard &) = delete;
|
2018-03-13 14:03:46 +01:00
|
|
|
LambdaGuard(LambdaGuard &&other) : func_(std::move(other.func_)), dismissed_(other.dismissed_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
other.dismissed_ = true;
|
|
|
|
}
|
2023-05-05 12:51:19 +02:00
|
|
|
LambdaGuard &operator=(LambdaGuard &&) = delete;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-10-18 18:26:14 +02:00
|
|
|
void dismiss() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
dismissed_ = true;
|
|
|
|
}
|
|
|
|
|
2021-10-18 18:26:14 +02:00
|
|
|
~LambdaGuard() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!dismissed_) {
|
|
|
|
func_();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FunctionT func_;
|
2018-03-13 14:03:46 +01:00
|
|
|
bool dismissed_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2018-03-13 14:03:46 +01:00
|
|
|
template <class F>
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<Guard> create_lambda_guard(F &&f) {
|
|
|
|
return make_unique<LambdaGuard<F>>(std::forward<F>(f));
|
2018-03-13 14:03:46 +01:00
|
|
|
}
|
|
|
|
template <class F>
|
2018-03-14 15:43:00 +01:00
|
|
|
std::shared_ptr<Guard> create_shared_lambda_guard(F &&f) {
|
2018-03-13 14:03:46 +01:00
|
|
|
return std::make_shared<LambdaGuard<F>>(std::forward<F>(f));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-03-13 14:03:46 +01:00
|
|
|
enum class ScopeExit {};
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class FunctionT>
|
|
|
|
auto operator+(ScopeExit, FunctionT &&func) {
|
2018-03-13 14:03:46 +01:00
|
|
|
return LambdaGuard<std::decay_t<FunctionT>>(std::forward<FunctionT>(func));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|
|
|
|
|
2020-02-26 16:15:19 +01:00
|
|
|
#define SCOPE_EXIT auto TD_CONCAT(SCOPE_EXIT_VAR_, __LINE__) = ::td::ScopeExit() + [&]
|