tdutils: merge Destructor and ScopeGuard implementations
GitOrigin-RevId: b599ca74a5109491ce772cf995b7b6814b9cc7b1
This commit is contained in:
parent
52f45abf65
commit
c29f5e9432
@ -10,45 +10,65 @@
|
||||
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace td {
|
||||
|
||||
namespace detail {
|
||||
template <class FunctionT>
|
||||
class ScopeGuard {
|
||||
class Guard {
|
||||
public:
|
||||
explicit ScopeGuard(const FunctionT &func) : func_(func) {
|
||||
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() {
|
||||
abort();
|
||||
}
|
||||
explicit ScopeGuard(FunctionT &&func) : func_(std::move(func)) {
|
||||
};
|
||||
|
||||
template <class FunctionT>
|
||||
class LambdaGuard : public Guard {
|
||||
public:
|
||||
explicit LambdaGuard(const FunctionT &func) : func_(func) {
|
||||
}
|
||||
ScopeGuard(const ScopeGuard &other) = delete;
|
||||
ScopeGuard &operator=(const ScopeGuard &other) = delete;
|
||||
ScopeGuard(ScopeGuard &&other) : dismissed_(other.dismissed_), func_(std::move(other.func_)) {
|
||||
explicit LambdaGuard(FunctionT &&func) : func_(std::move(func)) {
|
||||
}
|
||||
LambdaGuard(const LambdaGuard &other) = delete;
|
||||
LambdaGuard &operator=(const LambdaGuard &other) = delete;
|
||||
LambdaGuard(LambdaGuard &&other) : func_(std::move(other.func_)), dismissed_(other.dismissed_) {
|
||||
other.dismissed_ = true;
|
||||
}
|
||||
ScopeGuard &operator=(ScopeGuard &&other) = delete;
|
||||
LambdaGuard &operator=(LambdaGuard &&other) = delete;
|
||||
|
||||
void dismiss() {
|
||||
dismissed_ = true;
|
||||
}
|
||||
|
||||
~ScopeGuard() {
|
||||
~LambdaGuard() {
|
||||
if (!dismissed_) {
|
||||
func_();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
bool dismissed_ = false;
|
||||
FunctionT func_;
|
||||
bool dismissed_ = false;
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
template <class F>
|
||||
std::unique_ptr<Guard> create_lamda_guard(F &&f) {
|
||||
return std::make_unique<LambdaGuard<F>>(std::forward<F>(f));
|
||||
}
|
||||
template <class F>
|
||||
std::shared_ptr<Guard> create_shared_lamda_guard(F &&f) {
|
||||
return std::make_shared<LambdaGuard<F>>(std::forward<F>(f));
|
||||
}
|
||||
|
||||
enum class ScopeExit {};
|
||||
|
||||
template <class FunctionT>
|
||||
auto operator+(ScopeExit, FunctionT &&func) {
|
||||
return detail::ScopeGuard<std::decay_t<FunctionT>>(std::forward<FunctionT>(func));
|
||||
return LambdaGuard<std::decay_t<FunctionT>>(std::forward<FunctionT>(func));
|
||||
}
|
||||
|
||||
} // namespace td
|
||||
|
@ -13,11 +13,11 @@ namespace td {
|
||||
namespace detail {
|
||||
|
||||
static TD_THREAD_LOCAL int32 thread_id_;
|
||||
static TD_THREAD_LOCAL std::vector<std::unique_ptr<Destructor>> *thread_local_destructors;
|
||||
static TD_THREAD_LOCAL std::vector<std::unique_ptr<Guard>> *thread_local_destructors;
|
||||
|
||||
void add_thread_local_destructor(std::unique_ptr<Destructor> destructor) {
|
||||
void add_thread_local_destructor(std::unique_ptr<Guard> destructor) {
|
||||
if (thread_local_destructors == nullptr) {
|
||||
thread_local_destructors = new std::vector<std::unique_ptr<Destructor>>();
|
||||
thread_local_destructors = new std::vector<std::unique_ptr<Guard>>();
|
||||
}
|
||||
thread_local_destructors->push_back(std::move(destructor));
|
||||
}
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include "td/utils/port/config.h"
|
||||
|
||||
#include "td/utils/common.h"
|
||||
#include "td/utils/ScopeGuard.h"
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
@ -42,47 +43,14 @@ void set_thread_id(int32 id);
|
||||
int32 get_thread_id();
|
||||
|
||||
namespace detail {
|
||||
|
||||
class Destructor {
|
||||
public:
|
||||
Destructor() = default;
|
||||
Destructor(const Destructor &other) = delete;
|
||||
Destructor &operator=(const Destructor &other) = delete;
|
||||
Destructor(Destructor &&other) = default;
|
||||
Destructor &operator=(Destructor &&other) = default;
|
||||
virtual ~Destructor() = default;
|
||||
};
|
||||
|
||||
template <class F>
|
||||
class LambdaDestructor : public Destructor {
|
||||
public:
|
||||
explicit LambdaDestructor(F &&f) : f_(std::move(f)) {
|
||||
}
|
||||
LambdaDestructor(const LambdaDestructor &other) = delete;
|
||||
LambdaDestructor &operator=(const LambdaDestructor &other) = delete;
|
||||
LambdaDestructor(LambdaDestructor &&other) = default;
|
||||
LambdaDestructor &operator=(LambdaDestructor &&other) = default;
|
||||
~LambdaDestructor() override {
|
||||
f_();
|
||||
}
|
||||
|
||||
private:
|
||||
F f_;
|
||||
};
|
||||
|
||||
template <class F>
|
||||
std::unique_ptr<Destructor> create_destructor(F &&f) {
|
||||
return std::make_unique<LambdaDestructor<F>>(std::forward<F>(f));
|
||||
}
|
||||
|
||||
void add_thread_local_destructor(std::unique_ptr<Destructor> destructor);
|
||||
void add_thread_local_destructor(std::unique_ptr<Guard> destructor);
|
||||
|
||||
template <class T, class P, class... ArgsT>
|
||||
void do_init_thread_local(P &raw_ptr, ArgsT &&... args) {
|
||||
auto ptr = std::make_unique<T>(std::forward<ArgsT>(args)...);
|
||||
raw_ptr = ptr.get();
|
||||
|
||||
detail::add_thread_local_destructor(detail::create_destructor([ptr = std::move(ptr), &raw_ptr]() mutable {
|
||||
detail::add_thread_local_destructor(create_lamda_guard([ptr = std::move(ptr), &raw_ptr]() mutable {
|
||||
ptr.reset();
|
||||
raw_ptr = nullptr;
|
||||
}));
|
||||
|
Loading…
Reference in New Issue
Block a user