variant.h 26 KB

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