variant.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /**************************************************************************/
  2. /* variant.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_H
  31. #define VARIANT_H
  32. #include "core/core_string_names.h"
  33. #include "core/input/input_enums.h"
  34. #include "core/io/ip_address.h"
  35. #include "core/math/aabb.h"
  36. #include "core/math/basis.h"
  37. #include "core/math/color.h"
  38. #include "core/math/face3.h"
  39. #include "core/math/plane.h"
  40. #include "core/math/projection.h"
  41. #include "core/math/quaternion.h"
  42. #include "core/math/rect2.h"
  43. #include "core/math/rect2i.h"
  44. #include "core/math/transform_2d.h"
  45. #include "core/math/transform_3d.h"
  46. #include "core/math/vector2.h"
  47. #include "core/math/vector2i.h"
  48. #include "core/math/vector3.h"
  49. #include "core/math/vector3i.h"
  50. #include "core/math/vector4.h"
  51. #include "core/math/vector4i.h"
  52. #include "core/object/object_id.h"
  53. #include "core/os/keyboard.h"
  54. #include "core/string/node_path.h"
  55. #include "core/string/ustring.h"
  56. #include "core/templates/paged_allocator.h"
  57. #include "core/templates/rid.h"
  58. #include "core/variant/array.h"
  59. #include "core/variant/callable.h"
  60. #include "core/variant/dictionary.h"
  61. class Object;
  62. class RefCounted;
  63. template <typename T>
  64. class Ref;
  65. struct PropertyInfo;
  66. struct MethodInfo;
  67. typedef Vector<uint8_t> PackedByteArray;
  68. typedef Vector<int32_t> PackedInt32Array;
  69. typedef Vector<int64_t> PackedInt64Array;
  70. typedef Vector<float> PackedFloat32Array;
  71. typedef Vector<double> PackedFloat64Array;
  72. typedef Vector<real_t> PackedRealArray;
  73. typedef Vector<String> PackedStringArray;
  74. typedef Vector<Vector2> PackedVector2Array;
  75. typedef Vector<Vector3> PackedVector3Array;
  76. typedef Vector<Color> PackedColorArray;
  77. typedef Vector<Vector4> PackedVector4Array;
  78. class Variant {
  79. public:
  80. // If this changes the table in variant_op must be updated
  81. enum Type {
  82. NIL,
  83. // atomic types
  84. BOOL,
  85. INT,
  86. FLOAT,
  87. STRING,
  88. // math types
  89. VECTOR2,
  90. VECTOR2I,
  91. RECT2,
  92. RECT2I,
  93. VECTOR3,
  94. VECTOR3I,
  95. TRANSFORM2D,
  96. VECTOR4,
  97. VECTOR4I,
  98. PLANE,
  99. QUATERNION,
  100. AABB,
  101. BASIS,
  102. TRANSFORM3D,
  103. PROJECTION,
  104. // misc types
  105. COLOR,
  106. STRING_NAME,
  107. NODE_PATH,
  108. RID,
  109. OBJECT,
  110. CALLABLE,
  111. SIGNAL,
  112. DICTIONARY,
  113. ARRAY,
  114. // typed arrays
  115. PACKED_BYTE_ARRAY,
  116. PACKED_INT32_ARRAY,
  117. PACKED_INT64_ARRAY,
  118. PACKED_FLOAT32_ARRAY,
  119. PACKED_FLOAT64_ARRAY,
  120. PACKED_STRING_ARRAY,
  121. PACKED_VECTOR2_ARRAY,
  122. PACKED_VECTOR3_ARRAY,
  123. PACKED_COLOR_ARRAY,
  124. PACKED_VECTOR4_ARRAY,
  125. VARIANT_MAX
  126. };
  127. enum {
  128. // Maximum recursion depth allowed when serializing variants.
  129. MAX_RECURSION_DEPTH = 1024,
  130. };
  131. private:
  132. struct Pools {
  133. union BucketSmall {
  134. BucketSmall() {}
  135. ~BucketSmall() {}
  136. Transform2D _transform2d;
  137. ::AABB _aabb;
  138. };
  139. union BucketMedium {
  140. BucketMedium() {}
  141. ~BucketMedium() {}
  142. Basis _basis;
  143. Transform3D _transform3d;
  144. };
  145. union BucketLarge {
  146. BucketLarge() {}
  147. ~BucketLarge() {}
  148. Projection _projection;
  149. };
  150. static PagedAllocator<BucketSmall, true> _bucket_small;
  151. static PagedAllocator<BucketMedium, true> _bucket_medium;
  152. static PagedAllocator<BucketLarge, true> _bucket_large;
  153. };
  154. friend struct _VariantCall;
  155. friend class VariantInternal;
  156. // Variant takes 24 bytes when real_t is float, and 40 bytes if double.
  157. // It only allocates extra memory for AABB/Transform2D (24, 48 if double),
  158. // Basis/Transform3D (48, 96 if double), Projection (64, 128 if double),
  159. // and PackedArray/Array/Dictionary (platform-dependent).
  160. Type type = NIL;
  161. struct ObjData {
  162. ObjectID id;
  163. Object *obj = nullptr;
  164. void ref(const ObjData &p_from);
  165. void ref_pointer(Object *p_object);
  166. void ref_pointer(RefCounted *p_object);
  167. void unref();
  168. template <typename T>
  169. _ALWAYS_INLINE_ void ref(const Ref<T> &p_from) {
  170. if (p_from.is_valid()) {
  171. ref(ObjData{ p_from->get_instance_id(), p_from.ptr() });
  172. } else {
  173. unref();
  174. }
  175. }
  176. };
  177. /* array helpers */
  178. struct PackedArrayRefBase {
  179. SafeRefCount refcount;
  180. _FORCE_INLINE_ PackedArrayRefBase *reference() {
  181. if (refcount.ref()) {
  182. return this;
  183. } else {
  184. return nullptr;
  185. }
  186. }
  187. static _FORCE_INLINE_ PackedArrayRefBase *reference_from(PackedArrayRefBase *p_base, PackedArrayRefBase *p_from) {
  188. if (p_base == p_from) {
  189. return p_base; //same thing, do nothing
  190. }
  191. if (p_from->reference()) {
  192. if (p_base->refcount.unref()) {
  193. memdelete(p_base);
  194. }
  195. return p_from;
  196. } else {
  197. return p_base; //keep, could not reference new
  198. }
  199. }
  200. static _FORCE_INLINE_ void destroy(PackedArrayRefBase *p_array) {
  201. if (p_array->refcount.unref()) {
  202. memdelete(p_array);
  203. }
  204. }
  205. _FORCE_INLINE_ virtual ~PackedArrayRefBase() {} //needs virtual destructor, but make inline
  206. };
  207. template <typename T>
  208. struct PackedArrayRef : public PackedArrayRefBase {
  209. Vector<T> array;
  210. static _FORCE_INLINE_ PackedArrayRef<T> *create() {
  211. return memnew(PackedArrayRef<T>);
  212. }
  213. static _FORCE_INLINE_ PackedArrayRef<T> *create(const Vector<T> &p_from) {
  214. return memnew(PackedArrayRef<T>(p_from));
  215. }
  216. static _FORCE_INLINE_ const Vector<T> &get_array(PackedArrayRefBase *p_base) {
  217. return static_cast<PackedArrayRef<T> *>(p_base)->array;
  218. }
  219. static _FORCE_INLINE_ Vector<T> *get_array_ptr(const PackedArrayRefBase *p_base) {
  220. return &const_cast<PackedArrayRef<T> *>(static_cast<const PackedArrayRef<T> *>(p_base))->array;
  221. }
  222. _FORCE_INLINE_ PackedArrayRef(const Vector<T> &p_from) {
  223. array = p_from;
  224. refcount.init();
  225. }
  226. _FORCE_INLINE_ PackedArrayRef() {
  227. refcount.init();
  228. }
  229. };
  230. /* end of array helpers */
  231. _ALWAYS_INLINE_ ObjData &_get_obj();
  232. _ALWAYS_INLINE_ const ObjData &_get_obj() const;
  233. union {
  234. bool _bool;
  235. int64_t _int;
  236. double _float;
  237. Transform2D *_transform2d;
  238. ::AABB *_aabb;
  239. Basis *_basis;
  240. Transform3D *_transform3d;
  241. Projection *_projection;
  242. PackedArrayRefBase *packed_array;
  243. void *_ptr; //generic pointer
  244. uint8_t _mem[sizeof(ObjData) > (sizeof(real_t) * 4) ? sizeof(ObjData) : (sizeof(real_t) * 4)]{ 0 };
  245. } _data alignas(8);
  246. void reference(const Variant &p_variant);
  247. void _clear_internal();
  248. _FORCE_INLINE_ void clear() {
  249. static const bool needs_deinit[Variant::VARIANT_MAX] = {
  250. false, //NIL,
  251. false, //BOOL,
  252. false, //INT,
  253. false, //FLOAT,
  254. true, //STRING,
  255. false, //VECTOR2,
  256. false, //VECTOR2I,
  257. false, //RECT2,
  258. false, //RECT2I,
  259. false, //VECTOR3,
  260. false, //VECTOR3I,
  261. true, //TRANSFORM2D,
  262. false, //VECTOR4,
  263. false, //VECTOR4I,
  264. false, //PLANE,
  265. false, //QUATERNION,
  266. true, //AABB,
  267. true, //BASIS,
  268. true, //TRANSFORM,
  269. true, //PROJECTION,
  270. // misc types
  271. false, //COLOR,
  272. true, //STRING_NAME,
  273. true, //NODE_PATH,
  274. false, //RID,
  275. true, //OBJECT,
  276. true, //CALLABLE,
  277. true, //SIGNAL,
  278. true, //DICTIONARY,
  279. true, //ARRAY,
  280. // typed arrays
  281. true, //PACKED_BYTE_ARRAY,
  282. true, //PACKED_INT32_ARRAY,
  283. true, //PACKED_INT64_ARRAY,
  284. true, //PACKED_FLOAT32_ARRAY,
  285. true, //PACKED_FLOAT64_ARRAY,
  286. true, //PACKED_STRING_ARRAY,
  287. true, //PACKED_VECTOR2_ARRAY,
  288. true, //PACKED_VECTOR3_ARRAY,
  289. true, //PACKED_COLOR_ARRAY,
  290. true, //PACKED_VECTOR4_ARRAY,
  291. };
  292. if (unlikely(needs_deinit[type])) { // Make it fast for types that don't need deinit.
  293. _clear_internal();
  294. }
  295. type = NIL;
  296. }
  297. static void _register_variant_operators();
  298. static void _unregister_variant_operators();
  299. static void _register_variant_methods();
  300. static void _unregister_variant_methods();
  301. static void _register_variant_setters_getters();
  302. static void _unregister_variant_setters_getters();
  303. static void _register_variant_constructors();
  304. static void _unregister_variant_destructors();
  305. static void _register_variant_destructors();
  306. static void _unregister_variant_constructors();
  307. static void _register_variant_utility_functions();
  308. static void _unregister_variant_utility_functions();
  309. void _variant_call_error(const String &p_method, Callable::CallError &error);
  310. // Avoid accidental conversion. If you reached this point, it's because you most likely forgot to dereference
  311. // a Variant pointer (so add * like this: *variant_pointer).
  312. Variant(const Variant *) {}
  313. Variant(const Variant **) {}
  314. public:
  315. _FORCE_INLINE_ Type get_type() const {
  316. return type;
  317. }
  318. static String get_type_name(Variant::Type p_type);
  319. static bool can_convert(Type p_type_from, Type p_type_to);
  320. static bool can_convert_strict(Type p_type_from, Type p_type_to);
  321. static bool is_type_shared(Variant::Type p_type);
  322. bool is_ref_counted() const;
  323. _FORCE_INLINE_ bool is_num() const {
  324. return type == INT || type == FLOAT;
  325. }
  326. _FORCE_INLINE_ bool is_string() const {
  327. return type == STRING || type == STRING_NAME;
  328. }
  329. _FORCE_INLINE_ bool is_array() const {
  330. return type >= ARRAY;
  331. }
  332. bool is_shared() const;
  333. bool is_zero() const;
  334. bool is_one() const;
  335. bool is_null() const;
  336. bool is_read_only() const;
  337. // Make sure Variant is not implicitly cast when accessing it with bracket notation (GH-49469).
  338. Variant &operator[](const Variant &p_key) = delete;
  339. const Variant &operator[](const Variant &p_key) const = delete;
  340. operator bool() const;
  341. operator int64_t() const;
  342. operator int32_t() const;
  343. operator int16_t() const;
  344. operator int8_t() const;
  345. operator uint64_t() const;
  346. operator uint32_t() const;
  347. operator uint16_t() const;
  348. operator uint8_t() const;
  349. operator ObjectID() const;
  350. operator char32_t() const;
  351. operator float() const;
  352. operator double() const;
  353. operator String() const;
  354. operator StringName() const;
  355. operator Vector2() const;
  356. operator Vector2i() const;
  357. operator Rect2() const;
  358. operator Rect2i() const;
  359. operator Vector3() const;
  360. operator Vector3i() const;
  361. operator Vector4() const;
  362. operator Vector4i() const;
  363. operator Plane() const;
  364. operator ::AABB() const;
  365. operator Quaternion() const;
  366. operator Basis() const;
  367. operator Transform2D() const;
  368. operator Transform3D() const;
  369. operator Projection() const;
  370. operator Color() const;
  371. operator NodePath() const;
  372. operator ::RID() const;
  373. operator Object *() const;
  374. operator Callable() const;
  375. operator Signal() const;
  376. operator Dictionary() const;
  377. operator Array() const;
  378. operator PackedByteArray() const;
  379. operator PackedInt32Array() const;
  380. operator PackedInt64Array() const;
  381. operator PackedFloat32Array() const;
  382. operator PackedFloat64Array() const;
  383. operator PackedStringArray() const;
  384. operator PackedVector3Array() const;
  385. operator PackedVector2Array() const;
  386. operator PackedColorArray() const;
  387. operator PackedVector4Array() const;
  388. operator Vector<::RID>() const;
  389. operator Vector<Plane>() const;
  390. operator Vector<Face3>() const;
  391. operator Vector<Variant>() const;
  392. operator Vector<StringName>() const;
  393. // some core type enums to convert to
  394. operator Side() const;
  395. operator Orientation() const;
  396. operator IPAddress() const;
  397. Object *get_validated_object() const;
  398. Object *get_validated_object_with_check(bool &r_previously_freed) const;
  399. Variant(bool p_bool);
  400. Variant(int64_t p_int64);
  401. Variant(int32_t p_int32);
  402. Variant(int16_t p_int16);
  403. Variant(int8_t p_int8);
  404. Variant(uint64_t p_uint64);
  405. Variant(uint32_t p_uint32);
  406. Variant(uint16_t p_uint16);
  407. Variant(uint8_t p_uint8);
  408. Variant(float p_float);
  409. Variant(double p_double);
  410. Variant(const ObjectID &p_id);
  411. Variant(const String &p_string);
  412. Variant(const StringName &p_string);
  413. Variant(const char *const p_cstring);
  414. Variant(const char32_t *p_wstring);
  415. Variant(const Vector2 &p_vector2);
  416. Variant(const Vector2i &p_vector2i);
  417. Variant(const Rect2 &p_rect2);
  418. Variant(const Rect2i &p_rect2i);
  419. Variant(const Vector3 &p_vector3);
  420. Variant(const Vector3i &p_vector3i);
  421. Variant(const Vector4 &p_vector4);
  422. Variant(const Vector4i &p_vector4i);
  423. Variant(const Plane &p_plane);
  424. Variant(const ::AABB &p_aabb);
  425. Variant(const Quaternion &p_quat);
  426. Variant(const Basis &p_matrix);
  427. Variant(const Transform2D &p_transform);
  428. Variant(const Transform3D &p_transform);
  429. Variant(const Projection &p_projection);
  430. Variant(const Color &p_color);
  431. Variant(const NodePath &p_node_path);
  432. Variant(const ::RID &p_rid);
  433. Variant(const Object *p_object);
  434. Variant(const Callable &p_callable);
  435. Variant(const Signal &p_signal);
  436. Variant(const Dictionary &p_dictionary);
  437. Variant(const Array &p_array);
  438. Variant(const PackedByteArray &p_byte_array);
  439. Variant(const PackedInt32Array &p_int32_array);
  440. Variant(const PackedInt64Array &p_int64_array);
  441. Variant(const PackedFloat32Array &p_float32_array);
  442. Variant(const PackedFloat64Array &p_float64_array);
  443. Variant(const PackedStringArray &p_string_array);
  444. Variant(const PackedVector2Array &p_vector2_array);
  445. Variant(const PackedVector3Array &p_vector3_array);
  446. Variant(const PackedColorArray &p_color_array);
  447. Variant(const PackedVector4Array &p_vector4_array);
  448. Variant(const Vector<::RID> &p_array); // helper
  449. Variant(const Vector<Plane> &p_array); // helper
  450. Variant(const Vector<Face3> &p_face_array);
  451. Variant(const Vector<Variant> &p_array);
  452. Variant(const Vector<StringName> &p_array);
  453. Variant(const IPAddress &p_address);
  454. #define VARIANT_ENUM_CLASS_CONSTRUCTOR(m_enum) \
  455. Variant(m_enum p_value) : \
  456. type(INT) { \
  457. _data._int = (int64_t)p_value; \
  458. }
  459. // Only enum classes that need to be bound need this to be defined.
  460. VARIANT_ENUM_CLASS_CONSTRUCTOR(EulerOrder)
  461. VARIANT_ENUM_CLASS_CONSTRUCTOR(JoyAxis)
  462. VARIANT_ENUM_CLASS_CONSTRUCTOR(JoyButton)
  463. VARIANT_ENUM_CLASS_CONSTRUCTOR(Key)
  464. VARIANT_ENUM_CLASS_CONSTRUCTOR(KeyLocation)
  465. VARIANT_ENUM_CLASS_CONSTRUCTOR(MIDIMessage)
  466. VARIANT_ENUM_CLASS_CONSTRUCTOR(MouseButton)
  467. #undef VARIANT_ENUM_CLASS_CONSTRUCTOR
  468. // If this changes the table in variant_op must be updated
  469. enum Operator {
  470. //comparison
  471. OP_EQUAL,
  472. OP_NOT_EQUAL,
  473. OP_LESS,
  474. OP_LESS_EQUAL,
  475. OP_GREATER,
  476. OP_GREATER_EQUAL,
  477. //mathematic
  478. OP_ADD,
  479. OP_SUBTRACT,
  480. OP_MULTIPLY,
  481. OP_DIVIDE,
  482. OP_NEGATE,
  483. OP_POSITIVE,
  484. OP_MODULE,
  485. OP_POWER,
  486. //bitwise
  487. OP_SHIFT_LEFT,
  488. OP_SHIFT_RIGHT,
  489. OP_BIT_AND,
  490. OP_BIT_OR,
  491. OP_BIT_XOR,
  492. OP_BIT_NEGATE,
  493. //logic
  494. OP_AND,
  495. OP_OR,
  496. OP_XOR,
  497. OP_NOT,
  498. //containment
  499. OP_IN,
  500. OP_MAX
  501. };
  502. static String get_operator_name(Operator p_op);
  503. static void evaluate(const Operator &p_op, const Variant &p_a, const Variant &p_b, Variant &r_ret, bool &r_valid);
  504. static _FORCE_INLINE_ Variant evaluate(const Operator &p_op, const Variant &p_a, const Variant &p_b) {
  505. bool valid = true;
  506. Variant res;
  507. evaluate(p_op, p_a, p_b, res, valid);
  508. return res;
  509. }
  510. static Variant::Type get_operator_return_type(Operator p_operator, Type p_type_a, Type p_type_b);
  511. typedef void (*ValidatedOperatorEvaluator)(const Variant *left, const Variant *right, Variant *r_ret);
  512. static ValidatedOperatorEvaluator get_validated_operator_evaluator(Operator p_operator, Type p_type_a, Type p_type_b);
  513. typedef void (*PTROperatorEvaluator)(const void *left, const void *right, void *r_ret);
  514. static PTROperatorEvaluator get_ptr_operator_evaluator(Operator p_operator, Type p_type_a, Type p_type_b);
  515. void zero();
  516. Variant duplicate(bool p_deep = false) const;
  517. Variant recursive_duplicate(bool p_deep, int recursion_count) const;
  518. /* Built-In Methods */
  519. typedef void (*ValidatedBuiltInMethod)(Variant *base, const Variant **p_args, int p_argcount, Variant *r_ret);
  520. typedef void (*PTRBuiltInMethod)(void *p_base, const void **p_args, void *r_ret, int p_argcount);
  521. static bool has_builtin_method(Variant::Type p_type, const StringName &p_method);
  522. static ValidatedBuiltInMethod get_validated_builtin_method(Variant::Type p_type, const StringName &p_method);
  523. static PTRBuiltInMethod get_ptr_builtin_method(Variant::Type p_type, const StringName &p_method);
  524. static MethodInfo get_builtin_method_info(Variant::Type p_type, const StringName &p_method);
  525. static int get_builtin_method_argument_count(Variant::Type p_type, const StringName &p_method);
  526. static Variant::Type get_builtin_method_argument_type(Variant::Type p_type, const StringName &p_method, int p_argument);
  527. static String get_builtin_method_argument_name(Variant::Type p_type, const StringName &p_method, int p_argument);
  528. static Vector<Variant> get_builtin_method_default_arguments(Variant::Type p_type, const StringName &p_method);
  529. static bool has_builtin_method_return_value(Variant::Type p_type, const StringName &p_method);
  530. static Variant::Type get_builtin_method_return_type(Variant::Type p_type, const StringName &p_method);
  531. static bool is_builtin_method_const(Variant::Type p_type, const StringName &p_method);
  532. static bool is_builtin_method_static(Variant::Type p_type, const StringName &p_method);
  533. static bool is_builtin_method_vararg(Variant::Type p_type, const StringName &p_method);
  534. static void get_builtin_method_list(Variant::Type p_type, List<StringName> *p_list);
  535. static int get_builtin_method_count(Variant::Type p_type);
  536. static uint32_t get_builtin_method_hash(Variant::Type p_type, const StringName &p_method);
  537. void callp(const StringName &p_method, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error);
  538. template <typename... VarArgs>
  539. Variant call(const StringName &p_method, VarArgs... p_args) {
  540. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  541. const Variant *argptrs[sizeof...(p_args) + 1];
  542. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  543. argptrs[i] = &args[i];
  544. }
  545. Callable::CallError cerr;
  546. Variant ret;
  547. callp(p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args), ret, cerr);
  548. if (cerr.error != Callable::CallError::CALL_OK) {
  549. _variant_call_error(p_method, cerr);
  550. }
  551. return ret;
  552. }
  553. void call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error);
  554. static void call_static(Variant::Type p_type, const StringName &p_method, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error);
  555. static String get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);
  556. static String get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);
  557. static String get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);
  558. //dynamic (includes Object)
  559. void get_method_list(List<MethodInfo> *p_list) const;
  560. bool has_method(const StringName &p_method) const;
  561. /* Constructors */
  562. typedef void (*ValidatedConstructor)(Variant *r_base, const Variant **p_args);
  563. typedef void (*PTRConstructor)(void *base, const void **p_args);
  564. static int get_constructor_count(Variant::Type p_type);
  565. static ValidatedConstructor get_validated_constructor(Variant::Type p_type, int p_constructor);
  566. static PTRConstructor get_ptr_constructor(Variant::Type p_type, int p_constructor);
  567. static int get_constructor_argument_count(Variant::Type p_type, int p_constructor);
  568. static Variant::Type get_constructor_argument_type(Variant::Type p_type, int p_constructor, int p_argument);
  569. static String get_constructor_argument_name(Variant::Type p_type, int p_constructor, int p_argument);
  570. static void construct(Variant::Type, Variant &base, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  571. static void get_constructor_list(Type p_type, List<MethodInfo> *r_list); //convenience
  572. /* Destructors */
  573. // Only ptrcall is available.
  574. typedef void (*PTRDestructor)(void *base);
  575. static PTRDestructor get_ptr_destructor(Variant::Type p_type);
  576. static bool has_destructor(Variant::Type p_type);
  577. /* Properties */
  578. void set_named(const StringName &p_member, const Variant &p_value, bool &r_valid);
  579. Variant get_named(const StringName &p_member, bool &r_valid) const;
  580. typedef void (*ValidatedSetter)(Variant *base, const Variant *value);
  581. typedef void (*ValidatedGetter)(const Variant *base, Variant *value);
  582. static bool has_member(Variant::Type p_type, const StringName &p_member);
  583. static Variant::Type get_member_type(Variant::Type p_type, const StringName &p_member);
  584. static void get_member_list(Type p_type, List<StringName> *r_members);
  585. static int get_member_count(Type p_type);
  586. static ValidatedSetter get_member_validated_setter(Variant::Type p_type, const StringName &p_member);
  587. static ValidatedGetter get_member_validated_getter(Variant::Type p_type, const StringName &p_member);
  588. typedef void (*PTRSetter)(void *base, const void *value);
  589. typedef void (*PTRGetter)(const void *base, void *value);
  590. static PTRSetter get_member_ptr_setter(Variant::Type p_type, const StringName &p_member);
  591. static PTRGetter get_member_ptr_getter(Variant::Type p_type, const StringName &p_member);
  592. /* Indexing */
  593. static bool has_indexing(Variant::Type p_type);
  594. static Variant::Type get_indexed_element_type(Variant::Type p_type);
  595. static uint32_t get_indexed_element_usage(Variant::Type p_type);
  596. typedef void (*ValidatedIndexedSetter)(Variant *base, int64_t index, const Variant *value, bool *oob);
  597. typedef void (*ValidatedIndexedGetter)(const Variant *base, int64_t index, Variant *value, bool *oob);
  598. static ValidatedIndexedSetter get_member_validated_indexed_setter(Variant::Type p_type);
  599. static ValidatedIndexedGetter get_member_validated_indexed_getter(Variant::Type p_type);
  600. typedef void (*PTRIndexedSetter)(void *base, int64_t index, const void *value);
  601. typedef void (*PTRIndexedGetter)(const void *base, int64_t index, void *value);
  602. static PTRIndexedSetter get_member_ptr_indexed_setter(Variant::Type p_type);
  603. static PTRIndexedGetter get_member_ptr_indexed_getter(Variant::Type p_type);
  604. void set_indexed(int64_t p_index, const Variant &p_value, bool &r_valid, bool &r_oob);
  605. Variant get_indexed(int64_t p_index, bool &r_valid, bool &r_oob) const;
  606. uint64_t get_indexed_size() const;
  607. /* Keying */
  608. static bool is_keyed(Variant::Type p_type);
  609. typedef void (*ValidatedKeyedSetter)(Variant *base, const Variant *key, const Variant *value, bool *valid);
  610. typedef void (*ValidatedKeyedGetter)(const Variant *base, const Variant *key, Variant *value, bool *valid);
  611. typedef bool (*ValidatedKeyedChecker)(const Variant *base, const Variant *key, bool *valid);
  612. static ValidatedKeyedSetter get_member_validated_keyed_setter(Variant::Type p_type);
  613. static ValidatedKeyedGetter get_member_validated_keyed_getter(Variant::Type p_type);
  614. static ValidatedKeyedChecker get_member_validated_keyed_checker(Variant::Type p_type);
  615. typedef void (*PTRKeyedSetter)(void *base, const void *key, const void *value);
  616. typedef void (*PTRKeyedGetter)(const void *base, const void *key, void *value);
  617. typedef uint32_t (*PTRKeyedChecker)(const void *base, const void *key);
  618. static PTRKeyedSetter get_member_ptr_keyed_setter(Variant::Type p_type);
  619. static PTRKeyedGetter get_member_ptr_keyed_getter(Variant::Type p_type);
  620. static PTRKeyedChecker get_member_ptr_keyed_checker(Variant::Type p_type);
  621. void set_keyed(const Variant &p_key, const Variant &p_value, bool &r_valid);
  622. Variant get_keyed(const Variant &p_key, bool &r_valid) const;
  623. bool has_key(const Variant &p_key, bool &r_valid) const;
  624. /* Generic */
  625. enum VariantSetError {
  626. SET_OK,
  627. SET_KEYED_ERR,
  628. SET_NAMED_ERR,
  629. SET_INDEXED_ERR
  630. };
  631. enum VariantGetError {
  632. GET_OK,
  633. GET_KEYED_ERR,
  634. GET_NAMED_ERR,
  635. GET_INDEXED_ERR
  636. };
  637. void set(const Variant &p_index, const Variant &p_value, bool *r_valid = nullptr, VariantSetError *err_code = nullptr);
  638. Variant get(const Variant &p_index, bool *r_valid = nullptr, VariantGetError *err_code = nullptr) const;
  639. bool in(const Variant &p_index, bool *r_valid = nullptr) const;
  640. bool iter_init(Variant &r_iter, bool &r_valid) const;
  641. bool iter_next(Variant &r_iter, bool &r_valid) const;
  642. Variant iter_get(const Variant &r_iter, bool &r_valid) const;
  643. void get_property_list(List<PropertyInfo> *p_list) const;
  644. static void call_utility_function(const StringName &p_name, Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  645. static bool has_utility_function(const StringName &p_name);
  646. typedef void (*ValidatedUtilityFunction)(Variant *r_ret, const Variant **p_args, int p_argcount);
  647. typedef void (*PTRUtilityFunction)(void *r_ret, const void **p_args, int p_argcount);
  648. static ValidatedUtilityFunction get_validated_utility_function(const StringName &p_name);
  649. static PTRUtilityFunction get_ptr_utility_function(const StringName &p_name);
  650. enum UtilityFunctionType {
  651. UTILITY_FUNC_TYPE_MATH,
  652. UTILITY_FUNC_TYPE_RANDOM,
  653. UTILITY_FUNC_TYPE_GENERAL,
  654. };
  655. static UtilityFunctionType get_utility_function_type(const StringName &p_name);
  656. static MethodInfo get_utility_function_info(const StringName &p_name);
  657. static int get_utility_function_argument_count(const StringName &p_name);
  658. static Variant::Type get_utility_function_argument_type(const StringName &p_name, int p_arg);
  659. static String get_utility_function_argument_name(const StringName &p_name, int p_arg);
  660. static bool has_utility_function_return_value(const StringName &p_name);
  661. static Variant::Type get_utility_function_return_type(const StringName &p_name);
  662. static bool is_utility_function_vararg(const StringName &p_name);
  663. static uint32_t get_utility_function_hash(const StringName &p_name);
  664. static void get_utility_function_list(List<StringName> *r_functions);
  665. static int get_utility_function_count();
  666. //argsVariant call()
  667. bool operator==(const Variant &p_variant) const;
  668. bool operator!=(const Variant &p_variant) const;
  669. bool operator<(const Variant &p_variant) const;
  670. uint32_t hash() const;
  671. uint32_t recursive_hash(int recursion_count) const;
  672. // By default, performs a semantic comparison. Otherwise, numeric/binary comparison (if appropriate).
  673. bool hash_compare(const Variant &p_variant, int recursion_count = 0, bool semantic_comparison = true) const;
  674. bool identity_compare(const Variant &p_variant) const;
  675. bool booleanize() const;
  676. String stringify(int recursion_count = 0) const;
  677. String to_json_string() const;
  678. static void get_constants_for_type(Variant::Type p_type, List<StringName> *p_constants);
  679. static int get_constants_count_for_type(Variant::Type p_type);
  680. static bool has_constant(Variant::Type p_type, const StringName &p_value);
  681. static Variant get_constant_value(Variant::Type p_type, const StringName &p_value, bool *r_valid = nullptr);
  682. static void get_enums_for_type(Variant::Type p_type, List<StringName> *p_enums);
  683. static void get_enumerations_for_enum(Variant::Type p_type, const StringName &p_enum_name, List<StringName> *p_enumerations);
  684. static int get_enum_value(Variant::Type p_type, const StringName &p_enum_name, const StringName &p_enumeration, bool *r_valid = nullptr);
  685. static bool has_enum(Variant::Type p_type, const StringName &p_enum_name);
  686. static StringName get_enum_for_enumeration(Variant::Type p_type, const StringName &p_enumeration);
  687. typedef String (*ObjectDeConstruct)(const Variant &p_object, void *ud);
  688. typedef void (*ObjectConstruct)(const String &p_text, void *ud, Variant &r_value);
  689. String get_construct_string() const;
  690. static void construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct = nullptr, void *p_construct_ud = nullptr);
  691. void operator=(const Variant &p_variant); // only this is enough for all the other types
  692. static void register_types();
  693. static void unregister_types();
  694. Variant(const Variant &p_variant);
  695. _FORCE_INLINE_ Variant() {}
  696. _FORCE_INLINE_ ~Variant() {
  697. clear();
  698. }
  699. };
  700. //typedef Dictionary Dictionary; no
  701. //typedef Array Array;
  702. template <typename... VarArgs>
  703. Vector<Variant> varray(VarArgs... p_args) {
  704. Vector<Variant> v;
  705. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  706. uint32_t argc = sizeof...(p_args);
  707. if (argc > 0) {
  708. v.resize(argc);
  709. Variant *vw = v.ptrw();
  710. for (uint32_t i = 0; i < argc; i++) {
  711. vw[i] = args[i];
  712. }
  713. }
  714. return v;
  715. }
  716. struct VariantHasher {
  717. static _FORCE_INLINE_ uint32_t hash(const Variant &p_variant) { return p_variant.hash(); }
  718. };
  719. struct VariantComparator {
  720. static _FORCE_INLINE_ bool compare(const Variant &p_lhs, const Variant &p_rhs) { return p_lhs.hash_compare(p_rhs); }
  721. };
  722. struct StringLikeVariantComparator {
  723. static bool compare(const Variant &p_lhs, const Variant &p_rhs);
  724. };
  725. struct StringLikeVariantOrder {
  726. static _ALWAYS_INLINE_ bool compare(const Variant &p_lhs, const Variant &p_rhs) {
  727. if (p_lhs.is_string() && p_rhs.is_string()) {
  728. return p_lhs.operator String() < p_rhs.operator String();
  729. }
  730. return p_lhs < p_rhs;
  731. }
  732. _ALWAYS_INLINE_ bool operator()(const Variant &p_lhs, const Variant &p_rhs) const {
  733. return compare(p_lhs, p_rhs);
  734. }
  735. };
  736. Variant::ObjData &Variant::_get_obj() {
  737. return *reinterpret_cast<ObjData *>(&_data._mem[0]);
  738. }
  739. const Variant::ObjData &Variant::_get_obj() const {
  740. return *reinterpret_cast<const ObjData *>(&_data._mem[0]);
  741. }
  742. template <typename... VarArgs>
  743. String vformat(const String &p_text, const VarArgs... p_args) {
  744. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  745. Array args_array;
  746. args_array.resize(sizeof...(p_args));
  747. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  748. args_array[i] = args[i];
  749. }
  750. bool error = false;
  751. String fmt = p_text.sprintf(args_array, &error);
  752. ERR_FAIL_COND_V_MSG(error, String(), String("Formatting error in string \"") + p_text + "\": " + fmt + ".");
  753. return fmt;
  754. }
  755. template <typename... VarArgs>
  756. Variant Callable::call(VarArgs... p_args) const {
  757. Variant args[sizeof...(p_args) + 1] = { p_args..., 0 }; // +1 makes sure zero sized arrays are also supported.
  758. const Variant *argptrs[sizeof...(p_args) + 1];
  759. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  760. argptrs[i] = &args[i];
  761. }
  762. Variant ret;
  763. CallError ce;
  764. callp(sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args), ret, ce);
  765. return ret;
  766. }
  767. template <typename... VarArgs>
  768. Callable Callable::bind(VarArgs... p_args) const {
  769. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  770. const Variant *argptrs[sizeof...(p_args) + 1];
  771. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  772. argptrs[i] = &args[i];
  773. }
  774. return bindp(sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  775. }
  776. Variant &Array::Iterator::operator*() const {
  777. if (unlikely(read_only)) {
  778. *read_only = *element_ptr;
  779. return *read_only;
  780. }
  781. return *element_ptr;
  782. }
  783. Variant *Array::Iterator::operator->() const {
  784. if (unlikely(read_only)) {
  785. *read_only = *element_ptr;
  786. return read_only;
  787. }
  788. return element_ptr;
  789. }
  790. Array::Iterator &Array::Iterator::operator++() {
  791. element_ptr++;
  792. return *this;
  793. }
  794. Array::Iterator &Array::Iterator::operator--() {
  795. element_ptr--;
  796. return *this;
  797. }
  798. const Variant &Array::ConstIterator::operator*() const {
  799. if (unlikely(read_only)) {
  800. *read_only = *element_ptr;
  801. return *read_only;
  802. }
  803. return *element_ptr;
  804. }
  805. const Variant *Array::ConstIterator::operator->() const {
  806. if (unlikely(read_only)) {
  807. *read_only = *element_ptr;
  808. return read_only;
  809. }
  810. return element_ptr;
  811. }
  812. Array::ConstIterator &Array::ConstIterator::operator++() {
  813. element_ptr++;
  814. return *this;
  815. }
  816. Array::ConstIterator &Array::ConstIterator::operator--() {
  817. element_ptr--;
  818. return *this;
  819. }
  820. #endif // VARIANT_H