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 | 1f553f46fb4f3c5eec631352cdd900a0709016af (patch) | |
tree | a231fba2c03b440becaea6c86a2702d0bfb0336e /library/cpp/messagebus/misc/weak_ptr.h | |
parent | c4de7efdedc25b49cbea74bd589eecb61b55b60a (diff) | |
download | ydb-1f553f46fb4f3c5eec631352cdd900a0709016af.tar.gz |
Restoring authorship annotation for <nga@yandex-team.ru>. Commit 1 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 70fdeb0e2a..46b6496d86 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(); + } + } +}; |