summaryrefslogtreecommitdiffhomepage
path: root/compat/borrowed-ptr.hpp
blob: 10b95be9bcb1ee78d692f25e2d06f03616443a0e (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
#pragma once

namespace floormat::detail_borrowed_ptr {
struct control_block_;
} // namespace floormat::detail_borrowed_ptr

namespace floormat {
template<typename T> class bptr;
template<typename T> bptr<T> static_pointer_cast(const bptr<T>& p);

template<typename T>
class bptr final // NOLINT(*-special-member-functions)
{
    T* ptr; // todo add simple_bptr that doesn't allow casting. should only have the control block element.
    detail_borrowed_ptr::control_block_* blk;

    constexpr bptr(NoInitT) noexcept;

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

    constexpr bptr(std::nullptr_t) noexcept; // NOLINT(*-explicit-conversions)
    constexpr bptr() noexcept;
    explicit bptr(T* ptr) noexcept;
    ~bptr() noexcept;

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

    void swap() noexcept;
    T* get() noexcept;
    const T* get() const noexcept;
    T& operator*() const noexcept;
    T* operator->() const noexcept;
    uint32_t use_count() const noexcept;
    explicit operator bool() const noexcept;

    friend bptr<T> static_pointer_cast<T>(const bptr<T>& p);
};

template<typename T> constexpr bptr<T>::bptr(NoInitT) noexcept {};
template<typename T> constexpr bptr<T>::bptr(std::nullptr_t) noexcept: ptr{nullptr}, blk{nullptr} {}
template<typename T> constexpr bptr<T>::bptr() noexcept: bptr{nullptr} {}


template<typename T> bptr(T* ptr) -> bptr<T>;

} // namespace floormat