Change how no-arg constructor is handled internally in Variant.

This commit is contained in:
reduz 2020-11-09 15:16:39 -03:00
parent b4a0661885
commit 9979abce74
2 changed files with 320 additions and 98 deletions

View file

@ -448,6 +448,90 @@ public:
}
};
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) {
PtrToArg<T>::encode(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) {
PtrToArg<Object *>::encode(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;
}
};
struct VariantConstructData {
void (*construct)(Variant &r_base, const Variant **p_args, Callable::CallError &r_error);
Variant::ValidatedConstructor validated_construct;
@ -459,35 +543,6 @@ struct VariantConstructData {
static LocalVector<VariantConstructData> construct_data[Variant::VARIANT_MAX];
static void variant_initialize_nil(Variant *v) {
VariantInternal::clear(v);
}
template <class T>
static void variant_initialize(Variant *v) {
VariantTypeChanger<T>::change(v);
}
template <class T>
static void variant_initialize_zero(Variant *v) {
VariantTypeChanger<T>::change(v);
*VariantGetInternalPtr<T>::get_ptr(v) = 0;
}
static void variant_initialize_false(Variant *v) {
VariantTypeChanger<bool>::change(v);
*VariantGetInternalPtr<bool>::get_ptr(v) = false;
}
static void variant_initialize_obj(Variant *v) {
VariantInternal::clear(v);
VariantInternal::object_assign_null(v);
}
typedef void (*VariantInitializeFunc)(Variant *v);
static VariantInitializeFunc initialize_funcs[Variant::VARIANT_MAX];
template <class T>
static void add_constructor(const Vector<String> &arg_names) {
ERR_FAIL_COND_MSG(arg_names.size() != T::get_argument_count(), "Argument names size mismatch for " + Variant::get_type_name(T::get_base_type()) + ".");
@ -503,93 +558,119 @@ static void add_constructor(const Vector<String> &arg_names) {
}
void Variant::_register_variant_constructors() {
add_constructor<VariantConstructNoArgsNil>(sarray());
add_constructor<VariantConstructorNil>(sarray("from"));
add_constructor<VariantConstructNoArgs<bool>>(sarray());
add_constructor<VariantConstructor<bool, bool>>(sarray("from"));
add_constructor<VariantConstructor<bool, int64_t>>(sarray("from"));
add_constructor<VariantConstructor<bool, double>>(sarray("from"));
add_constructor<VariantConstructNoArgs<int64_t>>(sarray());
add_constructor<VariantConstructor<int64_t, int64_t>>(sarray("from"));
add_constructor<VariantConstructor<int64_t, double>>(sarray("from"));
add_constructor<VariantConstructNoArgs<double>>(sarray());
add_constructor<VariantConstructor<double, double>>(sarray("from"));
add_constructor<VariantConstructor<double, int64_t>>(sarray("from"));
add_constructor<VariantConstructNoArgs<String>>(sarray());
add_constructor<VariantConstructor<String, String>>(sarray("from"));
add_constructor<VariantConstructor<String, StringName>>(sarray("from"));
add_constructor<VariantConstructor<String, NodePath>>(sarray("from"));
add_constructor<VariantConstructNoArgs<Vector2>>(sarray());
add_constructor<VariantConstructor<Vector2, Vector2>>(sarray("from"));
add_constructor<VariantConstructor<Vector2, Vector2i>>(sarray("from"));
add_constructor<VariantConstructor<Vector2, double, double>>(sarray("x", "y"));
add_constructor<VariantConstructNoArgs<Vector2i>>(sarray());
add_constructor<VariantConstructor<Vector2i, Vector2i>>(sarray("from"));
add_constructor<VariantConstructor<Vector2i, Vector2>>(sarray("from"));
add_constructor<VariantConstructor<Vector2i, int64_t, int64_t>>(sarray("x", "y"));
add_constructor<VariantConstructNoArgs<Rect2>>(sarray());
add_constructor<VariantConstructor<Rect2, Rect2>>(sarray("from"));
add_constructor<VariantConstructor<Rect2, Rect2i>>(sarray("from"));
add_constructor<VariantConstructor<Rect2, Vector2, Vector2>>(sarray("position", "size"));
add_constructor<VariantConstructor<Rect2, double, double, double, double>>(sarray("x", "y", "width", "height"));
add_constructor<VariantConstructNoArgs<Rect2i>>(sarray());
add_constructor<VariantConstructor<Rect2i, Rect2i>>(sarray("from"));
add_constructor<VariantConstructor<Rect2i, Rect2>>(sarray("from"));
add_constructor<VariantConstructor<Rect2i, Vector2i, Vector2i>>(sarray("position", "size"));
add_constructor<VariantConstructor<Rect2i, int64_t, int64_t, int64_t, int64_t>>(sarray("x", "y", "width", "height"));
add_constructor<VariantConstructNoArgs<Vector3>>(sarray());
add_constructor<VariantConstructor<Vector3, Vector3>>(sarray("from"));
add_constructor<VariantConstructor<Vector3, Vector3i>>(sarray("from"));
add_constructor<VariantConstructor<Vector3, double, double, double>>(sarray("x", "y", "z"));
add_constructor<VariantConstructNoArgs<Vector3i>>(sarray());
add_constructor<VariantConstructor<Vector3i, Vector3i>>(sarray("from"));
add_constructor<VariantConstructor<Vector3i, Vector3>>(sarray("from"));
add_constructor<VariantConstructor<Vector3i, int64_t, int64_t, int64_t>>(sarray("x", "y", "z"));
add_constructor<VariantConstructNoArgs<Transform2D>>(sarray());
add_constructor<VariantConstructor<Transform2D, Transform2D>>(sarray("from"));
add_constructor<VariantConstructor<Transform2D, Vector2, Vector2, Vector2>>(sarray("x", "y", "origin"));
add_constructor<VariantConstructNoArgs<Plane>>(sarray());
add_constructor<VariantConstructor<Plane, Plane>>(sarray("from"));
add_constructor<VariantConstructor<Plane, Vector3, double>>(sarray("normal", "d"));
add_constructor<VariantConstructor<Plane, Vector3, Vector3>>(sarray("point", "normal"));
add_constructor<VariantConstructor<Plane, Vector3, Vector3, Vector3>>(sarray("point1", "point2", "point3"));
add_constructor<VariantConstructNoArgs<Quat>>(sarray());
add_constructor<VariantConstructor<Quat, Quat>>(sarray("from"));
add_constructor<VariantConstructor<Quat, Basis>>(sarray("from"));
add_constructor<VariantConstructor<Quat, Vector3>>(sarray("euler"));
add_constructor<VariantConstructor<Quat, Vector3, double>>(sarray("axis", "angle"));
add_constructor<VariantConstructor<Quat, Vector3, Vector3>>(sarray("arc_from", "arc_to"));
add_constructor<VariantConstructNoArgs<::AABB>>(sarray());
add_constructor<VariantConstructor<::AABB, ::AABB>>(sarray("from"));
add_constructor<VariantConstructor<::AABB, Vector3, Vector3>>(sarray("position", "size"));
add_constructor<VariantConstructNoArgs<Basis>>(sarray());
add_constructor<VariantConstructor<Basis, Basis>>(sarray("from"));
add_constructor<VariantConstructor<Basis, Quat>>(sarray("from"));
add_constructor<VariantConstructor<Basis, Vector3, Vector3, Vector3>>(sarray("x", "y", "z"));
add_constructor<VariantConstructNoArgs<Transform>>(sarray());
add_constructor<VariantConstructor<Transform, Transform>>(sarray("from"));
add_constructor<VariantConstructor<Transform, Basis, Vector3>>(sarray("basis", "origin"));
add_constructor<VariantConstructNoArgs<Color>>(sarray());
add_constructor<VariantConstructor<Color, Color>>(sarray("from"));
add_constructor<VariantConstructNoArgs<StringName>>(sarray());
add_constructor<VariantConstructor<StringName, StringName>>(sarray("from"));
add_constructor<VariantConstructor<StringName, String>>(sarray("from"));
add_constructor<VariantConstructNoArgs<NodePath>>(sarray());
add_constructor<VariantConstructor<NodePath, NodePath>>(sarray("from"));
add_constructor<VariantConstructor<NodePath, String>>(sarray("from"));
add_constructor<VariantConstructNoArgs<::RID>>(sarray());
add_constructor<VariantConstructor<::RID, ::RID>>(sarray("from"));
add_constructor<VariantConstructNoArgsObject>(sarray());
add_constructor<VariantConstructorObject>(sarray("from"));
add_constructor<VariantConstructorNilObject>(sarray("from"));
add_constructor<VariantConstructNoArgs<Callable>>(sarray());
add_constructor<VariantConstructor<Callable, Callable>>(sarray("from"));
add_constructor<VariantConstructorCallableArgs>(sarray("object", "method"));
add_constructor<VariantConstructNoArgs<Signal>>(sarray());
add_constructor<VariantConstructor<Signal, Signal>>(sarray("from"));
add_constructor<VariantConstructorSignalArgs>(sarray("object", "signal"));
add_constructor<VariantConstructNoArgs<Dictionary>>(sarray());
add_constructor<VariantConstructor<Dictionary, Dictionary>>(sarray("from"));
add_constructor<VariantConstructNoArgs<Array>>(sarray());
add_constructor<VariantConstructor<Array, Array>>(sarray("from"));
add_constructor<VariantConstructorToArray<PackedByteArray>>(sarray("from"));
add_constructor<VariantConstructorToArray<PackedInt32Array>>(sarray("from"));
@ -601,76 +682,41 @@ void Variant::_register_variant_constructors() {
add_constructor<VariantConstructorToArray<PackedVector3Array>>(sarray("from"));
add_constructor<VariantConstructorToArray<PackedColorArray>>(sarray("from"));
add_constructor<VariantConstructNoArgs<PackedByteArray>>(sarray());
add_constructor<VariantConstructor<PackedByteArray, PackedByteArray>>(sarray("from"));
add_constructor<VariantConstructorFromArray<PackedByteArray>>(sarray("from"));
add_constructor<VariantConstructNoArgs<PackedInt32Array>>(sarray());
add_constructor<VariantConstructor<PackedInt32Array, PackedInt32Array>>(sarray("from"));
add_constructor<VariantConstructorFromArray<PackedInt32Array>>(sarray("from"));
add_constructor<VariantConstructNoArgs<PackedInt64Array>>(sarray());
add_constructor<VariantConstructor<PackedInt64Array, PackedInt64Array>>(sarray("from"));
add_constructor<VariantConstructorFromArray<PackedInt64Array>>(sarray("from"));
add_constructor<VariantConstructNoArgs<PackedFloat32Array>>(sarray());
add_constructor<VariantConstructor<PackedFloat32Array, PackedFloat32Array>>(sarray("from"));
add_constructor<VariantConstructorFromArray<PackedFloat32Array>>(sarray("from"));
add_constructor<VariantConstructNoArgs<PackedFloat64Array>>(sarray());
add_constructor<VariantConstructor<PackedFloat64Array, PackedFloat64Array>>(sarray("from"));
add_constructor<VariantConstructorFromArray<PackedFloat64Array>>(sarray("from"));
add_constructor<VariantConstructNoArgs<PackedStringArray>>(sarray());
add_constructor<VariantConstructor<PackedStringArray, PackedStringArray>>(sarray("from"));
add_constructor<VariantConstructorFromArray<PackedStringArray>>(sarray("from"));
add_constructor<VariantConstructNoArgs<PackedVector2Array>>(sarray());
add_constructor<VariantConstructor<PackedVector2Array, PackedVector2Array>>(sarray("from"));
add_constructor<VariantConstructorFromArray<PackedVector2Array>>(sarray("from"));
add_constructor<VariantConstructNoArgs<PackedVector3Array>>(sarray());
add_constructor<VariantConstructor<PackedVector3Array, PackedVector3Array>>(sarray("from"));
add_constructor<VariantConstructorFromArray<PackedVector3Array>>(sarray("from"));
add_constructor<VariantConstructNoArgs<PackedColorArray>>(sarray());
add_constructor<VariantConstructor<PackedColorArray, PackedColorArray>>(sarray("from"));
add_constructor<VariantConstructorFromArray<PackedColorArray>>(sarray("from"));
initialize_funcs[NIL] = variant_initialize_nil;
// atomic types
initialize_funcs[BOOL] = variant_initialize_false;
initialize_funcs[INT] = variant_initialize_zero<int64_t>;
initialize_funcs[FLOAT] = variant_initialize_zero<double>;
initialize_funcs[STRING] = variant_initialize<String>;
// math types
initialize_funcs[VECTOR2] = variant_initialize<Vector2>;
initialize_funcs[VECTOR2I] = variant_initialize<Vector2i>;
initialize_funcs[RECT2] = variant_initialize<Rect2>;
initialize_funcs[RECT2I] = variant_initialize<Rect2i>;
initialize_funcs[VECTOR3] = variant_initialize<Vector3>;
initialize_funcs[VECTOR3I] = variant_initialize<Vector3i>;
initialize_funcs[TRANSFORM2D] = variant_initialize<Transform2D>;
initialize_funcs[PLANE] = variant_initialize<Plane>;
initialize_funcs[QUAT] = variant_initialize<Quat>;
initialize_funcs[AABB] = variant_initialize<::AABB>;
initialize_funcs[BASIS] = variant_initialize<Basis>;
initialize_funcs[TRANSFORM] = variant_initialize<Transform>;
// misc types
initialize_funcs[COLOR] = variant_initialize<Color>;
initialize_funcs[STRING_NAME] = variant_initialize<StringName>;
initialize_funcs[NODE_PATH] = variant_initialize<NodePath>;
initialize_funcs[RID] = variant_initialize<::RID>;
initialize_funcs[OBJECT] = variant_initialize_obj;
initialize_funcs[CALLABLE] = variant_initialize<Callable>;
initialize_funcs[SIGNAL] = variant_initialize<Signal>;
initialize_funcs[DICTIONARY] = variant_initialize<Dictionary>;
initialize_funcs[ARRAY] = variant_initialize<Array>;
// typed arrays
initialize_funcs[PACKED_BYTE_ARRAY] = variant_initialize<PackedByteArray>;
initialize_funcs[PACKED_INT32_ARRAY] = variant_initialize<PackedInt32Array>;
initialize_funcs[PACKED_INT64_ARRAY] = variant_initialize<PackedInt64Array>;
initialize_funcs[PACKED_FLOAT32_ARRAY] = variant_initialize<PackedFloat32Array>;
initialize_funcs[PACKED_FLOAT64_ARRAY] = variant_initialize<PackedFloat64Array>;
initialize_funcs[PACKED_STRING_ARRAY] = variant_initialize<PackedStringArray>;
initialize_funcs[PACKED_VECTOR2_ARRAY] = variant_initialize<PackedVector2Array>;
initialize_funcs[PACKED_VECTOR3_ARRAY] = variant_initialize<PackedVector3Array>;
initialize_funcs[PACKED_COLOR_ARRAY] = variant_initialize<PackedColorArray>;
}
void Variant::_unregister_variant_constructors() {
@ -680,35 +726,29 @@ void Variant::_unregister_variant_constructors() {
}
void Variant::construct(Variant::Type p_type, Variant &base, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
if (p_argcount == 0) {
initialize_funcs[p_type](&base);
r_error.error = Callable::CallError::CALL_OK;
} else {
uint32_t s = construct_data[p_type].size();
for (uint32_t i = 0; i < s; i++) {
int argc = construct_data[p_type][i].argument_count;
if (argc != p_argcount) {
continue;
uint32_t s = construct_data[p_type].size();
for (uint32_t i = 0; i < s; i++) {
int argc = construct_data[p_type][i].argument_count;
if (argc != p_argcount) {
continue;
}
bool args_match = true;
for (int j = 0; j < argc; j++) {
if (!Variant::can_convert_strict(p_args[j]->get_type(), construct_data[p_type][i].get_argument_type(j))) {
args_match = false;
break;
}
bool args_match = true;
for (int j = 0; j < argc; j++) {
if (!Variant::can_convert_strict(p_args[j]->get_type(), construct_data[p_type][i].get_argument_type(j))) {
args_match = false;
break;
}
}
if (!args_match) {
continue;
}
construct_data[p_type][i].construct(base, p_args, r_error);
return;
}
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
if (!args_match) {
continue;
}
construct_data[p_type][i].construct(base, p_args, r_error);
return;
}
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
}
int Variant::get_constructor_count(Variant::Type p_type) {

View file

@ -936,6 +936,180 @@ struct VariantInitializer<PackedColorArray> {
static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_color_array(v); }
};
template <class T>
struct VariantZeroAssigner {
};
template <>
struct VariantZeroAssigner<bool> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_bool(v) = false; }
};
template <>
struct VariantZeroAssigner<int64_t> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_int(v) = 0; }
};
template <>
struct VariantZeroAssigner<double> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_float(v) = 0.0; }
};
template <>
struct VariantZeroAssigner<float> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_float(v) = 0.0; }
};
template <>
struct VariantZeroAssigner<String> {
static _FORCE_INLINE_ void zero(Variant *v) {}
};
template <>
struct VariantZeroAssigner<Vector2> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_vector2(v) = Vector2(); }
};
template <>
struct VariantZeroAssigner<Vector2i> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_vector2i(v) = Vector2i(); }
};
template <>
struct VariantZeroAssigner<Rect2> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_rect2(v) = Rect2(); }
};
template <>
struct VariantZeroAssigner<Rect2i> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_rect2i(v) = Rect2i(); }
};
template <>
struct VariantZeroAssigner<Vector3> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_vector3(v) = Vector3(); }
};
template <>
struct VariantZeroAssigner<Vector3i> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_vector3i(v) = Vector3i(); }
};
template <>
struct VariantZeroAssigner<Transform2D> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_transform2d(v) = Transform2D(); }
};
template <>
struct VariantZeroAssigner<Plane> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_plane(v) = Plane(); }
};
template <>
struct VariantZeroAssigner<Quat> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_quat(v) = Quat(); }
};
template <>
struct VariantZeroAssigner<AABB> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_aabb(v) = AABB(); }
};
template <>
struct VariantZeroAssigner<Basis> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_basis(v) = Basis(); }
};
template <>
struct VariantZeroAssigner<Transform> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_transform(v) = Transform(); }
};
template <>
struct VariantZeroAssigner<Color> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_color(v) = Color(); }
};
template <>
struct VariantZeroAssigner<StringName> {
static _FORCE_INLINE_ void zero(Variant *v) {}
};
template <>
struct VariantZeroAssigner<NodePath> {
static _FORCE_INLINE_ void zero(Variant *v) {}
};
template <>
struct VariantZeroAssigner<::RID> {
static _FORCE_INLINE_ void zero(Variant *v) { *VariantInternal::get_rid(v) = RID(); }
};
template <>
struct VariantZeroAssigner<Callable> {
static _FORCE_INLINE_ void zero(Variant *v) {}
};
template <>
struct VariantZeroAssigner<Signal> {
static _FORCE_INLINE_ void zero(Variant *v) {}
};
template <>
struct VariantZeroAssigner<Dictionary> {
static _FORCE_INLINE_ void zero(Variant *v) {}
};
template <>
struct VariantZeroAssigner<Array> {
static _FORCE_INLINE_ void zero(Variant *v) {}
};
template <>
struct VariantZeroAssigner<PackedByteArray> {
static _FORCE_INLINE_ void zero(Variant *v) {}
};
template <>
struct VariantZeroAssigner<PackedInt32Array> {
static _FORCE_INLINE_ void zero(Variant *v) {}
};
template <>
struct VariantZeroAssigner<PackedInt64Array> {
static _FORCE_INLINE_ void zero(Variant *v) {}
};
template <>
struct VariantZeroAssigner<PackedFloat32Array> {
static _FORCE_INLINE_ void zero(Variant *v) {}
};
template <>
struct VariantZeroAssigner<PackedFloat64Array> {
static _FORCE_INLINE_ void zero(Variant *v) {}
};
template <>
struct VariantZeroAssigner<PackedStringArray> {
static _FORCE_INLINE_ void zero(Variant *v) {}
};
template <>
struct VariantZeroAssigner<PackedVector2Array> {
static _FORCE_INLINE_ void zero(Variant *v) {}
};
template <>
struct VariantZeroAssigner<PackedVector3Array> {
static _FORCE_INLINE_ void zero(Variant *v) {}
};
template <>
struct VariantZeroAssigner<PackedColorArray> {
static _FORCE_INLINE_ void zero(Variant *v) {}
};
template <class T>
struct VariantTypeChanger {
static _FORCE_INLINE_ void change(Variant *v) {
@ -944,6 +1118,14 @@ struct VariantTypeChanger {
VariantInitializer<T>::init(v);
}
}
static _FORCE_INLINE_ void change_and_reset(Variant *v) {
if (v->get_type() != GetTypeInfo<T>::VARIANT_TYPE || GetTypeInfo<T>::VARIANT_TYPE >= Variant::PACKED_BYTE_ARRAY) { //second condition removed by optimizer
VariantInternal::clear(v);
VariantInitializer<T>::init(v);
}
VariantZeroAssigner<T>::zero(v);
}
};
#endif // VARIANT_INTERNAL_H