-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_ptr.cpp
More file actions
103 lines (83 loc) · 2.03 KB
/
Copy pathobject_ptr.cpp
File metadata and controls
103 lines (83 loc) · 2.03 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "PAZ_Engine"
#include "object.hpp"
//TEMP - VECTOR REALLOC CHANGES IDS/PTRS (NEED MODIFY OBJ'S MOVE CTOR & DELETE OBJ'S COPY CTOR TO FIX)
#define OBJ_EXISTS objects().count(_id) //TEMP - wrong if new object has same ID
static const auto NullId = reinterpret_cast<std::uintptr_t>(nullptr);
paz::ObjectPtr::ObjectPtr() : _id(NullId) {}
paz::ObjectPtr::ObjectPtr(const Object& o) : _id(reinterpret_cast<std::
uintptr_t>(&o)) {}
paz::ObjectPtr::ObjectPtr(std::nullptr_t) : ObjectPtr() {}
void paz::ObjectPtr::swap(ObjectPtr& p) noexcept
{
std::swap(_id, p._id);
}
const paz::Object* paz::ObjectPtr::get() const
{
if(_id == NullId)
{
throw std::runtime_error("Object pointer is null.");
}
if(!OBJ_EXISTS)
{
throw std::runtime_error("Object has been deleted.");
}
return reinterpret_cast<const Object*>(_id);
}
paz::Object* paz::ObjectPtr::get()
{
if(_id == NullId)
{
throw std::runtime_error("Object pointer is null.");
}
if(!OBJ_EXISTS)
{
throw std::runtime_error("Object has been deleted.");
}
return reinterpret_cast<Object*>(_id);
}
paz::ObjectPtr& paz::ObjectPtr::operator=(std::nullptr_t)
{
_id = NullId;
return *this;
}
const paz::Object& paz::ObjectPtr::operator*() const
{
return *get();
}
paz::Object& paz::ObjectPtr::operator*()
{
return *get();
}
const paz::Object* paz::ObjectPtr::operator->() const
{
return get();
}
paz::Object* paz::ObjectPtr::operator->()
{
return get();
}
paz::ObjectPtr::operator bool() const
{
return _id != NullId && OBJ_EXISTS;
}
bool paz::ObjectPtr::operator==(const ObjectPtr& p) const
{
return _id == p._id;
}
bool paz::ObjectPtr::operator!=(const ObjectPtr& p) const
{
return !(*this == p);
}
void paz::ObjectPtr::reset() noexcept
{
_id = NullId;
}
void paz::ObjectPtr::reset(const Object& o) noexcept
{
_id = reinterpret_cast<std::uintptr_t>(&o);
}
std::ostream& paz::operator<<(std::ostream& stream, const paz::ObjectPtr& p)
{
stream << p.get();
return stream;
}