2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
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;
|
|
|
|
Guard(const Guard &other) = delete;
|
|
|
|
Guard &operator=(const Guard &other) = delete;
|
|
|
|
Guard(Guard &&other) = default;
|
|
|
|
Guard &operator=(Guard &&other) = default;
|
|
|
|
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
|
|
|
}
|
2018-03-13 14:03:46 +01:00
|
|
|
LambdaGuard(const LambdaGuard &other) = delete;
|
|
|
|
LambdaGuard &operator=(const LambdaGuard &other) = delete;
|
|
|
|
LambdaGuard(LambdaGuard &&other) : func_(std::move(other.func_)), dismissed_(other.dismissed_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
other.dismissed_ = true;
|
|
|
|
}
|
2018-03-13 14:03:46 +01:00
|
|
|
LambdaGuard &operator=(LambdaGuard &&other) = delete;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void dismiss() {
|
|
|
|
dismissed_ = true;
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:03:46 +01:00
|
|
|
~LambdaGuard() {
|
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() + [&]
|