diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2021-08-10 16:42:31 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-10 16:42:31 +0200 |
commit | 46beaacec3d18c0d5f1d744e2c63ca09f5152c85 (patch) | |
tree | e9520a708f1a8f65a829547c281f1cbedf38cca8 /core/variant | |
parent | 50d5569ad49752803b980eca85590f5362b12409 (diff) | |
parent | ee6e05ee68afc7841848382fbd538fdf7369b214 (diff) |
Merge pull request #51017 from vnen/extension-fixes
Diffstat (limited to 'core/variant')
-rw-r--r-- | core/variant/variant.cpp | 3 | ||||
-rw-r--r-- | core/variant/variant.h | 12 | ||||
-rw-r--r-- | core/variant/variant_call.cpp | 2 | ||||
-rw-r--r-- | core/variant/variant_destruct.cpp | 78 | ||||
-rw-r--r-- | core/variant/variant_destruct.h | 76 | ||||
-rw-r--r-- | core/variant/variant_internal.h | 20 |
6 files changed, 188 insertions, 3 deletions
diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp index 97a1b4c02a..d538b9faff 100644 --- a/core/variant/variant.cpp +++ b/core/variant/variant.cpp @@ -3533,12 +3533,13 @@ void Variant::register_types() { _register_variant_methods(); _register_variant_setters_getters(); _register_variant_constructors(); + _register_variant_destructors(); _register_variant_utility_functions(); } void Variant::unregister_types() { _unregister_variant_operators(); _unregister_variant_methods(); _unregister_variant_setters_getters(); - _unregister_variant_constructors(); + _unregister_variant_destructors(); _unregister_variant_utility_functions(); } diff --git a/core/variant/variant.h b/core/variant/variant.h index 780f9b4e70..9ec131a1b8 100644 --- a/core/variant/variant.h +++ b/core/variant/variant.h @@ -208,7 +208,7 @@ private: Transform3D *_transform3d; PackedArrayRefBase *packed_array; void *_ptr; //generic pointer - uint8_t _mem[sizeof(ObjData) > (sizeof(real_t) * 4) ? sizeof(ObjData) : (sizeof(real_t) * 4)]; + uint8_t _mem[sizeof(ObjData) > (sizeof(real_t) * 4) ? sizeof(ObjData) : (sizeof(real_t) * 4)]{ 0 }; } _data alignas(8); void reference(const Variant &p_variant); @@ -271,6 +271,8 @@ private: static void _register_variant_setters_getters(); static void _unregister_variant_setters_getters(); static void _register_variant_constructors(); + static void _unregister_variant_destructors(); + static void _register_variant_destructors(); static void _unregister_variant_constructors(); static void _register_variant_utility_functions(); static void _unregister_variant_utility_functions(); @@ -534,6 +536,14 @@ public: static void get_constructor_list(Type p_type, List<MethodInfo> *r_list); //convenience + /* Destructors */ + + // Only ptrcall is available. + typedef void (*PTRDestructor)(void *base); + + static PTRDestructor get_ptr_destructor(Variant::Type p_type); + static bool has_destructor(Variant::Type p_type); + /* Properties */ void set_named(const StringName &p_member, const Variant &p_value, bool &r_valid); diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 14840f0d8a..c3481d4896 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1709,7 +1709,7 @@ static void _register_variant_builtin_methods() { bind_method(Transform2D, interpolate_with, sarray("xform", "weight"), varray()); bind_method(Transform2D, is_equal_approx, sarray("xform"), varray()); bind_method(Transform2D, set_rotation, sarray("rotation"), varray()); - bind_method(Transform2D, looking_at, sarray("target"), varray(Transform2D())); + bind_method(Transform2D, looking_at, sarray("target"), varray(Vector2())); /* Basis */ diff --git a/core/variant/variant_destruct.cpp b/core/variant/variant_destruct.cpp new file mode 100644 index 0000000000..366b71df3a --- /dev/null +++ b/core/variant/variant_destruct.cpp @@ -0,0 +1,78 @@ +/*************************************************************************/ +/* variant_destruct.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "variant_destruct.h" + +#include "core/templates/local_vector.h" + +static Variant::PTRDestructor destruct_pointers[Variant::VARIANT_MAX] = { nullptr }; + +template <class T> +static void add_destructor() { + destruct_pointers[T::get_base_type()] = T::ptr_destruct; +} + +void Variant::_register_variant_destructors() { + add_destructor<VariantDestruct<String>>(); + add_destructor<VariantDestruct<Transform2D>>(); + add_destructor<VariantDestruct<::AABB>>(); + add_destructor<VariantDestruct<Basis>>(); + add_destructor<VariantDestruct<Transform3D>>(); + add_destructor<VariantDestruct<StringName>>(); + add_destructor<VariantDestruct<NodePath>>(); + add_destructor<VariantDestruct<::RID>>(); + add_destructor<VariantDestruct<Callable>>(); + add_destructor<VariantDestruct<Signal>>(); + add_destructor<VariantDestruct<Dictionary>>(); + add_destructor<VariantDestruct<Array>>(); + add_destructor<VariantDestruct<PackedByteArray>>(); + add_destructor<VariantDestruct<PackedInt32Array>>(); + add_destructor<VariantDestruct<PackedInt64Array>>(); + add_destructor<VariantDestruct<PackedFloat32Array>>(); + add_destructor<VariantDestruct<PackedFloat64Array>>(); + add_destructor<VariantDestruct<PackedStringArray>>(); + add_destructor<VariantDestruct<PackedVector2Array>>(); + add_destructor<VariantDestruct<PackedVector3Array>>(); + add_destructor<VariantDestruct<PackedColorArray>>(); +} + +void Variant::_unregister_variant_destructors() { + // Nothing to be done. +} + +Variant::PTRDestructor Variant::get_ptr_destructor(Variant::Type p_type) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr); + return destruct_pointers[p_type]; +} + +bool Variant::has_destructor(Variant::Type p_type) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, false); + return destruct_pointers[p_type] != nullptr; +} diff --git a/core/variant/variant_destruct.h b/core/variant/variant_destruct.h new file mode 100644 index 0000000000..7356e42201 --- /dev/null +++ b/core/variant/variant_destruct.h @@ -0,0 +1,76 @@ +/*************************************************************************/ +/* variant_destruct.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef VARIANT_DESTRUCT_H +#define VARIANT_DESTRUCT_H + +#include "core/variant/variant.h" + +#include "core/object/class_db.h" + +template <class T> +struct VariantDestruct {}; + +#define MAKE_PTRDESTRUCT(m_type) \ + template <> \ + struct VariantDestruct<m_type> { \ + _FORCE_INLINE_ static void ptr_destruct(void *p_ptr) { \ + reinterpret_cast<m_type *>(p_ptr)->~m_type(); \ + } \ + _FORCE_INLINE_ static Variant::Type get_base_type() { \ + return GetTypeInfo<m_type>::VARIANT_TYPE; \ + } \ + } + +MAKE_PTRDESTRUCT(String); +MAKE_PTRDESTRUCT(Transform2D); +MAKE_PTRDESTRUCT(AABB); +MAKE_PTRDESTRUCT(Basis); +MAKE_PTRDESTRUCT(Transform3D); +MAKE_PTRDESTRUCT(StringName); +MAKE_PTRDESTRUCT(NodePath); +MAKE_PTRDESTRUCT(RID); +MAKE_PTRDESTRUCT(Callable); +MAKE_PTRDESTRUCT(Signal); +MAKE_PTRDESTRUCT(Dictionary); +MAKE_PTRDESTRUCT(Array); +MAKE_PTRDESTRUCT(PackedByteArray); +MAKE_PTRDESTRUCT(PackedInt32Array); +MAKE_PTRDESTRUCT(PackedInt64Array); +MAKE_PTRDESTRUCT(PackedFloat32Array); +MAKE_PTRDESTRUCT(PackedFloat64Array); +MAKE_PTRDESTRUCT(PackedStringArray); +MAKE_PTRDESTRUCT(PackedVector2Array); +MAKE_PTRDESTRUCT(PackedVector3Array); +MAKE_PTRDESTRUCT(PackedColorArray); + +#undef MAKE_PTRDESTRUCT + +#endif // VARIANT_DESTRUCT_H diff --git a/core/variant/variant_internal.h b/core/variant/variant_internal.h index 78e1ad06ae..566b14736e 100644 --- a/core/variant/variant_internal.h +++ b/core/variant/variant_internal.h @@ -1171,6 +1171,11 @@ struct VariantInitializer<PackedColorArray> { static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_color_array(v); } }; +template <> +struct VariantInitializer<Object *> { + static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::object_assign_null(v); } +}; + template <class T> struct VariantZeroAssigner { }; @@ -1385,4 +1390,19 @@ struct VariantTypeAdjust<Object *> { } }; +// GDNative extension helpers. + +template <class T> +struct VariantTypeConstructor { + _FORCE_INLINE_ static void variant_from_type(void *p_variant, void *p_value) { + Variant *variant = reinterpret_cast<Variant *>(p_variant); + VariantInitializer<T>::init(variant); + VariantInternalAccessor<T>::set(variant, *((T *)p_value)); + } + + _FORCE_INLINE_ static void type_from_variant(void *p_value, void *p_variant) { + *((T *)p_value) = VariantInternalAccessor<T>::get(reinterpret_cast<Variant *>(p_variant)); + } +}; + #endif // VARIANT_INTERNAL_H |