variant.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /*************************************************************************/
  2. /* variant.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef VARIANT_H
  31. #define VARIANT_H
  32. #include "core/input/input_enums.h"
  33. #include "core/io/ip_address.h"
  34. #include "core/math/aabb.h"
  35. #include "core/math/basis.h"
  36. #include "core/math/color.h"
  37. #include "core/math/face3.h"
  38. #include "core/math/plane.h"
  39. #include "core/math/quaternion.h"
  40. #include "core/math/rect2.h"
  41. #include "core/math/rect2i.h"
  42. #include "core/math/transform_2d.h"
  43. #include "core/math/transform_3d.h"
  44. #include "core/math/vector2.h"
  45. #include "core/math/vector2i.h"
  46. #include "core/math/vector3.h"
  47. #include "core/math/vector3i.h"
  48. #include "core/object/object_id.h"
  49. #include "core/os/keyboard.h"
  50. #include "core/string/node_path.h"
  51. #include "core/string/ustring.h"
  52. #include "core/templates/rid.h"
  53. #include "core/variant/array.h"
  54. #include "core/variant/callable.h"
  55. #include "core/variant/dictionary.h"
  56. class Object;
  57. struct PropertyInfo;
  58. struct MethodInfo;
  59. typedef Vector<uint8_t> PackedByteArray;
  60. typedef Vector<int32_t> PackedInt32Array;
  61. typedef Vector<int64_t> PackedInt64Array;
  62. typedef Vector<float> PackedFloat32Array;
  63. typedef Vector<double> PackedFloat64Array;
  64. typedef Vector<String> PackedStringArray;
  65. typedef Vector<Vector2> PackedVector2Array;
  66. typedef Vector<Vector3> PackedVector3Array;
  67. typedef Vector<Color> PackedColorArray;
  68. class Variant {
  69. public:
  70. // If this changes the table in variant_op must be updated
  71. enum Type {
  72. NIL,
  73. // atomic types
  74. BOOL,
  75. INT,
  76. FLOAT,
  77. STRING,
  78. // math types
  79. VECTOR2,
  80. VECTOR2I,
  81. RECT2,
  82. RECT2I,
  83. VECTOR3,
  84. VECTOR3I,
  85. TRANSFORM2D,
  86. PLANE,
  87. QUATERNION,
  88. AABB,
  89. BASIS,
  90. TRANSFORM3D,
  91. // misc types
  92. COLOR,
  93. STRING_NAME,
  94. NODE_PATH,
  95. RID,
  96. OBJECT,
  97. CALLABLE,
  98. SIGNAL,
  99. DICTIONARY,
  100. ARRAY,
  101. // typed arrays
  102. PACKED_BYTE_ARRAY,
  103. PACKED_INT32_ARRAY,
  104. PACKED_INT64_ARRAY,
  105. PACKED_FLOAT32_ARRAY,
  106. PACKED_FLOAT64_ARRAY,
  107. PACKED_STRING_ARRAY,
  108. PACKED_VECTOR2_ARRAY,
  109. PACKED_VECTOR3_ARRAY,
  110. PACKED_COLOR_ARRAY,
  111. VARIANT_MAX
  112. };
  113. enum {
  114. // Maximum recursion depth allowed when serializing variants.
  115. MAX_RECURSION_DEPTH = 1024,
  116. };
  117. private:
  118. friend struct _VariantCall;
  119. friend class VariantInternal;
  120. // Variant takes 20 bytes when real_t is float, and 36 if double
  121. // it only allocates extra memory for aabb/matrix.
  122. Type type = NIL;
  123. struct ObjData {
  124. ObjectID id;
  125. Object *obj = nullptr;
  126. };
  127. /* array helpers */
  128. struct PackedArrayRefBase {
  129. SafeRefCount refcount;
  130. _FORCE_INLINE_ PackedArrayRefBase *reference() {
  131. if (this->refcount.ref()) {
  132. return this;
  133. } else {
  134. return nullptr;
  135. }
  136. }
  137. static _FORCE_INLINE_ PackedArrayRefBase *reference_from(PackedArrayRefBase *p_base, PackedArrayRefBase *p_from) {
  138. if (p_base == p_from) {
  139. return p_base; //same thing, do nothing
  140. }
  141. if (p_from->reference()) {
  142. if (p_base->refcount.unref()) {
  143. memdelete(p_base);
  144. }
  145. return p_from;
  146. } else {
  147. return p_base; //keep, could not reference new
  148. }
  149. }
  150. static _FORCE_INLINE_ void destroy(PackedArrayRefBase *p_array) {
  151. if (p_array->refcount.unref()) {
  152. memdelete(p_array);
  153. }
  154. }
  155. _FORCE_INLINE_ virtual ~PackedArrayRefBase() {} //needs virtual destructor, but make inline
  156. };
  157. template <class T>
  158. struct PackedArrayRef : public PackedArrayRefBase {
  159. Vector<T> array;
  160. static _FORCE_INLINE_ PackedArrayRef<T> *create() {
  161. return memnew(PackedArrayRef<T>);
  162. }
  163. static _FORCE_INLINE_ PackedArrayRef<T> *create(const Vector<T> &p_from) {
  164. return memnew(PackedArrayRef<T>(p_from));
  165. }
  166. static _FORCE_INLINE_ const Vector<T> &get_array(PackedArrayRefBase *p_base) {
  167. return static_cast<PackedArrayRef<T> *>(p_base)->array;
  168. }
  169. static _FORCE_INLINE_ Vector<T> *get_array_ptr(const PackedArrayRefBase *p_base) {
  170. return &const_cast<PackedArrayRef<T> *>(static_cast<const PackedArrayRef<T> *>(p_base))->array;
  171. }
  172. _FORCE_INLINE_ PackedArrayRef(const Vector<T> &p_from) {
  173. array = p_from;
  174. refcount.init();
  175. }
  176. _FORCE_INLINE_ PackedArrayRef() {
  177. refcount.init();
  178. }
  179. };
  180. /* end of array helpers */
  181. _ALWAYS_INLINE_ ObjData &_get_obj();
  182. _ALWAYS_INLINE_ const ObjData &_get_obj() const;
  183. union {
  184. bool _bool;
  185. int64_t _int;
  186. double _float;
  187. Transform2D *_transform2d;
  188. ::AABB *_aabb;
  189. Basis *_basis;
  190. Transform3D *_transform3d;
  191. PackedArrayRefBase *packed_array;
  192. void *_ptr; //generic pointer
  193. uint8_t _mem[sizeof(ObjData) > (sizeof(real_t) * 4) ? sizeof(ObjData) : (sizeof(real_t) * 4)]{ 0 };
  194. } _data alignas(8);
  195. void reference(const Variant &p_variant);
  196. static bool initialize_ref(Object *p_object);
  197. void _clear_internal();
  198. _FORCE_INLINE_ void clear() {
  199. static const bool needs_deinit[Variant::VARIANT_MAX] = {
  200. false, //NIL,
  201. false, //BOOL,
  202. false, //INT,
  203. false, //FLOAT,
  204. true, //STRING,
  205. false, //VECTOR2,
  206. false, //VECTOR2I,
  207. false, //RECT2,
  208. false, //RECT2I,
  209. false, //VECTOR3,
  210. false, //VECTOR3I,
  211. true, //TRANSFORM2D,
  212. false, //PLANE,
  213. false, //QUATERNION,
  214. true, //AABB,
  215. true, //BASIS,
  216. true, //TRANSFORM,
  217. // misc types
  218. false, //COLOR,
  219. true, //STRING_NAME,
  220. true, //NODE_PATH,
  221. false, //RID,
  222. true, //OBJECT,
  223. true, //CALLABLE,
  224. true, //SIGNAL,
  225. true, //DICTIONARY,
  226. true, //ARRAY,
  227. // typed arrays
  228. true, //PACKED_BYTE_ARRAY,
  229. true, //PACKED_INT32_ARRAY,
  230. true, //PACKED_INT64_ARRAY,
  231. true, //PACKED_FLOAT32_ARRAY,
  232. true, //PACKED_FLOAT64_ARRAY,
  233. true, //PACKED_STRING_ARRAY,
  234. true, //PACKED_VECTOR2_ARRAY,
  235. true, //PACKED_VECTOR3_ARRAY,
  236. true, //PACKED_COLOR_ARRAY,
  237. };
  238. if (unlikely(needs_deinit[type])) { // Make it fast for types that don't need deinit.
  239. _clear_internal();
  240. }
  241. type = NIL;
  242. }
  243. static void _register_variant_operators();
  244. static void _unregister_variant_operators();
  245. static void _register_variant_methods();
  246. static void _unregister_variant_methods();
  247. static void _register_variant_setters_getters();
  248. static void _unregister_variant_setters_getters();
  249. static void _register_variant_constructors();
  250. static void _unregister_variant_destructors();
  251. static void _register_variant_destructors();
  252. static void _unregister_variant_constructors();
  253. static void _register_variant_utility_functions();
  254. static void _unregister_variant_utility_functions();
  255. void _variant_call_error(const String &p_method, Callable::CallError &error);
  256. // Avoid accidental conversion. If you reached this point, it's because you most likely forgot to dereference
  257. // a Variant pointer (so add * like this: *variant_pointer).
  258. Variant(const Variant *) {}
  259. Variant(const Variant **) {}
  260. public:
  261. _FORCE_INLINE_ Type get_type() const {
  262. return type;
  263. }
  264. static String get_type_name(Variant::Type p_type);
  265. static bool can_convert(Type p_type_from, Type p_type_to);
  266. static bool can_convert_strict(Type p_type_from, Type p_type_to);
  267. bool is_ref_counted() const;
  268. _FORCE_INLINE_ bool is_num() const {
  269. return type == INT || type == FLOAT;
  270. }
  271. _FORCE_INLINE_ bool is_array() const {
  272. return type >= ARRAY;
  273. }
  274. bool is_shared() const;
  275. bool is_zero() const;
  276. bool is_one() const;
  277. bool is_null() const;
  278. operator bool() const;
  279. operator signed int() const;
  280. operator unsigned int() const; // this is the real one
  281. operator signed short() const;
  282. operator unsigned short() const;
  283. operator signed char() const;
  284. operator unsigned char() const;
  285. //operator long unsigned int() const;
  286. operator int64_t() const;
  287. operator uint64_t() const;
  288. #ifdef NEED_LONG_INT
  289. operator signed long() const;
  290. operator unsigned long() const;
  291. #endif
  292. operator ObjectID() const;
  293. operator char32_t() const;
  294. operator float() const;
  295. operator double() const;
  296. operator String() const;
  297. operator StringName() const;
  298. operator Vector2() const;
  299. operator Vector2i() const;
  300. operator Rect2() const;
  301. operator Rect2i() const;
  302. operator Vector3() const;
  303. operator Vector3i() const;
  304. operator Plane() const;
  305. operator ::AABB() const;
  306. operator Quaternion() const;
  307. operator Basis() const;
  308. operator Transform2D() const;
  309. operator Transform3D() const;
  310. operator Color() const;
  311. operator NodePath() const;
  312. operator ::RID() const;
  313. operator Object *() const;
  314. operator Callable() const;
  315. operator Signal() const;
  316. operator Dictionary() const;
  317. operator Array() const;
  318. operator Vector<uint8_t>() const;
  319. operator Vector<int32_t>() const;
  320. operator Vector<int64_t>() const;
  321. operator Vector<float>() const;
  322. operator Vector<double>() const;
  323. operator Vector<String>() const;
  324. operator Vector<Vector3>() const;
  325. operator Vector<Color>() const;
  326. operator Vector<Plane>() const;
  327. operator Vector<Face3>() const;
  328. operator Vector<Variant>() const;
  329. operator Vector<StringName>() const;
  330. operator Vector<::RID>() const;
  331. operator Vector<Vector2>() const;
  332. // some core type enums to convert to
  333. operator Side() const;
  334. operator Orientation() const;
  335. operator IPAddress() const;
  336. Object *get_validated_object() const;
  337. Object *get_validated_object_with_check(bool &r_previously_freed) const;
  338. Variant(bool p_bool);
  339. Variant(signed int p_int); // real one
  340. Variant(unsigned int p_int);
  341. #ifdef NEED_LONG_INT
  342. Variant(signed long p_long); // real one
  343. Variant(unsigned long p_long);
  344. #endif
  345. Variant(signed short p_short); // real one
  346. Variant(unsigned short p_short);
  347. Variant(signed char p_char); // real one
  348. Variant(unsigned char p_char);
  349. Variant(int64_t p_int); // real one
  350. Variant(uint64_t p_int);
  351. Variant(float p_float);
  352. Variant(double p_double);
  353. Variant(const ObjectID &p_id);
  354. Variant(const String &p_string);
  355. Variant(const StringName &p_string);
  356. Variant(const char *const p_cstring);
  357. Variant(const char32_t *p_wstring);
  358. Variant(const Vector2 &p_vector2);
  359. Variant(const Vector2i &p_vector2i);
  360. Variant(const Rect2 &p_rect2);
  361. Variant(const Rect2i &p_rect2i);
  362. Variant(const Vector3 &p_vector3);
  363. Variant(const Vector3i &p_vector3i);
  364. Variant(const Plane &p_plane);
  365. Variant(const ::AABB &p_aabb);
  366. Variant(const Quaternion &p_quat);
  367. Variant(const Basis &p_matrix);
  368. Variant(const Transform2D &p_transform);
  369. Variant(const Transform3D &p_transform);
  370. Variant(const Color &p_color);
  371. Variant(const NodePath &p_node_path);
  372. Variant(const ::RID &p_rid);
  373. Variant(const Object *p_object);
  374. Variant(const Callable &p_callable);
  375. Variant(const Signal &p_signal);
  376. Variant(const Dictionary &p_dictionary);
  377. Variant(const Array &p_array);
  378. Variant(const Vector<Plane> &p_array); // helper
  379. Variant(const Vector<uint8_t> &p_byte_array);
  380. Variant(const Vector<int32_t> &p_int32_array);
  381. Variant(const Vector<int64_t> &p_int64_array);
  382. Variant(const Vector<float> &p_float32_array);
  383. Variant(const Vector<double> &p_float64_array);
  384. Variant(const Vector<String> &p_string_array);
  385. Variant(const Vector<Vector3> &p_vector3_array);
  386. Variant(const Vector<Color> &p_color_array);
  387. Variant(const Vector<Face3> &p_face_array);
  388. Variant(const Vector<Variant> &p_array);
  389. Variant(const Vector<StringName> &p_array);
  390. Variant(const Vector<::RID> &p_array); // helper
  391. Variant(const Vector<Vector2> &p_array); // helper
  392. Variant(const IPAddress &p_address);
  393. #define VARIANT_ENUM_CLASS_CONSTRUCTOR(m_enum) \
  394. Variant(const m_enum &p_value) { \
  395. type = INT; \
  396. _data._int = (int64_t)p_value; \
  397. }
  398. // Only enum classes that need to be bound need this to be defined.
  399. VARIANT_ENUM_CLASS_CONSTRUCTOR(JoyAxis)
  400. VARIANT_ENUM_CLASS_CONSTRUCTOR(JoyButton)
  401. VARIANT_ENUM_CLASS_CONSTRUCTOR(Key)
  402. VARIANT_ENUM_CLASS_CONSTRUCTOR(MIDIMessage)
  403. VARIANT_ENUM_CLASS_CONSTRUCTOR(MouseButton)
  404. #undef VARIANT_ENUM_CLASS_CONSTRUCTOR
  405. // If this changes the table in variant_op must be updated
  406. enum Operator {
  407. //comparison
  408. OP_EQUAL,
  409. OP_NOT_EQUAL,
  410. OP_LESS,
  411. OP_LESS_EQUAL,
  412. OP_GREATER,
  413. OP_GREATER_EQUAL,
  414. //mathematic
  415. OP_ADD,
  416. OP_SUBTRACT,
  417. OP_MULTIPLY,
  418. OP_DIVIDE,
  419. OP_NEGATE,
  420. OP_POSITIVE,
  421. OP_MODULE,
  422. OP_POWER,
  423. //bitwise
  424. OP_SHIFT_LEFT,
  425. OP_SHIFT_RIGHT,
  426. OP_BIT_AND,
  427. OP_BIT_OR,
  428. OP_BIT_XOR,
  429. OP_BIT_NEGATE,
  430. //logic
  431. OP_AND,
  432. OP_OR,
  433. OP_XOR,
  434. OP_NOT,
  435. //containment
  436. OP_IN,
  437. OP_MAX
  438. };
  439. static String get_operator_name(Operator p_op);
  440. static void evaluate(const Operator &p_op, const Variant &p_a, const Variant &p_b, Variant &r_ret, bool &r_valid);
  441. static _FORCE_INLINE_ Variant evaluate(const Operator &p_op, const Variant &p_a, const Variant &p_b) {
  442. bool valid = true;
  443. Variant res;
  444. evaluate(p_op, p_a, p_b, res, valid);
  445. return res;
  446. }
  447. static Variant::Type get_operator_return_type(Operator p_operator, Type p_type_a, Type p_type_b);
  448. typedef void (*ValidatedOperatorEvaluator)(const Variant *left, const Variant *right, Variant *r_ret);
  449. static ValidatedOperatorEvaluator get_validated_operator_evaluator(Operator p_operator, Type p_type_a, Type p_type_b);
  450. typedef void (*PTROperatorEvaluator)(const void *left, const void *right, void *r_ret);
  451. static PTROperatorEvaluator get_ptr_operator_evaluator(Operator p_operator, Type p_type_a, Type p_type_b);
  452. void zero();
  453. Variant duplicate(bool p_deep = false) const;
  454. Variant recursive_duplicate(bool p_deep, int recursion_count) const;
  455. static void blend(const Variant &a, const Variant &b, float c, Variant &r_dst);
  456. static void interpolate(const Variant &a, const Variant &b, float c, Variant &r_dst);
  457. static void sub(const Variant &a, const Variant &b, Variant &r_dst);
  458. /* Built-In Methods */
  459. typedef void (*ValidatedBuiltInMethod)(Variant *base, const Variant **p_args, int p_argcount, Variant *r_ret);
  460. typedef void (*PTRBuiltInMethod)(void *p_base, const void **p_args, void *r_ret, int p_argcount);
  461. static bool has_builtin_method(Variant::Type p_type, const StringName &p_method);
  462. static ValidatedBuiltInMethod get_validated_builtin_method(Variant::Type p_type, const StringName &p_method);
  463. static PTRBuiltInMethod get_ptr_builtin_method(Variant::Type p_type, const StringName &p_method);
  464. static int get_builtin_method_argument_count(Variant::Type p_type, const StringName &p_method);
  465. static Variant::Type get_builtin_method_argument_type(Variant::Type p_type, const StringName &p_method, int p_argument);
  466. static String get_builtin_method_argument_name(Variant::Type p_type, const StringName &p_method, int p_argument);
  467. static Vector<Variant> get_builtin_method_default_arguments(Variant::Type p_type, const StringName &p_method);
  468. static bool has_builtin_method_return_value(Variant::Type p_type, const StringName &p_method);
  469. static Variant::Type get_builtin_method_return_type(Variant::Type p_type, const StringName &p_method);
  470. static bool is_builtin_method_const(Variant::Type p_type, const StringName &p_method);
  471. static bool is_builtin_method_static(Variant::Type p_type, const StringName &p_method);
  472. static bool is_builtin_method_vararg(Variant::Type p_type, const StringName &p_method);
  473. static void get_builtin_method_list(Variant::Type p_type, List<StringName> *p_list);
  474. static int get_builtin_method_count(Variant::Type p_type);
  475. static uint32_t get_builtin_method_hash(Variant::Type p_type, const StringName &p_method);
  476. void callp(const StringName &p_method, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error);
  477. template <typename... VarArgs>
  478. Variant call(const StringName &p_method, VarArgs... p_args) {
  479. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  480. const Variant *argptrs[sizeof...(p_args) + 1];
  481. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  482. argptrs[i] = &args[i];
  483. }
  484. Callable::CallError cerr;
  485. Variant ret;
  486. callp(p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args), ret, cerr);
  487. if (cerr.error != Callable::CallError::CALL_OK) {
  488. _variant_call_error(p_method, cerr);
  489. }
  490. return ret;
  491. }
  492. 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);
  493. static String get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);
  494. static String get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);
  495. static String get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);
  496. //dynamic (includes Object)
  497. void get_method_list(List<MethodInfo> *p_list) const;
  498. bool has_method(const StringName &p_method) const;
  499. /* Constructors */
  500. typedef void (*ValidatedConstructor)(Variant *r_base, const Variant **p_args);
  501. typedef void (*PTRConstructor)(void *base, const void **p_args);
  502. static int get_constructor_count(Variant::Type p_type);
  503. static ValidatedConstructor get_validated_constructor(Variant::Type p_type, int p_constructor);
  504. static PTRConstructor get_ptr_constructor(Variant::Type p_type, int p_constructor);
  505. static int get_constructor_argument_count(Variant::Type p_type, int p_constructor);
  506. static Variant::Type get_constructor_argument_type(Variant::Type p_type, int p_constructor, int p_argument);
  507. static String get_constructor_argument_name(Variant::Type p_type, int p_constructor, int p_argument);
  508. static void construct(Variant::Type, Variant &base, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  509. static void get_constructor_list(Type p_type, List<MethodInfo> *r_list); //convenience
  510. /* Destructors */
  511. // Only ptrcall is available.
  512. typedef void (*PTRDestructor)(void *base);
  513. static PTRDestructor get_ptr_destructor(Variant::Type p_type);
  514. static bool has_destructor(Variant::Type p_type);
  515. /* Properties */
  516. void set_named(const StringName &p_member, const Variant &p_value, bool &r_valid);
  517. Variant get_named(const StringName &p_member, bool &r_valid) const;
  518. typedef void (*ValidatedSetter)(Variant *base, const Variant *value);
  519. typedef void (*ValidatedGetter)(const Variant *base, Variant *value);
  520. static bool has_member(Variant::Type p_type, const StringName &p_member);
  521. static Variant::Type get_member_type(Variant::Type p_type, const StringName &p_member);
  522. static void get_member_list(Type p_type, List<StringName> *r_members);
  523. static int get_member_count(Type p_type);
  524. static ValidatedSetter get_member_validated_setter(Variant::Type p_type, const StringName &p_member);
  525. static ValidatedGetter get_member_validated_getter(Variant::Type p_type, const StringName &p_member);
  526. typedef void (*PTRSetter)(void *base, const void *value);
  527. typedef void (*PTRGetter)(const void *base, void *value);
  528. static PTRSetter get_member_ptr_setter(Variant::Type p_type, const StringName &p_member);
  529. static PTRGetter get_member_ptr_getter(Variant::Type p_type, const StringName &p_member);
  530. /* Indexing */
  531. static bool has_indexing(Variant::Type p_type);
  532. static Variant::Type get_indexed_element_type(Variant::Type p_type);
  533. typedef void (*ValidatedIndexedSetter)(Variant *base, int64_t index, const Variant *value, bool *oob);
  534. typedef void (*ValidatedIndexedGetter)(const Variant *base, int64_t index, Variant *value, bool *oob);
  535. static ValidatedIndexedSetter get_member_validated_indexed_setter(Variant::Type p_type);
  536. static ValidatedIndexedGetter get_member_validated_indexed_getter(Variant::Type p_type);
  537. typedef void (*PTRIndexedSetter)(void *base, int64_t index, const void *value);
  538. typedef void (*PTRIndexedGetter)(const void *base, int64_t index, void *value);
  539. static PTRIndexedSetter get_member_ptr_indexed_setter(Variant::Type p_type);
  540. static PTRIndexedGetter get_member_ptr_indexed_getter(Variant::Type p_type);
  541. void set_indexed(int64_t p_index, const Variant &p_value, bool &r_valid, bool &r_oob);
  542. Variant get_indexed(int64_t p_index, bool &r_valid, bool &r_oob) const;
  543. uint64_t get_indexed_size() const;
  544. /* Keying */
  545. static bool is_keyed(Variant::Type p_type);
  546. typedef void (*ValidatedKeyedSetter)(Variant *base, const Variant *key, const Variant *value, bool *valid);
  547. typedef void (*ValidatedKeyedGetter)(const Variant *base, const Variant *key, Variant *value, bool *valid);
  548. typedef bool (*ValidatedKeyedChecker)(const Variant *base, const Variant *key, bool *valid);
  549. static ValidatedKeyedSetter get_member_validated_keyed_setter(Variant::Type p_type);
  550. static ValidatedKeyedGetter get_member_validated_keyed_getter(Variant::Type p_type);
  551. static ValidatedKeyedChecker get_member_validated_keyed_checker(Variant::Type p_type);
  552. typedef void (*PTRKeyedSetter)(void *base, const void *key, const void *value);
  553. typedef void (*PTRKeyedGetter)(const void *base, const void *key, void *value);
  554. typedef uint32_t (*PTRKeyedChecker)(const void *base, const void *key);
  555. static PTRKeyedSetter get_member_ptr_keyed_setter(Variant::Type p_type);
  556. static PTRKeyedGetter get_member_ptr_keyed_getter(Variant::Type p_type);
  557. static PTRKeyedChecker get_member_ptr_keyed_checker(Variant::Type p_type);
  558. void set_keyed(const Variant &p_key, const Variant &p_value, bool &r_valid);
  559. Variant get_keyed(const Variant &p_key, bool &r_valid) const;
  560. bool has_key(const Variant &p_key, bool &r_valid) const;
  561. /* Generic */
  562. void set(const Variant &p_index, const Variant &p_value, bool *r_valid = nullptr);
  563. Variant get(const Variant &p_index, bool *r_valid = nullptr) const;
  564. bool in(const Variant &p_index, bool *r_valid = nullptr) const;
  565. bool iter_init(Variant &r_iter, bool &r_valid) const;
  566. bool iter_next(Variant &r_iter, bool &r_valid) const;
  567. Variant iter_get(const Variant &r_iter, bool &r_valid) const;
  568. void get_property_list(List<PropertyInfo> *p_list) const;
  569. static void call_utility_function(const StringName &p_name, Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  570. static bool has_utility_function(const StringName &p_name);
  571. typedef void (*ValidatedUtilityFunction)(Variant *r_ret, const Variant **p_args, int p_argcount);
  572. typedef void (*PTRUtilityFunction)(void *r_ret, const void **p_args, int p_argcount);
  573. static ValidatedUtilityFunction get_validated_utility_function(const StringName &p_name);
  574. static PTRUtilityFunction get_ptr_utility_function(const StringName &p_name);
  575. enum UtilityFunctionType {
  576. UTILITY_FUNC_TYPE_MATH,
  577. UTILITY_FUNC_TYPE_RANDOM,
  578. UTILITY_FUNC_TYPE_GENERAL,
  579. };
  580. static UtilityFunctionType get_utility_function_type(const StringName &p_name);
  581. static MethodInfo get_utility_function_info(const StringName &p_name);
  582. static int get_utility_function_argument_count(const StringName &p_name);
  583. static Variant::Type get_utility_function_argument_type(const StringName &p_name, int p_arg);
  584. static String get_utility_function_argument_name(const StringName &p_name, int p_arg);
  585. static bool has_utility_function_return_value(const StringName &p_name);
  586. static Variant::Type get_utility_function_return_type(const StringName &p_name);
  587. static bool is_utility_function_vararg(const StringName &p_name);
  588. static uint32_t get_utility_function_hash(const StringName &p_name);
  589. static void get_utility_function_list(List<StringName> *r_functions);
  590. static int get_utility_function_count();
  591. //argsVariant call()
  592. bool operator==(const Variant &p_variant) const;
  593. bool operator!=(const Variant &p_variant) const;
  594. bool operator<(const Variant &p_variant) const;
  595. uint32_t hash() const;
  596. uint32_t recursive_hash(int recursion_count) const;
  597. bool hash_compare(const Variant &p_variant, int recursion_count = 0) const;
  598. bool booleanize() const;
  599. String stringify(int recursion_count = 0) const;
  600. String to_json_string() const;
  601. void static_assign(const Variant &p_variant);
  602. static void get_constants_for_type(Variant::Type p_type, List<StringName> *p_constants);
  603. static int get_constants_count_for_type(Variant::Type p_type);
  604. static bool has_constant(Variant::Type p_type, const StringName &p_value);
  605. static Variant get_constant_value(Variant::Type p_type, const StringName &p_value, bool *r_valid = nullptr);
  606. typedef String (*ObjectDeConstruct)(const Variant &p_object, void *ud);
  607. typedef void (*ObjectConstruct)(const String &p_text, void *ud, Variant &r_value);
  608. String get_construct_string() const;
  609. static void construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct = nullptr, void *p_construct_ud = nullptr);
  610. void operator=(const Variant &p_variant); // only this is enough for all the other types
  611. static void register_types();
  612. static void unregister_types();
  613. Variant(const Variant &p_variant);
  614. _FORCE_INLINE_ Variant() {}
  615. _FORCE_INLINE_ ~Variant() {
  616. clear();
  617. }
  618. };
  619. //typedef Dictionary Dictionary; no
  620. //typedef Array Array;
  621. Vector<Variant> varray();
  622. Vector<Variant> varray(const Variant &p_arg1);
  623. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2);
  624. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3);
  625. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4);
  626. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5);
  627. struct VariantHasher {
  628. static _FORCE_INLINE_ uint32_t hash(const Variant &p_variant) { return p_variant.hash(); }
  629. };
  630. struct VariantComparator {
  631. static _FORCE_INLINE_ bool compare(const Variant &p_lhs, const Variant &p_rhs) { return p_lhs.hash_compare(p_rhs); }
  632. };
  633. Variant::ObjData &Variant::_get_obj() {
  634. return *reinterpret_cast<ObjData *>(&_data._mem[0]);
  635. }
  636. const Variant::ObjData &Variant::_get_obj() const {
  637. return *reinterpret_cast<const ObjData *>(&_data._mem[0]);
  638. }
  639. 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());
  640. #endif // VARIANT_H