variant.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*************************************************************************/
  2. /* variant.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 <godot_cpp/variant/variant.hpp>
  31. #include <godot_cpp/godot.hpp>
  32. #include <godot_cpp/core/binder_common.hpp>
  33. #include <godot_cpp/core/defs.hpp>
  34. #include <utility>
  35. namespace godot {
  36. GDNativeVariantFromTypeConstructorFunc Variant::from_type_constructor[Variant::VARIANT_MAX]{};
  37. GDNativeTypeFromVariantConstructorFunc Variant::to_type_constructor[Variant::VARIANT_MAX]{};
  38. void Variant::init_bindings() {
  39. // Start from 1 to skip NIL.
  40. for (int i = 1; i < VARIANT_MAX; i++) {
  41. from_type_constructor[i] = internal::gdn_interface->get_variant_from_type_constructor((GDNativeVariantType)i);
  42. to_type_constructor[i] = internal::gdn_interface->get_variant_to_type_constructor((GDNativeVariantType)i);
  43. }
  44. StringName::init_bindings();
  45. String::init_bindings();
  46. NodePath::init_bindings();
  47. RID::init_bindings();
  48. Callable::init_bindings();
  49. Signal::init_bindings();
  50. Dictionary::init_bindings();
  51. Array::init_bindings();
  52. PackedByteArray::init_bindings();
  53. PackedInt32Array::init_bindings();
  54. PackedInt64Array::init_bindings();
  55. PackedFloat32Array::init_bindings();
  56. PackedFloat64Array::init_bindings();
  57. PackedStringArray::init_bindings();
  58. PackedVector2Array::init_bindings();
  59. PackedVector3Array::init_bindings();
  60. PackedColorArray::init_bindings();
  61. }
  62. Variant::Variant() {
  63. internal::gdn_interface->variant_new_nil(_native_ptr());
  64. }
  65. Variant::Variant(const GDNativeVariantPtr native_ptr) {
  66. internal::gdn_interface->variant_new_copy(_native_ptr(), native_ptr);
  67. }
  68. Variant::Variant(const Variant &other) {
  69. internal::gdn_interface->variant_new_copy(_native_ptr(), other._native_ptr());
  70. }
  71. Variant::Variant(Variant &&other) {
  72. std::swap(opaque, other.opaque);
  73. }
  74. Variant::Variant(bool v) {
  75. GDNativeBool encoded;
  76. PtrToArg<bool>::encode(v, &encoded);
  77. from_type_constructor[BOOL](_native_ptr(), &encoded);
  78. }
  79. Variant::Variant(int64_t v) {
  80. GDNativeInt encoded;
  81. PtrToArg<int64_t>::encode(v, &encoded);
  82. from_type_constructor[INT](_native_ptr(), &encoded);
  83. }
  84. Variant::Variant(double v) {
  85. double encoded;
  86. PtrToArg<double>::encode(v, &encoded);
  87. from_type_constructor[FLOAT](_native_ptr(), &encoded);
  88. }
  89. Variant::Variant(const String &v) {
  90. from_type_constructor[STRING](_native_ptr(), v._native_ptr());
  91. }
  92. Variant::Variant(const Vector2 &v) {
  93. from_type_constructor[VECTOR2](_native_ptr(), (GDNativeTypePtr)&v);
  94. }
  95. Variant::Variant(const Vector2i &v) {
  96. from_type_constructor[VECTOR2I](_native_ptr(), (GDNativeTypePtr)&v);
  97. }
  98. Variant::Variant(const Rect2 &v) {
  99. from_type_constructor[RECT2](_native_ptr(), (GDNativeTypePtr)&v);
  100. }
  101. Variant::Variant(const Rect2i &v) {
  102. from_type_constructor[RECT2I](_native_ptr(), (GDNativeTypePtr)&v);
  103. }
  104. Variant::Variant(const Vector3 &v) {
  105. from_type_constructor[VECTOR3](_native_ptr(), (GDNativeTypePtr)&v);
  106. }
  107. Variant::Variant(const Vector3i &v) {
  108. from_type_constructor[VECTOR3I](_native_ptr(), (GDNativeTypePtr)&v);
  109. }
  110. Variant::Variant(const Transform2D &v) {
  111. from_type_constructor[TRANSFORM2D](_native_ptr(), (GDNativeTypePtr)&v);
  112. }
  113. Variant::Variant(const Vector4 &v) {
  114. from_type_constructor[VECTOR4](_native_ptr(), (GDNativeTypePtr)&v);
  115. }
  116. Variant::Variant(const Vector4i &v) {
  117. from_type_constructor[VECTOR4I](_native_ptr(), (GDNativeTypePtr)&v);
  118. }
  119. Variant::Variant(const Plane &v) {
  120. from_type_constructor[PLANE](_native_ptr(), (GDNativeTypePtr)&v);
  121. }
  122. Variant::Variant(const Quaternion &v) {
  123. from_type_constructor[QUATERNION](_native_ptr(), (GDNativeTypePtr)&v);
  124. }
  125. Variant::Variant(const godot::AABB &v) {
  126. from_type_constructor[AABB](_native_ptr(), (GDNativeTypePtr)&v);
  127. }
  128. Variant::Variant(const Basis &v) {
  129. from_type_constructor[BASIS](_native_ptr(), (GDNativeTypePtr)&v);
  130. }
  131. Variant::Variant(const Transform3D &v) {
  132. from_type_constructor[TRANSFORM3D](_native_ptr(), (GDNativeTypePtr)&v);
  133. }
  134. Variant::Variant(const Projection &v) {
  135. from_type_constructor[PROJECTION](_native_ptr(), (GDNativeTypePtr)&v);
  136. }
  137. Variant::Variant(const Color &v) {
  138. from_type_constructor[COLOR](_native_ptr(), (GDNativeTypePtr)&v);
  139. }
  140. Variant::Variant(const StringName &v) {
  141. from_type_constructor[STRING_NAME](_native_ptr(), v._native_ptr());
  142. }
  143. Variant::Variant(const NodePath &v) {
  144. from_type_constructor[NODE_PATH](_native_ptr(), v._native_ptr());
  145. }
  146. Variant::Variant(const godot::RID &v) {
  147. from_type_constructor[RID](_native_ptr(), v._native_ptr());
  148. }
  149. Variant::Variant(const Object *v) {
  150. if (v) {
  151. from_type_constructor[OBJECT](_native_ptr(), const_cast<GodotObject **>(&v->_owner));
  152. } else {
  153. GodotObject *nullobject = nullptr;
  154. from_type_constructor[OBJECT](_native_ptr(), &nullobject);
  155. }
  156. }
  157. Variant::Variant(const Callable &v) {
  158. from_type_constructor[CALLABLE](_native_ptr(), v._native_ptr());
  159. }
  160. Variant::Variant(const Signal &v) {
  161. from_type_constructor[SIGNAL](_native_ptr(), v._native_ptr());
  162. }
  163. Variant::Variant(const Dictionary &v) {
  164. from_type_constructor[DICTIONARY](_native_ptr(), v._native_ptr());
  165. }
  166. Variant::Variant(const Array &v) {
  167. from_type_constructor[ARRAY](_native_ptr(), v._native_ptr());
  168. }
  169. Variant::Variant(const PackedByteArray &v) {
  170. from_type_constructor[PACKED_BYTE_ARRAY](_native_ptr(), v._native_ptr());
  171. }
  172. Variant::Variant(const PackedInt32Array &v) {
  173. from_type_constructor[PACKED_INT32_ARRAY](_native_ptr(), v._native_ptr());
  174. }
  175. Variant::Variant(const PackedInt64Array &v) {
  176. from_type_constructor[PACKED_INT64_ARRAY](_native_ptr(), v._native_ptr());
  177. }
  178. Variant::Variant(const PackedFloat32Array &v) {
  179. from_type_constructor[PACKED_FLOAT32_ARRAY](_native_ptr(), v._native_ptr());
  180. }
  181. Variant::Variant(const PackedFloat64Array &v) {
  182. from_type_constructor[PACKED_FLOAT64_ARRAY](_native_ptr(), v._native_ptr());
  183. }
  184. Variant::Variant(const PackedStringArray &v) {
  185. from_type_constructor[PACKED_STRING_ARRAY](_native_ptr(), v._native_ptr());
  186. }
  187. Variant::Variant(const PackedVector2Array &v) {
  188. from_type_constructor[PACKED_VECTOR2_ARRAY](_native_ptr(), v._native_ptr());
  189. }
  190. Variant::Variant(const PackedVector3Array &v) {
  191. from_type_constructor[PACKED_VECTOR3_ARRAY](_native_ptr(), v._native_ptr());
  192. }
  193. Variant::Variant(const PackedColorArray &v) {
  194. from_type_constructor[PACKED_COLOR_ARRAY](_native_ptr(), v._native_ptr());
  195. }
  196. Variant::~Variant() {
  197. internal::gdn_interface->variant_destroy(_native_ptr());
  198. }
  199. Variant::operator bool() const {
  200. GDNativeBool result;
  201. to_type_constructor[BOOL](&result, _native_ptr());
  202. return PtrToArg<bool>::convert(&result);
  203. }
  204. Variant::operator int64_t() const {
  205. GDNativeInt result;
  206. to_type_constructor[INT](&result, _native_ptr());
  207. return PtrToArg<int64_t>::convert(&result);
  208. }
  209. Variant::operator int32_t() const {
  210. return static_cast<int32_t>(operator int64_t());
  211. }
  212. Variant::operator uint64_t() const {
  213. return static_cast<uint64_t>(operator int64_t());
  214. }
  215. Variant::operator uint32_t() const {
  216. return static_cast<uint32_t>(operator int64_t());
  217. }
  218. Variant::operator double() const {
  219. double result;
  220. to_type_constructor[FLOAT](&result, _native_ptr());
  221. return PtrToArg<double>::convert(&result);
  222. }
  223. Variant::operator float() const {
  224. return static_cast<float>(operator double());
  225. }
  226. Variant::operator String() const {
  227. String result;
  228. to_type_constructor[STRING](result._native_ptr(), _native_ptr());
  229. return result;
  230. }
  231. Variant::operator Vector2() const {
  232. Vector2 result;
  233. to_type_constructor[VECTOR2]((GDNativeTypePtr)&result, _native_ptr());
  234. return result;
  235. }
  236. Variant::operator Vector2i() const {
  237. Vector2i result;
  238. to_type_constructor[VECTOR2I]((GDNativeTypePtr)&result, _native_ptr());
  239. return result;
  240. }
  241. Variant::operator Rect2() const {
  242. Rect2 result;
  243. to_type_constructor[RECT2]((GDNativeTypePtr)&result, _native_ptr());
  244. return result;
  245. }
  246. Variant::operator Rect2i() const {
  247. Rect2i result;
  248. to_type_constructor[RECT2I]((GDNativeTypePtr)&result, _native_ptr());
  249. return result;
  250. }
  251. Variant::operator Vector3() const {
  252. Vector3 result;
  253. to_type_constructor[VECTOR3]((GDNativeTypePtr)&result, _native_ptr());
  254. return result;
  255. }
  256. Variant::operator Vector3i() const {
  257. Vector3i result;
  258. to_type_constructor[VECTOR3I]((GDNativeTypePtr)&result, _native_ptr());
  259. return result;
  260. }
  261. Variant::operator Transform2D() const {
  262. Transform2D result;
  263. to_type_constructor[TRANSFORM2D]((GDNativeTypePtr)&result, _native_ptr());
  264. return result;
  265. }
  266. Variant::operator Vector4() const {
  267. Vector4 result;
  268. to_type_constructor[VECTOR4]((GDNativeTypePtr)&result, _native_ptr());
  269. return result;
  270. }
  271. Variant::operator Vector4i() const {
  272. Vector4i result;
  273. to_type_constructor[VECTOR4I]((GDNativeTypePtr)&result, _native_ptr());
  274. return result;
  275. }
  276. Variant::operator Plane() const {
  277. Plane result;
  278. to_type_constructor[PLANE]((GDNativeTypePtr)&result, _native_ptr());
  279. return result;
  280. }
  281. Variant::operator Quaternion() const {
  282. Quaternion result;
  283. to_type_constructor[QUATERNION]((GDNativeTypePtr)&result, _native_ptr());
  284. return result;
  285. }
  286. Variant::operator godot::AABB() const {
  287. godot::AABB result;
  288. to_type_constructor[AABB]((GDNativeTypePtr)&result, _native_ptr());
  289. return result;
  290. }
  291. Variant::operator Basis() const {
  292. Basis result;
  293. to_type_constructor[BASIS]((GDNativeTypePtr)&result, _native_ptr());
  294. return result;
  295. }
  296. Variant::operator Transform3D() const {
  297. Transform3D result;
  298. to_type_constructor[TRANSFORM3D]((GDNativeTypePtr)&result, _native_ptr());
  299. return result;
  300. }
  301. Variant::operator Projection() const {
  302. Projection result;
  303. to_type_constructor[PROJECTION]((GDNativeTypePtr)&result, _native_ptr());
  304. return result;
  305. }
  306. Variant::operator Color() const {
  307. Color result;
  308. to_type_constructor[COLOR]((GDNativeTypePtr)&result, _native_ptr());
  309. return result;
  310. }
  311. Variant::operator StringName() const {
  312. StringName result;
  313. to_type_constructor[STRING_NAME](result._native_ptr(), _native_ptr());
  314. return result;
  315. }
  316. Variant::operator NodePath() const {
  317. NodePath result;
  318. to_type_constructor[NODE_PATH](result._native_ptr(), _native_ptr());
  319. return result;
  320. }
  321. Variant::operator godot::RID() const {
  322. godot::RID result;
  323. to_type_constructor[RID](result._native_ptr(), _native_ptr());
  324. return result;
  325. }
  326. Variant::operator Object *() const {
  327. GodotObject *obj;
  328. to_type_constructor[OBJECT](&obj, _native_ptr());
  329. if (obj == nullptr) {
  330. return nullptr;
  331. }
  332. return reinterpret_cast<Object *>(internal::gdn_interface->object_get_instance_binding(obj, internal::token, &Object::___binding_callbacks));
  333. }
  334. Variant::operator Callable() const {
  335. Callable result;
  336. to_type_constructor[CALLABLE](result._native_ptr(), _native_ptr());
  337. return result;
  338. }
  339. Variant::operator Signal() const {
  340. Signal result;
  341. to_type_constructor[SIGNAL](result._native_ptr(), _native_ptr());
  342. return result;
  343. }
  344. Variant::operator Dictionary() const {
  345. Dictionary result;
  346. to_type_constructor[DICTIONARY](result._native_ptr(), _native_ptr());
  347. return result;
  348. }
  349. Variant::operator Array() const {
  350. Array result;
  351. to_type_constructor[ARRAY](result._native_ptr(), _native_ptr());
  352. return result;
  353. }
  354. Variant::operator PackedByteArray() const {
  355. PackedByteArray result;
  356. to_type_constructor[PACKED_BYTE_ARRAY](result._native_ptr(), _native_ptr());
  357. return result;
  358. }
  359. Variant::operator PackedInt32Array() const {
  360. PackedInt32Array result;
  361. to_type_constructor[PACKED_INT32_ARRAY](result._native_ptr(), _native_ptr());
  362. return result;
  363. }
  364. Variant::operator PackedInt64Array() const {
  365. PackedInt64Array result;
  366. to_type_constructor[PACKED_INT64_ARRAY](result._native_ptr(), _native_ptr());
  367. return result;
  368. }
  369. Variant::operator PackedFloat32Array() const {
  370. PackedFloat32Array result;
  371. to_type_constructor[PACKED_FLOAT32_ARRAY](result._native_ptr(), _native_ptr());
  372. return result;
  373. }
  374. Variant::operator PackedFloat64Array() const {
  375. PackedFloat64Array result;
  376. to_type_constructor[PACKED_FLOAT64_ARRAY](result._native_ptr(), _native_ptr());
  377. return result;
  378. }
  379. Variant::operator PackedStringArray() const {
  380. PackedStringArray result;
  381. to_type_constructor[PACKED_STRING_ARRAY](result._native_ptr(), _native_ptr());
  382. return result;
  383. }
  384. Variant::operator PackedVector2Array() const {
  385. PackedVector2Array result;
  386. to_type_constructor[PACKED_VECTOR2_ARRAY](result._native_ptr(), _native_ptr());
  387. return result;
  388. }
  389. Variant::operator PackedVector3Array() const {
  390. PackedVector3Array result;
  391. to_type_constructor[PACKED_VECTOR3_ARRAY](result._native_ptr(), _native_ptr());
  392. return result;
  393. }
  394. Variant::operator PackedColorArray() const {
  395. PackedColorArray result;
  396. to_type_constructor[PACKED_COLOR_ARRAY](result._native_ptr(), _native_ptr());
  397. return result;
  398. }
  399. Variant &Variant::operator=(const Variant &other) {
  400. clear();
  401. internal::gdn_interface->variant_new_copy(_native_ptr(), other._native_ptr());
  402. return *this;
  403. }
  404. Variant &Variant::operator=(Variant &&other) {
  405. std::swap(opaque, other.opaque);
  406. return *this;
  407. }
  408. bool Variant::operator==(const Variant &other) const {
  409. if (get_type() != other.get_type()) {
  410. return false;
  411. }
  412. bool valid = false;
  413. Variant result;
  414. evaluate(OP_EQUAL, *this, other, result, valid);
  415. return result.operator bool();
  416. }
  417. bool Variant::operator!=(const Variant &other) const {
  418. if (get_type() != other.get_type()) {
  419. return true;
  420. }
  421. bool valid = false;
  422. Variant result;
  423. evaluate(OP_NOT_EQUAL, *this, other, result, valid);
  424. return result.operator bool();
  425. }
  426. bool Variant::operator<(const Variant &other) const {
  427. if (get_type() != other.get_type()) {
  428. return get_type() < other.get_type();
  429. }
  430. bool valid = false;
  431. Variant result;
  432. evaluate(OP_LESS, *this, other, result, valid);
  433. return result.operator bool();
  434. }
  435. void Variant::call(const StringName &method, const Variant **args, int argcount, Variant &r_ret, GDNativeCallError &r_error) {
  436. internal::gdn_interface->variant_call(_native_ptr(), method._native_ptr(), reinterpret_cast<const GDNativeVariantPtr *>(const_cast<Variant **>(args)), argcount, r_ret._native_ptr(), &r_error);
  437. }
  438. void Variant::call_static(Variant::Type type, const StringName &method, const Variant **args, int argcount, Variant &r_ret, GDNativeCallError &r_error) {
  439. internal::gdn_interface->variant_call_static(static_cast<GDNativeVariantType>(type), method._native_ptr(), reinterpret_cast<const GDNativeVariantPtr *>(const_cast<Variant **>(args)), argcount, r_ret._native_ptr(), &r_error);
  440. }
  441. void Variant::evaluate(const Operator &op, const Variant &a, const Variant &b, Variant &r_ret, bool &r_valid) {
  442. GDNativeBool valid;
  443. internal::gdn_interface->variant_evaluate(static_cast<GDNativeVariantOperator>(op), a._native_ptr(), b._native_ptr(), r_ret._native_ptr(), &valid);
  444. r_valid = PtrToArg<bool>::convert(&valid);
  445. }
  446. void Variant::set(const Variant &key, const Variant &value, bool *r_valid) {
  447. GDNativeBool valid;
  448. internal::gdn_interface->variant_set(_native_ptr(), key._native_ptr(), value._native_ptr(), &valid);
  449. if (r_valid) {
  450. *r_valid = PtrToArg<bool>::convert(&valid);
  451. }
  452. }
  453. void Variant::set_named(const StringName &name, const Variant &value, bool &r_valid) {
  454. GDNativeBool valid;
  455. internal::gdn_interface->variant_set_named(_native_ptr(), name._native_ptr(), value._native_ptr(), &valid);
  456. r_valid = PtrToArg<bool>::convert(&valid);
  457. }
  458. void Variant::set_indexed(int64_t index, const Variant &value, bool &r_valid, bool &r_oob) {
  459. GDNativeBool valid, oob;
  460. internal::gdn_interface->variant_set_indexed(_native_ptr(), index, value._native_ptr(), &valid, &oob);
  461. r_valid = PtrToArg<bool>::convert(&valid);
  462. r_oob = PtrToArg<bool>::convert(&oob);
  463. }
  464. void Variant::set_keyed(const Variant &key, const Variant &value, bool &r_valid) {
  465. GDNativeBool valid;
  466. internal::gdn_interface->variant_set_keyed(_native_ptr(), key._native_ptr(), value._native_ptr(), &valid);
  467. r_valid = PtrToArg<bool>::convert(&valid);
  468. }
  469. Variant Variant::get(const Variant &key, bool *r_valid) const {
  470. Variant result;
  471. GDNativeBool valid;
  472. internal::gdn_interface->variant_get(_native_ptr(), key._native_ptr(), result._native_ptr(), &valid);
  473. if (r_valid) {
  474. *r_valid = PtrToArg<bool>::convert(&valid);
  475. }
  476. return result;
  477. }
  478. Variant Variant::get_named(const StringName &name, bool &r_valid) const {
  479. Variant result;
  480. GDNativeBool valid;
  481. internal::gdn_interface->variant_get_named(_native_ptr(), name._native_ptr(), result._native_ptr(), &valid);
  482. r_valid = PtrToArg<bool>::convert(&valid);
  483. return result;
  484. }
  485. Variant Variant::get_indexed(int64_t index, bool &r_valid, bool &r_oob) const {
  486. Variant result;
  487. GDNativeBool valid;
  488. GDNativeBool oob;
  489. internal::gdn_interface->variant_get_indexed(_native_ptr(), index, result._native_ptr(), &valid, &oob);
  490. r_valid = PtrToArg<bool>::convert(&valid);
  491. r_oob = PtrToArg<bool>::convert(&oob);
  492. return result;
  493. }
  494. Variant Variant::get_keyed(const Variant &key, bool &r_valid) const {
  495. Variant result;
  496. GDNativeBool valid;
  497. internal::gdn_interface->variant_get_keyed(_native_ptr(), key._native_ptr(), result._native_ptr(), &valid);
  498. r_valid = PtrToArg<bool>::convert(&valid);
  499. return result;
  500. }
  501. bool Variant::in(const Variant &index, bool *r_valid) const {
  502. Variant result;
  503. bool valid;
  504. evaluate(OP_IN, *this, index, result, valid);
  505. if (r_valid) {
  506. *r_valid = valid;
  507. }
  508. return result.operator bool();
  509. }
  510. bool Variant::iter_init(Variant &r_iter, bool &r_valid) const {
  511. GDNativeBool valid;
  512. internal::gdn_interface->variant_iter_init(_native_ptr(), r_iter._native_ptr(), &valid);
  513. return PtrToArg<bool>::convert(&valid);
  514. }
  515. bool Variant::iter_next(Variant &r_iter, bool &r_valid) const {
  516. GDNativeBool valid;
  517. internal::gdn_interface->variant_iter_next(_native_ptr(), r_iter._native_ptr(), &valid);
  518. return PtrToArg<bool>::convert(&valid);
  519. }
  520. Variant Variant::iter_get(const Variant &r_iter, bool &r_valid) const {
  521. Variant result;
  522. GDNativeBool valid;
  523. internal::gdn_interface->variant_iter_get(_native_ptr(), r_iter._native_ptr(), result._native_ptr(), &valid);
  524. r_valid = PtrToArg<bool>::convert(&valid);
  525. return result;
  526. }
  527. Variant::Type Variant::get_type() const {
  528. return static_cast<Variant::Type>(internal::gdn_interface->variant_get_type(_native_ptr()));
  529. }
  530. bool Variant::has_method(const StringName &method) const {
  531. GDNativeBool has = internal::gdn_interface->variant_has_method(_native_ptr(), method._native_ptr());
  532. return PtrToArg<bool>::convert(&has);
  533. }
  534. bool Variant::has_key(const Variant &key, bool *r_valid) const {
  535. GDNativeBool valid;
  536. GDNativeBool has = internal::gdn_interface->variant_has_key(_native_ptr(), key._native_ptr(), &valid);
  537. if (r_valid) {
  538. *r_valid = PtrToArg<bool>::convert(&valid);
  539. }
  540. return PtrToArg<bool>::convert(&has);
  541. }
  542. bool Variant::has_member(Variant::Type type, const StringName &member) {
  543. GDNativeBool has = internal::gdn_interface->variant_has_member(static_cast<GDNativeVariantType>(type), member._native_ptr());
  544. return PtrToArg<bool>::convert(&has);
  545. }
  546. uint32_t Variant::hash() const {
  547. GDNativeInt hash = internal::gdn_interface->variant_hash(_native_ptr());
  548. return PtrToArg<uint32_t>::convert(&hash);
  549. }
  550. uint32_t Variant::recursive_hash(int recursion_count) const {
  551. GDNativeInt hash = internal::gdn_interface->variant_recursive_hash(_native_ptr(), recursion_count);
  552. return PtrToArg<uint32_t>::convert(&hash);
  553. }
  554. bool Variant::hash_compare(const Variant &variant) const {
  555. GDNativeBool compare = internal::gdn_interface->variant_hash_compare(_native_ptr(), variant._native_ptr());
  556. return PtrToArg<bool>::convert(&compare);
  557. }
  558. bool Variant::booleanize() const {
  559. GDNativeBool booleanized = internal::gdn_interface->variant_booleanize(_native_ptr());
  560. return PtrToArg<bool>::convert(&booleanized);
  561. }
  562. String Variant::stringify() const {
  563. String result;
  564. internal::gdn_interface->variant_stringify(_native_ptr(), result._native_ptr());
  565. return result;
  566. }
  567. Variant Variant::duplicate(bool deep) const {
  568. Variant result;
  569. GDNativeBool _deep;
  570. PtrToArg<bool>::encode(deep, &_deep);
  571. internal::gdn_interface->variant_duplicate(_native_ptr(), result._native_ptr(), _deep);
  572. return result;
  573. }
  574. String Variant::get_type_name(Variant::Type type) {
  575. String result;
  576. internal::gdn_interface->variant_get_type_name(static_cast<GDNativeVariantType>(type), result._native_ptr());
  577. return result;
  578. }
  579. bool Variant::can_convert(Variant::Type from, Variant::Type to) {
  580. GDNativeBool can;
  581. internal::gdn_interface->variant_can_convert(static_cast<GDNativeVariantType>(from), static_cast<GDNativeVariantType>(to));
  582. return PtrToArg<bool>::convert(&can);
  583. }
  584. bool Variant::can_convert_strict(Variant::Type from, Variant::Type to) {
  585. GDNativeBool can;
  586. internal::gdn_interface->variant_can_convert_strict(static_cast<GDNativeVariantType>(from), static_cast<GDNativeVariantType>(to));
  587. return PtrToArg<bool>::convert(&can);
  588. }
  589. void Variant::clear() {
  590. static const bool needs_deinit[Variant::VARIANT_MAX] = {
  591. false, // NIL,
  592. false, // BOOL,
  593. false, // INT,
  594. false, // FLOAT,
  595. true, // STRING,
  596. false, // VECTOR2,
  597. false, // VECTOR2I,
  598. false, // RECT2,
  599. false, // RECT2I,
  600. false, // VECTOR3,
  601. false, // VECTOR3I,
  602. true, // TRANSFORM2D,
  603. false, // VECTOR4,
  604. false, // VECTOR4I,
  605. false, // PLANE,
  606. false, // QUATERNION,
  607. true, // AABB,
  608. true, // BASIS,
  609. true, // TRANSFORM3D,
  610. true, // PROJECTION,
  611. // misc types
  612. false, // COLOR,
  613. true, // STRING_NAME,
  614. true, // NODE_PATH,
  615. false, // RID,
  616. true, // OBJECT,
  617. true, // CALLABLE,
  618. true, // SIGNAL,
  619. true, // DICTIONARY,
  620. true, // ARRAY,
  621. // typed arrays
  622. true, // PACKED_BYTE_ARRAY,
  623. true, // PACKED_INT32_ARRAY,
  624. true, // PACKED_INT64_ARRAY,
  625. true, // PACKED_FLOAT32_ARRAY,
  626. true, // PACKED_FLOAT64_ARRAY,
  627. true, // PACKED_STRING_ARRAY,
  628. true, // PACKED_VECTOR2_ARRAY,
  629. true, // PACKED_VECTOR3_ARRAY,
  630. true, // PACKED_COLOR_ARRAY,
  631. };
  632. if (unlikely(needs_deinit[get_type()])) { // Make it fast for types that don't need deinit.
  633. internal::gdn_interface->variant_destroy(_native_ptr());
  634. }
  635. internal::gdn_interface->variant_new_nil(_native_ptr());
  636. }
  637. } // namespace godot