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

#ifdef FM_NO_WEAK_BPTR
#error weak_bptr not supported!
#endif

namespace floormat {

template<typename T>
class weak_bptr final
{
    detail_bptr::control_block* blk;

    static detail_bptr::control_block* _copy(detail_bptr::control_block* ptr);
    weak_bptr& _copy_assign(detail_bptr::control_block* other) noexcept;
    weak_bptr& _move_assign(detail_bptr::control_block*& other) noexcept;

public:
    weak_bptr(std::nullptr_t) noexcept;
    weak_bptr& operator=(std::nullptr_t) noexcept;
    weak_bptr() noexcept;
    ~weak_bptr() noexcept;

    template<detail_bptr::DerivedFrom<T> Y> weak_bptr(const bptr<Y>& ptr) noexcept;
    template<detail_bptr::DerivedFrom<T> Y> weak_bptr(const weak_bptr<Y>& ptr) noexcept;
    weak_bptr(const weak_bptr& ptr) noexcept;

    template<detail_bptr::DerivedFrom<T> Y> weak_bptr& operator=(const bptr<Y>& ptr) noexcept;
    template<detail_bptr::DerivedFrom<T> Y> weak_bptr& operator=(const weak_bptr<Y>& ptr) noexcept;
    weak_bptr& operator=(const weak_bptr& ptr) noexcept;

    template<detail_bptr::DerivedFrom<T> Y> weak_bptr(weak_bptr<Y>&& ptr) noexcept;
    weak_bptr(weak_bptr&& ptr) noexcept;

    template<detail_bptr::DerivedFrom<T> Y> weak_bptr& operator=(weak_bptr<Y>&& ptr) noexcept;
    weak_bptr& operator=(weak_bptr&& ptr) noexcept;

    void reset() noexcept;
    void swap(weak_bptr& other) noexcept;

    uint32_t use_count() const noexcept;
    bool expired() const noexcept;
    bptr<T> lock() const noexcept;

    bool operator==(const weak_bptr<const T>& other) const noexcept;
};

} // namespace floormat