blob: 8cbd134c9b29792d52c14d65304fa0f90f04f750 (
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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
#ifndef REF_H
#define REF_H
#include "GodotGlobal.hpp"
#include "Reference.hpp"
#include "Variant.hpp"
namespace godot {
// Replicates Godot's Ref<T> behavior
// Rewritten from f5234e70be7dec4930c2d5a0e829ff480d044b1d.
template <class T>
class Ref {
T *reference = nullptr;
void ref(const Ref &p_from) {
if (p_from.reference == reference)
return;
unref();
reference = p_from.reference;
if (reference)
reference->reference();
}
void ref_pointer(T *p_ref) {
ERR_FAIL_COND(!p_ref);
if (p_ref->init_ref())
reference = p_ref;
}
public:
inline bool operator<(const Ref<T> &p_r) const {
return reference < p_r.reference;
}
inline bool operator==(const Ref<T> &p_r) const {
return reference == p_r.reference;
}
inline bool operator!=(const Ref<T> &p_r) const {
return reference != p_r.reference;
}
inline T *operator->() {
return reference;
}
inline T *operator*() {
return reference;
}
inline const T *operator->() const {
return reference;
}
inline const T *ptr() const {
return reference;
}
inline T *ptr() {
return reference;
}
inline const T *operator*() const {
return reference;
}
operator Variant() const {
// Note: the C API handles the cases where the object is a Reference,
// so the Variant will be correctly constructed with a RefPtr engine-side
return Variant((Object *)reference);
}
void operator=(const Ref &p_from) {
ref(p_from);
}
template <class T_Other>
void operator=(const Ref<T_Other> &p_from) {
// TODO We need a safe cast
Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
if (!refb) {
unref();
return;
}
Ref r;
//r.reference = Object::cast_to<T>(refb);
r.reference = (T *)refb;
ref(r);
r.reference = nullptr;
}
void operator=(const Variant &p_variant) {
// TODO We need a safe cast
Reference *refb = (Reference *)T::___get_from_variant(p_variant);
if (!refb) {
unref();
return;
}
Ref r;
// TODO We need a safe cast
//r.reference = Object::cast_to<T>(refb);
r.reference = (T *)refb;
ref(r);
r.reference = nullptr;
}
Ref(const Ref &p_from) {
reference = nullptr;
ref(p_from);
}
template <class T_Other>
Ref(const Ref<T_Other> &p_from) {
reference = nullptr;
// TODO We need a safe cast
Reference *refb = const_cast<Reference *>(static_cast<const Reference *>(p_from.ptr()));
if (!refb) {
unref();
return;
}
Ref r;
// TODO We need a safe cast
//r.reference = Object::cast_to<T>(refb);
r.reference = (T *)refb;
ref(r);
r.reference = nullptr;
}
Ref(T *p_reference) {
if (p_reference)
ref_pointer(p_reference);
else
reference = nullptr;
}
Ref(const Variant &p_variant) {
reference = nullptr;
// TODO We need a safe cast
Reference *refb = (Reference *)T::___get_from_variant(p_variant);
if (!refb) {
unref();
return;
}
Ref r;
// TODO We need a safe cast
//r.reference = Object::cast_to<T>(refb);
r.reference = (T *)refb;
ref(r);
r.reference = nullptr;
}
inline bool is_valid() const { return reference != nullptr; }
inline bool is_null() const { return reference == nullptr; }
void unref() {
//TODO this should be moved to mutexes, since this engine does not really
// do a lot of referencing on references and stuff
// mutexes will avoid more crashes?
if (reference && reference->unreference()) {
//memdelete(reference);
reference->free();
}
reference = nullptr;
}
void instance() {
//ref(memnew(T));
ref(T::_new());
}
Ref() {
reference = nullptr;
}
~Ref() {
unref();
}
// Used exclusively in the bindings to recreate the Ref Godot encapsulates in return values,
// without adding to the refcount.
inline static Ref<T> __internal_constructor(Object *obj) {
Ref<T> r;
r.reference = (T *)obj;
return r;
}
};
} // namespace godot
#endif
|