Skip to content

Commit cd08b22

Browse files
committed
Implement Ref copy constructor
1 parent dce9f77 commit cd08b22

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

include/godot_cpp/classes/ref.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,31 @@ class Ref {
120120

121121
template <class T_Other>
122122
void operator=(const Ref<T_Other> &p_from) {
123+
/* Object::cast_to<T> does not work reliably, see: https://github.com/godotengine/godot-cpp/issues/610
123124
RefCounted *refb = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_from.ptr()));
124125
if (!refb) {
125126
unref();
126127
return;
127128
}
129+
128130
Ref r;
129131
r.reference = Object::cast_to<T>(refb);
130132
ref(r);
131133
r.reference = nullptr;
134+
*/
135+
136+
// Let C++ do the casting, more limited but will give a compiler error for incompatible casts...
137+
Ref r;
138+
r.reference = const_cast<T *>(static_cast<const T *>(p_from.ptr()));
139+
ref(r);
140+
r.reference = nullptr;
132141
}
133142

134143
void operator=(const Variant &p_variant) {
135144
// FIXME
145+
// Object::cast_to<T> does not work reliably, see: https://github.com/godotengine/godot-cpp/issues/610
146+
// As variant stores our Godot pointer (_owner) we can't reliably cast here until we fix the above mentioned issue
147+
136148
// Object *object = p_variant.get_validated_object();
137149

138150
// if (object == reference) {
@@ -168,6 +180,28 @@ class Ref {
168180
ref(p_from);
169181
}
170182

183+
template <class T_Other>
184+
Ref(const Ref<T_Other> &p_from) {
185+
/* Object::cast_to<T> does not work reliably, see: https://github.com/godotengine/godot-cpp/issues/610
186+
RefCounted *refb = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_from.ptr()));
187+
if (!refb) {
188+
unref();
189+
return;
190+
}
191+
192+
Ref r;
193+
r.reference = Object::cast_to<T>(refb);
194+
ref(r);
195+
r.reference = nullptr;
196+
*/
197+
198+
// cast to the type we contain
199+
reference = const_cast<T *>(static_cast<const T *>(p_from.ptr()));
200+
if (reference) { // pretty sure we would get a compiler error if not applicable...
201+
reference->reference();
202+
}
203+
}
204+
171205
Ref(T *p_reference) {
172206
if (p_reference) {
173207
ref_pointer(p_reference);
@@ -176,6 +210,9 @@ class Ref {
176210

177211
Ref(const Variant &p_variant) {
178212
// FIXME
213+
// Object::cast_to<T> does not work reliably, see: https://github.com/godotengine/godot-cpp/issues/610
214+
// As variant stores our Godot pointer (_owner) we can't reliably cast here until we fix the above mentioned issue
215+
179216
// Object *object = p_variant.get_validated_object();
180217

181218
// if (!object) {

0 commit comments

Comments
 (0)