-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGcPointer.hpp
More file actions
80 lines (66 loc) · 1.81 KB
/
GcPointer.hpp
File metadata and controls
80 lines (66 loc) · 1.81 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
#pragma once
#include "common_macros.h"
template<class T>
class GcPointer final {
public:
GcPointer() noexcept {
setRawPointerNull();
}
GcPointer(const GcPointer& rhs) noexcept {
this->rawPointer = rhs.rawPointer;
ensureRawPointerInMemory();
}
GcPointer(GcPointer&& rhs) noexcept {
this->rawPointer = rhs.rawPointer;
rhs.rawPointer = nullptr;
ensureRawPointerInMemory();
rhs.ensureRawPointerInMemory();
}
explicit GcPointer(T* rhs) noexcept {
this->rawPointer = rhs;
ensureRawPointerInMemory();
}
GcPointer& operator=(const GcPointer& rhs) noexcept {
if (this == &rhs) {
return *this;
}
this->rawPointer = rhs.rawPointer;
ensureRawPointerInMemory();
return *this;
}
GcPointer& operator=(GcPointer&& rhs) noexcept {
this->rawPointer = rhs.rawPointer;
rhs.rawPointer = nullptr;
ensureRawPointerInMemory();
rhs.ensureRawPointerInMemory();
return *this;
}
GcPointer& operator=(T* rhs) noexcept {
this->rawPointer = rhs;
ensureRawPointerInMemory();
return *this;
}
~GcPointer() noexcept {
setRawPointerNull();
}
T* getRawPointer() const noexcept {
return this->rawPointer;
}
T* operator->() const noexcept {
return this->rawPointer;
}
private:
T* rawPointer;
void setRawPointerNull() noexcept {
rawPointer = nullptr;
ensureRawPointerInMemory();
}
void ensureRawPointerInMemory() const noexcept {
__asm__ __volatile__ ("" : : "m"(rawPointer));
}
};
template<class T, class... Args>
static GcPointer<T> GcNew(const Args&... args)
{
return GcPointer<T>{new T { COMMON_MACROS_helper_forward<Args>(args)... }};
}