variant.h 14 KB

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