variant.h 15 KB

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