summaryrefslogtreecommitdiffhomepage
path: root/compat/borrowed-ptr.hpp
blob: 6862af2ee2a9eaefb45c7399733c046d6c341d98 (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
#pragma once
#include "borrowed-ptr-fwd.hpp"

namespace floormat::detail_borrowed_ptr {

struct control_block;

template<typename From, typename To>
concept StaticCastable = requires(From* from) {
    static_cast<To*>(from);
};

template<typename From, typename To>
concept DerivedFrom = requires(From* x) {
    !std::is_same_v<From, To>;
    std::is_convertible_v<From*, To*>;
};

} // namespace floormat::detail_borrowed_ptr

namespace floormat {

struct bptr_base
{
    virtual ~bptr_base() noexcept;
    bptr_base() noexcept;
    bptr_base(const bptr_base&) noexcept;
    bptr_base(bptr_base&&) noexcept;
    bptr_base& operator=(const bptr_base&) noexcept;
    bptr_base& operator=(bptr_base&&) noexcept;
};

template<typename T>
class bptr final // NOLINT(*-special-member-functions)
{
    detail_borrowed_ptr::control_block* blk;

    template<typename Y>
    requires std::is_convertible_v<T*, const bptr_base*>
    bptr(const bptr<Y>& other, std::nullptr_t) noexcept;

    template<typename Y> bptr& _copy_assign(const bptr<Y>& other) noexcept;

    template<typename Y>
    requires std::is_convertible_v<T*, const bptr_base*>
    bptr(bptr<Y>&& other, std::nullptr_t) noexcept;

    template<typename Y> bptr& _move_assign(bptr<Y>&& other) noexcept;

public:
    template<typename... Ts>
    requires (std::is_constructible_v<std::remove_const_t<T>, Ts&&...> && std::is_convertible_v<T*, const bptr_base*>)
    explicit bptr(InPlaceInitT, Ts&&... args) noexcept;

    explicit bptr(T* ptr) noexcept requires std::is_convertible_v<T*, const bptr_base*>;
    bptr() noexcept requires std::is_convertible_v<T*, const bptr_base*>;
    ~bptr() noexcept;

    bptr(std::nullptr_t) noexcept requires std::is_convertible_v<T*, const bptr_base*>; // NOLINT(*-explicit-conversions)
    bptr& operator=(std::nullptr_t) noexcept requires std::is_convertible_v<T*, const bptr_base*>;

    bptr(const bptr&) noexcept requires std::is_convertible_v<T*, const bptr_base*>;
    bptr& operator=(const bptr&) noexcept requires std::is_convertible_v<T*, const bptr_base*>;

    template<detail_borrowed_ptr::DerivedFrom<T> Y> bptr(const bptr<Y>&) noexcept
    requires std::is_convertible_v<T*, const bptr_base*>;

    template<detail_borrowed_ptr::DerivedFrom<T> Y> bptr& operator=(const bptr<Y>&) noexcept
    requires std::is_convertible_v<T*, const bptr_base*>;

    bptr(bptr&&) noexcept requires std::is_convertible_v<T*, const bptr_base*>;
    bptr& operator=(bptr&&) noexcept requires std::is_convertible_v<T*, const bptr_base*>;

    template<detail_borrowed_ptr::DerivedFrom<T> Y> bptr(bptr<Y>&&) noexcept
    requires std::is_convertible_v<T*, const bptr_base*>;
    template<detail_borrowed_ptr::DerivedFrom<T> Y> bptr& operator=(bptr<Y>&&) noexcept
    requires std::is_convertible_v<T*, const bptr_base*>;

    void reset() noexcept;
    void destroy() noexcept;
    void swap(bptr& other) noexcept;
    uint32_t use_count() const noexcept;

    T* get() const noexcept;
    T* operator->() const noexcept;
    T& operator*() const noexcept;

    explicit operator bool() const noexcept;
    bool operator==(const bptr<const std::remove_const_t<T>>& other) const noexcept;
    bool operator==(const bptr<std::remove_const_t<T>>& other) const noexcept;

    template<typename U> friend class bptr;

    template<typename To, typename From>
    requires (std::is_convertible_v<To*, const bptr_base*> && detail_borrowed_ptr::StaticCastable<From, To>)
    friend bptr<To> static_pointer_cast(const bptr<From>& p) noexcept;
};

template<typename To, typename From>
requires (std::is_convertible_v<To*, const bptr_base*> && detail_borrowed_ptr::StaticCastable<From, To>)
bptr<To> static_pointer_cast(const bptr<From>& p) noexcept
{
    if (p.blk && p.blk->_ptr) [[likely]]
        return bptr<To>{p, nullptr};
    return bptr<To>{nullptr};
}

} // namespace floormat