blob: 95cd3baed1ce2dd80d57b75ee226894afe70102b (
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
|
#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:
~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{ Utility::forward<Ts>(args)... })
{}
safe_ptr& operator=(safe_ptr&& other) noexcept
{
fm_assert(this != &other);
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; }
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
|