variant.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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/array.h"
  33. #include "core/color.h"
  34. #include "core/dictionary.h"
  35. #include "core/io/ip_address.h"
  36. #include "core/math/aabb.h"
  37. #include "core/math/basis.h"
  38. #include "core/math/face3.h"
  39. #include "core/math/plane.h"
  40. #include "core/math/quat.h"
  41. #include "core/math/transform.h"
  42. #include "core/math/transform_2d.h"
  43. #include "core/math/vector3.h"
  44. #include "core/node_path.h"
  45. #include "core/object_id.h"
  46. #include "core/pool_vector.h"
  47. #include "core/ref_ptr.h"
  48. #include "core/rid.h"
  49. #include "core/ustring.h"
  50. class Object;
  51. class ObjectRC;
  52. class Node; // helper
  53. class Control; // helper
  54. struct PropertyInfo;
  55. struct MethodInfo;
  56. typedef PoolVector<uint8_t> PoolByteArray;
  57. typedef PoolVector<int> PoolIntArray;
  58. typedef PoolVector<real_t> PoolRealArray;
  59. typedef PoolVector<String> PoolStringArray;
  60. typedef PoolVector<Vector2> PoolVector2Array;
  61. typedef PoolVector<Vector3> PoolVector3Array;
  62. typedef PoolVector<Color> PoolColorArray;
  63. // Temporary workaround until c++11 alignas()
  64. #ifdef __GNUC__
  65. #define GCC_ALIGNED_8 __attribute__((aligned(8)))
  66. #else
  67. #define GCC_ALIGNED_8
  68. #endif
  69. // With DEBUG_ENABLED, the pointer to a deleted object stored in ObjectRC is set to nullptr,
  70. // so _OBJ_PTR is not useful for checks in which we want to act as if we still believed the
  71. // object is alive; e.g., comparing a Variant that points to a deleted object with NIL,
  72. // should return false regardless DEBUG_ENABLED is defined or not.
  73. // So in cases like that we use _UNSAFE_OBJ_PROXY_PTR, which serves that purpose. With DEBUG_ENABLED
  74. // it won't be the real pointer to the object for non-Reference types, but that's fine.
  75. // We just need it to be unique for each object, to be comparable and not to be forced to NULL
  76. // when the object is freed.
  77. #ifdef DEBUG_ENABLED
  78. #define _REF_OBJ_PTR(m_variant) (reinterpret_cast<Ref<Reference> *>((m_variant)._get_obj().ref.get_data())->ptr())
  79. #define _OBJ_PTR(m_variant) ((m_variant)._get_obj().rc ? (m_variant)._get_obj().rc->get_ptr() : _REF_OBJ_PTR(m_variant))
  80. #define _UNSAFE_OBJ_PROXY_PTR(m_variant) ((m_variant)._get_obj().rc ? reinterpret_cast<uint8_t *>((m_variant)._get_obj().rc) : reinterpret_cast<uint8_t *>(_REF_OBJ_PTR(m_variant)))
  81. #else
  82. #define _OBJ_PTR(m_variant) ((m_variant)._get_obj().obj)
  83. #define _UNSAFE_OBJ_PROXY_PTR(m_variant) _OBJ_PTR(m_variant)
  84. #endif
  85. class Variant {
  86. public:
  87. // If this changes the table in variant_op must be updated
  88. enum Type {
  89. NIL,
  90. // atomic types
  91. BOOL,
  92. INT,
  93. REAL,
  94. STRING,
  95. // math types
  96. VECTOR2, // 5
  97. RECT2,
  98. VECTOR3,
  99. TRANSFORM2D,
  100. PLANE,
  101. QUAT, // 10
  102. AABB,
  103. BASIS,
  104. TRANSFORM,
  105. // misc types
  106. COLOR,
  107. NODE_PATH, // 15
  108. _RID,
  109. OBJECT,
  110. DICTIONARY,
  111. ARRAY,
  112. // arrays
  113. POOL_BYTE_ARRAY, // 20
  114. POOL_INT_ARRAY,
  115. POOL_REAL_ARRAY,
  116. POOL_STRING_ARRAY,
  117. POOL_VECTOR2_ARRAY,
  118. POOL_VECTOR3_ARRAY, // 25
  119. POOL_COLOR_ARRAY,
  120. VARIANT_MAX
  121. };
  122. private:
  123. friend struct _VariantCall;
  124. // Variant takes 20 bytes when real_t is float, and 36 if double
  125. // it only allocates extra memory for aabb/matrix.
  126. Type type;
  127. struct ObjData {
  128. #ifdef DEBUG_ENABLED
  129. // Will be null for every type deriving from Reference as they have their
  130. // own reference count mechanism
  131. ObjectRC *rc;
  132. #else
  133. Object *obj;
  134. #endif
  135. // Always initialized, but will be null if the Ref<> assigned was null
  136. // or this Variant is not even holding a Reference-derived object
  137. RefPtr ref;
  138. };
  139. _FORCE_INLINE_ ObjData &_get_obj();
  140. _FORCE_INLINE_ const ObjData &_get_obj() const;
  141. union {
  142. bool _bool;
  143. int64_t _int;
  144. double _real;
  145. Transform2D *_transform2d;
  146. ::AABB *_aabb;
  147. Basis *_basis;
  148. Transform *_transform;
  149. void *_ptr; //generic pointer
  150. uint8_t _mem[sizeof(ObjData) > (sizeof(real_t) * 4) ? sizeof(ObjData) : (sizeof(real_t) * 4)];
  151. } _data GCC_ALIGNED_8;
  152. void reference(const Variant &p_variant);
  153. void clear();
  154. public:
  155. _FORCE_INLINE_ Type get_type() const { return type; }
  156. static String get_type_name(Variant::Type p_type);
  157. static bool can_convert(Type p_type_from, Type p_type_to);
  158. static bool can_convert_strict(Type p_type_from, Type p_type_to);
  159. bool is_ref() const;
  160. _FORCE_INLINE_ bool is_num() const { return type == INT || type == REAL; };
  161. _FORCE_INLINE_ bool is_array() const { return type >= ARRAY; };
  162. bool is_shared() const;
  163. bool is_zero() const;
  164. bool is_one() const;
  165. operator bool() const;
  166. operator signed int() const;
  167. operator unsigned int() const; // this is the real one
  168. operator signed short() const;
  169. operator unsigned short() const;
  170. operator signed char() const;
  171. operator unsigned char() const;
  172. //operator long unsigned int() const;
  173. operator int64_t() const;
  174. operator uint64_t() const;
  175. #ifdef NEED_LONG_INT
  176. operator signed long() const;
  177. operator unsigned long() const;
  178. #endif
  179. operator CharType() const;
  180. operator float() const;
  181. operator double() const;
  182. operator String() const;
  183. operator StringName() const;
  184. operator Vector2() const;
  185. operator Rect2() const;
  186. operator Vector3() const;
  187. operator Plane() const;
  188. operator ::AABB() const;
  189. operator Quat() const;
  190. operator Basis() const;
  191. operator Transform() const;
  192. operator Transform2D() const;
  193. operator Color() const;
  194. operator NodePath() const;
  195. operator RefPtr() const;
  196. operator RID() const;
  197. operator Object *() const;
  198. operator Node *() const;
  199. operator Control *() const;
  200. operator Dictionary() const;
  201. operator Array() const;
  202. operator PoolVector<uint8_t>() const;
  203. operator PoolVector<int>() const;
  204. operator PoolVector<real_t>() const;
  205. operator PoolVector<String>() const;
  206. operator PoolVector<Vector3>() const;
  207. operator PoolVector<Color>() const;
  208. operator PoolVector<Plane>() const;
  209. operator PoolVector<Face3>() const;
  210. operator Vector<Variant>() const;
  211. operator Vector<uint8_t>() const;
  212. operator Vector<int>() const;
  213. operator Vector<real_t>() const;
  214. operator Vector<String>() const;
  215. operator Vector<StringName>() const;
  216. operator Vector<Vector3>() const;
  217. operator Vector<Color>() const;
  218. operator Vector<RID>() const;
  219. operator Vector<Vector2>() const;
  220. operator PoolVector<Vector2>() const;
  221. operator Vector<Plane>() const;
  222. // some core type enums to convert to
  223. operator Margin() const;
  224. operator Orientation() const;
  225. operator IP_Address() const;
  226. Variant(bool p_bool);
  227. Variant(signed int p_int); // real one
  228. Variant(unsigned int p_int);
  229. #ifdef NEED_LONG_INT
  230. Variant(signed long p_long); // real one
  231. Variant(unsigned long p_long);
  232. //Variant(long unsigned int p_long);
  233. #endif
  234. Variant(signed short p_short); // real one
  235. Variant(unsigned short p_short);
  236. Variant(signed char p_char); // real one
  237. Variant(unsigned char p_char);
  238. Variant(int64_t p_int); // real one
  239. Variant(uint64_t p_int);
  240. Variant(float p_float);
  241. Variant(double p_double);
  242. Variant(const String &p_string);
  243. Variant(const StringName &p_string);
  244. Variant(const char *const p_cstring);
  245. Variant(const CharType *p_wstring);
  246. Variant(const Vector2 &p_vector2);
  247. Variant(const Rect2 &p_rect2);
  248. Variant(const Vector3 &p_vector3);
  249. Variant(const Plane &p_plane);
  250. Variant(const ::AABB &p_aabb);
  251. Variant(const Quat &p_quat);
  252. Variant(const Basis &p_matrix);
  253. Variant(const Transform2D &p_transform);
  254. Variant(const Transform &p_transform);
  255. Variant(const Color &p_color);
  256. Variant(const NodePath &p_node_path);
  257. Variant(const RefPtr &p_resource);
  258. Variant(const RID &p_rid);
  259. Variant(const Object *p_object);
  260. Variant(const Dictionary &p_dictionary);
  261. Variant(const Array &p_array);
  262. Variant(const PoolVector<Plane> &p_array); // helper
  263. Variant(const PoolVector<uint8_t> &p_raw_array);
  264. Variant(const PoolVector<int> &p_int_array);
  265. Variant(const PoolVector<real_t> &p_real_array);
  266. Variant(const PoolVector<String> &p_string_array);
  267. Variant(const PoolVector<Vector3> &p_vector3_array);
  268. Variant(const PoolVector<Color> &p_color_array);
  269. Variant(const PoolVector<Face3> &p_face_array);
  270. Variant(const Vector<Variant> &p_array);
  271. Variant(const Vector<uint8_t> &p_array);
  272. Variant(const Vector<int> &p_array);
  273. Variant(const Vector<real_t> &p_array);
  274. Variant(const Vector<String> &p_array);
  275. Variant(const Vector<StringName> &p_array);
  276. Variant(const Vector<Vector3> &p_array);
  277. Variant(const Vector<Color> &p_array);
  278. Variant(const Vector<Plane> &p_array); // helper
  279. Variant(const Vector<RID> &p_array); // helper
  280. Variant(const Vector<Vector2> &p_array); // helper
  281. Variant(const PoolVector<Vector2> &p_vector2_array); // helper
  282. Variant(const IP_Address &p_address);
  283. // If this changes the table in variant_op must be updated
  284. enum Operator {
  285. //comparison
  286. OP_EQUAL,
  287. OP_NOT_EQUAL,
  288. OP_LESS,
  289. OP_LESS_EQUAL,
  290. OP_GREATER,
  291. OP_GREATER_EQUAL,
  292. //mathematic
  293. OP_ADD,
  294. OP_SUBTRACT,
  295. OP_MULTIPLY,
  296. OP_DIVIDE,
  297. OP_NEGATE,
  298. OP_POSITIVE,
  299. OP_MODULE,
  300. OP_STRING_CONCAT,
  301. //bitwise
  302. OP_SHIFT_LEFT,
  303. OP_SHIFT_RIGHT,
  304. OP_BIT_AND,
  305. OP_BIT_OR,
  306. OP_BIT_XOR,
  307. OP_BIT_NEGATE,
  308. //logic
  309. OP_AND,
  310. OP_OR,
  311. OP_XOR,
  312. OP_NOT,
  313. //containment
  314. OP_IN,
  315. OP_MAX
  316. };
  317. static String get_operator_name(Operator p_op);
  318. static void evaluate(const Operator &p_op, const Variant &p_a, const Variant &p_b, Variant &r_ret, bool &r_valid);
  319. static _FORCE_INLINE_ Variant evaluate(const Operator &p_op, const Variant &p_a, const Variant &p_b) {
  320. bool valid = true;
  321. Variant res;
  322. evaluate(p_op, p_a, p_b, res, valid);
  323. return res;
  324. }
  325. void zero();
  326. Variant duplicate(bool deep = false) const;
  327. static void blend(const Variant &a, const Variant &b, float c, Variant &r_dst);
  328. static void interpolate(const Variant &a, const Variant &b, float c, Variant &r_dst);
  329. struct CallError {
  330. enum Error {
  331. CALL_OK,
  332. CALL_ERROR_INVALID_METHOD,
  333. CALL_ERROR_INVALID_ARGUMENT,
  334. CALL_ERROR_TOO_MANY_ARGUMENTS,
  335. CALL_ERROR_TOO_FEW_ARGUMENTS,
  336. CALL_ERROR_INSTANCE_IS_NULL,
  337. };
  338. Error error;
  339. int argument;
  340. Type expected;
  341. };
  342. void call_ptr(const StringName &p_method, const Variant **p_args, int p_argcount, Variant *r_ret, CallError &r_error);
  343. Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, CallError &r_error);
  344. 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());
  345. static String get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Variant::CallError &ce);
  346. static Variant construct(const Variant::Type, const Variant **p_args, int p_argcount, CallError &r_error, bool p_strict = true);
  347. void get_method_list(List<MethodInfo> *p_list) const;
  348. bool has_method(const StringName &p_method) const;
  349. static Vector<Variant::Type> get_method_argument_types(Variant::Type p_type, const StringName &p_method);
  350. static Vector<Variant> get_method_default_arguments(Variant::Type p_type, const StringName &p_method);
  351. static Variant::Type get_method_return_type(Variant::Type p_type, const StringName &p_method, bool *r_has_return = NULL);
  352. static Vector<StringName> get_method_argument_names(Variant::Type p_type, const StringName &p_method);
  353. static bool is_method_const(Variant::Type p_type, const StringName &p_method);
  354. void set_named(const StringName &p_index, const Variant &p_value, bool *r_valid = NULL);
  355. Variant get_named(const StringName &p_index, bool *r_valid = NULL) const;
  356. void set(const Variant &p_index, const Variant &p_value, bool *r_valid = NULL);
  357. Variant get(const Variant &p_index, bool *r_valid = NULL) const;
  358. bool in(const Variant &p_index, bool *r_valid = NULL) const;
  359. bool iter_init(Variant &r_iter, bool &r_valid) const;
  360. bool iter_next(Variant &r_iter, bool &r_valid) const;
  361. Variant iter_get(const Variant &r_iter, bool &r_valid) const;
  362. void get_property_list(List<PropertyInfo> *p_list) const;
  363. //argsVariant call()
  364. bool operator==(const Variant &p_variant) const;
  365. bool operator!=(const Variant &p_variant) const;
  366. bool operator<(const Variant &p_variant) const;
  367. uint32_t hash() const;
  368. bool hash_compare(const Variant &p_variant) const;
  369. bool booleanize() const;
  370. String stringify(List<const void *> &stack) const;
  371. void static_assign(const Variant &p_variant);
  372. static void get_constructor_list(Variant::Type p_type, List<MethodInfo> *p_list);
  373. static void get_constants_for_type(Variant::Type p_type, List<StringName> *p_constants);
  374. static bool has_constant(Variant::Type p_type, const StringName &p_value);
  375. static Variant get_constant_value(Variant::Type p_type, const StringName &p_value, bool *r_valid = NULL);
  376. typedef String (*ObjectDeConstruct)(const Variant &p_object, void *ud);
  377. typedef void (*ObjectConstruct)(const String &p_text, void *ud, Variant &r_value);
  378. String get_construct_string() const;
  379. static void construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct = NULL, void *p_construct_ud = NULL);
  380. void operator=(const Variant &p_variant); // only this is enough for all the other types
  381. Variant(const Variant &p_variant);
  382. _FORCE_INLINE_ Variant() { type = NIL; }
  383. _FORCE_INLINE_ ~Variant() {
  384. if (type != Variant::NIL) clear();
  385. }
  386. };
  387. //typedef Dictionary Dictionary; no
  388. //typedef Array Array;
  389. Vector<Variant> varray();
  390. Vector<Variant> varray(const Variant &p_arg1);
  391. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2);
  392. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3);
  393. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4);
  394. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5);
  395. struct VariantHasher {
  396. static _FORCE_INLINE_ uint32_t hash(const Variant &p_variant) { return p_variant.hash(); }
  397. };
  398. struct VariantComparator {
  399. static _FORCE_INLINE_ bool compare(const Variant &p_lhs, const Variant &p_rhs) { return p_lhs.hash_compare(p_rhs); }
  400. };
  401. Variant::ObjData &Variant::_get_obj() {
  402. return *reinterpret_cast<ObjData *>(&_data._mem[0]);
  403. }
  404. const Variant::ObjData &Variant::_get_obj() const {
  405. return *reinterpret_cast<const ObjData *>(&_data._mem[0]);
  406. }
  407. 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());
  408. #endif