aboutsummaryrefslogtreecommitdiff
path: root/include/salticidae/ref.h
blob: c0b77a32ac1e089a277d578ef4b88bf49b79dbca (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/**
 * Copyright (c) 2018 Cornell University.
 *
 * Author: Ted Yin <[email protected]>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef _SALTICIDAE_REF_H
#define _SALTICIDAE_REF_H

#include <atomic>
#include <functional>

namespace salticidae {

template<typename T>
struct default_delete {
    constexpr default_delete() = default;
    void operator()(T *ptr) const {
        static_assert(!std::is_void<T>::value,
                    "can't delete pointer to incomplete type");
        static_assert(sizeof(T) > 0,
                    "can't delete pointer to incomplete type");
        delete ptr;
    }
};

template<typename T>
struct default_delete<T[]> {
    constexpr default_delete() = default;
    void operator()(T *ptr) const {
        static_assert(sizeof(T) > 0,
                    "can't delete pointer to incomplete type");
        delete [] ptr;
    }
};

template<typename T, typename D>
class BoxObj;

template<typename T, typename D>
class _BoxObj {
    protected:
    using ptr_type = T *;
    ptr_type obj;
    constexpr _BoxObj(ptr_type obj): obj(obj) {}

    public:

    constexpr _BoxObj(): obj(nullptr) {}
    _BoxObj(const _BoxObj &other) = delete;
    _BoxObj(_BoxObj &&other): obj(other.obj) {
        other.obj = nullptr;
    }


    ~_BoxObj() {
        if (obj) D()(obj);
    }

    void swap(_BoxObj &other) {
        std::swap(obj, other.obj);
    }

    _BoxObj &operator=(const _BoxObj &other) = delete;
    _BoxObj &operator=(_BoxObj &&other) {
        if (this != &other)
        {
            _BoxObj tmp(std::move(other));
            tmp.swap(*this);
        }
        return *this;
    }

    T &operator *() const { return *obj; }
    T *get() const { return obj; }
    operator bool() const { return obj != nullptr; }
    bool operator==(const _BoxObj &other) const { return obj == other.obj; }
    bool operator!=(const _BoxObj &other) const { return obj != other.obj; }
    bool operator==(std::nullptr_t) const { return obj == nullptr; }
    bool operator!=(std::nullptr_t) const { return obj != nullptr; }
    bool operator<(const _BoxObj &other) const { return obj < other.obj; }
    bool operator>(const _BoxObj &other) const { return obj > other.obj; }
    bool operator<=(const _BoxObj &other) const { return obj <= other.obj; }
    bool operator>=(const _BoxObj &other) const { return obj >= other.obj; }
};

template<typename T, typename D = default_delete<T>>
class BoxObj: public _BoxObj<T, D> {
    using base_t = _BoxObj<T, D>;
    template<typename T__, typename D__, typename T_, typename D_>
    friend BoxObj<T__, D__> static_pointer_cast(BoxObj<T_, D_> &&other);

    public:
    BoxObj() = default;
    BoxObj(T *obj): base_t(obj) {}
    template<typename T_, typename =
                typename std::enable_if<!std::is_array<T_>::value>::type>
    BoxObj(BoxObj<T_> &&other): base_t(other) {}

    T *operator->() const { return base_t::obj; }
};

template<typename T, typename D>
class BoxObj<T[], D>: public _BoxObj<T, D> {
    using base_t = _BoxObj<T, D>;
    template<typename T__, typename D__, typename T_, typename D_>
    friend BoxObj<T__, D__> static_pointer_cast(BoxObj<T_, D_> &&other);

    public:
    BoxObj() = default;
    BoxObj(T obj[]): base_t(obj) {}
    template<typename T_>
    BoxObj(BoxObj<T_[]> &&other): base_t(std::move(other)) {}

    T &operator[](size_t idx) { return base_t::obj[idx]; }
};

template<typename T, typename D = default_delete<T>, typename T_, typename D_>
BoxObj<T, D> static_pointer_cast(BoxObj<T_, D_> &&other) {
    BoxObj<T, D> box{};
    box.obj = static_cast<typename BoxObj<T, D>::ptr_type>(other.obj);
    return std::move(box);
}

struct _RCCtl {
    size_t ref_cnt;
    size_t weak_cnt;
    void add_ref() { ref_cnt++; }
    void add_weak() { weak_cnt++; }
    bool release_ref() {
        if (--ref_cnt) return false;
        if (weak_cnt) return true;
        delete this;
        return true;
    }
    void release_weak() {
        if (--weak_cnt) return;
        if (ref_cnt) return;
        delete this;
    }
    size_t get_cnt() { return ref_cnt; }
    _RCCtl(): ref_cnt(1), weak_cnt(0) {}
    ~_RCCtl() {}
};

struct _ARCCtl {
    std::atomic_size_t ref_cnt;
    std::atomic_size_t weak_cnt;
    std::atomic_uint8_t dcnt;
    void add_ref() { ref_cnt++; }
    void add_weak() { weak_cnt++; }
    bool release_ref() {
        dcnt++;
        if (--ref_cnt) { dcnt--; return false; }
        if (weak_cnt) { dcnt--; return true; }
        if (!--dcnt) delete this;
        return true;
    }
    void release_weak() {
        dcnt++;
        if (--weak_cnt) { dcnt--; return; }
        if (ref_cnt) { dcnt--; return; }
        if (!--dcnt) delete this;
    }
    size_t get_cnt() { return ref_cnt.load(); }
    _ARCCtl(): ref_cnt(1), weak_cnt(0), dcnt(0) {}
    ~_ARCCtl() {}
};

template<typename T, typename R, typename D> class _RcObjBase;
template<typename T, typename R, typename D> class RcObjBase;

template<typename T, typename R>
class _WeakObjBase {
    T *obj;
    R *ctl;

    template<typename T_, typename R_, typename D>
    friend class _RcObjBase;

    public:
    _WeakObjBase(): ctl(nullptr) {}

    void swap(_WeakObjBase &other) {
        std::swap(obj, other.obj);
        std::swap(ctl, other.ctl);
    }

    _WeakObjBase &operator=(const _WeakObjBase &other) {
        if (this != &other)
        {
            _WeakObjBase tmp(other);
            tmp.swap(*this);
        }
        return *this;
    }

    _WeakObjBase &operator=(_WeakObjBase &&other) {
        if (this != &other)
        {
            _WeakObjBase tmp(std::move(other));
            tmp.swap(*this);
        }
        return *this;
    }

    _WeakObjBase(const _WeakObjBase &other):
            obj(other.obj), ctl(other.ctl) {
        if (ctl) ctl->add_weak();
    }

    _WeakObjBase(_WeakObjBase &&other):
            obj(other.obj), ctl(other.ctl) {
        other.ctl = nullptr;
    }

    template<typename D>
    _WeakObjBase(const _RcObjBase<T, R, D> &other);

    ~_WeakObjBase() { if (ctl) ctl->release_weak(); }
};


template<typename T, typename R>
class WeakObjBase: public _WeakObjBase<T, R> {
    using base_t = _WeakObjBase<T, R>;
    friend std::hash<WeakObjBase>;
    public:
    WeakObjBase() = default;
    WeakObjBase(const WeakObjBase &other) = default;
    WeakObjBase(WeakObjBase &&other) = default;
    template<typename D>
    WeakObjBase(const RcObjBase<T, R, D> &other): base_t(other) {}
    WeakObjBase &operator=(const WeakObjBase &) = default;
    WeakObjBase &operator=(WeakObjBase &&) = default;
};

template<typename T, typename R>
class WeakObjBase<T[], R>: public _WeakObjBase<T, R> {
    using base_t = _WeakObjBase<T, R>;
    friend std::hash<WeakObjBase>;
    public:
    WeakObjBase() = default;
    WeakObjBase(const WeakObjBase &other) = default;
    WeakObjBase(WeakObjBase &&other) = default;
    template<typename D>
    WeakObjBase(const RcObjBase<T[], R, D> &other): base_t(other) {}
    WeakObjBase &operator=(const WeakObjBase &) = default;
    WeakObjBase &operator=(WeakObjBase &&) = default;
};


template<typename T, typename R, typename D>
class _RcObjBase {
    protected:
    using ptr_type = T *;
    ptr_type obj;
    R *ctl;

    friend _WeakObjBase<T, R>;
    template<typename T_, typename R_, typename D_>
    friend class _RcObjBase;

    public:
    _RcObjBase(): obj(nullptr), ctl(nullptr) {}
    _RcObjBase(ptr_type obj): obj(obj), ctl(new R()) {}
    _RcObjBase(BoxObj<T> &&box_ref): obj(box_ref.obj), ctl(new R()) {
        box_ref.obj = nullptr;
    }

    void swap(_RcObjBase &other) {
        std::swap(obj, other.obj);
        std::swap(ctl, other.ctl);
    }

    _RcObjBase &operator=(const _RcObjBase &other) {
        if (this != &other)
        {
            _RcObjBase tmp(other);
            tmp.swap(*this);
        }
        return *this;
    }

    _RcObjBase &operator=(_RcObjBase &&other) {
        if (this != &other)
        {
            _RcObjBase tmp(std::move(other));
            tmp.swap(*this);
        }
        return *this;
    }

    _RcObjBase(const _RcObjBase &other):
            obj(other.obj), ctl(other.ctl) {
        if (ctl) ctl->add_ref();
    }

    template<typename T_, typename D_>
    _RcObjBase(const _RcObjBase<T_, R, D_> &other):
            obj(other.obj), ctl(other.ctl) {
        if (ctl) ctl->add_ref();
    }

    _RcObjBase(_RcObjBase &&other):
            obj(other.obj), ctl(other.ctl) {
        other.ctl = nullptr;
    }

    template<typename T_, typename D_>
    _RcObjBase(_RcObjBase<T_, R, D_> &&other):
            obj(other.obj), ctl(other.ctl) {
        other.ctl = nullptr;
    }

    _RcObjBase(const _WeakObjBase<T, R> &other) {
        if (other.ctl && other.ctl->ref_cnt)
        {
            obj = other.obj;
            ctl = other.ctl;
            ctl->add_ref();
        }
        else
        {
            obj = nullptr;
            ctl = nullptr;
        }
    }

    ~_RcObjBase() {
        if (ctl && ctl->release_ref())
            D()(obj);
    }

    size_t get_cnt() const { return ctl ? ctl->get_cnt() : 0; }
    T &operator *() const { return *obj; }
    T *get() const { return obj; }
    operator bool() const { return obj != nullptr; }
    bool operator==(const _RcObjBase &other) const { return obj == other.obj; }
    bool operator!=(const _RcObjBase &other) const { return obj != other.obj; }
    bool operator==(std::nullptr_t) const { return obj == nullptr; }
    bool operator!=(std::nullptr_t) const { return obj != nullptr; }
    bool operator<(const _RcObjBase &other) const { return obj < other.obj; }
    bool operator>(const _RcObjBase &other) const { return obj > other.obj; }
    bool operator<=(const _RcObjBase &other) const { return obj <= other.obj; }
    bool operator>=(const _RcObjBase &other) const { return obj >= other.obj; }
};

template<typename T, typename R, typename D = default_delete<T>>
class RcObjBase: public _RcObjBase<T, R, D> {
    using base_t = _RcObjBase<T, R, D>;
    friend std::hash<RcObjBase>;
    template<typename T__, typename D__, typename T_, typename R_, typename D_>
    friend RcObjBase<T__, R_, D__> static_pointer_cast(const RcObjBase<T_, R_, D_> &other);
    template<typename T__, typename D__, typename T_, typename R_, typename D_>
    friend RcObjBase<T__, R_, D__> static_pointer_cast(RcObjBase<T_, R_, D_> &&other);

    public:
    T *operator->() const { return base_t::obj; }
    RcObjBase() = default;
    RcObjBase(T *obj): base_t(obj) {}
    RcObjBase(BoxObj<T> &&box_ref): base_t(std::move(box_ref)) {}

    template<typename T_, typename D_,
            typename = typename std::enable_if<!std::is_array<T_>::value>::type>
    RcObjBase(const RcObjBase<T_, R, D_> &other): base_t(other) {}

    template<typename T_, typename D_,
            typename = typename std::enable_if<!std::is_array<T_>::value>::type>
    RcObjBase(RcObjBase<T_, R, D_> &&other): base_t(std::move(other)) {}

    RcObjBase(const WeakObjBase<T, R> &other): base_t(other) {}

    RcObjBase(const RcObjBase &other) = default;
    RcObjBase(RcObjBase &&other) = default;
    RcObjBase &operator=(const RcObjBase &) = default;
    RcObjBase &operator=(RcObjBase &&) = default;
};

template<typename T, typename R, typename D>
class RcObjBase<T[], R, D>: public _RcObjBase<T, R, D> {
    using base_t = _RcObjBase<T, R, D>;
    friend std::hash<RcObjBase>;
    template<typename T__, typename D__, typename T_, typename R_, typename D_>
    friend RcObjBase<T__, R_, D__> static_pointer_cast(const RcObjBase<T_, R_, D_> &other);
    template<typename T__, typename D__, typename T_, typename R_, typename D_>
    friend RcObjBase<T__, R_, D__> static_pointer_cast(RcObjBase<T_, R_, D_> &&other);

    public:
    T &operator[](size_t idx) { return base_t::obj; }
    RcObjBase() = default;
    RcObjBase(T obj[]): base_t(obj) {}
    RcObjBase(BoxObj<T[]> &&box_ref): base_t(std::move(box_ref)) {}

    template<typename T_, typename D_>
    RcObjBase(const RcObjBase<T_[], R, D_> &other): base_t(other) {}

    template<typename T_, typename D_>
    RcObjBase(RcObjBase<T_[], R, D_> &&other): base_t(std::move(other)) {}

    RcObjBase(const WeakObjBase<T[], R> &other): base_t(other) {}

    RcObjBase(const RcObjBase &other) = default;
    RcObjBase(RcObjBase &&other) = default;
    RcObjBase &operator=(const RcObjBase &) = default;
    RcObjBase &operator=(RcObjBase &&) = default;
};


template<typename T, typename D = default_delete<T>,
        typename T_, typename R_, typename D_>
RcObjBase<T, R_, D> static_pointer_cast(const RcObjBase<T_, R_, D_> &other) {
    RcObjBase<T, R_, D> rc{};
    rc.obj = static_cast<typename RcObjBase<T, R_, D>::ptr_type>(other.obj);
    if ((rc.ctl = other.ctl))
        rc.ctl->add_ref();
    return std::move(rc);
}

template<typename T, typename D = default_delete<T>,
        typename T_, typename R_, typename D_>
RcObjBase<T, R_, D> static_pointer_cast(RcObjBase<T_, R_, D_> &&other) {
    RcObjBase<T, R_, D> rc{};
    rc.obj = static_cast<typename RcObjBase<T, R_, D>::ptr_type>(other.obj);
    rc.ctl = other.ctl;
    other.ctl = nullptr;
    return std::move(rc);
}

template<typename T, typename R>
template<typename D>
inline _WeakObjBase<T, R>::_WeakObjBase(const _RcObjBase<T, R, D> &other):
        obj(other.obj), ctl(other.ctl) {
    if (ctl) ctl->add_weak();
}

template<typename T> using RcObj = RcObjBase<T, _RCCtl>;
template<typename T> using WeakObj = WeakObjBase<T, _RCCtl>;

template<typename T> using ArcObj = RcObjBase<T, _ARCCtl>;
template<typename T> using AweakObj = WeakObjBase<T, _ARCCtl>;

}

namespace std {
    template<typename T, typename R, typename D>
    struct hash<salticidae::RcObjBase<T, R, D>> {
        size_t operator()(const salticidae::RcObjBase<T, R, D> &k) const {
            return (size_t)k.obj;
        }
    };
}

#endif