variant.h 14 KB

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