variant.h 15 KB

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