summaryrefslogtreecommitdiffhomepage
path: root/compat/safe-ptr.hpp
blob: ec4a8e0233258da6cb088fd22e09d626d2ecb45b (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
#pragma once
#include "compat/assert.hpp"
#include "compat/defs.hpp"
#include <type_traits>
#include <Corrade/Tags.h>
#include <Corrade/Utility/Move.h>

namespace floormat {

template<typename T>
class safe_ptr final
{
    T* ptr;

public:
    template<typename... Ts>
    requires requires (Ts&&... xs) {
        new T{Utility::forward<Ts>(xs)...};
    }
    safe_ptr(InPlaceInitT, Ts&&... args) noexcept:
        ptr{new T{Utility::forward<Ts>(args)...}}
    {}

    explicit safe_ptr(T*&& ptr) noexcept: ptr{ptr}
    {
        fm_assert(ptr != nullptr);
        ptr = nullptr;
    }

    ~safe_ptr() noexcept
    {
        if (ptr) [[likely]]
            delete ptr;
        ptr = (T*)-0xbadbabe;
    }

    explicit safe_ptr(safe_ptr&& other) noexcept: ptr{other.ptr}
    {
        other.ptr = nullptr;
    }

    safe_ptr& operator=(safe_ptr&& other) noexcept
    {
        fm_assert(this != &other);
        if (ptr) [[likely]]
            delete ptr;
        ptr = other.ptr;
        other.ptr = nullptr;
        return *this;
    }

    fm_DECLARE_DELETED_COPY_ASSIGNMENT(safe_ptr);

    //explicit operator bool() const noexcept { return ptr != nullptr; }

    const T& operator*() const noexcept { return *ptr; }
    T& operator*() noexcept { return *ptr; }
    const T* operator->() const noexcept { return ptr; }
    T* operator->() noexcept { return ptr; }
};

} // namespace floormat