diff options
author | George Marques <george@gmarqu.es> | 2020-12-15 16:31:18 -0300 |
---|---|---|
committer | George Marques <george@gmarqu.es> | 2020-12-15 16:57:44 -0300 |
commit | 35e7490c9583f5fed9c419666be7451d1a4f035c (patch) | |
tree | 5bb5395266dd1a0d05f9b4010f4ec5587313f1bd /core/variant | |
parent | edb3686ee2379ad28d1273797db8bd9ded38ff1e (diff) |
Add PtrConstruct template to use in Variant constructors
Since the PtrToArg::encode requires the value to be constructed
previously. With PtrConstruct this is not required.
Diffstat (limited to 'core/variant')
-rw-r--r-- | core/variant/variant_construct.cpp | 74 |
1 files changed, 64 insertions, 10 deletions
diff --git a/core/variant/variant_construct.cpp b/core/variant/variant_construct.cpp index 3bb3fa3634..02866b4475 100644 --- a/core/variant/variant_construct.cpp +++ b/core/variant/variant_construct.cpp @@ -39,6 +39,60 @@ #include "core/templates/local_vector.h" #include "core/templates/oa_hash_map.h" +template <class T> +struct PtrConstruct {}; + +#define MAKE_PTRCONSTRUCT(m_type) \ + template <> \ + struct PtrConstruct<m_type> { \ + _FORCE_INLINE_ static void construct(const m_type &p_value, void *p_ptr) { \ + memnew_placement(p_ptr, m_type(p_value)); \ + } \ + }; + +MAKE_PTRCONSTRUCT(bool); +MAKE_PTRCONSTRUCT(int64_t); +MAKE_PTRCONSTRUCT(double); +MAKE_PTRCONSTRUCT(String); +MAKE_PTRCONSTRUCT(Vector2); +MAKE_PTRCONSTRUCT(Vector2i); +MAKE_PTRCONSTRUCT(Rect2); +MAKE_PTRCONSTRUCT(Rect2i); +MAKE_PTRCONSTRUCT(Vector3); +MAKE_PTRCONSTRUCT(Vector3i); +MAKE_PTRCONSTRUCT(Transform2D); +MAKE_PTRCONSTRUCT(Plane); +MAKE_PTRCONSTRUCT(Quat); +MAKE_PTRCONSTRUCT(AABB); +MAKE_PTRCONSTRUCT(Basis); +MAKE_PTRCONSTRUCT(Transform); +MAKE_PTRCONSTRUCT(Color); +MAKE_PTRCONSTRUCT(StringName); +MAKE_PTRCONSTRUCT(NodePath); +MAKE_PTRCONSTRUCT(RID); + +template <> +struct PtrConstruct<Object *> { + _FORCE_INLINE_ static void construct(Object *p_value, void *p_ptr) { + *((Object **)p_ptr) = p_value; + } +}; + +MAKE_PTRCONSTRUCT(Callable); +MAKE_PTRCONSTRUCT(Signal); +MAKE_PTRCONSTRUCT(Dictionary); +MAKE_PTRCONSTRUCT(Array); +MAKE_PTRCONSTRUCT(PackedByteArray); +MAKE_PTRCONSTRUCT(PackedInt32Array); +MAKE_PTRCONSTRUCT(PackedInt64Array); +MAKE_PTRCONSTRUCT(PackedFloat32Array); +MAKE_PTRCONSTRUCT(PackedFloat64Array); +MAKE_PTRCONSTRUCT(PackedStringArray); +MAKE_PTRCONSTRUCT(PackedVector2Array); +MAKE_PTRCONSTRUCT(PackedVector3Array); +MAKE_PTRCONSTRUCT(PackedColorArray); +MAKE_PTRCONSTRUCT(Variant); + template <class T, class... P> class VariantConstructor { template <size_t... Is> @@ -59,7 +113,7 @@ class VariantConstructor { template <size_t... Is> static _FORCE_INLINE_ void ptr_construct_helper(void *base, const void **p_args, IndexSequence<Is...>) { - PtrToArg<T>::encode(T(PtrToArg<P>::convert(p_args[Is])...), base); + PtrConstruct<T>::construct(T(PtrToArg<P>::convert(p_args[Is])...), base); } public: @@ -112,7 +166,7 @@ public: VariantInternal::object_assign(&r_ret, p_args[0]); } static void ptr_construct(void *base, const void **p_args) { - PtrToArg<Object *>::encode(PtrToArg<Object *>::convert(p_args[0]), base); + PtrConstruct<Object *>::construct(PtrToArg<Object *>::convert(p_args[0]), base); } static int get_argument_count() { @@ -146,7 +200,7 @@ public: VariantInternal::object_assign_null(&r_ret); } static void ptr_construct(void *base, const void **p_args) { - PtrToArg<Object *>::encode(nullptr, base); + PtrConstruct<Object *>::construct(nullptr, base); } static int get_argument_count() { @@ -199,7 +253,7 @@ public: *VariantGetInternalPtr<Callable>::get_ptr(&r_ret) = Callable(VariantInternal::get_object_id(p_args[0]), *VariantGetInternalPtr<StringName>::get_ptr(p_args[1])); } static void ptr_construct(void *base, const void **p_args) { - PtrToArg<Callable>::encode(Callable(PtrToArg<Object *>::convert(p_args[0]), PtrToArg<StringName>::convert(p_args[1])), base); + PtrConstruct<Callable>::construct(Callable(PtrToArg<Object *>::convert(p_args[0]), PtrToArg<StringName>::convert(p_args[1])), base); } static int get_argument_count() { @@ -256,7 +310,7 @@ public: *VariantGetInternalPtr<Signal>::get_ptr(&r_ret) = Signal(VariantInternal::get_object_id(p_args[0]), *VariantGetInternalPtr<StringName>::get_ptr(p_args[1])); } static void ptr_construct(void *base, const void **p_args) { - PtrToArg<Signal>::encode(Signal(PtrToArg<Object *>::convert(p_args[0]), PtrToArg<StringName>::convert(p_args[1])), base); + PtrConstruct<Signal>::construct(Signal(PtrToArg<Object *>::convert(p_args[0]), PtrToArg<StringName>::convert(p_args[1])), base); } static int get_argument_count() { @@ -319,7 +373,7 @@ public: dst_arr[i] = src_arr[i]; } - PtrToArg<Array>::encode(dst_arr, base); + PtrConstruct<Array>::construct(dst_arr, base); } static int get_argument_count() { @@ -378,7 +432,7 @@ public: dst_arr.write[i] = src_arr[i]; } - PtrToArg<T>::encode(dst_arr, base); + PtrConstruct<T>::construct(dst_arr, base); } static int get_argument_count() { @@ -412,7 +466,7 @@ public: VariantInternal::clear(&r_ret); } static void ptr_construct(void *base, const void **p_args) { - PtrToArg<Variant>::encode(Variant(), base); + PtrConstruct<Variant>::construct(Variant(), base); } static int get_argument_count() { @@ -440,7 +494,7 @@ public: VariantTypeChanger<T>::change_and_reset(&r_ret); } static void ptr_construct(void *base, const void **p_args) { - PtrToArg<T>::encode(T(), base); + PtrConstruct<T>::construct(T(), base); } static int get_argument_count() { @@ -496,7 +550,7 @@ public: VariantInternal::object_assign_null(&r_ret); } static void ptr_construct(void *base, const void **p_args) { - PtrToArg<Object *>::encode(nullptr, base); + PtrConstruct<Object *>::construct(nullptr, base); } static int get_argument_count() { |