variant_construct.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /**************************************************************************/
  2. /* variant_construct.h */
  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. #ifndef VARIANT_CONSTRUCT_H
  31. #define VARIANT_CONSTRUCT_H
  32. #include "variant.h"
  33. #include "core/crypto/crypto_core.h"
  34. #include "core/debugger/engine_debugger.h"
  35. #include "core/io/compression.h"
  36. #include "core/object/class_db.h"
  37. #include "core/os/os.h"
  38. #include "core/templates/local_vector.h"
  39. #include "core/templates/oa_hash_map.h"
  40. template <typename T>
  41. struct PtrConstruct {};
  42. #define MAKE_PTRCONSTRUCT(m_type) \
  43. template <> \
  44. struct PtrConstruct<m_type> { \
  45. _FORCE_INLINE_ static void construct(const m_type &p_value, void *p_ptr) { \
  46. memnew_placement(p_ptr, m_type(p_value)); \
  47. } \
  48. };
  49. MAKE_PTRCONSTRUCT(bool);
  50. MAKE_PTRCONSTRUCT(int64_t);
  51. MAKE_PTRCONSTRUCT(double);
  52. MAKE_PTRCONSTRUCT(String);
  53. MAKE_PTRCONSTRUCT(Vector2);
  54. MAKE_PTRCONSTRUCT(Vector2i);
  55. MAKE_PTRCONSTRUCT(Rect2);
  56. MAKE_PTRCONSTRUCT(Rect2i);
  57. MAKE_PTRCONSTRUCT(Vector3);
  58. MAKE_PTRCONSTRUCT(Vector3i);
  59. MAKE_PTRCONSTRUCT(Vector4);
  60. MAKE_PTRCONSTRUCT(Vector4i);
  61. MAKE_PTRCONSTRUCT(Transform2D);
  62. MAKE_PTRCONSTRUCT(Plane);
  63. MAKE_PTRCONSTRUCT(Quaternion);
  64. MAKE_PTRCONSTRUCT(AABB);
  65. MAKE_PTRCONSTRUCT(Basis);
  66. MAKE_PTRCONSTRUCT(Transform3D);
  67. MAKE_PTRCONSTRUCT(Projection);
  68. MAKE_PTRCONSTRUCT(Color);
  69. MAKE_PTRCONSTRUCT(StringName);
  70. MAKE_PTRCONSTRUCT(NodePath);
  71. MAKE_PTRCONSTRUCT(RID);
  72. template <>
  73. struct PtrConstruct<Object *> {
  74. _FORCE_INLINE_ static void construct(Object *p_value, void *p_ptr) {
  75. *((Object **)p_ptr) = p_value;
  76. }
  77. };
  78. MAKE_PTRCONSTRUCT(Callable);
  79. MAKE_PTRCONSTRUCT(Signal);
  80. MAKE_PTRCONSTRUCT(Dictionary);
  81. MAKE_PTRCONSTRUCT(Array);
  82. MAKE_PTRCONSTRUCT(PackedByteArray);
  83. MAKE_PTRCONSTRUCT(PackedInt32Array);
  84. MAKE_PTRCONSTRUCT(PackedInt64Array);
  85. MAKE_PTRCONSTRUCT(PackedFloat32Array);
  86. MAKE_PTRCONSTRUCT(PackedFloat64Array);
  87. MAKE_PTRCONSTRUCT(PackedStringArray);
  88. MAKE_PTRCONSTRUCT(PackedVector2Array);
  89. MAKE_PTRCONSTRUCT(PackedVector3Array);
  90. MAKE_PTRCONSTRUCT(PackedColorArray);
  91. MAKE_PTRCONSTRUCT(PackedVector4Array);
  92. MAKE_PTRCONSTRUCT(Variant);
  93. template <typename T, typename... P>
  94. class VariantConstructor {
  95. template <size_t... Is>
  96. static _FORCE_INLINE_ void construct_helper(T &base, const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) {
  97. r_error.error = Callable::CallError::CALL_OK;
  98. #ifdef DEBUG_METHODS_ENABLED
  99. base = T(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  100. #else
  101. base = T(VariantCaster<P>::cast(*p_args[Is])...);
  102. #endif
  103. }
  104. template <size_t... Is>
  105. static _FORCE_INLINE_ void validated_construct_helper(T &base, const Variant **p_args, IndexSequence<Is...>) {
  106. base = T((*VariantGetInternalPtr<P>::get_ptr(p_args[Is]))...);
  107. }
  108. template <size_t... Is>
  109. static _FORCE_INLINE_ void ptr_construct_helper(void *base, const void **p_args, IndexSequence<Is...>) {
  110. PtrConstruct<T>::construct(T(PtrToArg<P>::convert(p_args[Is])...), base);
  111. }
  112. public:
  113. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  114. r_error.error = Callable::CallError::CALL_OK;
  115. VariantTypeChanger<T>::change(&r_ret);
  116. construct_helper(*VariantGetInternalPtr<T>::get_ptr(&r_ret), p_args, r_error, BuildIndexSequence<sizeof...(P)>{});
  117. }
  118. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  119. VariantTypeChanger<T>::change(r_ret);
  120. validated_construct_helper(*VariantGetInternalPtr<T>::get_ptr(r_ret), p_args, BuildIndexSequence<sizeof...(P)>{});
  121. }
  122. static void ptr_construct(void *base, const void **p_args) {
  123. ptr_construct_helper(base, p_args, BuildIndexSequence<sizeof...(P)>{});
  124. }
  125. static int get_argument_count() {
  126. return sizeof...(P);
  127. }
  128. static Variant::Type get_argument_type(int p_arg) {
  129. return call_get_argument_type<P...>(p_arg);
  130. }
  131. static Variant::Type get_base_type() {
  132. return GetTypeInfo<T>::VARIANT_TYPE;
  133. }
  134. };
  135. class VariantConstructorObject {
  136. public:
  137. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  138. if (p_args[0]->get_type() == Variant::NIL) {
  139. VariantInternal::clear(&r_ret);
  140. VariantTypeChanger<Object *>::change(&r_ret);
  141. VariantInternal::object_reset_data(&r_ret);
  142. r_error.error = Callable::CallError::CALL_OK;
  143. } else if (p_args[0]->get_type() == Variant::OBJECT) {
  144. VariantTypeChanger<Object *>::change(&r_ret);
  145. VariantInternal::object_assign(&r_ret, p_args[0]);
  146. r_error.error = Callable::CallError::CALL_OK;
  147. } else {
  148. VariantInternal::clear(&r_ret);
  149. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  150. r_error.argument = 0;
  151. r_error.expected = Variant::OBJECT;
  152. }
  153. }
  154. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  155. VariantTypeChanger<Object *>::change(r_ret);
  156. VariantInternal::object_assign(r_ret, p_args[0]);
  157. }
  158. static void ptr_construct(void *base, const void **p_args) {
  159. PtrConstruct<Object *>::construct(PtrToArg<Object *>::convert(p_args[0]), base);
  160. }
  161. static int get_argument_count() {
  162. return 1;
  163. }
  164. static Variant::Type get_argument_type(int p_arg) {
  165. return Variant::OBJECT;
  166. }
  167. static Variant::Type get_base_type() {
  168. return Variant::OBJECT;
  169. }
  170. };
  171. class VariantConstructorNilObject {
  172. public:
  173. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  174. if (p_args[0]->get_type() != Variant::NIL) {
  175. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  176. r_error.argument = 0;
  177. r_error.expected = Variant::NIL;
  178. }
  179. VariantInternal::clear(&r_ret);
  180. VariantTypeChanger<Object *>::change(&r_ret);
  181. VariantInternal::object_reset_data(&r_ret);
  182. }
  183. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  184. VariantInternal::clear(r_ret);
  185. VariantTypeChanger<Object *>::change(r_ret);
  186. VariantInternal::object_reset_data(r_ret);
  187. }
  188. static void ptr_construct(void *base, const void **p_args) {
  189. PtrConstruct<Object *>::construct(nullptr, base);
  190. }
  191. static int get_argument_count() {
  192. return 1;
  193. }
  194. static Variant::Type get_argument_type(int p_arg) {
  195. return Variant::NIL;
  196. }
  197. static Variant::Type get_base_type() {
  198. return Variant::OBJECT;
  199. }
  200. };
  201. template <typename T>
  202. class VariantConstructorFromString {
  203. public:
  204. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  205. if (!p_args[0]->is_string()) {
  206. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  207. r_error.argument = 0;
  208. r_error.expected = Variant::STRING;
  209. return;
  210. }
  211. VariantTypeChanger<T>::change(&r_ret);
  212. const String src_str = *p_args[0];
  213. if (r_ret.get_type() == Variant::Type::INT) {
  214. r_ret = src_str.to_int();
  215. } else if (r_ret.get_type() == Variant::Type::FLOAT) {
  216. r_ret = src_str.to_float();
  217. }
  218. }
  219. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  220. VariantTypeChanger<T>::change(r_ret);
  221. const String &src_str = *VariantGetInternalPtr<String>::get_ptr(p_args[0]);
  222. T ret = Variant();
  223. if (r_ret->get_type() == Variant::Type::INT) {
  224. ret = src_str.to_int();
  225. } else if (r_ret->get_type() == Variant::Type::FLOAT) {
  226. ret = src_str.to_float();
  227. }
  228. *r_ret = ret;
  229. }
  230. static void ptr_construct(void *base, const void **p_args) {
  231. String src_str = PtrToArg<String>::convert(p_args[0]);
  232. T dst_var = Variant();
  233. Variant type_test = Variant(dst_var);
  234. if (type_test.get_type() == Variant::Type::INT) {
  235. dst_var = src_str.to_int();
  236. } else if (type_test.get_type() == Variant::Type::FLOAT) {
  237. dst_var = src_str.to_float();
  238. }
  239. PtrConstruct<T>::construct(dst_var, base);
  240. }
  241. static int get_argument_count() {
  242. return 1;
  243. }
  244. static Variant::Type get_argument_type(int p_arg) {
  245. return Variant::STRING;
  246. }
  247. static Variant::Type get_base_type() {
  248. return GetTypeInfo<T>::VARIANT_TYPE;
  249. }
  250. };
  251. class VariantConstructorCallableArgs {
  252. public:
  253. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  254. ObjectID object_id;
  255. StringName method;
  256. if (p_args[0]->get_type() == Variant::NIL) {
  257. // leave as is
  258. } else if (p_args[0]->get_type() == Variant::OBJECT) {
  259. object_id = VariantInternal::get_object_id(p_args[0]);
  260. } else {
  261. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  262. r_error.argument = 0;
  263. r_error.expected = Variant::OBJECT;
  264. return;
  265. }
  266. if (p_args[1]->get_type() == Variant::STRING_NAME) {
  267. method = *VariantGetInternalPtr<StringName>::get_ptr(p_args[1]);
  268. } else if (p_args[1]->get_type() == Variant::STRING) {
  269. method = *VariantGetInternalPtr<String>::get_ptr(p_args[1]);
  270. } else {
  271. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  272. r_error.argument = 1;
  273. r_error.expected = Variant::STRING_NAME;
  274. return;
  275. }
  276. VariantTypeChanger<Callable>::change(&r_ret);
  277. *VariantGetInternalPtr<Callable>::get_ptr(&r_ret) = Callable(object_id, method);
  278. }
  279. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  280. VariantTypeChanger<Callable>::change(r_ret);
  281. *VariantGetInternalPtr<Callable>::get_ptr(r_ret) = Callable(VariantInternal::get_object_id(p_args[0]), *VariantGetInternalPtr<StringName>::get_ptr(p_args[1]));
  282. }
  283. static void ptr_construct(void *base, const void **p_args) {
  284. PtrConstruct<Callable>::construct(Callable(PtrToArg<Object *>::convert(p_args[0]), PtrToArg<StringName>::convert(p_args[1])), base);
  285. }
  286. static int get_argument_count() {
  287. return 2;
  288. }
  289. static Variant::Type get_argument_type(int p_arg) {
  290. if (p_arg == 0) {
  291. return Variant::OBJECT;
  292. } else {
  293. return Variant::STRING_NAME;
  294. }
  295. }
  296. static Variant::Type get_base_type() {
  297. return Variant::CALLABLE;
  298. }
  299. };
  300. class VariantConstructorSignalArgs {
  301. public:
  302. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  303. ObjectID object_id;
  304. StringName method;
  305. if (p_args[0]->get_type() == Variant::NIL) {
  306. // leave as is
  307. } else if (p_args[0]->get_type() == Variant::OBJECT) {
  308. object_id = VariantInternal::get_object_id(p_args[0]);
  309. } else {
  310. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  311. r_error.argument = 0;
  312. r_error.expected = Variant::OBJECT;
  313. return;
  314. }
  315. if (p_args[1]->get_type() == Variant::STRING_NAME) {
  316. method = *VariantGetInternalPtr<StringName>::get_ptr(p_args[1]);
  317. } else if (p_args[1]->get_type() == Variant::STRING) {
  318. method = *VariantGetInternalPtr<String>::get_ptr(p_args[1]);
  319. } else {
  320. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  321. r_error.argument = 1;
  322. r_error.expected = Variant::STRING_NAME;
  323. return;
  324. }
  325. VariantTypeChanger<Signal>::change(&r_ret);
  326. *VariantGetInternalPtr<Signal>::get_ptr(&r_ret) = Signal(object_id, method);
  327. }
  328. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  329. VariantTypeChanger<Signal>::change(r_ret);
  330. *VariantGetInternalPtr<Signal>::get_ptr(r_ret) = Signal(VariantInternal::get_object_id(p_args[0]), *VariantGetInternalPtr<StringName>::get_ptr(p_args[1]));
  331. }
  332. static void ptr_construct(void *base, const void **p_args) {
  333. PtrConstruct<Signal>::construct(Signal(PtrToArg<Object *>::convert(p_args[0]), PtrToArg<StringName>::convert(p_args[1])), base);
  334. }
  335. static int get_argument_count() {
  336. return 2;
  337. }
  338. static Variant::Type get_argument_type(int p_arg) {
  339. if (p_arg == 0) {
  340. return Variant::OBJECT;
  341. } else {
  342. return Variant::STRING_NAME;
  343. }
  344. }
  345. static Variant::Type get_base_type() {
  346. return Variant::SIGNAL;
  347. }
  348. };
  349. class VariantConstructorTypedDictionary {
  350. public:
  351. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  352. if (p_args[0]->get_type() != Variant::DICTIONARY) {
  353. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  354. r_error.argument = 0;
  355. r_error.expected = Variant::DICTIONARY;
  356. return;
  357. }
  358. if (p_args[1]->get_type() != Variant::INT) {
  359. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  360. r_error.argument = 1;
  361. r_error.expected = Variant::INT;
  362. return;
  363. }
  364. if (p_args[2]->get_type() != Variant::STRING_NAME) {
  365. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  366. r_error.argument = 2;
  367. r_error.expected = Variant::STRING_NAME;
  368. return;
  369. }
  370. if (p_args[4]->get_type() != Variant::INT) {
  371. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  372. r_error.argument = 4;
  373. r_error.expected = Variant::INT;
  374. return;
  375. }
  376. if (p_args[5]->get_type() != Variant::STRING_NAME) {
  377. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  378. r_error.argument = 5;
  379. r_error.expected = Variant::STRING_NAME;
  380. return;
  381. }
  382. const Dictionary &base_dict = *VariantGetInternalPtr<Dictionary>::get_ptr(p_args[0]);
  383. const uint32_t key_type = p_args[1]->operator uint32_t();
  384. const StringName &key_class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[2]);
  385. const uint32_t value_type = p_args[4]->operator uint32_t();
  386. const StringName &value_class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[5]);
  387. r_ret = Dictionary(base_dict, key_type, key_class_name, *p_args[3], value_type, value_class_name, *p_args[6]);
  388. }
  389. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  390. const Dictionary &base_dict = *VariantGetInternalPtr<Dictionary>::get_ptr(p_args[0]);
  391. const uint32_t key_type = p_args[1]->operator uint32_t();
  392. const StringName &key_class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[2]);
  393. const uint32_t value_type = p_args[4]->operator uint32_t();
  394. const StringName &value_class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[5]);
  395. *r_ret = Dictionary(base_dict, key_type, key_class_name, *p_args[3], value_type, value_class_name, *p_args[6]);
  396. }
  397. static void ptr_construct(void *base, const void **p_args) {
  398. const Dictionary &base_dict = PtrToArg<Dictionary>::convert(p_args[0]);
  399. const uint32_t key_type = PtrToArg<uint32_t>::convert(p_args[1]);
  400. const StringName &key_class_name = PtrToArg<StringName>::convert(p_args[2]);
  401. const Variant &key_script = PtrToArg<Variant>::convert(p_args[3]);
  402. const uint32_t value_type = PtrToArg<uint32_t>::convert(p_args[4]);
  403. const StringName &value_class_name = PtrToArg<StringName>::convert(p_args[5]);
  404. const Variant &value_script = PtrToArg<Variant>::convert(p_args[6]);
  405. Dictionary dst_arr = Dictionary(base_dict, key_type, key_class_name, key_script, value_type, value_class_name, value_script);
  406. PtrConstruct<Dictionary>::construct(dst_arr, base);
  407. }
  408. static int get_argument_count() {
  409. return 7;
  410. }
  411. static Variant::Type get_argument_type(int p_arg) {
  412. switch (p_arg) {
  413. case 0: {
  414. return Variant::DICTIONARY;
  415. } break;
  416. case 1: {
  417. return Variant::INT;
  418. } break;
  419. case 2: {
  420. return Variant::STRING_NAME;
  421. } break;
  422. case 3: {
  423. return Variant::NIL;
  424. } break;
  425. case 4: {
  426. return Variant::INT;
  427. } break;
  428. case 5: {
  429. return Variant::STRING_NAME;
  430. } break;
  431. case 6: {
  432. return Variant::NIL;
  433. } break;
  434. default: {
  435. return Variant::NIL;
  436. } break;
  437. }
  438. }
  439. static Variant::Type get_base_type() {
  440. return Variant::DICTIONARY;
  441. }
  442. };
  443. class VariantConstructorTypedArray {
  444. public:
  445. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  446. if (p_args[0]->get_type() != Variant::ARRAY) {
  447. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  448. r_error.argument = 0;
  449. r_error.expected = Variant::ARRAY;
  450. return;
  451. }
  452. if (p_args[1]->get_type() != Variant::INT) {
  453. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  454. r_error.argument = 1;
  455. r_error.expected = Variant::INT;
  456. return;
  457. }
  458. if (!p_args[2]->is_string()) {
  459. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  460. r_error.argument = 2;
  461. r_error.expected = Variant::STRING_NAME;
  462. return;
  463. }
  464. const Array &base_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
  465. const uint32_t type = p_args[1]->operator uint32_t();
  466. r_ret = Array(base_arr, type, *p_args[2], *p_args[3]);
  467. }
  468. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  469. const Array &base_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
  470. const uint32_t type = p_args[1]->operator uint32_t();
  471. const StringName &class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[2]);
  472. *r_ret = Array(base_arr, type, class_name, *p_args[3]);
  473. }
  474. static void ptr_construct(void *base, const void **p_args) {
  475. const Array &base_arr = PtrToArg<Array>::convert(p_args[0]);
  476. const uint32_t type = PtrToArg<uint32_t>::convert(p_args[1]);
  477. const StringName &class_name = PtrToArg<StringName>::convert(p_args[2]);
  478. const Variant &script = PtrToArg<Variant>::convert(p_args[3]);
  479. Array dst_arr = Array(base_arr, type, class_name, script);
  480. PtrConstruct<Array>::construct(dst_arr, base);
  481. }
  482. static int get_argument_count() {
  483. return 4;
  484. }
  485. static Variant::Type get_argument_type(int p_arg) {
  486. switch (p_arg) {
  487. case 0: {
  488. return Variant::ARRAY;
  489. } break;
  490. case 1: {
  491. return Variant::INT;
  492. } break;
  493. case 2: {
  494. return Variant::STRING_NAME;
  495. } break;
  496. case 3: {
  497. return Variant::NIL;
  498. } break;
  499. default: {
  500. return Variant::NIL;
  501. } break;
  502. }
  503. }
  504. static Variant::Type get_base_type() {
  505. return Variant::ARRAY;
  506. }
  507. };
  508. template <typename T>
  509. class VariantConstructorToArray {
  510. public:
  511. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  512. if (p_args[0]->get_type() != GetTypeInfo<T>::VARIANT_TYPE) {
  513. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  514. r_error.argument = 0;
  515. r_error.expected = GetTypeInfo<T>::VARIANT_TYPE;
  516. return;
  517. }
  518. r_ret = Array();
  519. Array &dst_arr = *VariantGetInternalPtr<Array>::get_ptr(&r_ret);
  520. const T &src_arr = *VariantGetInternalPtr<T>::get_ptr(p_args[0]);
  521. int size = src_arr.size();
  522. dst_arr.resize(size);
  523. for (int i = 0; i < size; i++) {
  524. dst_arr[i] = src_arr[i];
  525. }
  526. }
  527. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  528. *r_ret = Array();
  529. Array &dst_arr = *VariantGetInternalPtr<Array>::get_ptr(r_ret);
  530. const T &src_arr = *VariantGetInternalPtr<T>::get_ptr(p_args[0]);
  531. int size = src_arr.size();
  532. dst_arr.resize(size);
  533. for (int i = 0; i < size; i++) {
  534. dst_arr[i] = src_arr[i];
  535. }
  536. }
  537. static void ptr_construct(void *base, const void **p_args) {
  538. Array dst_arr;
  539. T src_arr = PtrToArg<T>::convert(p_args[0]);
  540. int size = src_arr.size();
  541. dst_arr.resize(size);
  542. for (int i = 0; i < size; i++) {
  543. dst_arr[i] = src_arr[i];
  544. }
  545. PtrConstruct<Array>::construct(dst_arr, base);
  546. }
  547. static int get_argument_count() {
  548. return 1;
  549. }
  550. static Variant::Type get_argument_type(int p_arg) {
  551. return GetTypeInfo<T>::VARIANT_TYPE;
  552. }
  553. static Variant::Type get_base_type() {
  554. return Variant::ARRAY;
  555. }
  556. };
  557. template <typename T>
  558. class VariantConstructorFromArray {
  559. public:
  560. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  561. if (p_args[0]->get_type() != Variant::ARRAY) {
  562. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  563. r_error.argument = 0;
  564. r_error.expected = Variant::ARRAY;
  565. return;
  566. }
  567. VariantTypeChanger<T>::change(&r_ret);
  568. const Array &src_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
  569. T &dst_arr = *VariantGetInternalPtr<T>::get_ptr(&r_ret);
  570. int size = src_arr.size();
  571. dst_arr.resize(size);
  572. for (int i = 0; i < size; i++) {
  573. dst_arr.write[i] = src_arr[i];
  574. }
  575. }
  576. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  577. VariantTypeChanger<T>::change(r_ret);
  578. const Array &src_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
  579. T &dst_arr = *VariantGetInternalPtr<T>::get_ptr(r_ret);
  580. int size = src_arr.size();
  581. dst_arr.resize(size);
  582. for (int i = 0; i < size; i++) {
  583. dst_arr.write[i] = src_arr[i];
  584. }
  585. }
  586. static void ptr_construct(void *base, const void **p_args) {
  587. Array src_arr = PtrToArg<Array>::convert(p_args[0]);
  588. T dst_arr;
  589. int size = src_arr.size();
  590. dst_arr.resize(size);
  591. for (int i = 0; i < size; i++) {
  592. dst_arr.write[i] = src_arr[i];
  593. }
  594. PtrConstruct<T>::construct(dst_arr, base);
  595. }
  596. static int get_argument_count() {
  597. return 1;
  598. }
  599. static Variant::Type get_argument_type(int p_arg) {
  600. return Variant::ARRAY;
  601. }
  602. static Variant::Type get_base_type() {
  603. return GetTypeInfo<T>::VARIANT_TYPE;
  604. }
  605. };
  606. class VariantConstructorNil {
  607. public:
  608. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  609. if (p_args[0]->get_type() != Variant::NIL) {
  610. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  611. r_error.argument = 0;
  612. r_error.expected = Variant::NIL;
  613. return;
  614. }
  615. r_error.error = Callable::CallError::CALL_OK;
  616. VariantInternal::clear(&r_ret);
  617. }
  618. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  619. VariantInternal::clear(r_ret);
  620. }
  621. static void ptr_construct(void *base, const void **p_args) {
  622. PtrConstruct<Variant>::construct(Variant(), base);
  623. }
  624. static int get_argument_count() {
  625. return 1;
  626. }
  627. static Variant::Type get_argument_type(int p_arg) {
  628. return Variant::NIL;
  629. }
  630. static Variant::Type get_base_type() {
  631. return Variant::NIL;
  632. }
  633. };
  634. template <typename T>
  635. class VariantConstructNoArgs {
  636. public:
  637. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  638. VariantTypeChanger<T>::change_and_reset(&r_ret);
  639. r_error.error = Callable::CallError::CALL_OK;
  640. }
  641. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  642. VariantTypeChanger<T>::change_and_reset(r_ret);
  643. }
  644. static void ptr_construct(void *base, const void **p_args) {
  645. PtrConstruct<T>::construct(T(), base);
  646. }
  647. static int get_argument_count() {
  648. return 0;
  649. }
  650. static Variant::Type get_argument_type(int p_arg) {
  651. return Variant::NIL;
  652. }
  653. static Variant::Type get_base_type() {
  654. return GetTypeInfo<T>::VARIANT_TYPE;
  655. }
  656. };
  657. class VariantConstructNoArgsNil {
  658. public:
  659. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  660. VariantInternal::clear(&r_ret);
  661. r_error.error = Callable::CallError::CALL_OK;
  662. }
  663. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  664. VariantInternal::clear(r_ret);
  665. }
  666. static void ptr_construct(void *base, const void **p_args) {
  667. ERR_FAIL_MSG("Cannot ptrcall nil constructor");
  668. }
  669. static int get_argument_count() {
  670. return 0;
  671. }
  672. static Variant::Type get_argument_type(int p_arg) {
  673. return Variant::NIL;
  674. }
  675. static Variant::Type get_base_type() {
  676. return Variant::NIL;
  677. }
  678. };
  679. class VariantConstructNoArgsObject {
  680. public:
  681. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  682. r_ret = (Object *)nullptr; // Must construct a TYPE_OBJECT containing nullptr.
  683. r_error.error = Callable::CallError::CALL_OK;
  684. }
  685. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  686. *r_ret = (Object *)nullptr; // Must construct a TYPE_OBJECT containing nullptr.
  687. }
  688. static void ptr_construct(void *base, const void **p_args) {
  689. PtrConstruct<Object *>::construct(nullptr, base);
  690. }
  691. static int get_argument_count() {
  692. return 0;
  693. }
  694. static Variant::Type get_argument_type(int p_arg) {
  695. return Variant::NIL;
  696. }
  697. static Variant::Type get_base_type() {
  698. return Variant::OBJECT;
  699. }
  700. };
  701. #endif // VARIANT_CONSTRUCT_H