variant.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*************************************************************************/
  2. /* variant.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #ifndef VARIANT_H
  31. #define VARIANT_H
  32. #include "core/io/ip_address.h"
  33. #include "core/math/aabb.h"
  34. #include "core/math/basis.h"
  35. #include "core/math/color.h"
  36. #include "core/math/face3.h"
  37. #include "core/math/plane.h"
  38. #include "core/math/quat.h"
  39. #include "core/math/transform.h"
  40. #include "core/math/transform_2d.h"
  41. #include "core/math/vector3.h"
  42. #include "core/math/vector3i.h"
  43. #include "core/object/object_id.h"
  44. #include "core/string/node_path.h"
  45. #include "core/string/ustring.h"
  46. #include "core/templates/rid.h"
  47. #include "core/variant/array.h"
  48. #include "core/variant/callable.h"
  49. #include "core/variant/dictionary.h"
  50. class Object;
  51. class Node; // helper
  52. class Control; // helper
  53. struct PropertyInfo;
  54. struct MethodInfo;
  55. typedef Vector<uint8_t> PackedByteArray;
  56. typedef Vector<int32_t> PackedInt32Array;
  57. typedef Vector<int64_t> PackedInt64Array;
  58. typedef Vector<float> PackedFloat32Array;
  59. typedef Vector<double> PackedFloat64Array;
  60. typedef Vector<String> PackedStringArray;
  61. typedef Vector<Vector2> PackedVector2Array;
  62. typedef Vector<Vector3> PackedVector3Array;
  63. typedef Vector<Color> PackedColorArray;
  64. class Variant {
  65. public:
  66. // If this changes the table in variant_op must be updated
  67. enum Type {
  68. NIL,
  69. // atomic types
  70. BOOL,
  71. INT,
  72. FLOAT,
  73. STRING,
  74. // math types
  75. VECTOR2,
  76. VECTOR2I,
  77. RECT2,
  78. RECT2I,
  79. VECTOR3,
  80. VECTOR3I,
  81. TRANSFORM2D,
  82. PLANE,
  83. QUAT,
  84. AABB,
  85. BASIS,
  86. TRANSFORM,
  87. // misc types
  88. COLOR,
  89. STRING_NAME,
  90. NODE_PATH,
  91. RID,
  92. OBJECT,
  93. CALLABLE,
  94. SIGNAL,
  95. DICTIONARY,
  96. ARRAY,
  97. // typed arrays
  98. PACKED_BYTE_ARRAY,
  99. PACKED_INT32_ARRAY,
  100. PACKED_INT64_ARRAY,
  101. PACKED_FLOAT32_ARRAY,
  102. PACKED_FLOAT64_ARRAY,
  103. PACKED_STRING_ARRAY,
  104. PACKED_VECTOR2_ARRAY,
  105. PACKED_VECTOR3_ARRAY,
  106. PACKED_COLOR_ARRAY,
  107. VARIANT_MAX
  108. };
  109. private:
  110. friend struct _VariantCall;
  111. friend class VariantInternal;
  112. // Variant takes 20 bytes when real_t is float, and 36 if double
  113. // it only allocates extra memory for aabb/matrix.
  114. Type type = NIL;
  115. struct ObjData {
  116. ObjectID id;
  117. Object *obj = nullptr;
  118. };
  119. /* array helpers */
  120. struct PackedArrayRefBase {
  121. SafeRefCount refcount;
  122. _FORCE_INLINE_ PackedArrayRefBase *reference() {
  123. if (this->refcount.ref()) {
  124. return this;
  125. } else {
  126. return nullptr;
  127. }
  128. }
  129. static _FORCE_INLINE_ PackedArrayRefBase *reference_from(PackedArrayRefBase *p_base, PackedArrayRefBase *p_from) {
  130. if (p_base == p_from) {
  131. return p_base; //same thing, do nothing
  132. }
  133. if (p_from->reference()) {
  134. if (p_base->refcount.unref()) {
  135. memdelete(p_base);
  136. }
  137. return p_from;
  138. } else {
  139. return p_base; //keep, could not reference new
  140. }
  141. }
  142. static _FORCE_INLINE_ void destroy(PackedArrayRefBase *p_array) {
  143. if (p_array->refcount.unref()) {
  144. memdelete(p_array);
  145. }
  146. }
  147. _FORCE_INLINE_ virtual ~PackedArrayRefBase() {} //needs virtual destructor, but make inline
  148. };
  149. template <class T>
  150. struct PackedArrayRef : public PackedArrayRefBase {
  151. Vector<T> array;
  152. static _FORCE_INLINE_ PackedArrayRef<T> *create() {
  153. return memnew(PackedArrayRef<T>);
  154. }
  155. static _FORCE_INLINE_ PackedArrayRef<T> *create(const Vector<T> &p_from) {
  156. return memnew(PackedArrayRef<T>(p_from));
  157. }
  158. static _FORCE_INLINE_ const Vector<T> &get_array(PackedArrayRefBase *p_base) {
  159. return static_cast<PackedArrayRef<T> *>(p_base)->array;
  160. }
  161. static _FORCE_INLINE_ Vector<T> *get_array_ptr(const PackedArrayRefBase *p_base) {
  162. return &const_cast<PackedArrayRef<T> *>(static_cast<const PackedArrayRef<T> *>(p_base))->array;
  163. }
  164. _FORCE_INLINE_ PackedArrayRef(const Vector<T> &p_from) {
  165. array = p_from;
  166. refcount.init();
  167. }
  168. _FORCE_INLINE_ PackedArrayRef() {
  169. refcount.init();
  170. }
  171. };
  172. /* end of array helpers */
  173. _ALWAYS_INLINE_ ObjData &_get_obj();
  174. _ALWAYS_INLINE_ const ObjData &_get_obj() const;
  175. union {
  176. bool _bool;
  177. int64_t _int;
  178. double _float;
  179. Transform2D *_transform2d;
  180. ::AABB *_aabb;
  181. Basis *_basis;
  182. Transform *_transform;
  183. PackedArrayRefBase *packed_array;
  184. void *_ptr; //generic pointer
  185. uint8_t _mem[sizeof(ObjData) > (sizeof(real_t) * 4) ? sizeof(ObjData) : (sizeof(real_t) * 4)];
  186. } _data alignas(8);
  187. void reference(const Variant &p_variant);
  188. void _clear_internal();
  189. _FORCE_INLINE_ void clear() {
  190. static const bool needs_deinit[Variant::VARIANT_MAX] = {
  191. false, //NIL,
  192. false, //BOOL,
  193. false, //INT,
  194. false, //FLOAT,
  195. true, //STRING,
  196. false, //VECTOR2,
  197. false, //VECTOR2I,
  198. false, //RECT2,
  199. false, //RECT2I,
  200. false, //VECTOR3,
  201. false, //VECTOR3I,
  202. true, //TRANSFORM2D,
  203. false, //PLANE,
  204. false, //QUAT,
  205. true, //AABB,
  206. true, //BASIS,
  207. true, //TRANSFORM,
  208. // misc types
  209. false, //COLOR,
  210. true, //STRING_NAME,
  211. true, //NODE_PATH,
  212. false, //RID,
  213. true, //OBJECT,
  214. true, //CALLABLE,
  215. true, //SIGNAL,
  216. true, //DICTIONARY,
  217. true, //ARRAY,
  218. // typed arrays
  219. true, //PACKED_BYTE_ARRAY,
  220. true, //PACKED_INT32_ARRAY,
  221. true, //PACKED_INT64_ARRAY,
  222. true, //PACKED_FLOAT32_ARRAY,
  223. true, //PACKED_FLOAT64_ARRAY,
  224. true, //PACKED_STRING_ARRAY,
  225. true, //PACKED_VECTOR2_ARRAY,
  226. true, //PACKED_VECTOR3_ARRAY,
  227. true, //PACKED_COLOR_ARRAY,
  228. };
  229. if (unlikely(needs_deinit[type])) { //make it fast for types that dont need deinit
  230. _clear_internal();
  231. }
  232. type = NIL;
  233. }
  234. static void _register_variant_operators();
  235. static void _unregister_variant_operators();
  236. static void _register_variant_methods();
  237. static void _unregister_variant_methods();
  238. static void _register_variant_setters_getters();
  239. static void _unregister_variant_setters_getters();
  240. static void _register_variant_constructors();
  241. static void _unregister_variant_constructors();
  242. static void _register_variant_utility_functions();
  243. static void _unregister_variant_utility_functions();
  244. public:
  245. _FORCE_INLINE_ Type get_type() const {
  246. return type;
  247. }
  248. static String get_type_name(Variant::Type p_type);
  249. static bool can_convert(Type p_type_from, Type p_type_to);
  250. static bool can_convert_strict(Type p_type_from, Type p_type_to);
  251. bool is_ref() const;
  252. _FORCE_INLINE_ bool is_num() const {
  253. return type == INT || type == FLOAT;
  254. }
  255. _FORCE_INLINE_ bool is_array() const {
  256. return type >= ARRAY;
  257. }
  258. bool is_shared() const;
  259. bool is_zero() const;
  260. bool is_one() const;
  261. bool is_null() const;
  262. operator bool() const;
  263. operator signed int() const;
  264. operator unsigned int() const; // this is the real one
  265. operator signed short() const;
  266. operator unsigned short() const;
  267. operator signed char() const;
  268. operator unsigned char() const;
  269. //operator long unsigned int() const;
  270. operator int64_t() const;
  271. operator uint64_t() const;
  272. #ifdef NEED_LONG_INT
  273. operator signed long() const;
  274. operator unsigned long() const;
  275. #endif
  276. operator ObjectID() const;
  277. operator char32_t() const;
  278. operator float() const;
  279. operator double() const;
  280. operator String() const;
  281. operator StringName() const;
  282. operator Vector2() const;
  283. operator Vector2i() const;
  284. operator Rect2() const;
  285. operator Rect2i() const;
  286. operator Vector3() const;
  287. operator Vector3i() const;
  288. operator Plane() const;
  289. operator ::AABB() const;
  290. operator Quat() const;
  291. operator Basis() const;
  292. operator Transform() const;
  293. operator Transform2D() const;
  294. operator Color() const;
  295. operator NodePath() const;
  296. operator ::RID() const;
  297. operator Object *() const;
  298. operator Node *() const;
  299. operator Control *() const;
  300. operator Callable() const;
  301. operator Signal() const;
  302. operator Dictionary() const;
  303. operator Array() const;
  304. operator Vector<uint8_t>() const;
  305. operator Vector<int32_t>() const;
  306. operator Vector<int64_t>() const;
  307. operator Vector<float>() const;
  308. operator Vector<double>() const;
  309. operator Vector<String>() const;
  310. operator Vector<Vector3>() const;
  311. operator Vector<Color>() const;
  312. operator Vector<Plane>() const;
  313. operator Vector<Face3>() const;
  314. operator Vector<Variant>() const;
  315. operator Vector<StringName>() const;
  316. operator Vector<::RID>() const;
  317. operator Vector<Vector2>() const;
  318. // some core type enums to convert to
  319. operator Side() const;
  320. operator Orientation() const;
  321. operator IP_Address() const;
  322. Object *get_validated_object() const;
  323. Object *get_validated_object_with_check(bool &r_previously_freed) const;
  324. Variant(bool p_bool);
  325. Variant(signed int p_int); // real one
  326. Variant(unsigned int p_int);
  327. #ifdef NEED_LONG_INT
  328. Variant(signed long p_long); // real one
  329. Variant(unsigned long p_long);
  330. #endif
  331. Variant(signed short p_short); // real one
  332. Variant(unsigned short p_short);
  333. Variant(signed char p_char); // real one
  334. Variant(unsigned char p_char);
  335. Variant(int64_t p_int); // real one
  336. Variant(uint64_t p_int);
  337. Variant(float p_float);
  338. Variant(double p_double);
  339. Variant(const ObjectID &p_id);
  340. Variant(const String &p_string);
  341. Variant(const StringName &p_string);
  342. Variant(const char *const p_cstring);
  343. Variant(const char32_t *p_wstring);
  344. Variant(const Vector2 &p_vector2);
  345. Variant(const Vector2i &p_vector2i);
  346. Variant(const Rect2 &p_rect2);
  347. Variant(const Rect2i &p_rect2i);
  348. Variant(const Vector3 &p_vector3);
  349. Variant(const Vector3i &p_vector3i);
  350. Variant(const Plane &p_plane);
  351. Variant(const ::AABB &p_aabb);
  352. Variant(const Quat &p_quat);
  353. Variant(const Basis &p_matrix);
  354. Variant(const Transform2D &p_transform);
  355. Variant(const Transform &p_transform);
  356. Variant(const Color &p_color);
  357. Variant(const NodePath &p_node_path);
  358. Variant(const ::RID &p_rid);
  359. Variant(const Object *p_object);
  360. Variant(const Callable &p_callable);
  361. Variant(const Signal &p_signal);
  362. Variant(const Dictionary &p_dictionary);
  363. Variant(const Array &p_array);
  364. Variant(const Vector<Plane> &p_array); // helper
  365. Variant(const Vector<uint8_t> &p_byte_array);
  366. Variant(const Vector<int32_t> &p_int32_array);
  367. Variant(const Vector<int64_t> &p_int64_array);
  368. Variant(const Vector<float> &p_float32_array);
  369. Variant(const Vector<double> &p_float64_array);
  370. Variant(const Vector<String> &p_string_array);
  371. Variant(const Vector<Vector3> &p_vector3_array);
  372. Variant(const Vector<Color> &p_color_array);
  373. Variant(const Vector<Face3> &p_face_array);
  374. Variant(const Vector<Variant> &p_array);
  375. Variant(const Vector<StringName> &p_array);
  376. Variant(const Vector<::RID> &p_array); // helper
  377. Variant(const Vector<Vector2> &p_array); // helper
  378. Variant(const IP_Address &p_address);
  379. // If this changes the table in variant_op must be updated
  380. enum Operator {
  381. //comparison
  382. OP_EQUAL,
  383. OP_NOT_EQUAL,
  384. OP_LESS,
  385. OP_LESS_EQUAL,
  386. OP_GREATER,
  387. OP_GREATER_EQUAL,
  388. //mathematic
  389. OP_ADD,
  390. OP_SUBTRACT,
  391. OP_MULTIPLY,
  392. OP_DIVIDE,
  393. OP_NEGATE,
  394. OP_POSITIVE,
  395. OP_MODULE,
  396. //bitwise
  397. OP_SHIFT_LEFT,
  398. OP_SHIFT_RIGHT,
  399. OP_BIT_AND,
  400. OP_BIT_OR,
  401. OP_BIT_XOR,
  402. OP_BIT_NEGATE,
  403. //logic
  404. OP_AND,
  405. OP_OR,
  406. OP_XOR,
  407. OP_NOT,
  408. //containment
  409. OP_IN,
  410. OP_MAX
  411. };
  412. static String get_operator_name(Operator p_op);
  413. static void evaluate(const Operator &p_op, const Variant &p_a, const Variant &p_b, Variant &r_ret, bool &r_valid);
  414. static _FORCE_INLINE_ Variant evaluate(const Operator &p_op, const Variant &p_a, const Variant &p_b) {
  415. bool valid = true;
  416. Variant res;
  417. evaluate(p_op, p_a, p_b, res, valid);
  418. return res;
  419. }
  420. static Variant::Type get_operator_return_type(Operator p_operator, Type p_type_a, Type p_type_b);
  421. typedef void (*ValidatedOperatorEvaluator)(const Variant *left, const Variant *right, Variant *r_ret);
  422. static ValidatedOperatorEvaluator get_validated_operator_evaluator(Operator p_operator, Type p_type_a, Type p_type_b);
  423. typedef void (*PTROperatorEvaluator)(const void *left, const void *right, void *r_ret);
  424. static PTROperatorEvaluator get_ptr_operator_evaluator(Operator p_operator, Type p_type_a, Type p_type_b);
  425. void zero();
  426. Variant duplicate(bool deep = false) const;
  427. static void blend(const Variant &a, const Variant &b, float c, Variant &r_dst);
  428. static void interpolate(const Variant &a, const Variant &b, float c, Variant &r_dst);
  429. /* Built-In Methods */
  430. typedef void (*ValidatedBuiltInMethod)(Variant *base, const Variant **p_args, int p_argcount, Variant *r_ret);
  431. typedef void (*PTRBuiltInMethod)(void *p_base, const void **p_args, void *r_ret, int p_argcount);
  432. static bool has_builtin_method(Variant::Type p_type, const StringName &p_method);
  433. static ValidatedBuiltInMethod get_validated_builtin_method(Variant::Type p_type, const StringName &p_method);
  434. static PTRBuiltInMethod get_ptr_builtin_method(Variant::Type p_type, const StringName &p_method);
  435. static int get_builtin_method_argument_count(Variant::Type p_type, const StringName &p_method);
  436. static Variant::Type get_builtin_method_argument_type(Variant::Type p_type, const StringName &p_method, int p_argument);
  437. static String get_builtin_method_argument_name(Variant::Type p_type, const StringName &p_method, int p_argument);
  438. static Vector<Variant> get_builtin_method_default_arguments(Variant::Type p_type, const StringName &p_method);
  439. static bool has_builtin_method_return_value(Variant::Type p_type, const StringName &p_method);
  440. static Variant::Type get_builtin_method_return_type(Variant::Type p_type, const StringName &p_method);
  441. static bool is_builtin_method_const(Variant::Type p_type, const StringName &p_method);
  442. static bool is_builtin_method_vararg(Variant::Type p_type, const StringName &p_method);
  443. static void get_builtin_method_list(Variant::Type p_type, List<StringName> *p_list);
  444. static int get_builtin_method_count(Variant::Type p_type);
  445. void call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error);
  446. Variant call(const StringName &p_method, const Variant &p_arg1 = Variant(), const Variant &p_arg2 = Variant(), const Variant &p_arg3 = Variant(), const Variant &p_arg4 = Variant(), const Variant &p_arg5 = Variant());
  447. static String get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);
  448. static String get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);
  449. static String get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);
  450. //dynamic (includes Object)
  451. void get_method_list(List<MethodInfo> *p_list) const;
  452. bool has_method(const StringName &p_method) const;
  453. /* Constructors */
  454. typedef void (*ValidatedConstructor)(Variant *r_base, const Variant **p_args);
  455. typedef void (*PTRConstructor)(void *base, const void **p_args);
  456. static int get_constructor_count(Variant::Type p_type);
  457. static ValidatedConstructor get_validated_constructor(Variant::Type p_type, int p_constructor);
  458. static PTRConstructor get_ptr_constructor(Variant::Type p_type, int p_constructor);
  459. static int get_constructor_argument_count(Variant::Type p_type, int p_constructor);
  460. static Variant::Type get_constructor_argument_type(Variant::Type p_type, int p_constructor, int p_argument);
  461. static String get_constructor_argument_name(Variant::Type p_type, int p_constructor, int p_argument);
  462. static void construct(Variant::Type, Variant &base, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  463. static void get_constructor_list(Type p_type, List<MethodInfo> *r_list); //convenience
  464. /* Properties */
  465. void set_named(const StringName &p_member, const Variant &p_value, bool &r_valid);
  466. Variant get_named(const StringName &p_member, bool &r_valid) const;
  467. typedef void (*ValidatedSetter)(Variant *base, const Variant *value);
  468. typedef void (*ValidatedGetter)(const Variant *base, Variant *value);
  469. static bool has_member(Variant::Type p_type, const StringName &p_member);
  470. static Variant::Type get_member_type(Variant::Type p_type, const StringName &p_member);
  471. static void get_member_list(Type p_type, List<StringName> *r_members);
  472. static int get_member_count(Type p_type);
  473. static ValidatedSetter get_member_validated_setter(Variant::Type p_type, const StringName &p_member);
  474. static ValidatedGetter get_member_validated_getter(Variant::Type p_type, const StringName &p_member);
  475. typedef void (*PTRSetter)(void *base, const void *value);
  476. typedef void (*PTRGetter)(const void *base, void *value);
  477. static PTRSetter get_member_ptr_setter(Variant::Type p_type, const StringName &p_member);
  478. static PTRGetter get_member_ptr_getter(Variant::Type p_type, const StringName &p_member);
  479. /* Indexing */
  480. static bool has_indexing(Variant::Type p_type);
  481. static Variant::Type get_indexed_element_type(Variant::Type p_type);
  482. typedef void (*ValidatedIndexedSetter)(Variant *base, int64_t index, const Variant *value, bool *oob);
  483. typedef void (*ValidatedIndexedGetter)(const Variant *base, int64_t index, Variant *value, bool *oob);
  484. static ValidatedIndexedSetter get_member_validated_indexed_setter(Variant::Type p_type);
  485. static ValidatedIndexedGetter get_member_validated_indexed_getter(Variant::Type p_type);
  486. typedef void (*PTRIndexedSetter)(void *base, int64_t index, const void *value);
  487. typedef void (*PTRIndexedGetter)(const void *base, int64_t index, void *value);
  488. static PTRIndexedSetter get_member_ptr_indexed_setter(Variant::Type p_type);
  489. static PTRIndexedGetter get_member_ptr_indexed_getter(Variant::Type p_type);
  490. void set_indexed(int64_t p_index, const Variant &p_value, bool &r_valid, bool &r_oob);
  491. Variant get_indexed(int64_t p_index, bool &r_valid, bool &r_oob) const;
  492. uint64_t get_indexed_size() const;
  493. /* Keying */
  494. static bool is_keyed(Variant::Type p_type);
  495. typedef void (*ValidatedKeyedSetter)(Variant *base, const Variant *key, const Variant *value, bool *valid);
  496. typedef void (*ValidatedKeyedGetter)(const Variant *base, const Variant *key, Variant *value, bool *valid);
  497. typedef bool (*ValidatedKeyedChecker)(const Variant *base, const Variant *key, bool *valid);
  498. static ValidatedKeyedSetter get_member_validated_keyed_setter(Variant::Type p_type);
  499. static ValidatedKeyedGetter get_member_validated_keyed_getter(Variant::Type p_type);
  500. static ValidatedKeyedChecker get_member_validated_keyed_checker(Variant::Type p_type);
  501. typedef void (*PTRKeyedSetter)(void *base, const void *key, const void *value);
  502. typedef void (*PTRKeyedGetter)(const void *base, const void *key, void *value);
  503. typedef bool (*PTRKeyedChecker)(const void *base, const void *key);
  504. static PTRKeyedSetter get_member_ptr_keyed_setter(Variant::Type p_type);
  505. static PTRKeyedGetter get_member_ptr_keyed_getter(Variant::Type p_type);
  506. static PTRKeyedChecker get_member_ptr_keyed_checker(Variant::Type p_type);
  507. void set_keyed(const Variant &p_key, const Variant &p_value, bool &r_valid);
  508. Variant get_keyed(const Variant &p_key, bool &r_valid) const;
  509. bool has_key(const Variant &p_key, bool &r_valid) const;
  510. /* Generic */
  511. void set(const Variant &p_index, const Variant &p_value, bool *r_valid = nullptr);
  512. Variant get(const Variant &p_index, bool *r_valid = nullptr) const;
  513. bool in(const Variant &p_index, bool *r_valid = nullptr) const;
  514. bool iter_init(Variant &r_iter, bool &r_valid) const;
  515. bool iter_next(Variant &r_iter, bool &r_valid) const;
  516. Variant iter_get(const Variant &r_iter, bool &r_valid) const;
  517. void get_property_list(List<PropertyInfo> *p_list) const;
  518. static void call_utility_function(const StringName &p_name, Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  519. static bool has_utility_function(const StringName &p_name);
  520. typedef void (*ValidatedUtilityFunction)(Variant *r_ret, const Variant **p_args, int p_argcount);
  521. typedef void (*PTRUtilityFunction)(void *r_ret, const void **p_args, int p_argcount);
  522. static ValidatedUtilityFunction get_validated_utility_function(const StringName &p_name);
  523. static PTRUtilityFunction get_ptr_utility_function(const StringName &p_name);
  524. enum UtilityFunctionType {
  525. UTILITY_FUNC_TYPE_MATH,
  526. UTILITY_FUNC_TYPE_RANDOM,
  527. UTILITY_FUNC_TYPE_GENERAL,
  528. };
  529. static UtilityFunctionType get_utility_function_type(const StringName &p_name);
  530. static int get_utility_function_argument_count(const StringName &p_name);
  531. static Variant::Type get_utility_function_argument_type(const StringName &p_name, int p_arg);
  532. static String get_utility_function_argument_name(const StringName &p_name, int p_arg);
  533. static bool has_utility_function_return_value(const StringName &p_name);
  534. static Variant::Type get_utility_function_return_type(const StringName &p_name);
  535. static bool is_utility_function_vararg(const StringName &p_name);
  536. static void get_utility_function_list(List<StringName> *r_functions);
  537. static int get_utility_function_count();
  538. //argsVariant call()
  539. bool operator==(const Variant &p_variant) const;
  540. bool operator!=(const Variant &p_variant) const;
  541. bool operator<(const Variant &p_variant) const;
  542. uint32_t hash() const;
  543. bool hash_compare(const Variant &p_variant) const;
  544. bool booleanize() const;
  545. String stringify(List<const void *> &stack) const;
  546. void static_assign(const Variant &p_variant);
  547. static void get_constants_for_type(Variant::Type p_type, List<StringName> *p_constants);
  548. static int get_constants_count_for_type(Variant::Type p_type);
  549. static bool has_constant(Variant::Type p_type, const StringName &p_value);
  550. static Variant get_constant_value(Variant::Type p_type, const StringName &p_value, bool *r_valid = nullptr);
  551. typedef String (*ObjectDeConstruct)(const Variant &p_object, void *ud);
  552. typedef void (*ObjectConstruct)(const String &p_text, void *ud, Variant &r_value);
  553. String get_construct_string() const;
  554. static void construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct = nullptr, void *p_construct_ud = nullptr);
  555. void operator=(const Variant &p_variant); // only this is enough for all the other types
  556. static void register_types();
  557. static void unregister_types();
  558. Variant(const Variant &p_variant);
  559. _FORCE_INLINE_ Variant() {}
  560. _FORCE_INLINE_ ~Variant() {
  561. clear();
  562. }
  563. };
  564. //typedef Dictionary Dictionary; no
  565. //typedef Array Array;
  566. Vector<Variant> varray();
  567. Vector<Variant> varray(const Variant &p_arg1);
  568. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2);
  569. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3);
  570. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4);
  571. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5);
  572. struct VariantHasher {
  573. static _FORCE_INLINE_ uint32_t hash(const Variant &p_variant) { return p_variant.hash(); }
  574. };
  575. struct VariantComparator {
  576. static _FORCE_INLINE_ bool compare(const Variant &p_lhs, const Variant &p_rhs) { return p_lhs.hash_compare(p_rhs); }
  577. };
  578. Variant::ObjData &Variant::_get_obj() {
  579. return *reinterpret_cast<ObjData *>(&_data._mem[0]);
  580. }
  581. const Variant::ObjData &Variant::_get_obj() const {
  582. return *reinterpret_cast<const ObjData *>(&_data._mem[0]);
  583. }
  584. String vformat(const String &p_text, const Variant &p1 = Variant(), const Variant &p2 = Variant(), const Variant &p3 = Variant(), const Variant &p4 = Variant(), const Variant &p5 = Variant());
  585. #endif // VARIANT_H