Skip to content

Commit

Permalink
Merge pull request #21 from BastiaanOlij/ref_copy_constructor
Browse files Browse the repository at this point in the history
Implement Ref copy constructor
  • Loading branch information
BastiaanOlij authored Sep 27, 2021
2 parents 11e44af + 6e6886d commit 8fefe1a
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions include/godot_cpp/classes/ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,30 +125,33 @@ class Ref {
unref();
return;
}

Ref r;
r.reference = Object::cast_to<T>(refb);
ref(r);
r.reference = nullptr;
}

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;

// if (object == reference) {
// return;
// }
if (object == reference) {
return;
}

// unref();
unref();

// if (!object) {
// return;
// }
if (!object) {
return;
}

// T *r = Object::cast_to<T>(object);
// if (r && r->reference()) {
// reference = r;
// }
T *r = Object::cast_to<T>(object);
if (r && r->reference()) {
reference = r;
}
}

template <class T_Other>
Expand All @@ -168,24 +171,40 @@ class Ref {
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) {
if (p_reference) {
ref_pointer(p_reference);
}
}

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;

// if (!object) {
// return;
// }
if (!object) {
return;
}

// T *r = Object::cast_to<T>(object);
// if (r && r->reference()) {
// reference = r;
// }
T *r = Object::cast_to<T>(object);
if (r && r->reference()) {
reference = r;
}
}

inline bool is_valid() const { return reference != nullptr; }
Expand Down

0 comments on commit 8fefe1a

Please sign in to comment.