Implement Ref copy constructor

pull/602/head
Bastiaan Olij 2021-09-14 14:43:08 +10:00
parent ea7324afe8
commit fad6329699
1 changed files with 39 additions and 20 deletions

View File

@ -125,6 +125,7 @@ public:
unref(); unref();
return; return;
} }
Ref r; Ref r;
r.reference = Object::cast_to<T>(refb); r.reference = Object::cast_to<T>(refb);
ref(r); ref(r);
@ -132,23 +133,25 @@ public:
} }
void operator=(const Variant &p_variant) { void operator=(const Variant &p_variant) {
// FIXME // Needs testing, Variant has a cast to Object * here.
// Object *object = p_variant.get_validated_object(); // Object *object = p_variant.get_validated_object();
Object *object = p_variant;
// if (object == reference) { if (object == reference) {
// return; return;
// } }
// unref(); unref();
// if (!object) { if (!object) {
// return; return;
// } }
// T *r = Object::cast_to<T>(object); T *r = Object::cast_to<T>(object);
// if (r && r->reference()) { if (r && r->reference()) {
// reference = r; reference = r;
// } }
} }
template <class T_Other> template <class T_Other>
@ -168,6 +171,20 @@ public:
ref(p_from); ref(p_from);
} }
template <class T_Other>
Ref(const Ref<T_Other> &p_from) {
RefCounted *refb = const_cast<RefCounted *>(static_cast<const RefCounted *>(p_from.ptr()));
if (!refb) {
unref();
return;
}
Ref r;
r.reference = Object::cast_to<T>(refb);
ref(r);
r.reference = nullptr;
}
Ref(T *p_reference) { Ref(T *p_reference) {
if (p_reference) { if (p_reference) {
ref_pointer(p_reference); ref_pointer(p_reference);
@ -175,17 +192,19 @@ public:
} }
Ref(const Variant &p_variant) { Ref(const Variant &p_variant) {
// FIXME // Needs testing, Variant has a cast to Object * here.
// Object *object = p_variant.get_validated_object(); // Object *object = p_variant.get_validated_object();
Object *object = p_variant;
// if (!object) { if (!object) {
// return; return;
// } }
// T *r = Object::cast_to<T>(object); T *r = Object::cast_to<T>(object);
// if (r && r->reference()) { if (r && r->reference()) {
// reference = r; reference = r;
// } }
} }
inline bool is_valid() const { return reference != nullptr; } inline bool is_valid() const { return reference != nullptr; }