diff options
| author | Bastiaan Olij <mux213@gmail.com> | 2021-09-14 14:43:08 +1000 |
|---|---|---|
| committer | Bastiaan Olij <mux213@gmail.com> | 2021-09-27 23:08:12 +1000 |
| commit | fad6329699f336336d7c427d1df02ec849605828 (patch) | |
| tree | 1ae5196e4c226ca1d3338c6d2852e51d21a4778e /include/godot_cpp | |
| parent | ea7324afe8567db7601ffc50f85aec5b7e719eb6 (diff) | |
| download | redot-cpp-fad6329699f336336d7c427d1df02ec849605828.tar.gz | |
Implement Ref copy constructor
Diffstat (limited to 'include/godot_cpp')
| -rw-r--r-- | include/godot_cpp/classes/ref.hpp | 59 |
1 files changed, 39 insertions, 20 deletions
diff --git a/include/godot_cpp/classes/ref.hpp b/include/godot_cpp/classes/ref.hpp index 2ca5f83..b5fec34 100644 --- a/include/godot_cpp/classes/ref.hpp +++ b/include/godot_cpp/classes/ref.hpp @@ -125,6 +125,7 @@ public: unref(); return; } + Ref r; r.reference = Object::cast_to<T>(refb); ref(r); @@ -132,23 +133,25 @@ public: } 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> @@ -168,6 +171,20 @@ public: 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); @@ -175,17 +192,19 @@ public: } 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; } |
