aboutsummaryrefslogtreecommitdiff
path: root/include/salticidae/ref.h
blob: c2dcdd34aa025973c7aa5a2cfc51b9515600df5b (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
/**
 * Copyright (c) 2018 Cornell University.
 *
 * Author: Ted Yin <tederminant@gmail.com>
 *
 * 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>
class BoxObj {
    T *obj;

    void release() {
        if (obj) delete obj;
    }

    public:
    template<typename T__, typename T_>
    friend BoxObj<T__> static_pointer_cast(const BoxObj<T_> &other);
    operator T*() const { return obj; }
    T *operator->() const { return obj; }
    BoxObj(): obj(nullptr) {}
    BoxObj(T *obj): obj(obj) {}
    BoxObj &operator=(const BoxObj &other) = delete;
    BoxObj &operator=(BoxObj &&other) {
        release();
        obj = other.obj;
        other.obj = nullptr;
        return *this;
    }

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

    template<typename T_>
    BoxObj(BoxObj<T_> &&other): obj(other.obj) {
        other.obj = nullptr;
    }

    ~BoxObj() { release(); }
};

template<typename T, typename T_>
BoxObj<T> static_pointer_cast(BoxObj<T_> &&other) {
    BoxObj<T> box{};
    box.obj = static_cast<T *>(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> class RcObjBase;

template<typename T, typename R>
class WeakObjBase {
    T *obj;
    R *ctl;
    void release() { if (ctl) ctl->release_weak(); }
    public:
    friend RcObjBase<T, R>;
    friend std::hash<WeakObjBase<T, R>>;
    WeakObjBase(): ctl(nullptr) {}
    WeakObjBase &operator=(const WeakObjBase &other) {
        release();
        obj = other.obj;
        if ((ctl = other.ctl))
            ctl->add_weak();
        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;
    }

    WeakObjBase(const RcObjBase<T, R> &other);

    ~WeakObjBase() { release(); }
};

template<typename T, typename R>
class RcObjBase {
    T *obj;
    R *ctl;
    void release() {
        if (ctl && ctl->release_ref())
            delete obj;
    }
    public:
    friend WeakObjBase<T, R>;
    friend std::hash<RcObjBase<T, R>>;
    template<typename T__, typename T_, typename R_>
    friend RcObjBase<T__, R_> static_pointer_cast(const RcObjBase<T_, R_> &other);
    template<typename T__, typename T_, typename R_>
    friend RcObjBase<T__, R_> static_pointer_cast(RcObjBase<T_, R_> &&other);
    template<typename T_, typename R_> friend class RcObjBase;

    operator T*() const { return obj; }
    T *operator->() const { return obj; }
    RcObjBase(): obj(nullptr), ctl(nullptr) {}
    RcObjBase(T *obj): obj(obj), ctl(new R()) {}
    RcObjBase(BoxObj<T> &&box_ref): obj(box_ref.obj), ctl(new R()) {
        box_ref.obj = nullptr;
    }

    RcObjBase &operator=(const RcObjBase &other) {
        release();
        obj = other.obj;
        if ((ctl = other.ctl))
            ctl->add_ref();
        return *this;
    }

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

    template<typename T_>
    RcObjBase(const RcObjBase<T_, R> &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_>
    RcObjBase(RcObjBase<T_, R> &&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() { release(); }

    size_t get_cnt() const { return ctl ? ctl->get_cnt() : 0; }
};

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

template<typename T, typename T_, typename R>
RcObjBase<T, R> static_pointer_cast(RcObjBase<T_, R> &&other) {
    RcObjBase<T, R> rc{};
    rc.obj = static_cast<T *>(other.obj);
    rc.ctl = other.ctl;
    other.ctl = nullptr;
    return std::move(rc);
}

template<typename T, typename R>
inline WeakObjBase&l