summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/memory/weak_ptr.h
blob: d3598f62c74abdb142b724348955bdfddb44616e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#pragma once

#include "ref_counted.h"

#include <util/generic/hash.h>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

template <class T>
class TWeakPtr
{
public:
    using TUnderlying = T;
    using element_type = T;

    //! Default constructor.
    TWeakPtr() = default;

    //! Null constructor.
    TWeakPtr(std::nullptr_t);

    //! Constructor from an unqualified reference.
    /*!
     * Note that this constructor could be racy due to unsynchronized operations
     * on the object and on the counter.
     */
    explicit TWeakPtr(T* p) noexcept;

    //! Constructor from a strong reference.
    TWeakPtr(const TIntrusivePtr<T>& ptr) noexcept;

    //! Constructor from a strong reference with an upcast.
    template <class U, class = typename std::enable_if_t<std::is_convertible_v<U*, T*>>>
    TWeakPtr(const TIntrusivePtr<U>& ptr) noexcept;

    //! Copy constructor.
    TWeakPtr(const TWeakPtr& other) noexcept;

    //! Copy constructor with an upcast.
    template <class U, class = typename std::enable_if_t<std::is_convertible_v<U*, T*>>>
    TWeakPtr(const TWeakPtr<U>& other) noexcept;

    //! Move constructor.
    TWeakPtr(TWeakPtr&& other) noexcept;

    //! Move constructor with an upcast.
    template <class U, class = typename std::enable_if_t<std::is_convertible_v<U*, T*>>>
    TWeakPtr(TWeakPtr<U>&& other) noexcept;

    //! Destructor.
    ~TWeakPtr();

    //! Assignment operator from a strong reference.
    template <class U>
    TWeakPtr& operator=(const TIntrusivePtr<U>& ptr) noexcept;

    //! Copy assignment operator.
    TWeakPtr& operator=(const TWeakPtr& other) noexcept;

    //! Copy assignment operator with an upcast.
    template <class U>
    TWeakPtr& operator=(const TWeakPtr<U>& other) noexcept;

    //! Move assignment operator.
    TWeakPtr& operator=(TWeakPtr&& other) noexcept;

    //! Move assignment operator with an upcast.
    template <class U>
    TWeakPtr& operator=(TWeakPtr<U>&& other) noexcept;

    //! Drop the pointer.
    void Reset(); // noexcept

    //! Replace the pointer with a specified one.
    void Reset(T* p); // noexcept

    //! Replace the pointer with a specified one.
    template <class U>
    void Reset(const TIntrusivePtr<U>& ptr); // noexcept

    //! Swap the pointer with the other one.
    void Swap(TWeakPtr& other) noexcept;

    //! Acquire a strong reference to the pointee and return a strong pointer.
    TIntrusivePtr<T> Lock() const noexcept;

    //! Returns true if the pointee is null or already expired.
    bool IsExpired() const noexcept;

    //! Returns the underlying raw pointer. Note that it may point to an already
    //! expired object.
    T* Get() const noexcept;

private:
    void AcquireRef();
    void ReleaseRef();

    template <class U>
    friend class TWeakPtr;

    T* T_ = nullptr;
#if defined(_tsan_enabled_)
    const TRefCounter* RefCounter_ = nullptr;
#endif

    const TRefCounter* GetRefCounterImpl() const;
    const TRefCounter* TryGetRefCounterImpl() const;
};

////////////////////////////////////////////////////////////////////////////////

template <class T1, class T2>
bool operator==(const TWeakPtr<T1>& lhs, const TWeakPtr<T2>& rhs);

template <class T1, class T2>
bool operator==(const TIntrusivePtr<T1>& lhs, const TWeakPtr<T2>& rhs);

template <class T1, class T2>
bool operator==(const TWeakPtr<T1>& lhs, const TIntrusivePtr<T2>& rhs);

template <class T1, class T2>
bool operator==(T1* lhs, const TWeakPtr<T2>& rhs);

template <class T1, class T2>
bool operator==(const TWeakPtr<T1>& lhs, T2* rhs);

template <class T2>
bool operator==(std::nullptr_t, const TWeakPtr<T2>& rhs);

template <class T1>
bool operator==(const TWeakPtr<T1>& lhs, std::nullptr_t);

////////////////////////////////////////////////////////////////////////////////

//! Creates a weak pointer wrapper for a given raw pointer.
//! Compared to |TWeakPtr<T>::ctor|, type inference enables omitting |T|.
template <class T>
TWeakPtr<T> MakeWeak(T* p);

//! Creates a weak pointer wrapper for a given intrusive pointer.
//! Compared to |TWeakPtr<T>::ctor|, type inference enables omitting |T|.
template <class T>
TWeakPtr<T> MakeWeak(const TIntrusivePtr<T>& p);

//! A helper for acquiring weak pointer for pointee, resetting intrusive pointer and then
//! returning the pointee reference count using the acquired weak pointer.
//! This helper is designed for best effort in checking that the object is not leaked after
//! destructing (what seems to be) the last pointer to it.
//! NB: it is possible to rewrite this helper making it working event with intrinsic refcounted objects,
//! but it requires much nastier integration with the intrusive pointer destruction routines.
template <typename T>
int ResetAndGetResidualRefCount(TIntrusivePtr<T>& pointer);

////////////////////////////////////////////////////////////////////////////////

struct TTransparentWeakPtrHasher
{
    using is_transparent = void;

    template <class T>
    std::size_t operator()(const TWeakPtr<T>& ptr) const;

    template <class T>
    std::size_t operator()(const TIntrusivePtr<T>& ptr) const;

    template <class T>
    std::size_t operator()(T* ptr) const;
};

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT

template <class T>
struct THash<NYT::TWeakPtr<T>>
{
    size_t operator()(const NYT::TWeakPtr<T>& ptr) const;
};

#define WEAK_PTR_INL_H_
#include "weak_ptr-inl.h"
#undef WEAK_PTR_INL_H_