Make some variant internal functions public.

-Make constructors, ops and setget inline functions public
-Should help optimizing the GDScript VM
This commit is contained in:
reduz 2021-06-03 16:05:57 -03:00 committed by Juan Linietsky
parent 407229aeeb
commit 2c81e5fa0e
6 changed files with 2224 additions and 2150 deletions

View file

@ -28,543 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "variant.h"
#include "core/core_string_names.h"
#include "core/crypto/crypto_core.h"
#include "core/debugger/engine_debugger.h"
#include "core/io/compression.h"
#include "core/object/class_db.h"
#include "core/os/os.h"
#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(Quaternion);
MAKE_PTRCONSTRUCT(AABB);
MAKE_PTRCONSTRUCT(Basis);
MAKE_PTRCONSTRUCT(Transform3D);
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>
static _FORCE_INLINE_ void construct_helper(T &base, const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) {
r_error.error = Callable::CallError::CALL_OK;
#ifdef DEBUG_METHODS_ENABLED
base = T(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
#else
base = T(VariantCaster<P>::cast(*p_args[Is])...);
#endif
}
template <size_t... Is>
static _FORCE_INLINE_ void validated_construct_helper(T &base, const Variant **p_args, IndexSequence<Is...>) {
base = T((*VariantGetInternalPtr<P>::get_ptr(p_args[Is]))...);
}
template <size_t... Is>
static _FORCE_INLINE_ void ptr_construct_helper(void *base, const void **p_args, IndexSequence<Is...>) {
PtrConstruct<T>::construct(T(PtrToArg<P>::convert(p_args[Is])...), base);
}
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
r_error.error = Callable::CallError::CALL_OK;
VariantTypeChanger<T>::change(&r_ret);
construct_helper(*VariantGetInternalPtr<T>::get_ptr(&r_ret), p_args, r_error, BuildIndexSequence<sizeof...(P)>{});
}
static void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantTypeChanger<T>::change(r_ret);
validated_construct_helper(*VariantGetInternalPtr<T>::get_ptr(r_ret), p_args, BuildIndexSequence<sizeof...(P)>{});
}
static void ptr_construct(void *base, const void **p_args) {
ptr_construct_helper(base, p_args, BuildIndexSequence<sizeof...(P)>{});
}
static int get_argument_count() {
return sizeof...(P);
}
static Variant::Type get_argument_type(int p_arg) {
return call_get_argument_type<P...>(p_arg);
}
static Variant::Type get_base_type() {
return GetTypeInfo<T>::VARIANT_TYPE;
}
};
class VariantConstructorObject {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
VariantInternal::clear(&r_ret);
if (p_args[0]->get_type() == Variant::NIL) {
VariantInternal::object_assign_null(&r_ret);
r_error.error = Callable::CallError::CALL_OK;
} else if (p_args[0]->get_type() == Variant::OBJECT) {
VariantInternal::object_assign(&r_ret, p_args[0]);
r_error.error = Callable::CallError::CALL_OK;
} else {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::OBJECT;
}
}
static void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantInternal::clear(r_ret);
VariantInternal::object_assign(r_ret, p_args[0]);
}
static void ptr_construct(void *base, const void **p_args) {
PtrConstruct<Object *>::construct(PtrToArg<Object *>::convert(p_args[0]), base);
}
static int get_argument_count() {
return 1;
}
static Variant::Type get_argument_type(int p_arg) {
return Variant::OBJECT;
}
static Variant::Type get_base_type() {
return Variant::OBJECT;
}
};
class VariantConstructorNilObject {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
if (p_args[0]->get_type() != Variant::NIL) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::NIL;
}
VariantInternal::clear(&r_ret);
VariantInternal::object_assign_null(&r_ret);
}
static void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantInternal::clear(r_ret);
VariantInternal::object_assign_null(r_ret);
}
static void ptr_construct(void *base, const void **p_args) {
PtrConstruct<Object *>::construct(nullptr, base);
}
static int get_argument_count() {
return 1;
}
static Variant::Type get_argument_type(int p_arg) {
return Variant::NIL;
}
static Variant::Type get_base_type() {
return Variant::OBJECT;
}
};
class VariantConstructorCallableArgs {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
ObjectID object_id;
StringName method;
if (p_args[0]->get_type() == Variant::NIL) {
// leave as is
} else if (p_args[0]->get_type() == Variant::OBJECT) {
object_id = VariantInternal::get_object_id(p_args[0]);
} else {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::OBJECT;
return;
}
if (p_args[1]->get_type() == Variant::STRING_NAME) {
method = *VariantGetInternalPtr<StringName>::get_ptr(p_args[1]);
} else if (p_args[1]->get_type() == Variant::STRING) {
method = *VariantGetInternalPtr<String>::get_ptr(p_args[1]);
} else {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 1;
r_error.expected = Variant::STRING_NAME;
return;
}
VariantTypeChanger<Callable>::change(&r_ret);
*VariantGetInternalPtr<Callable>::get_ptr(&r_ret) = Callable(object_id, method);
}
static void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantTypeChanger<Callable>::change(r_ret);
*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) {
PtrConstruct<Callable>::construct(Callable(PtrToArg<Object *>::convert(p_args[0]), PtrToArg<StringName>::convert(p_args[1])), base);
}
static int get_argument_count() {
return 2;
}
static Variant::Type get_argument_type(int p_arg) {
if (p_arg == 0) {
return Variant::OBJECT;
} else {
return Variant::STRING_NAME;
}
}
static Variant::Type get_base_type() {
return Variant::CALLABLE;
}
};
class VariantConstructorSignalArgs {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
ObjectID object_id;
StringName method;
if (p_args[0]->get_type() == Variant::NIL) {
// leave as is
} else if (p_args[0]->get_type() == Variant::OBJECT) {
object_id = VariantInternal::get_object_id(p_args[0]);
} else {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::OBJECT;
return;
}
if (p_args[1]->get_type() == Variant::STRING_NAME) {
method = *VariantGetInternalPtr<StringName>::get_ptr(p_args[1]);
} else if (p_args[1]->get_type() == Variant::STRING) {
method = *VariantGetInternalPtr<String>::get_ptr(p_args[1]);
} else {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 1;
r_error.expected = Variant::STRING_NAME;
return;
}
VariantTypeChanger<Signal>::change(&r_ret);
*VariantGetInternalPtr<Signal>::get_ptr(&r_ret) = Signal(object_id, method);
}
static void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantTypeChanger<Signal>::change(r_ret);
*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) {
PtrConstruct<Signal>::construct(Signal(PtrToArg<Object *>::convert(p_args[0]), PtrToArg<StringName>::convert(p_args[1])), base);
}
static int get_argument_count() {
return 2;
}
static Variant::Type get_argument_type(int p_arg) {
if (p_arg == 0) {
return Variant::OBJECT;
} else {
return Variant::STRING_NAME;
}
}
static Variant::Type get_base_type() {
return Variant::SIGNAL;
}
};
template <class T>
class VariantConstructorToArray {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
if (p_args[0]->get_type() != GetTypeInfo<T>::VARIANT_TYPE) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = GetTypeInfo<T>::VARIANT_TYPE;
return;
}
VariantTypeChanger<Array>::change(&r_ret);
Array &dst_arr = *VariantGetInternalPtr<Array>::get_ptr(&r_ret);
const T &src_arr = *VariantGetInternalPtr<T>::get_ptr(p_args[0]);
int size = src_arr.size();
dst_arr.resize(size);
for (int i = 0; i < size; i++) {
dst_arr[i] = src_arr[i];
}
}
static void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantTypeChanger<Array>::change(r_ret);
Array &dst_arr = *VariantGetInternalPtr<Array>::get_ptr(r_ret);
const T &src_arr = *VariantGetInternalPtr<T>::get_ptr(p_args[0]);
int size = src_arr.size();
dst_arr.resize(size);
for (int i = 0; i < size; i++) {
dst_arr[i] = src_arr[i];
}
}
static void ptr_construct(void *base, const void **p_args) {
Array dst_arr;
T src_arr = PtrToArg<T>::convert(p_args[0]);
int size = src_arr.size();
dst_arr.resize(size);
for (int i = 0; i < size; i++) {
dst_arr[i] = src_arr[i];
}
PtrConstruct<Array>::construct(dst_arr, base);
}
static int get_argument_count() {
return 1;
}
static Variant::Type get_argument_type(int p_arg) {
return GetTypeInfo<T>::VARIANT_TYPE;
}
static Variant::Type get_base_type() {
return Variant::ARRAY;
}
};
template <class T>
class VariantConstructorFromArray {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
if (p_args[0]->get_type() != Variant::ARRAY) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::ARRAY;
return;
}
VariantTypeChanger<T>::change(&r_ret);
const Array &src_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
T &dst_arr = *VariantGetInternalPtr<T>::get_ptr(&r_ret);
int size = src_arr.size();
dst_arr.resize(size);
for (int i = 0; i < size; i++) {
dst_arr.write[i] = src_arr[i];
}
}
static void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantTypeChanger<T>::change(r_ret);
const Array &src_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
T &dst_arr = *VariantGetInternalPtr<T>::get_ptr(r_ret);
int size = src_arr.size();
dst_arr.resize(size);
for (int i = 0; i < size; i++) {
dst_arr.write[i] = src_arr[i];
}
}
static void ptr_construct(void *base, const void **p_args) {
Array src_arr = PtrToArg<Array>::convert(p_args[0]);
T dst_arr;
int size = src_arr.size();
dst_arr.resize(size);
for (int i = 0; i < size; i++) {
dst_arr.write[i] = src_arr[i];
}
PtrConstruct<T>::construct(dst_arr, base);
}
static int get_argument_count() {
return 1;
}
static Variant::Type get_argument_type(int p_arg) {
return Variant::ARRAY;
}
static Variant::Type get_base_type() {
return GetTypeInfo<T>::VARIANT_TYPE;
}
};
class VariantConstructorNil {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
if (p_args[0]->get_type() != Variant::NIL) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::NIL;
return;
}
r_error.error = Callable::CallError::CALL_OK;
VariantInternal::clear(&r_ret);
}
static void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantInternal::clear(r_ret);
}
static void ptr_construct(void *base, const void **p_args) {
PtrConstruct<Variant>::construct(Variant(), base);
}
static int get_argument_count() {
return 1;
}
static Variant::Type get_argument_type(int p_arg) {
return Variant::NIL;
}
static Variant::Type get_base_type() {
return Variant::NIL;
}
};
template <class T>
class VariantConstructNoArgs {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
VariantTypeChanger<T>::change_and_reset(&r_ret);
r_error.error = Callable::CallError::CALL_OK;
}
static void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantTypeChanger<T>::change_and_reset(r_ret);
}
static void ptr_construct(void *base, const void **p_args) {
PtrConstruct<T>::construct(T(), base);
}
static int get_argument_count() {
return 0;
}
static Variant::Type get_argument_type(int p_arg) {
return Variant::NIL;
}
static Variant::Type get_base_type() {
return GetTypeInfo<T>::VARIANT_TYPE;
}
};
class VariantConstructNoArgsNil {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
VariantInternal::clear(&r_ret);
r_error.error = Callable::CallError::CALL_OK;
}
static void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantInternal::clear(r_ret);
}
static void ptr_construct(void *base, const void **p_args) {
ERR_FAIL_MSG("can't ptrcall nil constructor");
}
static int get_argument_count() {
return 0;
}
static Variant::Type get_argument_type(int p_arg) {
return Variant::NIL;
}
static Variant::Type get_base_type() {
return Variant::NIL;
}
};
class VariantConstructNoArgsObject {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
VariantInternal::clear(&r_ret);
VariantInternal::object_assign_null(&r_ret);
r_error.error = Callable::CallError::CALL_OK;
}
static void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantInternal::clear(r_ret);
VariantInternal::object_assign_null(r_ret);
}
static void ptr_construct(void *base, const void **p_args) {
PtrConstruct<Object *>::construct(nullptr, base);
}
static int get_argument_count() {
return 0;
}
static Variant::Type get_argument_type(int p_arg) {
return Variant::NIL;
}
static Variant::Type get_base_type() {
return Variant::OBJECT;
}
};
#include "variant_construct.h"
struct VariantConstructData {
void (*construct)(Variant &r_base, const Variant **p_args, Callable::CallError &r_error);

View file

@ -0,0 +1,572 @@
/*************************************************************************/
/* variant_construct.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_CONSTRUCT_H
#define VARIANT_CONSTRUCT_H
#include "variant.h"
#include "core/core_string_names.h"
#include "core/crypto/crypto_core.h"
#include "core/debugger/engine_debugger.h"
#include "core/io/compression.h"
#include "core/object/class_db.h"
#include "core/os/os.h"
#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(Quaternion);
MAKE_PTRCONSTRUCT(AABB);
MAKE_PTRCONSTRUCT(Basis);
MAKE_PTRCONSTRUCT(Transform3D);
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>
static _FORCE_INLINE_ void construct_helper(T &base, const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) {
r_error.error = Callable::CallError::CALL_OK;
#ifdef DEBUG_METHODS_ENABLED
base = T(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
#else
base = T(VariantCaster<P>::cast(*p_args[Is])...);
#endif
}
template <size_t... Is>
static _FORCE_INLINE_ void validated_construct_helper(T &base, const Variant **p_args, IndexSequence<Is...>) {
base = T((*VariantGetInternalPtr<P>::get_ptr(p_args[Is]))...);
}
template <size_t... Is>
static _FORCE_INLINE_ void ptr_construct_helper(void *base, const void **p_args, IndexSequence<Is...>) {
PtrConstruct<T>::construct(T(PtrToArg<P>::convert(p_args[Is])...), base);
}
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
r_error.error = Callable::CallError::CALL_OK;
VariantTypeChanger<T>::change(&r_ret);
construct_helper(*VariantGetInternalPtr<T>::get_ptr(&r_ret), p_args, r_error, BuildIndexSequence<sizeof...(P)>{});
}
static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantTypeChanger<T>::change(r_ret);
validated_construct_helper(*VariantGetInternalPtr<T>::get_ptr(r_ret), p_args, BuildIndexSequence<sizeof...(P)>{});
}
static void ptr_construct(void *base, const void **p_args) {
ptr_construct_helper(base, p_args, BuildIndexSequence<sizeof...(P)>{});
}
static int get_argument_count() {
return sizeof...(P);
}
static Variant::Type get_argument_type(int p_arg) {
return call_get_argument_type<P...>(p_arg);
}
static Variant::Type get_base_type() {
return GetTypeInfo<T>::VARIANT_TYPE;
}
};
class VariantConstructorObject {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
VariantInternal::clear(&r_ret);
if (p_args[0]->get_type() == Variant::NIL) {
VariantInternal::object_assign_null(&r_ret);
r_error.error = Callable::CallError::CALL_OK;
} else if (p_args[0]->get_type() == Variant::OBJECT) {
VariantInternal::object_assign(&r_ret, p_args[0]);
r_error.error = Callable::CallError::CALL_OK;
} else {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::OBJECT;
}
}
static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantInternal::clear(r_ret);
VariantInternal::object_assign(r_ret, p_args[0]);
}
static void ptr_construct(void *base, const void **p_args) {
PtrConstruct<Object *>::construct(PtrToArg<Object *>::convert(p_args[0]), base);
}
static int get_argument_count() {
return 1;
}
static Variant::Type get_argument_type(int p_arg) {
return Variant::OBJECT;
}
static Variant::Type get_base_type() {
return Variant::OBJECT;
}
};
class VariantConstructorNilObject {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
if (p_args[0]->get_type() != Variant::NIL) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::NIL;
}
VariantInternal::clear(&r_ret);
VariantInternal::object_assign_null(&r_ret);
}
static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantInternal::clear(r_ret);
VariantInternal::object_assign_null(r_ret);
}
static void ptr_construct(void *base, const void **p_args) {
PtrConstruct<Object *>::construct(nullptr, base);
}
static int get_argument_count() {
return 1;
}
static Variant::Type get_argument_type(int p_arg) {
return Variant::NIL;
}
static Variant::Type get_base_type() {
return Variant::OBJECT;
}
};
class VariantConstructorCallableArgs {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
ObjectID object_id;
StringName method;
if (p_args[0]->get_type() == Variant::NIL) {
// leave as is
} else if (p_args[0]->get_type() == Variant::OBJECT) {
object_id = VariantInternal::get_object_id(p_args[0]);
} else {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::OBJECT;
return;
}
if (p_args[1]->get_type() == Variant::STRING_NAME) {
method = *VariantGetInternalPtr<StringName>::get_ptr(p_args[1]);
} else if (p_args[1]->get_type() == Variant::STRING) {
method = *VariantGetInternalPtr<String>::get_ptr(p_args[1]);
} else {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 1;
r_error.expected = Variant::STRING_NAME;
return;
}
VariantTypeChanger<Callable>::change(&r_ret);
*VariantGetInternalPtr<Callable>::get_ptr(&r_ret) = Callable(object_id, method);
}
static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantTypeChanger<Callable>::change(r_ret);
*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) {
PtrConstruct<Callable>::construct(Callable(PtrToArg<Object *>::convert(p_args[0]), PtrToArg<StringName>::convert(p_args[1])), base);
}
static int get_argument_count() {
return 2;
}
static Variant::Type get_argument_type(int p_arg) {
if (p_arg == 0) {
return Variant::OBJECT;
} else {
return Variant::STRING_NAME;
}
}
static Variant::Type get_base_type() {
return Variant::CALLABLE;
}
};
class VariantConstructorSignalArgs {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
ObjectID object_id;
StringName method;
if (p_args[0]->get_type() == Variant::NIL) {
// leave as is
} else if (p_args[0]->get_type() == Variant::OBJECT) {
object_id = VariantInternal::get_object_id(p_args[0]);
} else {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::OBJECT;
return;
}
if (p_args[1]->get_type() == Variant::STRING_NAME) {
method = *VariantGetInternalPtr<StringName>::get_ptr(p_args[1]);
} else if (p_args[1]->get_type() == Variant::STRING) {
method = *VariantGetInternalPtr<String>::get_ptr(p_args[1]);
} else {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 1;
r_error.expected = Variant::STRING_NAME;
return;
}
VariantTypeChanger<Signal>::change(&r_ret);
*VariantGetInternalPtr<Signal>::get_ptr(&r_ret) = Signal(object_id, method);
}
static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantTypeChanger<Signal>::change(r_ret);
*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) {
PtrConstruct<Signal>::construct(Signal(PtrToArg<Object *>::convert(p_args[0]), PtrToArg<StringName>::convert(p_args[1])), base);
}
static int get_argument_count() {
return 2;
}
static Variant::Type get_argument_type(int p_arg) {
if (p_arg == 0) {
return Variant::OBJECT;
} else {
return Variant::STRING_NAME;
}
}
static Variant::Type get_base_type() {
return Variant::SIGNAL;
}
};
template <class T>
class VariantConstructorToArray {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
if (p_args[0]->get_type() != GetTypeInfo<T>::VARIANT_TYPE) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = GetTypeInfo<T>::VARIANT_TYPE;
return;
}
VariantTypeChanger<Array>::change(&r_ret);
Array &dst_arr = *VariantGetInternalPtr<Array>::get_ptr(&r_ret);
const T &src_arr = *VariantGetInternalPtr<T>::get_ptr(p_args[0]);
int size = src_arr.size();
dst_arr.resize(size);
for (int i = 0; i < size; i++) {
dst_arr[i] = src_arr[i];
}
}
static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantTypeChanger<Array>::change(r_ret);
Array &dst_arr = *VariantGetInternalPtr<Array>::get_ptr(r_ret);
const T &src_arr = *VariantGetInternalPtr<T>::get_ptr(p_args[0]);
int size = src_arr.size();
dst_arr.resize(size);
for (int i = 0; i < size; i++) {
dst_arr[i] = src_arr[i];
}
}
static void ptr_construct(void *base, const void **p_args) {
Array dst_arr;
T src_arr = PtrToArg<T>::convert(p_args[0]);
int size = src_arr.size();
dst_arr.resize(size);
for (int i = 0; i < size; i++) {
dst_arr[i] = src_arr[i];
}
PtrConstruct<Array>::construct(dst_arr, base);
}
static int get_argument_count() {
return 1;
}
static Variant::Type get_argument_type(int p_arg) {
return GetTypeInfo<T>::VARIANT_TYPE;
}
static Variant::Type get_base_type() {
return Variant::ARRAY;
}
};
template <class T>
class VariantConstructorFromArray {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
if (p_args[0]->get_type() != Variant::ARRAY) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::ARRAY;
return;
}
VariantTypeChanger<T>::change(&r_ret);
const Array &src_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
T &dst_arr = *VariantGetInternalPtr<T>::get_ptr(&r_ret);
int size = src_arr.size();
dst_arr.resize(size);
for (int i = 0; i < size; i++) {
dst_arr.write[i] = src_arr[i];
}
}
static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantTypeChanger<T>::change(r_ret);
const Array &src_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
T &dst_arr = *VariantGetInternalPtr<T>::get_ptr(r_ret);
int size = src_arr.size();
dst_arr.resize(size);
for (int i = 0; i < size; i++) {
dst_arr.write[i] = src_arr[i];
}
}
static void ptr_construct(void *base, const void **p_args) {
Array src_arr = PtrToArg<Array>::convert(p_args[0]);
T dst_arr;
int size = src_arr.size();
dst_arr.resize(size);
for (int i = 0; i < size; i++) {
dst_arr.write[i] = src_arr[i];
}
PtrConstruct<T>::construct(dst_arr, base);
}
static int get_argument_count() {
return 1;
}
static Variant::Type get_argument_type(int p_arg) {
return Variant::ARRAY;
}
static Variant::Type get_base_type() {
return GetTypeInfo<T>::VARIANT_TYPE;
}
};
class VariantConstructorNil {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
if (p_args[0]->get_type() != Variant::NIL) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::NIL;
return;
}
r_error.error = Callable::CallError::CALL_OK;
VariantInternal::clear(&r_ret);
}
static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantInternal::clear(r_ret);
}
static void ptr_construct(void *base, const void **p_args) {
PtrConstruct<Variant>::construct(Variant(), base);
}
static int get_argument_count() {
return 1;
}
static Variant::Type get_argument_type(int p_arg) {
return Variant::NIL;
}
static Variant::Type get_base_type() {
return Variant::NIL;
}
};
template <class T>
class VariantConstructNoArgs {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
VariantTypeChanger<T>::change_and_reset(&r_ret);
r_error.error = Callable::CallError::CALL_OK;
}
static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantTypeChanger<T>::change_and_reset(r_ret);
}
static void ptr_construct(void *base, const void **p_args) {
PtrConstruct<T>::construct(T(), base);
}
static int get_argument_count() {
return 0;
}
static Variant::Type get_argument_type(int p_arg) {
return Variant::NIL;
}
static Variant::Type get_base_type() {
return GetTypeInfo<T>::VARIANT_TYPE;
}
};
class VariantConstructNoArgsNil {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
VariantInternal::clear(&r_ret);
r_error.error = Callable::CallError::CALL_OK;
}
static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantInternal::clear(r_ret);
}
static void ptr_construct(void *base, const void **p_args) {
ERR_FAIL_MSG("can't ptrcall nil constructor");
}
static int get_argument_count() {
return 0;
}
static Variant::Type get_argument_type(int p_arg) {
return Variant::NIL;
}
static Variant::Type get_base_type() {
return Variant::NIL;
}
};
class VariantConstructNoArgsObject {
public:
static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
VariantInternal::clear(&r_ret);
VariantInternal::object_assign_null(&r_ret);
r_error.error = Callable::CallError::CALL_OK;
}
static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
VariantInternal::clear(r_ret);
VariantInternal::object_assign_null(r_ret);
}
static void ptr_construct(void *base, const void **p_args) {
PtrConstruct<Object *>::construct(nullptr, base);
}
static int get_argument_count() {
return 0;
}
static Variant::Type get_argument_type(int p_arg) {
return Variant::NIL;
}
static Variant::Type get_base_type() {
return Variant::OBJECT;
}
};
#endif // VARIANT_CONSTRUCT_H

File diff suppressed because it is too large Load diff

1316
core/variant/variant_op.h Normal file

File diff suppressed because it is too large Load diff

View file

@ -28,282 +28,7 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "variant.h"
#include "core/core_string_names.h"
#include "core/debugger/engine_debugger.h"
#include "core/object/class_db.h"
#include "core/templates/local_vector.h"
#include "core/variant/variant_internal.h"
/**** NAMED SETTERS AND GETTERS ****/
#define SETGET_STRUCT(m_base_type, m_member_type, m_member) \
struct VariantSetGet_##m_base_type##_##m_member { \
static void get(const Variant *base, Variant *member) { \
VariantTypeAdjust<m_member_type>::adjust(member); \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member; \
} \
static void ptr_get(const void *base, void *member) { \
PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_member, member); \
} \
static void set(Variant *base, const Variant *value, bool &valid) { \
if (value->get_type() == GetTypeInfo<m_member_type>::VARIANT_TYPE) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
valid = true; \
} else { \
valid = false; \
} \
} \
static void validated_set(Variant *base, const Variant *value) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
} \
static void ptr_set(void *base, const void *member) { \
m_base_type b = PtrToArg<m_base_type>::convert(base); \
b.m_member = PtrToArg<m_member_type>::convert(member); \
PtrToArg<m_base_type>::encode(b, base); \
} \
static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
};
#define SETGET_NUMBER_STRUCT(m_base_type, m_member_type, m_member) \
struct VariantSetGet_##m_base_type##_##m_member { \
static void get(const Variant *base, Variant *member) { \
VariantTypeAdjust<m_member_type>::adjust(member); \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member; \
} \
static void ptr_get(const void *base, void *member) { \
PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_member, member); \
} \
static void set(Variant *base, const Variant *value, bool &valid) { \
if (value->get_type() == Variant::FLOAT) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<double>::get_ptr(value); \
valid = true; \
} else if (value->get_type() == Variant::INT) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<int64_t>::get_ptr(value); \
valid = true; \
} else { \
valid = false; \
} \
} \
static void validated_set(Variant *base, const Variant *value) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
} \
static void ptr_set(void *base, const void *member) { \
m_base_type b = PtrToArg<m_base_type>::convert(base); \
b.m_member = PtrToArg<m_member_type>::convert(member); \
PtrToArg<m_base_type>::encode(b, base); \
} \
static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
};
#define SETGET_STRUCT_CUSTOM(m_base_type, m_member_type, m_member, m_custom) \
struct VariantSetGet_##m_base_type##_##m_member { \
static void get(const Variant *base, Variant *member) { \
VariantTypeAdjust<m_member_type>::adjust(member); \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom; \
} \
static void ptr_get(const void *base, void *member) { \
PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_custom, member); \
} \
static void set(Variant *base, const Variant *value, bool &valid) { \
if (value->get_type() == GetTypeInfo<m_member_type>::VARIANT_TYPE) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
valid = true; \
} else { \
valid = false; \
} \
} \
static void validated_set(Variant *base, const Variant *value) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
} \
static void ptr_set(void *base, const void *member) { \
m_base_type b = PtrToArg<m_base_type>::convert(base); \
b.m_custom = PtrToArg<m_member_type>::convert(member); \
PtrToArg<m_base_type>::encode(b, base); \
} \
static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
};
#define SETGET_NUMBER_STRUCT_CUSTOM(m_base_type, m_member_type, m_member, m_custom) \
struct VariantSetGet_##m_base_type##_##m_member { \
static void get(const Variant *base, Variant *member) { \
VariantTypeAdjust<m_member_type>::adjust(member); \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom; \
} \
static void ptr_get(const void *base, void *member) { \
PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_custom, member); \
} \
static void set(Variant *base, const Variant *value, bool &valid) { \
if (value->get_type() == Variant::FLOAT) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<double>::get_ptr(value); \
valid = true; \
} else if (value->get_type() == Variant::INT) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<int64_t>::get_ptr(value); \
valid = true; \
} else { \
valid = false; \
} \
} \
static void validated_set(Variant *base, const Variant *value) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
} \
static void ptr_set(void *base, const void *member) { \
m_base_type b = PtrToArg<m_base_type>::convert(base); \
b.m_custom = PtrToArg<m_member_type>::convert(member); \
PtrToArg<m_base_type>::encode(b, base); \
} \
static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
};
#define SETGET_STRUCT_FUNC(m_base_type, m_member_type, m_member, m_setter, m_getter) \
struct VariantSetGet_##m_base_type##_##m_member { \
static void get(const Variant *base, Variant *member) { \
VariantTypeAdjust<m_member_type>::adjust(member); \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(); \
} \
static void ptr_get(const void *base, void *member) { \
PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_getter(), member); \
} \
static void set(Variant *base, const Variant *value, bool &valid) { \
if (value->get_type() == GetTypeInfo<m_member_type>::VARIANT_TYPE) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
valid = true; \
} else { \
valid = false; \
} \
} \
static void validated_set(Variant *base, const Variant *value) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
} \
static void ptr_set(void *base, const void *member) { \
m_base_type b = PtrToArg<m_base_type>::convert(base); \
b.m_setter(PtrToArg<m_member_type>::convert(member)); \
PtrToArg<m_base_type>::encode(b, base); \
} \
static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
};
#define SETGET_NUMBER_STRUCT_FUNC(m_base_type, m_member_type, m_member, m_setter, m_getter) \
struct VariantSetGet_##m_base_type##_##m_member { \
static void get(const Variant *base, Variant *member) { \
VariantTypeAdjust<m_member_type>::adjust(member); \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(); \
} \
static void ptr_get(const void *base, void *member) { \
PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_getter(), member); \
} \
static void set(Variant *base, const Variant *value, bool &valid) { \
if (value->get_type() == Variant::FLOAT) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<double>::get_ptr(value)); \
valid = true; \
} else if (value->get_type() == Variant::INT) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<int64_t>::get_ptr(value)); \
valid = true; \
} else { \
valid = false; \
} \
} \
static void validated_set(Variant *base, const Variant *value) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
} \
static void ptr_set(void *base, const void *member) { \
m_base_type b = PtrToArg<m_base_type>::convert(base); \
b.m_setter(PtrToArg<m_member_type>::convert(member)); \
PtrToArg<m_base_type>::encode(b, base); \
} \
static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
};
#define SETGET_STRUCT_FUNC_INDEX(m_base_type, m_member_type, m_member, m_setter, m_getter, m_index) \
struct VariantSetGet_##m_base_type##_##m_member { \
static void get(const Variant *base, Variant *member) { \
VariantTypeAdjust<m_member_type>::adjust(member); \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(m_index); \
} \
static void ptr_get(const void *base, void *member) { \
PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_getter(m_index), member); \
} \
static void set(Variant *base, const Variant *value, bool &valid) { \
if (value->get_type() == GetTypeInfo<m_member_type>::VARIANT_TYPE) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(m_index, *VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
valid = true; \
} else { \
valid = false; \
} \
} \
static void validated_set(Variant *base, const Variant *value) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(m_index, *VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
} \
static void ptr_set(void *base, const void *member) { \
m_base_type b = PtrToArg<m_base_type>::convert(base); \
b.m_setter(m_index, PtrToArg<m_member_type>::convert(member)); \
PtrToArg<m_base_type>::encode(b, base); \
} \
static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
};
SETGET_NUMBER_STRUCT(Vector2, double, x)
SETGET_NUMBER_STRUCT(Vector2, double, y)
SETGET_NUMBER_STRUCT(Vector2i, int64_t, x)
SETGET_NUMBER_STRUCT(Vector2i, int64_t, y)
SETGET_NUMBER_STRUCT(Vector3, double, x)
SETGET_NUMBER_STRUCT(Vector3, double, y)
SETGET_NUMBER_STRUCT(Vector3, double, z)
SETGET_NUMBER_STRUCT(Vector3i, int64_t, x)
SETGET_NUMBER_STRUCT(Vector3i, int64_t, y)
SETGET_NUMBER_STRUCT(Vector3i, int64_t, z)
SETGET_STRUCT(Rect2, Vector2, position)
SETGET_STRUCT(Rect2, Vector2, size)
SETGET_STRUCT_FUNC(Rect2, Vector2, end, set_end, get_end)
SETGET_STRUCT(Rect2i, Vector2i, position)
SETGET_STRUCT(Rect2i, Vector2i, size)
SETGET_STRUCT_FUNC(Rect2i, Vector2i, end, set_end, get_end)
SETGET_STRUCT(AABB, Vector3, position)
SETGET_STRUCT(AABB, Vector3, size)
SETGET_STRUCT_FUNC(AABB, Vector3, end, set_end, get_end)
SETGET_STRUCT_CUSTOM(Transform2D, Vector2, x, elements[0])
SETGET_STRUCT_CUSTOM(Transform2D, Vector2, y, elements[1])
SETGET_STRUCT_CUSTOM(Transform2D, Vector2, origin, elements[2])
SETGET_NUMBER_STRUCT_CUSTOM(Plane, double, x, normal.x)
SETGET_NUMBER_STRUCT_CUSTOM(Plane, double, y, normal.y)
SETGET_NUMBER_STRUCT_CUSTOM(Plane, double, z, normal.z)
SETGET_STRUCT(Plane, Vector3, normal)
SETGET_NUMBER_STRUCT(Plane, double, d)
SETGET_NUMBER_STRUCT(Quaternion, double, x)
SETGET_NUMBER_STRUCT(Quaternion, double, y)
SETGET_NUMBER_STRUCT(Quaternion, double, z)
SETGET_NUMBER_STRUCT(Quaternion, double, w)
SETGET_STRUCT_FUNC_INDEX(Basis, Vector3, x, set_axis, get_axis, 0)
SETGET_STRUCT_FUNC_INDEX(Basis, Vector3, y, set_axis, get_axis, 1)
SETGET_STRUCT_FUNC_INDEX(Basis, Vector3, z, set_axis, get_axis, 2)
SETGET_STRUCT(Transform3D, Basis, basis)
SETGET_STRUCT(Transform3D, Vector3, origin)
SETGET_NUMBER_STRUCT(Color, double, r)
SETGET_NUMBER_STRUCT(Color, double, g)
SETGET_NUMBER_STRUCT(Color, double, b)
SETGET_NUMBER_STRUCT(Color, double, a)
SETGET_NUMBER_STRUCT_FUNC(Color, int64_t, r8, set_r8, get_r8)
SETGET_NUMBER_STRUCT_FUNC(Color, int64_t, g8, set_g8, get_g8)
SETGET_NUMBER_STRUCT_FUNC(Color, int64_t, b8, set_b8, get_b8)
SETGET_NUMBER_STRUCT_FUNC(Color, int64_t, a8, set_a8, get_a8)
SETGET_NUMBER_STRUCT_FUNC(Color, double, h, set_h, get_h)
SETGET_NUMBER_STRUCT_FUNC(Color, double, s, set_s, get_s)
SETGET_NUMBER_STRUCT_FUNC(Color, double, v, set_v, get_v)
#include "variant_setget.h"
struct VariantSetterGetterInfo {
void (*setter)(Variant *base, const Variant *value, bool &valid);
@ -326,7 +51,7 @@ static void register_member(Variant::Type p_type, const StringName &p_member) {
sgi.ptr_setter = T::ptr_set;
sgi.getter = T::get;
sgi.validated_getter = T::get;
sgi.validated_getter = T::validated_get;
sgi.ptr_getter = T::ptr_get;
sgi.member_type = T::get_type();

View file

@ -0,0 +1,332 @@
/*************************************************************************/
/* variant_setget.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_SETGET_H
#define VARIANT_SETGET_H
#include "variant.h"
#include "core/core_string_names.h"
#include "core/debugger/engine_debugger.h"
#include "core/object/class_db.h"
#include "core/templates/local_vector.h"
#include "core/variant/variant_internal.h"
/**** NAMED SETTERS AND GETTERS ****/
#define SETGET_STRUCT(m_base_type, m_member_type, m_member) \
struct VariantSetGet_##m_base_type##_##m_member { \
static void get(const Variant *base, Variant *member) { \
VariantTypeAdjust<m_member_type>::adjust(member); \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member; \
} \
static inline void validated_get(const Variant *base, Variant *member) { \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member; \
} \
static void ptr_get(const void *base, void *member) { \
PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_member, member); \
} \
static void set(Variant *base, const Variant *value, bool &valid) { \
if (value->get_type() == GetTypeInfo<m_member_type>::VARIANT_TYPE) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
valid = true; \
} else { \
valid = false; \
} \
} \
static inline void validated_set(Variant *base, const Variant *value) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
} \
static void ptr_set(void *base, const void *member) { \
m_base_type b = PtrToArg<m_base_type>::convert(base); \
b.m_member = PtrToArg<m_member_type>::convert(member); \
PtrToArg<m_base_type>::encode(b, base); \
} \
static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
};
#define SETGET_NUMBER_STRUCT(m_base_type, m_member_type, m_member) \
struct VariantSetGet_##m_base_type##_##m_member { \
static void get(const Variant *base, Variant *member) { \
VariantTypeAdjust<m_member_type>::adjust(member); \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member; \
} \
static inline void validated_get(const Variant *base, Variant *member) { \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member; \
} \
static void ptr_get(const void *base, void *member) { \
PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_member, member); \
} \
static void set(Variant *base, const Variant *value, bool &valid) { \
if (value->get_type() == Variant::FLOAT) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<double>::get_ptr(value); \
valid = true; \
} else if (value->get_type() == Variant::INT) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<int64_t>::get_ptr(value); \
valid = true; \
} else { \
valid = false; \
} \
} \
static inline void validated_set(Variant *base, const Variant *value) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
} \
static void ptr_set(void *base, const void *member) { \
m_base_type b = PtrToArg<m_base_type>::convert(base); \
b.m_member = PtrToArg<m_member_type>::convert(member); \
PtrToArg<m_base_type>::encode(b, base); \
} \
static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
};
#define SETGET_STRUCT_CUSTOM(m_base_type, m_member_type, m_member, m_custom) \
struct VariantSetGet_##m_base_type##_##m_member { \
static void get(const Variant *base, Variant *member) { \
VariantTypeAdjust<m_member_type>::adjust(member); \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom; \
} \
static inline void validated_get(const Variant *base, Variant *member) { \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom; \
} \
static void ptr_get(const void *base, void *member) { \
PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_custom, member); \
} \
static void set(Variant *base, const Variant *value, bool &valid) { \
if (value->get_type() == GetTypeInfo<m_member_type>::VARIANT_TYPE) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
valid = true; \
} else { \
valid = false; \
} \
} \
static inline void validated_set(Variant *base, const Variant *value) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
} \
static void ptr_set(void *base, const void *member) { \
m_base_type b = PtrToArg<m_base_type>::convert(base); \
b.m_custom = PtrToArg<m_member_type>::convert(member); \
PtrToArg<m_base_type>::encode(b, base); \
} \
static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
};
#define SETGET_NUMBER_STRUCT_CUSTOM(m_base_type, m_member_type, m_member, m_custom) \
struct VariantSetGet_##m_base_type##_##m_member { \
static void get(const Variant *base, Variant *member) { \
VariantTypeAdjust<m_member_type>::adjust(member); \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom; \
} \
static inline void validated_get(const Variant *base, Variant *member) { \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom; \
} \
static void ptr_get(const void *base, void *member) { \
PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_custom, member); \
} \
static void set(Variant *base, const Variant *value, bool &valid) { \
if (value->get_type() == Variant::FLOAT) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<double>::get_ptr(value); \
valid = true; \
} else if (value->get_type() == Variant::INT) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<int64_t>::get_ptr(value); \
valid = true; \
} else { \
valid = false; \
} \
} \
static inline void validated_set(Variant *base, const Variant *value) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
} \
static void ptr_set(void *base, const void *member) { \
m_base_type b = PtrToArg<m_base_type>::convert(base); \
b.m_custom = PtrToArg<m_member_type>::convert(member); \
PtrToArg<m_base_type>::encode(b, base); \
} \
static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
};
#define SETGET_STRUCT_FUNC(m_base_type, m_member_type, m_member, m_setter, m_getter) \
struct VariantSetGet_##m_base_type##_##m_member { \
static void get(const Variant *base, Variant *member) { \
VariantTypeAdjust<m_member_type>::adjust(member); \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(); \
} \
static inline void validated_get(const Variant *base, Variant *member) { \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(); \
} \
static void ptr_get(const void *base, void *member) { \
PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_getter(), member); \
} \
static void set(Variant *base, const Variant *value, bool &valid) { \
if (value->get_type() == GetTypeInfo<m_member_type>::VARIANT_TYPE) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
valid = true; \
} else { \
valid = false; \
} \
} \
static inline void validated_set(Variant *base, const Variant *value) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
} \
static void ptr_set(void *base, const void *member) { \
m_base_type b = PtrToArg<m_base_type>::convert(base); \
b.m_setter(PtrToArg<m_member_type>::convert(member)); \
PtrToArg<m_base_type>::encode(b, base); \
} \
static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
};
#define SETGET_NUMBER_STRUCT_FUNC(m_base_type, m_member_type, m_member, m_setter, m_getter) \
struct VariantSetGet_##m_base_type##_##m_member { \
static void get(const Variant *base, Variant *member) { \
VariantTypeAdjust<m_member_type>::adjust(member); \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(); \
} \
static inline void validated_get(const Variant *base, Variant *member) { \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(); \
} \
static void ptr_get(const void *base, void *member) { \
PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_getter(), member); \
} \
static void set(Variant *base, const Variant *value, bool &valid) { \
if (value->get_type() == Variant::FLOAT) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<double>::get_ptr(value)); \
valid = true; \
} else if (value->get_type() == Variant::INT) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<int64_t>::get_ptr(value)); \
valid = true; \
} else { \
valid = false; \
} \
} \
static inline void validated_set(Variant *base, const Variant *value) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
} \
static void ptr_set(void *base, const void *member) { \
m_base_type b = PtrToArg<m_base_type>::convert(base); \
b.m_setter(PtrToArg<m_member_type>::convert(member)); \
PtrToArg<m_base_type>::encode(b, base); \
} \
static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
};
#define SETGET_STRUCT_FUNC_INDEX(m_base_type, m_member_type, m_member, m_setter, m_getter, m_index) \
struct VariantSetGet_##m_base_type##_##m_member { \
static void get(const Variant *base, Variant *member) { \
VariantTypeAdjust<m_member_type>::adjust(member); \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(m_index); \
} \
static inline void validated_get(const Variant *base, Variant *member) { \
*VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(m_index); \
} \
static void ptr_get(const void *base, void *member) { \
PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_getter(m_index), member); \
} \
static void set(Variant *base, const Variant *value, bool &valid) { \
if (value->get_type() == GetTypeInfo<m_member_type>::VARIANT_TYPE) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(m_index, *VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
valid = true; \
} else { \
valid = false; \
} \
} \
static inline void validated_set(Variant *base, const Variant *value) { \
VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(m_index, *VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
} \
static void ptr_set(void *base, const void *member) { \
m_base_type b = PtrToArg<m_base_type>::convert(base); \
b.m_setter(m_index, PtrToArg<m_member_type>::convert(member)); \
PtrToArg<m_base_type>::encode(b, base); \
} \
static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
};
SETGET_NUMBER_STRUCT(Vector2, double, x)
SETGET_NUMBER_STRUCT(Vector2, double, y)
SETGET_NUMBER_STRUCT(Vector2i, int64_t, x)
SETGET_NUMBER_STRUCT(Vector2i, int64_t, y)
SETGET_NUMBER_STRUCT(Vector3, double, x)
SETGET_NUMBER_STRUCT(Vector3, double, y)
SETGET_NUMBER_STRUCT(Vector3, double, z)
SETGET_NUMBER_STRUCT(Vector3i, int64_t, x)
SETGET_NUMBER_STRUCT(Vector3i, int64_t, y)
SETGET_NUMBER_STRUCT(Vector3i, int64_t, z)
SETGET_STRUCT(Rect2, Vector2, position)
SETGET_STRUCT(Rect2, Vector2, size)
SETGET_STRUCT_FUNC(Rect2, Vector2, end, set_end, get_end)
SETGET_STRUCT(Rect2i, Vector2i, position)
SETGET_STRUCT(Rect2i, Vector2i, size)
SETGET_STRUCT_FUNC(Rect2i, Vector2i, end, set_end, get_end)
SETGET_STRUCT(AABB, Vector3, position)
SETGET_STRUCT(AABB, Vector3, size)
SETGET_STRUCT_FUNC(AABB, Vector3, end, set_end, get_end)
SETGET_STRUCT_CUSTOM(Transform2D, Vector2, x, elements[0])
SETGET_STRUCT_CUSTOM(Transform2D, Vector2, y, elements[1])
SETGET_STRUCT_CUSTOM(Transform2D, Vector2, origin, elements[2])
SETGET_NUMBER_STRUCT_CUSTOM(Plane, double, x, normal.x)
SETGET_NUMBER_STRUCT_CUSTOM(Plane, double, y, normal.y)
SETGET_NUMBER_STRUCT_CUSTOM(Plane, double, z, normal.z)
SETGET_STRUCT(Plane, Vector3, normal)
SETGET_NUMBER_STRUCT(Plane, double, d)
SETGET_NUMBER_STRUCT(Quaternion, double, x)
SETGET_NUMBER_STRUCT(Quaternion, double, y)
SETGET_NUMBER_STRUCT(Quaternion, double, z)
SETGET_NUMBER_STRUCT(Quaternion, double, w)
SETGET_STRUCT_FUNC_INDEX(Basis, Vector3, x, set_axis, get_axis, 0)
SETGET_STRUCT_FUNC_INDEX(Basis, Vector3, y, set_axis, get_axis, 1)
SETGET_STRUCT_FUNC_INDEX(Basis, Vector3, z, set_axis, get_axis, 2)
SETGET_STRUCT(Transform3D, Basis, basis)
SETGET_STRUCT(Transform3D, Vector3, origin)
SETGET_NUMBER_STRUCT(Color, double, r)
SETGET_NUMBER_STRUCT(Color, double, g)
SETGET_NUMBER_STRUCT(Color, double, b)
SETGET_NUMBER_STRUCT(Color, double, a)
SETGET_NUMBER_STRUCT_FUNC(Color, int64_t, r8, set_r8, get_r8)
SETGET_NUMBER_STRUCT_FUNC(Color, int64_t, g8, set_g8, get_g8)
SETGET_NUMBER_STRUCT_FUNC(Color, int64_t, b8, set_b8, get_b8)
SETGET_NUMBER_STRUCT_FUNC(Color, int64_t, a8, set_a8, get_a8)
SETGET_NUMBER_STRUCT_FUNC(Color, double, h, set_h, get_h)
SETGET_NUMBER_STRUCT_FUNC(Color, double, s, set_s, get_s)
SETGET_NUMBER_STRUCT_FUNC(Color, double, v, set_v, get_v)
#endif // VARIANT_SETGET_H