diff --git a/tdutils/td/utils/ScopeGuard.h b/tdutils/td/utils/ScopeGuard.h index 76573605..541a0ce2 100644 --- a/tdutils/td/utils/ScopeGuard.h +++ b/tdutils/td/utils/ScopeGuard.h @@ -10,45 +10,65 @@ #include #include +#include +#include namespace td { - -namespace detail { -template -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 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 +std::unique_ptr create_lamda_guard(F &&f) { + return std::make_unique>(std::forward(f)); +} +template +std::shared_ptr create_shared_lamda_guard(F &&f) { + return std::make_shared>(std::forward(f)); +} enum class ScopeExit {}; - template auto operator+(ScopeExit, FunctionT &&func) { - return detail::ScopeGuard>(std::forward(func)); + return LambdaGuard>(std::forward(func)); } } // namespace td diff --git a/tdutils/td/utils/port/thread_local.cpp b/tdutils/td/utils/port/thread_local.cpp index de24fe46..aa4e3714 100644 --- a/tdutils/td/utils/port/thread_local.cpp +++ b/tdutils/td/utils/port/thread_local.cpp @@ -13,11 +13,11 @@ namespace td { namespace detail { static TD_THREAD_LOCAL int32 thread_id_; -static TD_THREAD_LOCAL std::vector> *thread_local_destructors; +static TD_THREAD_LOCAL std::vector> *thread_local_destructors; -void add_thread_local_destructor(std::unique_ptr destructor) { +void add_thread_local_destructor(std::unique_ptr destructor) { if (thread_local_destructors == nullptr) { - thread_local_destructors = new std::vector>(); + thread_local_destructors = new std::vector>(); } thread_local_destructors->push_back(std::move(destructor)); } diff --git a/tdutils/td/utils/port/thread_local.h b/tdutils/td/utils/port/thread_local.h index 3b98a2f3..d28fb7b5 100644 --- a/tdutils/td/utils/port/thread_local.h +++ b/tdutils/td/utils/port/thread_local.h @@ -9,6 +9,7 @@ #include "td/utils/port/config.h" #include "td/utils/common.h" +#include "td/utils/ScopeGuard.h" #include #include @@ -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 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 -std::unique_ptr create_destructor(F &&f) { - return std::make_unique>(std::forward(f)); -} - -void add_thread_local_destructor(std::unique_ptr destructor); +void add_thread_local_destructor(std::unique_ptr destructor); template void do_init_thread_local(P &raw_ptr, ArgsT &&... args) { auto ptr = std::make_unique(std::forward(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; }));