diff options
author | nga <nga@yandex-team.ru> | 2022-02-10 16:48:09 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:09 +0300 |
commit | c2a1af049e9deca890e9923abe64fe6c59060348 (patch) | |
tree | b222e5ac2e2e98872661c51ccceee5da0d291e13 /library/cpp/messagebus/misc/weak_ptr.h | |
parent | 1f553f46fb4f3c5eec631352cdd900a0709016af (diff) | |
download | ydb-c2a1af049e9deca890e9923abe64fe6c59060348.tar.gz |
Restoring authorship annotation for <nga@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/messagebus/misc/weak_ptr.h')
-rw-r--r-- | library/cpp/messagebus/misc/weak_ptr.h | 166 |
1 files changed, 83 insertions, 83 deletions
diff --git a/library/cpp/messagebus/misc/weak_ptr.h b/library/cpp/messagebus/misc/weak_ptr.h index 46b6496d86..70fdeb0e2a 100644 --- a/library/cpp/messagebus/misc/weak_ptr.h +++ b/library/cpp/messagebus/misc/weak_ptr.h @@ -1,99 +1,99 @@ -#pragma once - -#include <util/generic/ptr.h> -#include <util/system/mutex.h> - -template <typename T> -struct TWeakPtr; - -template <typename TSelf> -struct TWeakRefCounted { +#pragma once + +#include <util/generic/ptr.h> +#include <util/system/mutex.h> + +template <typename T> +struct TWeakPtr; + +template <typename TSelf> +struct TWeakRefCounted { template <typename> friend struct TWeakPtr; -private: +private: struct TRef: public TAtomicRefCount<TRef> { - TMutex Mutex; - TSelf* Outer; - + TMutex Mutex; + TSelf* Outer; + TRef(TSelf* outer) : Outer(outer) { } - - void Release() { - TGuard<TMutex> g(Mutex); + + void Release() { + TGuard<TMutex> g(Mutex); Y_ASSERT(!!Outer); Outer = nullptr; - } - - TIntrusivePtr<TSelf> Get() { - TGuard<TMutex> g(Mutex); + } + + TIntrusivePtr<TSelf> Get() { + TGuard<TMutex> g(Mutex); Y_ASSERT(!Outer || Outer->RefCount() > 0); - return Outer; - } - }; - - TAtomicCounter Counter; - TIntrusivePtr<TRef> RefPtr; - -public: - TWeakRefCounted() - : RefPtr(new TRef(static_cast<TSelf*>(this))) + return Outer; + } + }; + + TAtomicCounter Counter; + TIntrusivePtr<TRef> RefPtr; + +public: + TWeakRefCounted() + : RefPtr(new TRef(static_cast<TSelf*>(this))) { } - - void Ref() { - Counter.Inc(); - } - - void UnRef() { - if (Counter.Dec() == 0) { - RefPtr->Release(); - - // drop is to prevent dtor from reading it - RefPtr.Drop(); - - delete static_cast<TSelf*>(this); - } - } - - void DecRef() { - Counter.Dec(); - } - - unsigned RefCount() const { - return Counter.Val(); - } -}; - -template <typename T> -struct TWeakPtr { -private: - typedef TIntrusivePtr<typename T::TRef> TRefPtr; - TRefPtr RefPtr; - -public: + + void Ref() { + Counter.Inc(); + } + + void UnRef() { + if (Counter.Dec() == 0) { + RefPtr->Release(); + + // drop is to prevent dtor from reading it + RefPtr.Drop(); + + delete static_cast<TSelf*>(this); + } + } + + void DecRef() { + Counter.Dec(); + } + + unsigned RefCount() const { + return Counter.Val(); + } +}; + +template <typename T> +struct TWeakPtr { +private: + typedef TIntrusivePtr<typename T::TRef> TRefPtr; + TRefPtr RefPtr; + +public: TWeakPtr() { } - - TWeakPtr(T* t) { - if (!!t) { - RefPtr = t->RefPtr; - } - } - - TWeakPtr(TIntrusivePtr<T> t) { - if (!!t) { - RefPtr = t->RefPtr; - } - } - - TIntrusivePtr<T> Get() { - if (!RefPtr) { + + TWeakPtr(T* t) { + if (!!t) { + RefPtr = t->RefPtr; + } + } + + TWeakPtr(TIntrusivePtr<T> t) { + if (!!t) { + RefPtr = t->RefPtr; + } + } + + TIntrusivePtr<T> Get() { + if (!RefPtr) { return nullptr; - } else { - return RefPtr->Get(); - } - } -}; + } else { + return RefPtr->Get(); + } + } +}; |