aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/private/SkUniquePtr.h
blob: 5d6e7226580aed5b176d7bd050cea8caa5bd5b78 (plain)
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
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkUniquePtr_DEFINED
#define SkUniquePtr_DEFINED

#include "SkTLogic.h"
#include "SkUtility.h"

namespace skstd {

template <typename T> struct default_delete {
    /*constexpr*/ default_delete() /*noexcept*/ = default;

    template <typename U, typename = enable_if_t<is_convertible<U*, T*>::value>>
    default_delete(const default_delete<U>&) /*noexcept*/ {}

    void operator()(T* obj) const {
        static_assert(sizeof(T) > 0, "Deleting pointer to incomplete type!");
        delete obj;
    }
};
template <typename T> struct default_delete<T[]> {
    /*constexpr*/ default_delete() /*noexcept*/ = default;

    void operator()(T* obj) const {
        static_assert(sizeof(T) > 0, "Deleting pointer to incomplete type!");
        delete [] obj;
    }
};

template <typename T, typename D = default_delete<T>> class unique_ptr {
    // remove_reference_t<D>::pointer if that type exists, otherwise T*.
    struct pointer_type_detector {
        template <typename U> static typename U::pointer detector(typename U::pointer*);
        template <typename U> static T* detector(...);
        using type = decltype(detector<remove_reference_t<D>>(0));
    };

public:
    using pointer = typename pointer_type_detector::type;
    using element_type = T;
    using deleter_type = D;

private:
    template <typename B, bool = is_empty<B>::value /*&& !is_final<B>::value*/>
    struct compressed_base : private B {
        /*constexpr*/ compressed_base() : B() {}
        /*constexpr*/ compressed_base(const B& b) : B(b) {}
        /*constexpr*/ compressed_base(const B&& b) : B(move(b)) {}
        /*constexpr*/ B& get() /*noexcept*/ { return *this; }
        /*constexpr*/ B const& get() const /*noexcept*/ { return *this; }
        void swap(compressed_base&) /*noexcept*/ { }
    };

    template <typename B> struct compressed_base<B, false> {
        B fb;
        /*constexpr*/ compressed_base() : B() {}
        /*constexpr*/ compressed_base(const B& b) : fb(b) {}
        /*constexpr*/ compressed_base(const B&& b) : fb(move(b)) {}
        /*constexpr*/ B& get() /*noexcept*/ { return fb; }
        /*constexpr*/ B const& get() const /*noexcept*/ { return fb; }
        void swap(compressed_base& that) /*noexcept*/ { SkTSwap(fb, that.fB); }
    };

    struct compressed_data : private compressed_base<deleter_type> {
        pointer fPtr;
        /*constexpr*/ compressed_data() : compressed_base<deleter_type>(), fPtr() {}
        /*constexpr*/ compressed_data(const pointer& ptr, const deleter_type& d)
            : compressed_base<deleter_type>(d), fPtr(ptr) {}
        template <typename U1, typename U2, typename = enable_if_t<
            is_convertible<U1, pointer>::value && is_convertible<U2, deleter_type>::value
        >> /*constexpr*/ compressed_data(U1&& ptr, U2&& d)
            : compressed_base<deleter_type>(skstd::forward<U2>(d)), fPtr(skstd::forward<U1>(ptr)) {}
        /*constexpr*/ pointer& getPointer() /*noexcept*/ { return fPtr; }
        /*constexpr*/ pointer const& getPointer() const /*noexcept*/ { return fPtr; }
        /*constexpr*/ deleter_type& getDeleter() /*noexcept*/ {
            return compressed_base<deleter_type>::get();
        }
        /*constexpr*/ deleter_type const& getDeleter() const /*noexcept*/ {
            return compressed_base<deleter_type>::get();
        }
        void swap(compressed_data& that) /*noexcept*/ {
            compressed_base<deleter_type>::swap(static_cast<compressed_base<deleter_type>>(that));
            SkTSwap(fPtr, that.fPtr);
        }
    };
    compressed_data data;

public:
    /*constexpr*/ unique_ptr() /*noexcept*/ : data() {
        static_assert(!is_pointer<deleter_type>::value, "Deleter is nullptr function pointer!");
    }

    /*constexpr*/ unique_ptr(skstd::nullptr_t) /*noexcept*/ : unique_ptr() { }

    explicit unique_ptr(pointer ptr) /*noexcept*/ : data(ptr, deleter_type()) {
        static_assert(!is_pointer<deleter_type>::value, "Deleter is nullptr function pointer!");
    }

    unique_ptr(pointer ptr,
               conditional_t<is_reference<deleter_type>::value, deleter_type,const deleter_type&> d)
    /*noexcept*/ : data(ptr, d)
    {}

    unique_ptr(pointer ptr, remove_reference_t<deleter_type>&& d) /*noexcept*/
        : data(move(ptr), move(d))
    {
        static_assert(!is_reference<deleter_type>::value,
            "Binding an rvalue reference deleter as an lvalue reference deleter is not allowed.");
    }


    unique_ptr(unique_ptr&& that) /*noexcept*/
        : data(that.release(), forward<deleter_type>(that.get_deleter()))
    {}

    template <typename U, typename ThatD, typename = enable_if_t<
        is_convertible<typename unique_ptr<U, ThatD>::pointer, pointer>::value &&
        !is_array<U>::value &&
        conditional_t<is_reference<D>::value, is_same<ThatD, D>, is_convertible<ThatD, D>>::value>>
    unique_ptr(unique_ptr<U, ThatD>&& that) /*noexcept*/
        : data(that.release(), forward<ThatD>(that.get_deleter()))
    {}

    ~unique_ptr() /*noexcept*/ {
        pointer& ptr = data.getPointer();
        if (ptr != nullptr) {
            get_deleter()(ptr);
        }
        ptr = pointer();
    }

    unique_ptr& operator=(unique_ptr&& that) /*noexcept*/ {
        reset(that.release());
        get_deleter() = forward<deleter_type>(that.get_deleter());
        return *this;
    }

    template <typename U, typename ThatD> enable_if_t<
        is_convertible<typename unique_ptr<U, ThatD>::pointer, pointer>::value &&
        !is_array<U>::value,
    unique_ptr&> operator=(unique_ptr<U, ThatD>&& that) /*noexcept*/ {
        reset(that.release());
        get_deleter() = forward<ThatD>(that.get_deleter());
        return *this;
    }

    unique_ptr& operator=(skstd::nullptr_t) /*noexcept*/ {
        reset();
        return *this;
    }

    add_lvalue_reference_t<element_type> operator*() const {
        SkASSERT(get() != pointer());
        return *get();
    }

    pointer operator->() const /*noexcept*/ {
        SkASSERT(get() != pointer());
        return get();
    }

    pointer get() const /*noexcept*/ {
        return data.getPointer();
    }

    deleter_type& get_deleter() /*noexcept*/ {
        return data.getDeleter();
    }

    const deleter_type& get_deleter() const /*noexcept*/ {
        return data.getDeleter();
    }

    //explicit operator bool() const noexcept {
    bool is_attached() const /*noexcept*/ {
        return get() == pointer() ? false : true;
    }

    pointer release() /*noexcept*/ {
        pointer ptr = get();
        data.getPointer() = pointer();
        return ptr;
    }

    void reset(pointer ptr = pointer()) /*noexcept*/ {
        SkTSwap(data.getPointer(), ptr);
        if (ptr != pointer()) {
            get_deleter()(ptr);
        }
    }

    void swap(unique_ptr& that) /*noexcept*/ {
        SkTSwap(data, that.data);
    }

    unique_ptr(const unique_ptr&) = delete;
    unique_ptr& operator=(const unique_ptr&) = delete;
};

template <typename T, typename D> class unique_ptr<T[], D> {
    // remove_reference_t<D>::pointer if that type exists, otherwise T*.
    struct pointer_type_detector {
        template <typename U> static typename U::pointer detector(typename U::pointer*);
        template <typename U> static T* detector(...);
        using type = decltype(detector<remove_reference_t<D>>(0));
    };

public:
    using pointer = typename pointer_type_detector::type;
    using element_type = T;
    using deleter_type = D;

private:
    template <typename B, bool = is_empty<B>::value /*&& !is_final<B>::value*/>
    struct compressed_base : private B {
        /*constexpr*/ compressed_base() : B() {}
        /*constexpr*/ compressed_base(const B& b) : B(b) {}
        /*constexpr*/ compressed_base(const B&& b) : B(move(b)) {}
        /*constexpr*/ B& get() /*noexcept*/ { return *this; }
        /*constexpr*/ B const& get() const /*noexcept*/ { return *this; }
        void swap(compressed_base&) /*noexcept*/ { }
    };

    template <typename B> struct compressed_base<B, false> {
        B fb;
        /*constexpr*/ compressed_base() : B() {}
        /*constexpr*/ compressed_base(const B& b) : fb(b) {}
        /*constexpr*/ compressed_base(const B&& b) : fb(move(b)) {}
        /*constexpr*/ B& get() /*noexcept*/ { return fb; }
        /*constexpr*/ B const& get() const /*noexcept*/ { return fb; }
        void swap(compressed_base& that) /*noexcept*/ { SkTSwap(fb, that.fB); }
    };

    struct compressed_data : private compressed_base<deleter_type> {
        pointer fPtr;
        /*constexpr*/ compressed_data() : compressed_base<deleter_type>(), fPtr() {}
        /*constexpr*/ compressed_data(const pointer& ptr, const deleter_type& d)
            : compressed_base<deleter_type>(d), fPtr(ptr) {}
        template <typename U1, typename U2, typename = enable_if_t<
            is_convertible<U1, pointer>::value && is_convertible<U2, deleter_type>::value
        >> /*constexpr*/ compressed_data(U1&& ptr, U2&& d)
            : compressed_base<deleter_type>(skstd::forward<U2>(d)), fPtr(skstd::forward<U1>(ptr)) {}
        /*constexpr*/ pointer& getPointer() /*noexcept*/ { return fPtr; }
        /*constexpr*/ pointer const& getPointer() const /*noexcept*/ { return fPtr; }
        /*constexpr*/ deleter_type& getDeleter() /*noexcept*/ {
            return compressed_base<deleter_type>::get();
        }
        /*constexpr*/ deleter_type const& getDeleter() const /*noexcept*/ {
            return compressed_base<deleter_type>::get();
        }
        void swap(compressed_data& that) /*noexcept*/ {
            compressed_base<deleter_type>::swap(static_cast<compressed_base<deleter_type>>(that));
            SkTSwap(fPtr, that.fPtr);
        }
    };
    compressed_data data;

public:
    /*constexpr*/ unique_ptr() /*noexcept*/ : data() {
        static_assert(!is_pointer<deleter_type>::value, "Deleter is nullptr function pointer!");
    }

    /*constexpr*/ unique_ptr(skstd::nullptr_t) /*noexcept*/ : unique_ptr() { }

    explicit unique_ptr(pointer ptr) /*noexcept*/ : data(ptr, deleter_type()) {
        static_assert(!is_pointer<deleter_type>::value, "Deleter is nullptr function pointer!");
    }

    unique_ptr(pointer ptr,
               conditional_t<is_reference<deleter_type>::value, deleter_type,const deleter_type&> d)
    /*noexcept*/ : data(ptr, d)
    {}

    unique_ptr(pointer ptr, remove_reference_t<deleter_type>&& d) /*noexcept*/
        : data(move(ptr), move(d))
    {
        static_assert(!is_reference<deleter_type>::value,
            "Binding an rvalue reference deleter as an lvalue reference deleter is not allowed.");
    }

    unique_ptr(unique_ptr&& that) /*noexcept*/
        : data(that.release(), forward<deleter_type>(that.get_deleter()))
    {}

    ~unique_ptr() {
        pointer& ptr = data.getPointer();
        if (ptr != nullptr) {
          get_deleter()(ptr);
        }
        ptr = pointer();
    }

    unique_ptr& operator=(unique_ptr&& that) /*noexcept*/ {
        reset(that.release());
        get_deleter() = forward<deleter_type>(that.get_deleter());
        return *this;
    }

    unique_ptr& operator=(skstd::nullptr_t) /*noexcept*/ {
        reset();
        return *this;
    }

    add_lvalue_reference_t<element_type> operator[](size_t i) const {
        SkASSERT(get() != pointer());
        return get()[i];
    }

    pointer get() const /*noexcept*/ {
        return data.getPointer();
    }

    deleter_type& get_deleter() /*noexcept*/ {
        return data.getDeleter();
    }

    const deleter_type& get_deleter() const /*noexcept*/ {
        return data.getDeleter();
    }

    //explicit operator bool() const noexcept {
    bool is_attached() const /*noexcept*/ {
        return get() == pointer() ? false : true;
    }

    pointer release() /*noexcept*/ {
        pointer ptr = get();
        data.getPointer() = pointer();
        return ptr;
    }

    void reset(pointer ptr = pointer()) /*noexcept*/ {
        SkTSwap(data.getPointer(), ptr);
        if (ptr != pointer()) {
            get_deleter()(ptr);
        }
    }

    template <typename U> void reset(U*) = delete;

    void swap(unique_ptr& that) /*noexcept*/ {
        data.swap(that.data);
    }

    unique_ptr(const unique_ptr&) = delete;
    unique_ptr& operator=(const unique_ptr&) = delete;
};

template <typename T, typename D>
inline void swap(unique_ptr<T, D>& a, unique_ptr<T, D>& b) /*noexcept*/ {
    a.swap(b);
}

template <typename T, typename D, typename U, typename ThatD>
inline bool operator==(const unique_ptr<T, D>& a, const unique_ptr<U, ThatD>& b) {
    return a.get() == b.get();
}

template <typename T, typename D>
inline bool operator==(const unique_ptr<T, D>& a, skstd::nullptr_t) /*noexcept*/ {
    //return !a;
    return !a.is_attached();
}

template <typename T, typename D>
inline bool operator==(skstd::nullptr_t, const unique_ptr<T, D>& b) /*noexcept*/ {
    //return !b;
    return !b.is_attached();
}

template <typename T, typename D, typename U, typename ThatD>
inline bool operator!=(const unique_ptr<T, D>& a, const unique_ptr<U, ThatD>& b) {
    return a.get() != b.get();
}

template <typename T, typename D>
inline bool operator!=(const unique_ptr<T, D>& a, skstd::nullptr_t) /*noexcept*/ {
    //return (bool)a;
    return a.is_attached();
}

template <typename T, typename D>
inline bool operator!=(skstd::nullptr_t, const unique_ptr<T, D>& b) /*noexcept*/ {
    //return (bool)b;
    return b.is_attached();
}

}  // namespace skstd

#endif