Path: blob/master/core/variant/variant_construct.cpp
10277 views
/**************************************************************************/1/* variant_construct.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "variant_construct.h"3132struct VariantConstructData {33void (*construct)(Variant &r_base, const Variant **p_args, Callable::CallError &r_error) = nullptr;34Variant::ValidatedConstructor validated_construct = nullptr;35Variant::PTRConstructor ptr_construct = nullptr;36Variant::Type (*get_argument_type)(int) = nullptr;37int argument_count = 0;38Vector<String> arg_names;39};4041static LocalVector<VariantConstructData> construct_data[Variant::VARIANT_MAX];4243template <typename T>44static void add_constructor(const Vector<String> &arg_names) {45ERR_FAIL_COND_MSG(arg_names.size() != T::get_argument_count(), vformat("Argument names size mismatch for '%s'.", Variant::get_type_name(T::get_base_type())));4647VariantConstructData cd;48cd.construct = T::construct;49cd.validated_construct = T::validated_construct;50cd.ptr_construct = T::ptr_construct;51cd.get_argument_type = T::get_argument_type;52cd.argument_count = T::get_argument_count();53cd.arg_names = arg_names;54construct_data[T::get_base_type()].push_back(cd);55}5657void Variant::_register_variant_constructors() {58add_constructor<VariantConstructNoArgsNil>(sarray());59add_constructor<VariantConstructorNil>(sarray("from"));6061add_constructor<VariantConstructNoArgs<bool>>(sarray());62add_constructor<VariantConstructor<bool, bool>>(sarray("from"));63add_constructor<VariantConstructor<bool, int64_t>>(sarray("from"));64add_constructor<VariantConstructor<bool, double>>(sarray("from"));6566add_constructor<VariantConstructNoArgs<int64_t>>(sarray());67add_constructor<VariantConstructor<int64_t, int64_t>>(sarray("from"));68add_constructor<VariantConstructor<int64_t, double>>(sarray("from"));69add_constructor<VariantConstructor<int64_t, bool>>(sarray("from"));70add_constructor<VariantConstructorFromString<int64_t>>(sarray("from"));7172add_constructor<VariantConstructNoArgs<double>>(sarray());73add_constructor<VariantConstructor<double, double>>(sarray("from"));74add_constructor<VariantConstructor<double, int64_t>>(sarray("from"));75add_constructor<VariantConstructor<double, bool>>(sarray("from"));76add_constructor<VariantConstructorFromString<double>>(sarray("from"));7778add_constructor<VariantConstructNoArgs<String>>(sarray());79add_constructor<VariantConstructor<String, String>>(sarray("from"));80add_constructor<VariantConstructor<String, StringName>>(sarray("from"));81add_constructor<VariantConstructor<String, NodePath>>(sarray("from"));8283add_constructor<VariantConstructNoArgs<Vector2>>(sarray());84add_constructor<VariantConstructor<Vector2, Vector2>>(sarray("from"));85add_constructor<VariantConstructor<Vector2, Vector2i>>(sarray("from"));86add_constructor<VariantConstructor<Vector2, double, double>>(sarray("x", "y"));8788add_constructor<VariantConstructNoArgs<Vector2i>>(sarray());89add_constructor<VariantConstructor<Vector2i, Vector2i>>(sarray("from"));90add_constructor<VariantConstructor<Vector2i, Vector2>>(sarray("from"));91add_constructor<VariantConstructor<Vector2i, int64_t, int64_t>>(sarray("x", "y"));9293add_constructor<VariantConstructNoArgs<Rect2>>(sarray());94add_constructor<VariantConstructor<Rect2, Rect2>>(sarray("from"));95add_constructor<VariantConstructor<Rect2, Rect2i>>(sarray("from"));96add_constructor<VariantConstructor<Rect2, Vector2, Vector2>>(sarray("position", "size"));97add_constructor<VariantConstructor<Rect2, double, double, double, double>>(sarray("x", "y", "width", "height"));9899add_constructor<VariantConstructNoArgs<Rect2i>>(sarray());100add_constructor<VariantConstructor<Rect2i, Rect2i>>(sarray("from"));101add_constructor<VariantConstructor<Rect2i, Rect2>>(sarray("from"));102add_constructor<VariantConstructor<Rect2i, Vector2i, Vector2i>>(sarray("position", "size"));103add_constructor<VariantConstructor<Rect2i, int64_t, int64_t, int64_t, int64_t>>(sarray("x", "y", "width", "height"));104105add_constructor<VariantConstructNoArgs<Vector3>>(sarray());106add_constructor<VariantConstructor<Vector3, Vector3>>(sarray("from"));107add_constructor<VariantConstructor<Vector3, Vector3i>>(sarray("from"));108add_constructor<VariantConstructor<Vector3, double, double, double>>(sarray("x", "y", "z"));109110add_constructor<VariantConstructNoArgs<Vector3i>>(sarray());111add_constructor<VariantConstructor<Vector3i, Vector3i>>(sarray("from"));112add_constructor<VariantConstructor<Vector3i, Vector3>>(sarray("from"));113add_constructor<VariantConstructor<Vector3i, int64_t, int64_t, int64_t>>(sarray("x", "y", "z"));114115add_constructor<VariantConstructNoArgs<Vector4>>(sarray());116add_constructor<VariantConstructor<Vector4, Vector4>>(sarray("from"));117add_constructor<VariantConstructor<Vector4, Vector4i>>(sarray("from"));118add_constructor<VariantConstructor<Vector4, double, double, double, double>>(sarray("x", "y", "z", "w"));119120add_constructor<VariantConstructNoArgs<Vector4i>>(sarray());121add_constructor<VariantConstructor<Vector4i, Vector4i>>(sarray("from"));122add_constructor<VariantConstructor<Vector4i, Vector4>>(sarray("from"));123add_constructor<VariantConstructor<Vector4i, int64_t, int64_t, int64_t, int64_t>>(sarray("x", "y", "z", "w"));124125add_constructor<VariantConstructNoArgs<Transform2D>>(sarray());126add_constructor<VariantConstructor<Transform2D, Transform2D>>(sarray("from"));127add_constructor<VariantConstructor<Transform2D, float, Vector2>>(sarray("rotation", "position"));128add_constructor<VariantConstructor<Transform2D, float, Size2, float, Vector2>>(sarray("rotation", "scale", "skew", "position"));129add_constructor<VariantConstructor<Transform2D, Vector2, Vector2, Vector2>>(sarray("x_axis", "y_axis", "origin"));130131add_constructor<VariantConstructNoArgs<Plane>>(sarray());132add_constructor<VariantConstructor<Plane, Plane>>(sarray("from"));133add_constructor<VariantConstructor<Plane, Vector3>>(sarray("normal"));134add_constructor<VariantConstructor<Plane, Vector3, double>>(sarray("normal", "d"));135add_constructor<VariantConstructor<Plane, Vector3, Vector3>>(sarray("normal", "point"));136add_constructor<VariantConstructor<Plane, Vector3, Vector3, Vector3>>(sarray("point1", "point2", "point3"));137add_constructor<VariantConstructor<Plane, double, double, double, double>>(sarray("a", "b", "c", "d"));138139add_constructor<VariantConstructNoArgs<Quaternion>>(sarray());140add_constructor<VariantConstructor<Quaternion, Quaternion>>(sarray("from"));141add_constructor<VariantConstructor<Quaternion, Basis>>(sarray("from"));142add_constructor<VariantConstructor<Quaternion, Vector3, double>>(sarray("axis", "angle"));143add_constructor<VariantConstructor<Quaternion, Vector3, Vector3>>(sarray("arc_from", "arc_to"));144add_constructor<VariantConstructor<Quaternion, double, double, double, double>>(sarray("x", "y", "z", "w"));145146add_constructor<VariantConstructNoArgs<::AABB>>(sarray());147add_constructor<VariantConstructor<::AABB, ::AABB>>(sarray("from"));148add_constructor<VariantConstructor<::AABB, Vector3, Vector3>>(sarray("position", "size"));149150add_constructor<VariantConstructNoArgs<Basis>>(sarray());151add_constructor<VariantConstructor<Basis, Basis>>(sarray("from"));152add_constructor<VariantConstructor<Basis, Quaternion>>(sarray("from"));153add_constructor<VariantConstructor<Basis, Vector3, double>>(sarray("axis", "angle"));154add_constructor<VariantConstructor<Basis, Vector3, Vector3, Vector3>>(sarray("x_axis", "y_axis", "z_axis"));155156add_constructor<VariantConstructNoArgs<Transform3D>>(sarray());157add_constructor<VariantConstructor<Transform3D, Transform3D>>(sarray("from"));158add_constructor<VariantConstructor<Transform3D, Basis, Vector3>>(sarray("basis", "origin"));159add_constructor<VariantConstructor<Transform3D, Vector3, Vector3, Vector3, Vector3>>(sarray("x_axis", "y_axis", "z_axis", "origin"));160add_constructor<VariantConstructor<Transform3D, Projection>>(sarray("from"));161162add_constructor<VariantConstructNoArgs<Projection>>(sarray());163add_constructor<VariantConstructor<Projection, Projection>>(sarray("from"));164add_constructor<VariantConstructor<Projection, Transform3D>>(sarray("from"));165add_constructor<VariantConstructor<Projection, Vector4, Vector4, Vector4, Vector4>>(sarray("x_axis", "y_axis", "z_axis", "w_axis"));166167add_constructor<VariantConstructNoArgs<Color>>(sarray());168add_constructor<VariantConstructor<Color, Color>>(sarray("from"));169add_constructor<VariantConstructor<Color, Color, double>>(sarray("from", "alpha"));170add_constructor<VariantConstructor<Color, double, double, double>>(sarray("r", "g", "b"));171add_constructor<VariantConstructor<Color, double, double, double, double>>(sarray("r", "g", "b", "a"));172add_constructor<VariantConstructor<Color, String>>(sarray("code"));173add_constructor<VariantConstructor<Color, String, double>>(sarray("code", "alpha"));174175add_constructor<VariantConstructNoArgs<StringName>>(sarray());176add_constructor<VariantConstructor<StringName, StringName>>(sarray("from"));177add_constructor<VariantConstructor<StringName, String>>(sarray("from"));178179add_constructor<VariantConstructNoArgs<NodePath>>(sarray());180add_constructor<VariantConstructor<NodePath, NodePath>>(sarray("from"));181add_constructor<VariantConstructor<NodePath, String>>(sarray("from"));182183add_constructor<VariantConstructNoArgs<::RID>>(sarray());184add_constructor<VariantConstructor<::RID, ::RID>>(sarray("from"));185186add_constructor<VariantConstructNoArgsObject>(sarray());187add_constructor<VariantConstructorObject>(sarray("from"));188add_constructor<VariantConstructorNilObject>(sarray("from"));189190add_constructor<VariantConstructNoArgs<Callable>>(sarray());191add_constructor<VariantConstructor<Callable, Callable>>(sarray("from"));192add_constructor<VariantConstructorCallableArgs>(sarray("object", "method"));193194add_constructor<VariantConstructNoArgs<Signal>>(sarray());195add_constructor<VariantConstructor<Signal, Signal>>(sarray("from"));196add_constructor<VariantConstructorSignalArgs>(sarray("object", "signal"));197198add_constructor<VariantConstructNoArgs<Dictionary>>(sarray());199add_constructor<VariantConstructor<Dictionary, Dictionary>>(sarray("from"));200add_constructor<VariantConstructorTypedDictionary>(sarray("base", "key_type", "key_class_name", "key_script", "value_type", "value_class_name", "value_script"));201202add_constructor<VariantConstructNoArgs<Array>>(sarray());203add_constructor<VariantConstructor<Array, Array>>(sarray("from"));204add_constructor<VariantConstructorTypedArray>(sarray("base", "type", "class_name", "script"));205add_constructor<VariantConstructorToArray<PackedByteArray>>(sarray("from"));206add_constructor<VariantConstructorToArray<PackedInt32Array>>(sarray("from"));207add_constructor<VariantConstructorToArray<PackedInt64Array>>(sarray("from"));208add_constructor<VariantConstructorToArray<PackedFloat32Array>>(sarray("from"));209add_constructor<VariantConstructorToArray<PackedFloat64Array>>(sarray("from"));210add_constructor<VariantConstructorToArray<PackedStringArray>>(sarray("from"));211add_constructor<VariantConstructorToArray<PackedVector2Array>>(sarray("from"));212add_constructor<VariantConstructorToArray<PackedVector3Array>>(sarray("from"));213add_constructor<VariantConstructorToArray<PackedColorArray>>(sarray("from"));214add_constructor<VariantConstructorToArray<PackedVector4Array>>(sarray("from"));215216add_constructor<VariantConstructNoArgs<PackedByteArray>>(sarray());217add_constructor<VariantConstructor<PackedByteArray, PackedByteArray>>(sarray("from"));218add_constructor<VariantConstructorFromArray<PackedByteArray>>(sarray("from"));219220add_constructor<VariantConstructNoArgs<PackedInt32Array>>(sarray());221add_constructor<VariantConstructor<PackedInt32Array, PackedInt32Array>>(sarray("from"));222add_constructor<VariantConstructorFromArray<PackedInt32Array>>(sarray("from"));223224add_constructor<VariantConstructNoArgs<PackedInt64Array>>(sarray());225add_constructor<VariantConstructor<PackedInt64Array, PackedInt64Array>>(sarray("from"));226add_constructor<VariantConstructorFromArray<PackedInt64Array>>(sarray("from"));227228add_constructor<VariantConstructNoArgs<PackedFloat32Array>>(sarray());229add_constructor<VariantConstructor<PackedFloat32Array, PackedFloat32Array>>(sarray("from"));230add_constructor<VariantConstructorFromArray<PackedFloat32Array>>(sarray("from"));231232add_constructor<VariantConstructNoArgs<PackedFloat64Array>>(sarray());233add_constructor<VariantConstructor<PackedFloat64Array, PackedFloat64Array>>(sarray("from"));234add_constructor<VariantConstructorFromArray<PackedFloat64Array>>(sarray("from"));235236add_constructor<VariantConstructNoArgs<PackedStringArray>>(sarray());237add_constructor<VariantConstructor<PackedStringArray, PackedStringArray>>(sarray("from"));238add_constructor<VariantConstructorFromArray<PackedStringArray>>(sarray("from"));239240add_constructor<VariantConstructNoArgs<PackedVector2Array>>(sarray());241add_constructor<VariantConstructor<PackedVector2Array, PackedVector2Array>>(sarray("from"));242add_constructor<VariantConstructorFromArray<PackedVector2Array>>(sarray("from"));243244add_constructor<VariantConstructNoArgs<PackedVector3Array>>(sarray());245add_constructor<VariantConstructor<PackedVector3Array, PackedVector3Array>>(sarray("from"));246add_constructor<VariantConstructorFromArray<PackedVector3Array>>(sarray("from"));247248add_constructor<VariantConstructNoArgs<PackedColorArray>>(sarray());249add_constructor<VariantConstructor<PackedColorArray, PackedColorArray>>(sarray("from"));250add_constructor<VariantConstructorFromArray<PackedColorArray>>(sarray("from"));251252add_constructor<VariantConstructNoArgs<PackedVector4Array>>(sarray());253add_constructor<VariantConstructor<PackedVector4Array, PackedVector4Array>>(sarray("from"));254add_constructor<VariantConstructorFromArray<PackedVector4Array>>(sarray("from"));255}256257void Variant::_unregister_variant_constructors() {258for (int i = 0; i < Variant::VARIANT_MAX; i++) {259construct_data[i].clear();260}261}262263void Variant::construct(Variant::Type p_type, Variant &base, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {264ERR_FAIL_INDEX(p_type, Variant::VARIANT_MAX);265uint32_t s = construct_data[p_type].size();266for (uint32_t i = 0; i < s; i++) {267int argc = construct_data[p_type][i].argument_count;268if (argc != p_argcount) {269continue;270}271bool args_match = true;272for (int j = 0; j < argc; j++) {273if (!Variant::can_convert_strict(p_args[j]->get_type(), construct_data[p_type][i].get_argument_type(j))) {274args_match = false;275break;276}277}278279if (!args_match) {280continue;281}282283construct_data[p_type][i].construct(base, p_args, r_error);284return;285}286287r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;288}289290int Variant::get_constructor_count(Variant::Type p_type) {291ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, -1);292return construct_data[p_type].size();293}294295Variant::ValidatedConstructor Variant::get_validated_constructor(Variant::Type p_type, int p_constructor) {296ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr);297ERR_FAIL_INDEX_V(p_constructor, (int)construct_data[p_type].size(), nullptr);298return construct_data[p_type][p_constructor].validated_construct;299}300301Variant::PTRConstructor Variant::get_ptr_constructor(Variant::Type p_type, int p_constructor) {302ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr);303ERR_FAIL_INDEX_V(p_constructor, (int)construct_data[p_type].size(), nullptr);304return construct_data[p_type][p_constructor].ptr_construct;305}306307int Variant::get_constructor_argument_count(Variant::Type p_type, int p_constructor) {308ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, -1);309ERR_FAIL_INDEX_V(p_constructor, (int)construct_data[p_type].size(), -1);310return construct_data[p_type][p_constructor].argument_count;311}312313Variant::Type Variant::get_constructor_argument_type(Variant::Type p_type, int p_constructor, int p_argument) {314ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, Variant::VARIANT_MAX);315ERR_FAIL_INDEX_V(p_constructor, (int)construct_data[p_type].size(), Variant::VARIANT_MAX);316return construct_data[p_type][p_constructor].get_argument_type(p_argument);317}318319String Variant::get_constructor_argument_name(Variant::Type p_type, int p_constructor, int p_argument) {320ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, String());321ERR_FAIL_INDEX_V(p_constructor, (int)construct_data[p_type].size(), String());322return construct_data[p_type][p_constructor].arg_names[p_argument];323}324325void Variant::get_constructor_list(Type p_type, List<MethodInfo> *r_list) {326ERR_FAIL_INDEX(p_type, Variant::VARIANT_MAX);327328MethodInfo mi;329mi.return_val.type = p_type;330mi.name = get_type_name(p_type);331332for (int i = 0; i < get_constructor_count(p_type); i++) {333int ac = get_constructor_argument_count(p_type, i);334mi.arguments.clear();335for (int j = 0; j < ac; j++) {336PropertyInfo arg;337arg.name = get_constructor_argument_name(p_type, i, j);338arg.type = get_constructor_argument_type(p_type, i, j);339mi.arguments.push_back(arg);340}341r_list->push_back(mi);342}343}344345346