variant_construct.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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/core_string_names.h"
  34. #include "core/crypto/crypto_core.h"
  35. #include "core/debugger/engine_debugger.h"
  36. #include "core/io/compression.h"
  37. #include "core/object/class_db.h"
  38. #include "core/os/os.h"
  39. #include "core/templates/local_vector.h"
  40. #include "core/templates/oa_hash_map.h"
  41. template <class T>
  42. struct PtrConstruct {};
  43. #define MAKE_PTRCONSTRUCT(m_type) \
  44. template <> \
  45. struct PtrConstruct<m_type> { \
  46. _FORCE_INLINE_ static void construct(const m_type &p_value, void *p_ptr) { \
  47. memnew_placement(p_ptr, m_type(p_value)); \
  48. } \
  49. };
  50. MAKE_PTRCONSTRUCT(bool);
  51. MAKE_PTRCONSTRUCT(int64_t);
  52. MAKE_PTRCONSTRUCT(double);
  53. MAKE_PTRCONSTRUCT(String);
  54. MAKE_PTRCONSTRUCT(Vector2);
  55. MAKE_PTRCONSTRUCT(Vector2i);
  56. MAKE_PTRCONSTRUCT(Rect2);
  57. MAKE_PTRCONSTRUCT(Rect2i);
  58. MAKE_PTRCONSTRUCT(Vector3);
  59. MAKE_PTRCONSTRUCT(Vector3i);
  60. MAKE_PTRCONSTRUCT(Vector4);
  61. MAKE_PTRCONSTRUCT(Vector4i);
  62. MAKE_PTRCONSTRUCT(Transform2D);
  63. MAKE_PTRCONSTRUCT(Plane);
  64. MAKE_PTRCONSTRUCT(Quaternion);
  65. MAKE_PTRCONSTRUCT(AABB);
  66. MAKE_PTRCONSTRUCT(Basis);
  67. MAKE_PTRCONSTRUCT(Transform3D);
  68. MAKE_PTRCONSTRUCT(Projection);
  69. MAKE_PTRCONSTRUCT(Color);
  70. MAKE_PTRCONSTRUCT(StringName);
  71. MAKE_PTRCONSTRUCT(NodePath);
  72. MAKE_PTRCONSTRUCT(RID);
  73. template <>
  74. struct PtrConstruct<Object *> {
  75. _FORCE_INLINE_ static void construct(Object *p_value, void *p_ptr) {
  76. *((Object **)p_ptr) = p_value;
  77. }
  78. };
  79. MAKE_PTRCONSTRUCT(Callable);
  80. MAKE_PTRCONSTRUCT(Signal);
  81. MAKE_PTRCONSTRUCT(Dictionary);
  82. MAKE_PTRCONSTRUCT(Array);
  83. MAKE_PTRCONSTRUCT(PackedByteArray);
  84. MAKE_PTRCONSTRUCT(PackedInt32Array);
  85. MAKE_PTRCONSTRUCT(PackedInt64Array);
  86. MAKE_PTRCONSTRUCT(PackedFloat32Array);
  87. MAKE_PTRCONSTRUCT(PackedFloat64Array);
  88. MAKE_PTRCONSTRUCT(PackedStringArray);
  89. MAKE_PTRCONSTRUCT(PackedVector2Array);
  90. MAKE_PTRCONSTRUCT(PackedVector3Array);
  91. MAKE_PTRCONSTRUCT(PackedColorArray);
  92. MAKE_PTRCONSTRUCT(Variant);
  93. template <class T, class... 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. VariantInternal::clear(&r_ret);
  139. if (p_args[0]->get_type() == Variant::NIL) {
  140. VariantInternal::object_assign_null(&r_ret);
  141. r_error.error = Callable::CallError::CALL_OK;
  142. } else if (p_args[0]->get_type() == Variant::OBJECT) {
  143. VariantInternal::object_assign(&r_ret, p_args[0]);
  144. r_error.error = Callable::CallError::CALL_OK;
  145. } else {
  146. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  147. r_error.argument = 0;
  148. r_error.expected = Variant::OBJECT;
  149. }
  150. }
  151. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  152. VariantInternal::clear(r_ret);
  153. VariantInternal::object_assign(r_ret, p_args[0]);
  154. }
  155. static void ptr_construct(void *base, const void **p_args) {
  156. PtrConstruct<Object *>::construct(PtrToArg<Object *>::convert(p_args[0]), base);
  157. }
  158. static int get_argument_count() {
  159. return 1;
  160. }
  161. static Variant::Type get_argument_type(int p_arg) {
  162. return Variant::OBJECT;
  163. }
  164. static Variant::Type get_base_type() {
  165. return Variant::OBJECT;
  166. }
  167. };
  168. class VariantConstructorNilObject {
  169. public:
  170. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  171. if (p_args[0]->get_type() != Variant::NIL) {
  172. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  173. r_error.argument = 0;
  174. r_error.expected = Variant::NIL;
  175. }
  176. VariantInternal::clear(&r_ret);
  177. VariantInternal::object_assign_null(&r_ret);
  178. }
  179. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  180. VariantInternal::clear(r_ret);
  181. VariantInternal::object_assign_null(r_ret);
  182. }
  183. static void ptr_construct(void *base, const void **p_args) {
  184. PtrConstruct<Object *>::construct(nullptr, base);
  185. }
  186. static int get_argument_count() {
  187. return 1;
  188. }
  189. static Variant::Type get_argument_type(int p_arg) {
  190. return Variant::NIL;
  191. }
  192. static Variant::Type get_base_type() {
  193. return Variant::OBJECT;
  194. }
  195. };
  196. template <class T>
  197. class VariantConstructorFromString {
  198. public:
  199. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  200. if (p_args[0]->get_type() != Variant::STRING) {
  201. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  202. r_error.argument = 0;
  203. r_error.expected = Variant::STRING;
  204. return;
  205. }
  206. VariantTypeChanger<T>::change(&r_ret);
  207. const String &src_str = *VariantGetInternalPtr<String>::get_ptr(p_args[0]);
  208. if (r_ret.get_type() == Variant::Type::INT) {
  209. r_ret = src_str.to_int();
  210. } else if (r_ret.get_type() == Variant::Type::FLOAT) {
  211. r_ret = src_str.to_float();
  212. }
  213. }
  214. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  215. VariantTypeChanger<T>::change(r_ret);
  216. const String &src_str = *VariantGetInternalPtr<String>::get_ptr(p_args[0]);
  217. T ret = Variant();
  218. if (r_ret->get_type() == Variant::Type::INT) {
  219. ret = src_str.to_int();
  220. } else if (r_ret->get_type() == Variant::Type::FLOAT) {
  221. ret = src_str.to_float();
  222. }
  223. *r_ret = ret;
  224. }
  225. static void ptr_construct(void *base, const void **p_args) {
  226. String src_str = PtrToArg<String>::convert(p_args[0]);
  227. T dst_var = Variant();
  228. Variant type_test = Variant(dst_var);
  229. if (type_test.get_type() == Variant::Type::INT) {
  230. dst_var = src_str.to_int();
  231. } else if (type_test.get_type() == Variant::Type::FLOAT) {
  232. dst_var = src_str.to_float();
  233. }
  234. PtrConstruct<T>::construct(dst_var, base);
  235. }
  236. static int get_argument_count() {
  237. return 1;
  238. }
  239. static Variant::Type get_argument_type(int p_arg) {
  240. return Variant::STRING;
  241. }
  242. static Variant::Type get_base_type() {
  243. return GetTypeInfo<T>::VARIANT_TYPE;
  244. }
  245. };
  246. class VariantConstructorCallableArgs {
  247. public:
  248. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  249. ObjectID object_id;
  250. StringName method;
  251. if (p_args[0]->get_type() == Variant::NIL) {
  252. // leave as is
  253. } else if (p_args[0]->get_type() == Variant::OBJECT) {
  254. object_id = VariantInternal::get_object_id(p_args[0]);
  255. } else {
  256. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  257. r_error.argument = 0;
  258. r_error.expected = Variant::OBJECT;
  259. return;
  260. }
  261. if (p_args[1]->get_type() == Variant::STRING_NAME) {
  262. method = *VariantGetInternalPtr<StringName>::get_ptr(p_args[1]);
  263. } else if (p_args[1]->get_type() == Variant::STRING) {
  264. method = *VariantGetInternalPtr<String>::get_ptr(p_args[1]);
  265. } else {
  266. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  267. r_error.argument = 1;
  268. r_error.expected = Variant::STRING_NAME;
  269. return;
  270. }
  271. VariantTypeChanger<Callable>::change(&r_ret);
  272. *VariantGetInternalPtr<Callable>::get_ptr(&r_ret) = Callable(object_id, method);
  273. }
  274. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  275. VariantTypeChanger<Callable>::change(r_ret);
  276. *VariantGetInternalPtr<Callable>::get_ptr(r_ret) = Callable(VariantInternal::get_object_id(p_args[0]), *VariantGetInternalPtr<StringName>::get_ptr(p_args[1]));
  277. }
  278. static void ptr_construct(void *base, const void **p_args) {
  279. PtrConstruct<Callable>::construct(Callable(PtrToArg<Object *>::convert(p_args[0]), PtrToArg<StringName>::convert(p_args[1])), base);
  280. }
  281. static int get_argument_count() {
  282. return 2;
  283. }
  284. static Variant::Type get_argument_type(int p_arg) {
  285. if (p_arg == 0) {
  286. return Variant::OBJECT;
  287. } else {
  288. return Variant::STRING_NAME;
  289. }
  290. }
  291. static Variant::Type get_base_type() {
  292. return Variant::CALLABLE;
  293. }
  294. };
  295. class VariantConstructorSignalArgs {
  296. public:
  297. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  298. ObjectID object_id;
  299. StringName method;
  300. if (p_args[0]->get_type() == Variant::NIL) {
  301. // leave as is
  302. } else if (p_args[0]->get_type() == Variant::OBJECT) {
  303. object_id = VariantInternal::get_object_id(p_args[0]);
  304. } else {
  305. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  306. r_error.argument = 0;
  307. r_error.expected = Variant::OBJECT;
  308. return;
  309. }
  310. if (p_args[1]->get_type() == Variant::STRING_NAME) {
  311. method = *VariantGetInternalPtr<StringName>::get_ptr(p_args[1]);
  312. } else if (p_args[1]->get_type() == Variant::STRING) {
  313. method = *VariantGetInternalPtr<String>::get_ptr(p_args[1]);
  314. } else {
  315. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  316. r_error.argument = 1;
  317. r_error.expected = Variant::STRING_NAME;
  318. return;
  319. }
  320. VariantTypeChanger<Signal>::change(&r_ret);
  321. *VariantGetInternalPtr<Signal>::get_ptr(&r_ret) = Signal(object_id, method);
  322. }
  323. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  324. VariantTypeChanger<Signal>::change(r_ret);
  325. *VariantGetInternalPtr<Signal>::get_ptr(r_ret) = Signal(VariantInternal::get_object_id(p_args[0]), *VariantGetInternalPtr<StringName>::get_ptr(p_args[1]));
  326. }
  327. static void ptr_construct(void *base, const void **p_args) {
  328. PtrConstruct<Signal>::construct(Signal(PtrToArg<Object *>::convert(p_args[0]), PtrToArg<StringName>::convert(p_args[1])), base);
  329. }
  330. static int get_argument_count() {
  331. return 2;
  332. }
  333. static Variant::Type get_argument_type(int p_arg) {
  334. if (p_arg == 0) {
  335. return Variant::OBJECT;
  336. } else {
  337. return Variant::STRING_NAME;
  338. }
  339. }
  340. static Variant::Type get_base_type() {
  341. return Variant::SIGNAL;
  342. }
  343. };
  344. class VariantConstructorTypedArray {
  345. public:
  346. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  347. if (p_args[0]->get_type() != Variant::ARRAY) {
  348. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  349. r_error.argument = 0;
  350. r_error.expected = Variant::ARRAY;
  351. return;
  352. }
  353. if (p_args[1]->get_type() != Variant::INT) {
  354. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  355. r_error.argument = 1;
  356. r_error.expected = Variant::INT;
  357. return;
  358. }
  359. if (p_args[2]->get_type() != Variant::STRING_NAME) {
  360. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  361. r_error.argument = 2;
  362. r_error.expected = Variant::STRING_NAME;
  363. return;
  364. }
  365. const Array &base_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
  366. const uint32_t type = p_args[1]->operator uint32_t();
  367. const StringName &class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[2]);
  368. r_ret = Array(base_arr, type, class_name, *p_args[3]);
  369. }
  370. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  371. const Array &base_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
  372. const uint32_t type = p_args[1]->operator uint32_t();
  373. const StringName &class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[2]);
  374. *r_ret = Array(base_arr, type, class_name, *p_args[3]);
  375. }
  376. static void ptr_construct(void *base, const void **p_args) {
  377. const Array &base_arr = PtrToArg<Array>::convert(p_args[0]);
  378. const uint32_t type = PtrToArg<uint32_t>::convert(p_args[1]);
  379. const StringName &class_name = PtrToArg<StringName>::convert(p_args[2]);
  380. const Variant &script = PtrToArg<Variant>::convert(p_args[3]);
  381. Array dst_arr = Array(base_arr, type, class_name, script);
  382. PtrConstruct<Array>::construct(dst_arr, base);
  383. }
  384. static int get_argument_count() {
  385. return 4;
  386. }
  387. static Variant::Type get_argument_type(int p_arg) {
  388. switch (p_arg) {
  389. case 0: {
  390. return Variant::ARRAY;
  391. } break;
  392. case 1: {
  393. return Variant::INT;
  394. } break;
  395. case 2: {
  396. return Variant::STRING_NAME;
  397. } break;
  398. case 3: {
  399. return Variant::NIL;
  400. } break;
  401. default: {
  402. return Variant::NIL;
  403. } break;
  404. }
  405. }
  406. static Variant::Type get_base_type() {
  407. return Variant::ARRAY;
  408. }
  409. };
  410. template <class T>
  411. class VariantConstructorToArray {
  412. public:
  413. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  414. if (p_args[0]->get_type() != GetTypeInfo<T>::VARIANT_TYPE) {
  415. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  416. r_error.argument = 0;
  417. r_error.expected = GetTypeInfo<T>::VARIANT_TYPE;
  418. return;
  419. }
  420. r_ret = Array();
  421. Array &dst_arr = *VariantGetInternalPtr<Array>::get_ptr(&r_ret);
  422. const T &src_arr = *VariantGetInternalPtr<T>::get_ptr(p_args[0]);
  423. int size = src_arr.size();
  424. dst_arr.resize(size);
  425. for (int i = 0; i < size; i++) {
  426. dst_arr[i] = src_arr[i];
  427. }
  428. }
  429. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  430. *r_ret = Array();
  431. Array &dst_arr = *VariantGetInternalPtr<Array>::get_ptr(r_ret);
  432. const T &src_arr = *VariantGetInternalPtr<T>::get_ptr(p_args[0]);
  433. int size = src_arr.size();
  434. dst_arr.resize(size);
  435. for (int i = 0; i < size; i++) {
  436. dst_arr[i] = src_arr[i];
  437. }
  438. }
  439. static void ptr_construct(void *base, const void **p_args) {
  440. Array dst_arr;
  441. T src_arr = PtrToArg<T>::convert(p_args[0]);
  442. int size = src_arr.size();
  443. dst_arr.resize(size);
  444. for (int i = 0; i < size; i++) {
  445. dst_arr[i] = src_arr[i];
  446. }
  447. PtrConstruct<Array>::construct(dst_arr, base);
  448. }
  449. static int get_argument_count() {
  450. return 1;
  451. }
  452. static Variant::Type get_argument_type(int p_arg) {
  453. return GetTypeInfo<T>::VARIANT_TYPE;
  454. }
  455. static Variant::Type get_base_type() {
  456. return Variant::ARRAY;
  457. }
  458. };
  459. template <class T>
  460. class VariantConstructorFromArray {
  461. public:
  462. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  463. if (p_args[0]->get_type() != Variant::ARRAY) {
  464. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  465. r_error.argument = 0;
  466. r_error.expected = Variant::ARRAY;
  467. return;
  468. }
  469. VariantTypeChanger<T>::change(&r_ret);
  470. const Array &src_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
  471. T &dst_arr = *VariantGetInternalPtr<T>::get_ptr(&r_ret);
  472. int size = src_arr.size();
  473. dst_arr.resize(size);
  474. for (int i = 0; i < size; i++) {
  475. dst_arr.write[i] = src_arr[i];
  476. }
  477. }
  478. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  479. VariantTypeChanger<T>::change(r_ret);
  480. const Array &src_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
  481. T &dst_arr = *VariantGetInternalPtr<T>::get_ptr(r_ret);
  482. int size = src_arr.size();
  483. dst_arr.resize(size);
  484. for (int i = 0; i < size; i++) {
  485. dst_arr.write[i] = src_arr[i];
  486. }
  487. }
  488. static void ptr_construct(void *base, const void **p_args) {
  489. Array src_arr = PtrToArg<Array>::convert(p_args[0]);
  490. T dst_arr;
  491. int size = src_arr.size();
  492. dst_arr.resize(size);
  493. for (int i = 0; i < size; i++) {
  494. dst_arr.write[i] = src_arr[i];
  495. }
  496. PtrConstruct<T>::construct(dst_arr, base);
  497. }
  498. static int get_argument_count() {
  499. return 1;
  500. }
  501. static Variant::Type get_argument_type(int p_arg) {
  502. return Variant::ARRAY;
  503. }
  504. static Variant::Type get_base_type() {
  505. return GetTypeInfo<T>::VARIANT_TYPE;
  506. }
  507. };
  508. class VariantConstructorNil {
  509. public:
  510. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  511. if (p_args[0]->get_type() != Variant::NIL) {
  512. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  513. r_error.argument = 0;
  514. r_error.expected = Variant::NIL;
  515. return;
  516. }
  517. r_error.error = Callable::CallError::CALL_OK;
  518. VariantInternal::clear(&r_ret);
  519. }
  520. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  521. VariantInternal::clear(r_ret);
  522. }
  523. static void ptr_construct(void *base, const void **p_args) {
  524. PtrConstruct<Variant>::construct(Variant(), base);
  525. }
  526. static int get_argument_count() {
  527. return 1;
  528. }
  529. static Variant::Type get_argument_type(int p_arg) {
  530. return Variant::NIL;
  531. }
  532. static Variant::Type get_base_type() {
  533. return Variant::NIL;
  534. }
  535. };
  536. template <class T>
  537. class VariantConstructNoArgs {
  538. public:
  539. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  540. VariantTypeChanger<T>::change_and_reset(&r_ret);
  541. r_error.error = Callable::CallError::CALL_OK;
  542. }
  543. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  544. VariantTypeChanger<T>::change_and_reset(r_ret);
  545. }
  546. static void ptr_construct(void *base, const void **p_args) {
  547. PtrConstruct<T>::construct(T(), base);
  548. }
  549. static int get_argument_count() {
  550. return 0;
  551. }
  552. static Variant::Type get_argument_type(int p_arg) {
  553. return Variant::NIL;
  554. }
  555. static Variant::Type get_base_type() {
  556. return GetTypeInfo<T>::VARIANT_TYPE;
  557. }
  558. };
  559. class VariantConstructNoArgsNil {
  560. public:
  561. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  562. VariantInternal::clear(&r_ret);
  563. r_error.error = Callable::CallError::CALL_OK;
  564. }
  565. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  566. VariantInternal::clear(r_ret);
  567. }
  568. static void ptr_construct(void *base, const void **p_args) {
  569. ERR_FAIL_MSG("can't ptrcall nil constructor");
  570. }
  571. static int get_argument_count() {
  572. return 0;
  573. }
  574. static Variant::Type get_argument_type(int p_arg) {
  575. return Variant::NIL;
  576. }
  577. static Variant::Type get_base_type() {
  578. return Variant::NIL;
  579. }
  580. };
  581. class VariantConstructNoArgsObject {
  582. public:
  583. static void construct(Variant &r_ret, const Variant **p_args, Callable::CallError &r_error) {
  584. r_ret = (Object *)nullptr; // Must construct a TYPE_OBJECT containing nullptr.
  585. r_error.error = Callable::CallError::CALL_OK;
  586. }
  587. static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
  588. *r_ret = (Object *)nullptr; // Must construct a TYPE_OBJECT containing nullptr.
  589. }
  590. static void ptr_construct(void *base, const void **p_args) {
  591. PtrConstruct<Object *>::construct(nullptr, base);
  592. }
  593. static int get_argument_count() {
  594. return 0;
  595. }
  596. static Variant::Type get_argument_type(int p_arg) {
  597. return Variant::NIL;
  598. }
  599. static Variant::Type get_base_type() {
  600. return Variant::OBJECT;
  601. }
  602. };
  603. #endif // VARIANT_CONSTRUCT_H