forked from nbdd0121/GenerationalGC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandle.h
More file actions
141 lines (110 loc) · 3.2 KB
/
Handle.h
File metadata and controls
141 lines (110 loc) · 3.2 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef NORLIT_GC_HANDLE_H
#define NORLIT_GC_HANDLE_H
#include "Object.h"
#include <utility>
#include <typeinfo>
#include <type_traits>
namespace norlit {
namespace gc {
namespace detail {
class HandleBase {
Object** object_;
protected:
HandleBase();
HandleBase(Object* obj);
HandleBase(const HandleBase& obj);
HandleBase(HandleBase&& obj);
~HandleBase();
void operator =(Object* obj);
void operator =(const HandleBase& obj);
void operator =(HandleBase&& obj);
Object* Get() const {
return object_?*object_:nullptr;
}
};
}
template<typename T>
class Handle: detail::HandleBase {
public:
Handle(std::nullptr_t) : HandleBase() {}
Handle(T* obj) :HandleBase(reinterpret_cast<Object*>(obj)) {}
Handle() : HandleBase() {}
Handle(const Handle& h) :HandleBase(h) {}
Handle(Handle&& h) :HandleBase(std::move(h)) {}
template<typename U, typename = typename std::enable_if<
std::is_base_of<T, U>::value &&
!std::is_same<T, U>::value
>::type>
Handle(const Handle<U>& h) : HandleBase(h) {}
template<typename U, typename = typename std::enable_if<
std::is_base_of<T, U>::value &&
!std::is_same<T, U>::value
>::type>
Handle(Handle<U>&& h) : HandleBase(std::move(h)) {}
void operator =(T* obj) {
HandleBase::operator =(obj);
}
void operator =(const Handle& h) {
HandleBase::operator =(h);
}
void operator =(Handle&& h) {
HandleBase::operator =(std::move(h));
}
template<typename U, typename = typename std::enable_if<
std::is_base_of<T, U>::value &&
!std::is_same<T, U>::value
>::type>
void operator =(const Handle<U>& h) {
HandleBase::operator =(h);
}
template<typename U, typename = typename std::enable_if<
std::is_base_of<T, U>::value &&
!std::is_same<T, U>::value
>::type>
void operator =(Handle<U>&& h) {
HandleBase::operator =(std::move(h));
}
T* operator ->() const {
return static_cast<T*>(Get());
}
explicit operator bool() const {
return !!operator T*();
}
operator T*() const {
return reinterpret_cast<T*>(Get());
}
template<typename = std::enable_if<!std::is_same<T, Object>::value>>
explicit operator Object*() const {
return Get();
}
template<typename D>
explicit operator D*() const {
return static_cast<D*>(operator T*());
}
template<typename U>
Handle<U> CastTo() const {
return static_cast<U*>(operator T*());
}
const std::type_info& TypeId() const {
return typeid(*operator T*());
}
template<typename U>
bool ExactInstanceOf() const {
return TypeId() == typeid(U);
}
template<typename U>
Handle<U> DynamicCastTo() const {
return dynamic_cast<U*>(operator T*());
}
template<typename U>
Handle<U> ExactCheckedCastTo() const {
if (ExactInstanceOf<U>()) {
return CastTo<U>();
}
return nullptr;
}
template<class S> friend class Handle;
};
}
}
#endif