variant_construct.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /**************************************************************************/
  2. /* variant_construct.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "variant_construct.h"
  31. struct VariantConstructData {
  32. void (*construct)(Variant &r_base, const Variant **p_args, Callable::CallError &r_error) = nullptr;
  33. Variant::ValidatedConstructor validated_construct = nullptr;
  34. Variant::PTRConstructor ptr_construct = nullptr;
  35. Variant::Type (*get_argument_type)(int) = nullptr;
  36. int argument_count = 0;
  37. Vector<String> arg_names;
  38. };
  39. static LocalVector<VariantConstructData> construct_data[Variant::VARIANT_MAX];
  40. template <typename T>
  41. static void add_constructor(const Vector<String> &arg_names) {
  42. ERR_FAIL_COND_MSG(arg_names.size() != T::get_argument_count(), "Argument names size mismatch for " + Variant::get_type_name(T::get_base_type()) + ".");
  43. VariantConstructData cd;
  44. cd.construct = T::construct;
  45. cd.validated_construct = T::validated_construct;
  46. cd.ptr_construct = T::ptr_construct;
  47. cd.get_argument_type = T::get_argument_type;
  48. cd.argument_count = T::get_argument_count();
  49. cd.arg_names = arg_names;
  50. construct_data[T::get_base_type()].push_back(cd);
  51. }
  52. void Variant::_register_variant_constructors() {
  53. add_constructor<VariantConstructNoArgsNil>(sarray());
  54. add_constructor<VariantConstructorNil>(sarray("from"));
  55. add_constructor<VariantConstructNoArgs<bool>>(sarray());
  56. add_constructor<VariantConstructor<bool, bool>>(sarray("from"));
  57. add_constructor<VariantConstructor<bool, int64_t>>(sarray("from"));
  58. add_constructor<VariantConstructor<bool, double>>(sarray("from"));
  59. add_constructor<VariantConstructNoArgs<int64_t>>(sarray());
  60. add_constructor<VariantConstructor<int64_t, int64_t>>(sarray("from"));
  61. add_constructor<VariantConstructor<int64_t, double>>(sarray("from"));
  62. add_constructor<VariantConstructor<int64_t, bool>>(sarray("from"));
  63. add_constructor<VariantConstructorFromString<int64_t>>(sarray("from"));
  64. add_constructor<VariantConstructNoArgs<double>>(sarray());
  65. add_constructor<VariantConstructor<double, double>>(sarray("from"));
  66. add_constructor<VariantConstructor<double, int64_t>>(sarray("from"));
  67. add_constructor<VariantConstructor<double, bool>>(sarray("from"));
  68. add_constructor<VariantConstructorFromString<double>>(sarray("from"));
  69. add_constructor<VariantConstructNoArgs<String>>(sarray());
  70. add_constructor<VariantConstructor<String, String>>(sarray("from"));
  71. add_constructor<VariantConstructor<String, StringName>>(sarray("from"));
  72. add_constructor<VariantConstructor<String, NodePath>>(sarray("from"));
  73. add_constructor<VariantConstructNoArgs<Vector2>>(sarray());
  74. add_constructor<VariantConstructor<Vector2, Vector2>>(sarray("from"));
  75. add_constructor<VariantConstructor<Vector2, Vector2i>>(sarray("from"));
  76. add_constructor<VariantConstructor<Vector2, double, double>>(sarray("x", "y"));
  77. add_constructor<VariantConstructNoArgs<Vector2i>>(sarray());
  78. add_constructor<VariantConstructor<Vector2i, Vector2i>>(sarray("from"));
  79. add_constructor<VariantConstructor<Vector2i, Vector2>>(sarray("from"));
  80. add_constructor<VariantConstructor<Vector2i, int64_t, int64_t>>(sarray("x", "y"));
  81. add_constructor<VariantConstructNoArgs<Rect2>>(sarray());
  82. add_constructor<VariantConstructor<Rect2, Rect2>>(sarray("from"));
  83. add_constructor<VariantConstructor<Rect2, Rect2i>>(sarray("from"));
  84. add_constructor<VariantConstructor<Rect2, Vector2, Vector2>>(sarray("position", "size"));
  85. add_constructor<VariantConstructor<Rect2, double, double, double, double>>(sarray("x", "y", "width", "height"));
  86. add_constructor<VariantConstructNoArgs<Rect2i>>(sarray());
  87. add_constructor<VariantConstructor<Rect2i, Rect2i>>(sarray("from"));
  88. add_constructor<VariantConstructor<Rect2i, Rect2>>(sarray("from"));
  89. add_constructor<VariantConstructor<Rect2i, Vector2i, Vector2i>>(sarray("position", "size"));
  90. add_constructor<VariantConstructor<Rect2i, int64_t, int64_t, int64_t, int64_t>>(sarray("x", "y", "width", "height"));
  91. add_constructor<VariantConstructNoArgs<Vector3>>(sarray());
  92. add_constructor<VariantConstructor<Vector3, Vector3>>(sarray("from"));
  93. add_constructor<VariantConstructor<Vector3, Vector3i>>(sarray("from"));
  94. add_constructor<VariantConstructor<Vector3, double, double, double>>(sarray("x", "y", "z"));
  95. add_constructor<VariantConstructNoArgs<Vector3i>>(sarray());
  96. add_constructor<VariantConstructor<Vector3i, Vector3i>>(sarray("from"));
  97. add_constructor<VariantConstructor<Vector3i, Vector3>>(sarray("from"));
  98. add_constructor<VariantConstructor<Vector3i, int64_t, int64_t, int64_t>>(sarray("x", "y", "z"));
  99. add_constructor<VariantConstructNoArgs<Vector4>>(sarray());
  100. add_constructor<VariantConstructor<Vector4, Vector4>>(sarray("from"));
  101. add_constructor<VariantConstructor<Vector4, Vector4i>>(sarray("from"));
  102. add_constructor<VariantConstructor<Vector4, double, double, double, double>>(sarray("x", "y", "z", "w"));
  103. add_constructor<VariantConstructNoArgs<Vector4i>>(sarray());
  104. add_constructor<VariantConstructor<Vector4i, Vector4i>>(sarray("from"));
  105. add_constructor<VariantConstructor<Vector4i, Vector4>>(sarray("from"));
  106. add_constructor<VariantConstructor<Vector4i, int64_t, int64_t, int64_t, int64_t>>(sarray("x", "y", "z", "w"));
  107. add_constructor<VariantConstructNoArgs<Transform2D>>(sarray());
  108. add_constructor<VariantConstructor<Transform2D, Transform2D>>(sarray("from"));
  109. add_constructor<VariantConstructor<Transform2D, float, Vector2>>(sarray("rotation", "position"));
  110. add_constructor<VariantConstructor<Transform2D, float, Size2, float, Vector2>>(sarray("rotation", "scale", "skew", "position"));
  111. add_constructor<VariantConstructor<Transform2D, Vector2, Vector2, Vector2>>(sarray("x_axis", "y_axis", "origin"));
  112. add_constructor<VariantConstructNoArgs<Plane>>(sarray());
  113. add_constructor<VariantConstructor<Plane, Plane>>(sarray("from"));
  114. add_constructor<VariantConstructor<Plane, Vector3>>(sarray("normal"));
  115. add_constructor<VariantConstructor<Plane, Vector3, double>>(sarray("normal", "d"));
  116. add_constructor<VariantConstructor<Plane, Vector3, Vector3>>(sarray("normal", "point"));
  117. add_constructor<VariantConstructor<Plane, Vector3, Vector3, Vector3>>(sarray("point1", "point2", "point3"));
  118. add_constructor<VariantConstructor<Plane, double, double, double, double>>(sarray("a", "b", "c", "d"));
  119. add_constructor<VariantConstructNoArgs<Quaternion>>(sarray());
  120. add_constructor<VariantConstructor<Quaternion, Quaternion>>(sarray("from"));
  121. add_constructor<VariantConstructor<Quaternion, Basis>>(sarray("from"));
  122. add_constructor<VariantConstructor<Quaternion, Vector3, double>>(sarray("axis", "angle"));
  123. add_constructor<VariantConstructor<Quaternion, Vector3, Vector3>>(sarray("arc_from", "arc_to"));
  124. add_constructor<VariantConstructor<Quaternion, double, double, double, double>>(sarray("x", "y", "z", "w"));
  125. add_constructor<VariantConstructNoArgs<::AABB>>(sarray());
  126. add_constructor<VariantConstructor<::AABB, ::AABB>>(sarray("from"));
  127. add_constructor<VariantConstructor<::AABB, Vector3, Vector3>>(sarray("position", "size"));
  128. add_constructor<VariantConstructNoArgs<Basis>>(sarray());
  129. add_constructor<VariantConstructor<Basis, Basis>>(sarray("from"));
  130. add_constructor<VariantConstructor<Basis, Quaternion>>(sarray("from"));
  131. add_constructor<VariantConstructor<Basis, Vector3, double>>(sarray("axis", "angle"));
  132. add_constructor<VariantConstructor<Basis, Vector3, Vector3, Vector3>>(sarray("x_axis", "y_axis", "z_axis"));
  133. add_constructor<VariantConstructNoArgs<Transform3D>>(sarray());
  134. add_constructor<VariantConstructor<Transform3D, Transform3D>>(sarray("from"));
  135. add_constructor<VariantConstructor<Transform3D, Basis, Vector3>>(sarray("basis", "origin"));
  136. add_constructor<VariantConstructor<Transform3D, Vector3, Vector3, Vector3, Vector3>>(sarray("x_axis", "y_axis", "z_axis", "origin"));
  137. add_constructor<VariantConstructor<Transform3D, Projection>>(sarray("from"));
  138. add_constructor<VariantConstructNoArgs<Projection>>(sarray());
  139. add_constructor<VariantConstructor<Projection, Projection>>(sarray("from"));
  140. add_constructor<VariantConstructor<Projection, Transform3D>>(sarray("from"));
  141. add_constructor<VariantConstructor<Projection, Vector4, Vector4, Vector4, Vector4>>(sarray("x_axis", "y_axis", "z_axis", "w_axis"));
  142. add_constructor<VariantConstructNoArgs<Color>>(sarray());
  143. add_constructor<VariantConstructor<Color, Color>>(sarray("from"));
  144. add_constructor<VariantConstructor<Color, Color, double>>(sarray("from", "alpha"));
  145. add_constructor<VariantConstructor<Color, double, double, double>>(sarray("r", "g", "b"));
  146. add_constructor<VariantConstructor<Color, double, double, double, double>>(sarray("r", "g", "b", "a"));
  147. add_constructor<VariantConstructor<Color, String>>(sarray("code"));
  148. add_constructor<VariantConstructor<Color, String, double>>(sarray("code", "alpha"));
  149. add_constructor<VariantConstructNoArgs<StringName>>(sarray());
  150. add_constructor<VariantConstructor<StringName, StringName>>(sarray("from"));
  151. add_constructor<VariantConstructor<StringName, String>>(sarray("from"));
  152. add_constructor<VariantConstructNoArgs<NodePath>>(sarray());
  153. add_constructor<VariantConstructor<NodePath, NodePath>>(sarray("from"));
  154. add_constructor<VariantConstructor<NodePath, String>>(sarray("from"));
  155. add_constructor<VariantConstructNoArgs<::RID>>(sarray());
  156. add_constructor<VariantConstructor<::RID, ::RID>>(sarray("from"));
  157. add_constructor<VariantConstructNoArgsObject>(sarray());
  158. add_constructor<VariantConstructorObject>(sarray("from"));
  159. add_constructor<VariantConstructorNilObject>(sarray("from"));
  160. add_constructor<VariantConstructNoArgs<Callable>>(sarray());
  161. add_constructor<VariantConstructor<Callable, Callable>>(sarray("from"));
  162. add_constructor<VariantConstructorCallableArgs>(sarray("object", "method"));
  163. add_constructor<VariantConstructNoArgs<Signal>>(sarray());
  164. add_constructor<VariantConstructor<Signal, Signal>>(sarray("from"));
  165. add_constructor<VariantConstructorSignalArgs>(sarray("object", "signal"));
  166. add_constructor<VariantConstructNoArgs<Dictionary>>(sarray());
  167. add_constructor<VariantConstructor<Dictionary, Dictionary>>(sarray("from"));
  168. add_constructor<VariantConstructNoArgs<Array>>(sarray());
  169. add_constructor<VariantConstructor<Array, Array>>(sarray("from"));
  170. add_constructor<VariantConstructorTypedArray>(sarray("base", "type", "class_name", "script"));
  171. add_constructor<VariantConstructorToArray<PackedByteArray>>(sarray("from"));
  172. add_constructor<VariantConstructorToArray<PackedInt32Array>>(sarray("from"));
  173. add_constructor<VariantConstructorToArray<PackedInt64Array>>(sarray("from"));
  174. add_constructor<VariantConstructorToArray<PackedFloat32Array>>(sarray("from"));
  175. add_constructor<VariantConstructorToArray<PackedFloat64Array>>(sarray("from"));
  176. add_constructor<VariantConstructorToArray<PackedStringArray>>(sarray("from"));
  177. add_constructor<VariantConstructorToArray<PackedVector2Array>>(sarray("from"));
  178. add_constructor<VariantConstructorToArray<PackedVector3Array>>(sarray("from"));
  179. add_constructor<VariantConstructorToArray<PackedColorArray>>(sarray("from"));
  180. add_constructor<VariantConstructorToArray<PackedVector4Array>>(sarray("from"));
  181. add_constructor<VariantConstructNoArgs<PackedByteArray>>(sarray());
  182. add_constructor<VariantConstructor<PackedByteArray, PackedByteArray>>(sarray("from"));
  183. add_constructor<VariantConstructorFromArray<PackedByteArray>>(sarray("from"));
  184. add_constructor<VariantConstructNoArgs<PackedInt32Array>>(sarray());
  185. add_constructor<VariantConstructor<PackedInt32Array, PackedInt32Array>>(sarray("from"));
  186. add_constructor<VariantConstructorFromArray<PackedInt32Array>>(sarray("from"));
  187. add_constructor<VariantConstructNoArgs<PackedInt64Array>>(sarray());
  188. add_constructor<VariantConstructor<PackedInt64Array, PackedInt64Array>>(sarray("from"));
  189. add_constructor<VariantConstructorFromArray<PackedInt64Array>>(sarray("from"));
  190. add_constructor<VariantConstructNoArgs<PackedFloat32Array>>(sarray());
  191. add_constructor<VariantConstructor<PackedFloat32Array, PackedFloat32Array>>(sarray("from"));
  192. add_constructor<VariantConstructorFromArray<PackedFloat32Array>>(sarray("from"));
  193. add_constructor<VariantConstructNoArgs<PackedFloat64Array>>(sarray());
  194. add_constructor<VariantConstructor<PackedFloat64Array, PackedFloat64Array>>(sarray("from"));
  195. add_constructor<VariantConstructorFromArray<PackedFloat64Array>>(sarray("from"));
  196. add_constructor<VariantConstructNoArgs<PackedStringArray>>(sarray());
  197. add_constructor<VariantConstructor<PackedStringArray, PackedStringArray>>(sarray("from"));
  198. add_constructor<VariantConstructorFromArray<PackedStringArray>>(sarray("from"));
  199. add_constructor<VariantConstructNoArgs<PackedVector2Array>>(sarray());
  200. add_constructor<VariantConstructor<PackedVector2Array, PackedVector2Array>>(sarray("from"));
  201. add_constructor<VariantConstructorFromArray<PackedVector2Array>>(sarray("from"));
  202. add_constructor<VariantConstructNoArgs<PackedVector3Array>>(sarray());
  203. add_constructor<VariantConstructor<PackedVector3Array, PackedVector3Array>>(sarray("from"));
  204. add_constructor<VariantConstructorFromArray<PackedVector3Array>>(sarray("from"));
  205. add_constructor<VariantConstructNoArgs<PackedColorArray>>(sarray());
  206. add_constructor<VariantConstructor<PackedColorArray, PackedColorArray>>(sarray("from"));
  207. add_constructor<VariantConstructorFromArray<PackedColorArray>>(sarray("from"));
  208. add_constructor<VariantConstructNoArgs<PackedVector4Array>>(sarray());
  209. add_constructor<VariantConstructor<PackedVector4Array, PackedVector4Array>>(sarray("from"));
  210. add_constructor<VariantConstructorFromArray<PackedVector4Array>>(sarray("from"));
  211. }
  212. void Variant::_unregister_variant_constructors() {
  213. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  214. construct_data[i].clear();
  215. }
  216. }
  217. void Variant::construct(Variant::Type p_type, Variant &base, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  218. ERR_FAIL_INDEX(p_type, Variant::VARIANT_MAX);
  219. uint32_t s = construct_data[p_type].size();
  220. for (uint32_t i = 0; i < s; i++) {
  221. int argc = construct_data[p_type][i].argument_count;
  222. if (argc != p_argcount) {
  223. continue;
  224. }
  225. bool args_match = true;
  226. for (int j = 0; j < argc; j++) {
  227. if (!Variant::can_convert_strict(p_args[j]->get_type(), construct_data[p_type][i].get_argument_type(j))) {
  228. args_match = false;
  229. break;
  230. }
  231. }
  232. if (!args_match) {
  233. continue;
  234. }
  235. construct_data[p_type][i].construct(base, p_args, r_error);
  236. return;
  237. }
  238. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  239. }
  240. int Variant::get_constructor_count(Variant::Type p_type) {
  241. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, -1);
  242. return construct_data[p_type].size();
  243. }
  244. Variant::ValidatedConstructor Variant::get_validated_constructor(Variant::Type p_type, int p_constructor) {
  245. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr);
  246. ERR_FAIL_INDEX_V(p_constructor, (int)construct_data[p_type].size(), nullptr);
  247. return construct_data[p_type][p_constructor].validated_construct;
  248. }
  249. Variant::PTRConstructor Variant::get_ptr_constructor(Variant::Type p_type, int p_constructor) {
  250. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr);
  251. ERR_FAIL_INDEX_V(p_constructor, (int)construct_data[p_type].size(), nullptr);
  252. return construct_data[p_type][p_constructor].ptr_construct;
  253. }
  254. int Variant::get_constructor_argument_count(Variant::Type p_type, int p_constructor) {
  255. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, -1);
  256. ERR_FAIL_INDEX_V(p_constructor, (int)construct_data[p_type].size(), -1);
  257. return construct_data[p_type][p_constructor].argument_count;
  258. }
  259. Variant::Type Variant::get_constructor_argument_type(Variant::Type p_type, int p_constructor, int p_argument) {
  260. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, Variant::VARIANT_MAX);
  261. ERR_FAIL_INDEX_V(p_constructor, (int)construct_data[p_type].size(), Variant::VARIANT_MAX);
  262. return construct_data[p_type][p_constructor].get_argument_type(p_argument);
  263. }
  264. String Variant::get_constructor_argument_name(Variant::Type p_type, int p_constructor, int p_argument) {
  265. ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, String());
  266. ERR_FAIL_INDEX_V(p_constructor, (int)construct_data[p_type].size(), String());
  267. return construct_data[p_type][p_constructor].arg_names[p_argument];
  268. }
  269. void VariantInternal::refcounted_object_assign(Variant *v, const RefCounted *rc) {
  270. if (!rc || !const_cast<RefCounted *>(rc)->init_ref()) {
  271. v->_get_obj().obj = nullptr;
  272. v->_get_obj().id = ObjectID();
  273. return;
  274. }
  275. v->_get_obj().obj = const_cast<RefCounted *>(rc);
  276. v->_get_obj().id = rc->get_instance_id();
  277. }
  278. void VariantInternal::object_assign(Variant *v, const Object *o) {
  279. if (o) {
  280. if (o->is_ref_counted()) {
  281. RefCounted *ref_counted = const_cast<RefCounted *>(static_cast<const RefCounted *>(o));
  282. if (!ref_counted->init_ref()) {
  283. v->_get_obj().obj = nullptr;
  284. v->_get_obj().id = ObjectID();
  285. return;
  286. }
  287. }
  288. v->_get_obj().obj = const_cast<Object *>(o);
  289. v->_get_obj().id = o->get_instance_id();
  290. } else {
  291. v->_get_obj().obj = nullptr;
  292. v->_get_obj().id = ObjectID();
  293. }
  294. }
  295. void Variant::get_constructor_list(Type p_type, List<MethodInfo> *r_list) {
  296. ERR_FAIL_INDEX(p_type, Variant::VARIANT_MAX);
  297. MethodInfo mi;
  298. mi.return_val.type = p_type;
  299. mi.name = get_type_name(p_type);
  300. for (int i = 0; i < get_constructor_count(p_type); i++) {
  301. int ac = get_constructor_argument_count(p_type, i);
  302. mi.arguments.clear();
  303. for (int j = 0; j < ac; j++) {
  304. PropertyInfo arg;
  305. arg.name = get_constructor_argument_name(p_type, i, j);
  306. arg.type = get_constructor_argument_type(p_type, i, j);
  307. mi.arguments.push_back(arg);
  308. }
  309. r_list->push_back(mi);
  310. }
  311. }