aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/neh/smart_ptr.h
blob: 1ec4653304bbbc44c7c0b6a1aa5eaa4d0f3f6e8d (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#pragma once

#include <util/generic/ptr.h>
#include <library/cpp/deprecated/atomic/atomic.h>

namespace NNeh {
    //limited emulation shared_ptr/weak_ptr from boost lib.
    //the main value means the weak_ptr functionality, else recommended use types from util/generic/ptr.h

    //smart pointer counter shared between shared and weak ptrs.
    class TSPCounted: public TThrRefBase {
    public:
        inline TSPCounted() noexcept
            : C_(0)
        {
        }

        inline void Inc() noexcept {
            AtomicIncrement(C_);
        }

        //return false if C_ already 0, else increment and return true
        inline bool TryInc() noexcept {
            for (;;) {
                intptr_t curVal(AtomicGet(C_));

                if (!curVal) {
                    return false;
                }

                intptr_t newVal(curVal + 1);

                if (AtomicCas(&C_, newVal, curVal)) {
                    return true;
                }
            }
        }

        inline intptr_t Dec() noexcept {
            return AtomicDecrement(C_);
        }

        inline intptr_t Value() const noexcept {
            return AtomicGet(C_);
        }

    private:
        TAtomic C_;
    };

    typedef TIntrusivePtr<TSPCounted> TSPCountedRef;

    class TWeakCount;

    class TSPCount {
    public:
        TSPCount(TSPCounted* c = nullptr) noexcept
            : C_(c)
        {
        }

        inline void Swap(TSPCount& r) noexcept {
            DoSwap(C_, r.C_);
        }

        inline size_t UseCount() const noexcept {
            if (!C_) {
                return 0;
            }
            return C_->Value();
        }

        inline bool operator!() const noexcept {
            return !C_;
        }

        inline TSPCounted* GetCounted() const noexcept {
            return C_.Get();
        }

        inline void Reset() noexcept {
            if (!!C_) {
                C_.Drop();
            }
        }

    protected:
        TIntrusivePtr<TSPCounted> C_;
    };

    class TSharedCount: public TSPCount {
    public:
        inline TSharedCount() noexcept {
        }

        /// @throws std::bad_alloc
        inline explicit TSharedCount(const TSharedCount& r)
            : TSPCount(r.C_.Get())
        {
            if (!!C_) {
                (C_->Inc());
            }
        }

        //'c' must exist and has already increased ref
        inline explicit TSharedCount(TSPCounted* c) noexcept
            : TSPCount(c)
        {
        }

    public:
        /// @throws std::bad_alloc
        inline void Inc() {
            if (!C_) {
                TSPCountedRef(new TSPCounted()).Swap(C_);
            }
            C_->Inc();
        }

        inline bool TryInc() noexcept {
            if (!C_) {
                return false;
            }
            return C_->TryInc();
        }

        inline intptr_t Dec() noexcept {
            if (!C_) {
                Y_ASSERT(0);
                return 0;
            }
            return C_->Dec();
        }

        void Drop() noexcept {
            C_.Drop();
        }

    protected:
        template <class Y>
        friend class TSharedPtrB;

        // 'c' MUST BE already incremented
        void Assign(TSPCounted* c) noexcept {
            TSPCountedRef(c).Swap(C_);
        }

    private:
        TSharedCount& operator=(const TSharedCount&); //disable
    };

    class TWeakCount: public TSPCount {
    public:
        inline TWeakCount() noexcept {
        }

        inline explicit TWeakCount(const TWeakCount& r) noexcept
            : TSPCount(r.GetCounted())
        {
        }

        inline explicit TWeakCount(const TSharedCount& r) noexcept
            : TSPCount(r.GetCounted())
        {
        }

    private:
        TWeakCount& operator=(const TWeakCount&); //disable
    };

    template <class T>
    class TWeakPtrB;

    template <class T>
    class TSharedPtrB {
    public:
        inline TSharedPtrB() noexcept
            : T_(nullptr)
        {
        }

        /// @throws std::bad_alloc
        inline TSharedPtrB(T* t)
            : T_(nullptr)
        {
            if (t) {
                THolder<T> h(t);
                C_.Inc();
                T_ = h.Release();
            }
        }

        inline TSharedPtrB(const TSharedPtrB<T>& r) noexcept
            : T_(r.T_)
            , C_(r.C_)
        {
            Y_ASSERT((!!T_ && !!C_.UseCount()) || (!T_ && !C_.UseCount()));
        }

        inline TSharedPtrB(const TWeakPtrB<T>& r) noexcept
            : T_(r.T_)
        {
            if (T_) {
                TSPCounted* spc = r.C_.GetCounted();

                if (spc && spc->TryInc()) {
                    C_.Assign(spc);
                } else { //obsolete ptr
                    T_ = nullptr;
                }
            }
        }

        inline ~TSharedPtrB() {
            Reset();
        }

        TSharedPtrB& operator=(const TSharedPtrB<T>& r) noexcept {
            TSharedPtrB<T>(r).Swap(*this);
            return *this;
        }

        TSharedPtrB& operator=(const TWeakPtrB<T>& r) noexcept {
            TSharedPtrB<T>(r).Swap(*this);
            return *this;
        }

        void Swap(TSharedPtrB<T>& r) noexcept {
            DoSwap(T_, r.T_);
            DoSwap(C_, r.C_);
            Y_ASSERT((!!T_ && !!UseCount()) || (!T_ && !UseCount()));
        }

        inline bool operator!() const noexcept {
            return !T_;
        }

        inline T* Get() noexcept {
            return T_;
        }

        inline T* operator->() noexcept {
            return T_;
        }

        inline T* operator->() const noexcept {
            return T_;
        }

        inline T& operator*() noexcept {
            return *T_;
        }

        inline T& operator*() const noexcept {
            return *T_;
        }

        inline void Reset() noexcept {
            if (T_) {
                if (C_.Dec() == 0) {
                    delete T_;
                }
                T_ = nullptr;
                C_.Drop();
            }
        }

        inline size_t UseCount() const noexcept {
            return C_.UseCount();
        }

    protected:
        template <class Y>
        friend class TWeakPtrB;

        T* T_;
        TSharedCount C_;
    };

    template <class T>
    class TWeakPtrB {
    public:
        inline TWeakPtrB() noexcept
            : T_(nullptr)
        {
        }

        inline TWeakPtrB(const TWeakPtrB<T>& r) noexcept
            : T_(r.T_)
            , C_(r.C_)
        {
        }

        inline TWeakPtrB(const TSharedPtrB<T>& r) noexcept
            : T_(r.T_)
            , C_(r.C_)
        {
        }

        TWeakPtrB& operator=(const TWeakPtrB<T>& r) noexcept {
            TWeakPtrB(r).Swap(*this);
            return *this;
        }

        TWeakPtrB& operator=(const TSharedPtrB<T>& r) noexcept {
            TWeakPtrB(r).Swap(*this);
            return *this;
        }

        inline void Swap(TWeakPtrB<T>& r) noexcept {
            DoSwap(T_, r.T_);
            DoSwap(C_, r.C_);
        }

        inline void Reset() noexcept {
            T_ = 0;
            C_.Reset();
        }

        inline size_t UseCount() const noexcept {
            return C_.UseCount();
        }

    protected:
        template <class Y>
        friend class TSharedPtrB;

        T* T_;
        TWeakCount C_;
    };

}