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

#define FM_BPTR_DEBUG

#ifdef FM_NO_DEBUG
#undef FM_BPTR_DEBUG
#endif
#ifdef FM_BPTR_DEBUG
#include "compat/assert.hpp"
#define fm_bptr_assert(...) fm_assert(__VA_ARGS__)
#else
#define fm_bptr_assert(...) void()
#endif

namespace floormat::detail_borrowed_ptr {

//static_assert(std::is_same_v<T, U> || std::has_virtual_destructor_v<T> && std::has_virtual_destructor_v<T>); // todo! for simple_bptr

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

} // namespace floormat::detail_borrowed_ptr

namespace floormat {

template<typename T, typename U>
bptr<U> static_pointer_cast(const bptr<T>& p) noexcept
{
    static_assert(detail_borrowed_ptr::StaticCastable<T, U>);

    if (p.blk) [[likely]]
    {
        if (auto* ptr = p.blk->_ptr)
        {
            fm_bptr_assert(p.casted_ptr);
            auto* ret = static_cast<U*>(p.casted_ptr);
            return bptr<U>{DirectInit, ret, p.blk};
        }
    }
    return bptr<U>{nullptr};
}

} // namespace floormat