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

namespace floormat {

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

public:
    ~safe_ptr() noexcept
    {
        delete ptr;
        ptr = (T*)0xbadcafedeadbabe;
    }

    safe_ptr(std::nullptr_t) = delete;
    safe_ptr(T* ptr) noexcept: ptr{ptr} { fm_assert(ptr != nullptr); }
    safe_ptr(safe_ptr&& other) noexcept: ptr{other.ptr} { other.ptr = nullptr; }

    safe_ptr() noexcept: safe_ptr{InPlaceInit} {}

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

    safe_ptr(const safe_ptr& other) noexcept:
          ptr{new T{*other.ptr}}
    {}

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

    safe_ptr& operator=(const safe_ptr& other) noexcept
    {
        if (ptr != other.ptr)
        {
            delete ptr;
            ptr = nullptr;
            if (other.ptr)
                ptr = new T{*other.ptr};
        }
        return *this;
    }

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

    T* get() noexcept { return ptr; }
    const T* get() const noexcept { return ptr; }
    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