variant.h
1 /*************************************************************************/
2 /* variant.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef VARIANT_H
30 #define VARIANT_H
31 
36 #include "aabb.h"
37 #include "ustring.h"
38 #include "vector3.h"
39 #include "plane.h"
40 #include "quat.h"
41 #include "matrix3.h"
42 #include "transform.h"
43 #include "image.h"
44 #include "dvector.h"
45 #include "path_db.h"
46 #include "simple_type.h"
47 #include "os/input_event.h"
48 #include "color.h"
49 #include "face3.h"
50 #include "ref_ptr.h"
51 #include "math_2d.h"
52 #include "rid.h"
53 #include "io/ip_address.h"
54 #include "dictionary.h"
55 #include "array.h"
56 
57 class RefPtr;
58 class Object;
59 class Node; // helper
60 class Control; // helper
61 
62 struct PropertyInfo;
63 struct MethodInfo;
64 
65 
67 typedef DVector<int> IntArray;
73 
74 class Variant {
75 public:
76 
77  enum Type {
78 
79  NIL,
80 
81  // atomic types
82  BOOL,
83  INT,
84  REAL,
85  STRING,
86 
87  // math types
88 
89  VECTOR2, // 5
90  RECT2,
91  VECTOR3,
92  MATRIX32,
93  PLANE,
94  QUAT, // 10
95  _AABB, //sorry naming convention fail :( not like it's used often
96  MATRIX3,
97  TRANSFORM,
98 
99  // misc types
100  COLOR,
101  IMAGE, // 15
102  NODE_PATH,
103  _RID,
104  OBJECT,
105  INPUT_EVENT,
106  DICTIONARY, // 20
107  ARRAY,
108 
109  // arrays
110  RAW_ARRAY,
111  INT_ARRAY,
112  REAL_ARRAY,
113  STRING_ARRAY, // 25
114  VECTOR2_ARRAY,
115  VECTOR3_ARRAY,
116  COLOR_ARRAY,
117 
118  VARIANT_MAX
119 
120  };
121 
122 private:
123 
124 
125  friend class _VariantCall;
126  // Variant takes 20 bytes when real_t is float, and 36 if double
127  // it only allocates extra memory for aabb/matrix.
128 
129  Type type;
130 
131  struct ObjData {
132 
133  Object *obj;
134  RefPtr ref;
135  };
136 
137 
138  _FORCE_INLINE_ ObjData& _get_obj();
139  _FORCE_INLINE_ const ObjData& _get_obj() const;
140 
141  union {
142 
143  bool _bool;
144  int _int;
145  double _real;
146  Matrix32 *_matrix32;
147  AABB* _aabb;
148  Matrix3 *_matrix3;
149  Transform *_transform;
150  RefPtr *_resource;
151  InputEvent *_input_event;
152  Image *_image;
153  void *_ptr; //generic pointer
154 #ifdef USE_QUAD_VECTORS
155  uint8_t _mem[sizeof(ObjData) > (sizeof(real_t)*5) ? sizeof(ObjData) : (sizeof(real_t)*5)]; // plane uses an extra real
156 #else
157  uint8_t _mem[sizeof(ObjData) > (sizeof(real_t)*4) ? sizeof(ObjData) : (sizeof(real_t)*4)];
158 #endif
159  } _data;
160 
161 
162  void reference(const Variant& p_variant);
163  void clear();
164 public:
165 
166  _FORCE_INLINE_ Type get_type() const { return type; }
167  static String get_type_name(Variant::Type p_type);
168  static bool can_convert(Type p_type_from, Type p_type_to);
169  static bool can_convert_strict(Type p_type_from, Type p_type_to);
170 
171 
172 
173  template<class T>
174  static Type get_type_for() {
175 
177  Variant v(t.type);
178  Type r = v.get_type();
179  return r;
180  }
181 
182 
183  bool is_ref() const;
184  _FORCE_INLINE_ bool is_num() const { return type==INT || type==REAL; };
185  _FORCE_INLINE_ bool is_array() const { return type>=ARRAY; };
186  bool is_shared() const;
187  bool is_zero() const;
188  bool is_one() const;
189 
190  operator bool() const;
191  operator signed int() const;
192  operator unsigned int() const; // this is the real one
193  operator signed short() const;
194  operator unsigned short() const;
195  operator signed char() const;
196  operator unsigned char() const;
197  //operator long unsigned int() const;
198  operator int64_t() const;
199  operator uint64_t() const;
200 #ifdef NEED_LONG_INT
201  operator signed long() const;
202  operator unsigned long() const;
203 #endif
204 
205 #ifndef CHARTYPE_16BITS
206  operator CharType() const;
207 #endif
208  operator float() const;
209  operator double() const;
210  operator String() const;
211  operator StringName() const;
212  operator Vector2() const;
213  operator Rect2() const;
214  operator Vector3() const;
215  operator Plane() const;
216  operator AABB() const;
217  operator Quat() const;
218  operator Matrix3() const;
219  operator Transform() const;
220  operator Matrix32() const;
221 
222  operator Color() const;
223  operator Image() const;
224  operator NodePath() const;
225  operator RefPtr() const;
226  operator RID() const;
227  operator InputEvent() const;
228  operator Object*() const;
229  operator Node*() const;
230  operator Control*() const;
231 
232  operator Dictionary() const;
233  operator Array() const;
234 
235  operator DVector<uint8_t>() const;
236  operator DVector<int>() const;
237  operator DVector<real_t>() const;
238  operator DVector<String>() const;
239  operator DVector<Vector3>() const;
240  operator DVector<Color>() const;
241  operator DVector<Plane>() const;
242  operator DVector<Face3>() const;
243 
244 
245  operator Vector<Variant>() const;
246  operator Vector<uint8_t>() const;
247  operator Vector<int>() const;
248  operator Vector<real_t>() const;
249  operator Vector<String>() const;
250  operator Vector<Vector3>() const;
251  operator Vector<Color>() const;
252  operator Vector<RID>() const;
253  operator Vector<Vector2>() const;
254  operator DVector<Vector2>() const;
255  operator Vector<Plane>() const;
256 
257  // some core type enums to convert to
258  operator Margin() const;
259  operator Orientation() const;
260 
261  operator IP_Address() const;
262 
263 
264  Variant(bool p_bool);
265  Variant(signed int p_int); // real one
266  Variant(unsigned int p_int);
267 #ifdef NEED_LONG_INT
268  Variant(signed long p_long); // real one
269  Variant(unsigned long p_long);
270  //Variant(long unsigned int p_long);
271 #endif
272  Variant(signed short p_short); // real one
273  Variant(unsigned short p_short);
274  Variant(signed char p_char); // real one
275  Variant(unsigned char p_char);
276  Variant(int64_t p_char); // real one
277  Variant(uint64_t p_char);
278  Variant(float p_float);
279  Variant(double p_double);
280  Variant(const String& p_string);
281  Variant(const StringName& p_string);
282  Variant(const char * const p_cstring);
283  Variant(const CharType * p_wstring);
284  Variant(const Vector2& p_vector2);
285  Variant(const Rect2& p_rect2);
286  Variant(const Vector3& p_vector3);
287  Variant(const Plane& p_plane);
288  Variant(const AABB& p_aabb);
289  Variant(const Quat& p_quat);
290  Variant(const Matrix3& p_transform);
291  Variant(const Matrix32& p_transform);
292  Variant(const Transform& p_transform);
293  Variant(const Color& p_color);
294  Variant(const Image& p_image);
295  Variant(const NodePath& p_path);
296  Variant(const RefPtr& p_resource);
297  Variant(const RID& p_rid);
298  Variant(const Object* p_object);
299  Variant(const InputEvent& p_input_event);
300  Variant(const Dictionary& p_dictionary);
301 
302  Variant(const Array& p_array);
303  Variant(const DVector<Plane>& p_array); // helper
304  Variant(const DVector<uint8_t>& p_raw_array);
305  Variant(const DVector<int>& p_int_array);
306  Variant(const DVector<real_t>& p_real_array);
307  Variant(const DVector<String>& p_string_array);
308  Variant(const DVector<Vector3>& p_vector3_array);
309  Variant(const DVector<Color>& p_color_array);
310  Variant(const DVector<Face3>& p_face_array);
311 
312 
313  Variant(const Vector<Variant>& p_array);
314  Variant(const Vector<uint8_t>& p_raw_array);
315  Variant(const Vector<int>& p_int_array);
316  Variant(const Vector<real_t>& p_real_array);
317  Variant(const Vector<String>& p_string_array);
318  Variant(const Vector<Vector3>& p_vector3_array);
319  Variant(const Vector<Color>& p_color_array);
320  Variant(const Vector<Plane>& p_array); // helper
321  Variant(const Vector<RID>& p_array); // helper
322  Variant(const Vector<Vector2>& p_array); // helper
323  Variant(const DVector<Vector2>& p_array); // helper
324 
325  Variant(const IP_Address& p_address);
326 
327 
328 
329  enum Operator {
330 
331  //comparation
332  OP_EQUAL,
333  OP_NOT_EQUAL,
334  OP_LESS,
335  OP_LESS_EQUAL,
336  OP_GREATER,
337  OP_GREATER_EQUAL,
338  //mathematic
339  OP_ADD,
340  OP_SUBSTRACT,
341  OP_MULTIPLY,
342  OP_DIVIDE,
343  OP_NEGATE,
344  OP_MODULE,
345  OP_STRING_CONCAT,
346  //bitwise
347  OP_SHIFT_LEFT,
348  OP_SHIFT_RIGHT,
349  OP_BIT_AND,
350  OP_BIT_OR,
351  OP_BIT_XOR,
352  OP_BIT_NEGATE,
353  //logic
354  OP_AND,
355  OP_OR,
356  OP_XOR,
357  OP_NOT,
358  //containment
359  OP_IN,
360  OP_MAX
361 
362  };
363 
364 
365  static String get_operator_name(Operator p_op);
366  static void evaluate(const Operator& p_op,const Variant& p_a, const Variant& p_b,Variant &r_ret,bool &r_valid);
367  static _FORCE_INLINE_ Variant evaluate(const Operator& p_op,const Variant& p_a, const Variant& p_b) {
368 
369  bool valid=true;
370  Variant res;
371  evaluate(p_op,p_a,p_b,res,valid);
372  return res;
373  }
374 
375  void zero();
376  static void blend(const Variant& a, const Variant& b, float c,Variant &r_dst);
377  static void interpolate(const Variant& a, const Variant& b, float c,Variant &r_dst);
378 
379  struct CallError {
380  enum Error {
381  CALL_OK,
382  CALL_ERROR_INVALID_METHOD,
383  CALL_ERROR_INVALID_ARGUMENT,
384  CALL_ERROR_TOO_MANY_ARGUMENTS,
385  CALL_ERROR_TOO_FEW_ARGUMENTS,
386  CALL_ERROR_INSTANCE_IS_NULL,
387  };
388  Error error;
389  int argument;
390  Type expected;
391  };
392 
393  Variant call(const StringName& p_method,const Variant** p_args,int p_argcount,CallError &r_error);
394  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());
395 
396  static String get_call_error_text(Object* p_base, const StringName& p_method,const Variant** p_argptrs,int p_argcount,const Variant::CallError &ce);
397 
398  static Variant construct(const Variant::Type,const Variant** p_args,int p_argcount,CallError &r_error,bool p_strict=true);
399 
400  void get_method_list(List<MethodInfo> *p_list) const;
401  bool has_method(const StringName& p_method) const;
402 
403  void set_named(const StringName& p_index, const Variant& p_value, bool *r_valid=NULL);
404  Variant get_named(const StringName& p_index, bool *r_valid=NULL) const;
405 
406  void set(const Variant& p_index, const Variant& p_value, bool *r_valid=NULL);
407  Variant get(const Variant& p_index, bool *r_valid=NULL) const;
408  bool in(const Variant& p_index, bool *r_valid=NULL) const;
409 
410  bool iter_init(Variant& r_iter,bool &r_valid) const;
411  bool iter_next(Variant& r_iter,bool &r_valid) const;
412  Variant iter_get(const Variant& r_iter,bool &r_valid) const;
413 
414  void get_property_list(List<PropertyInfo> *p_list) const;
415 
416  //argsVariant call()
417 
418  bool operator==(const Variant& p_variant) const;
419  bool operator!=(const Variant& p_variant) const;
420  bool operator<(const Variant& p_variant) const;
421  uint32_t hash() const;
422 
423  bool booleanize(bool &valid) const;
424 
425  void static_assign(const Variant& p_variant);
426  static void get_constructor_list(Variant::Type p_type, List<MethodInfo> *p_list);
427  static void get_numeric_constants_for_type(Variant::Type p_type, List<StringName> *p_constants);
428  static bool has_numeric_constant(Variant::Type p_type, const StringName& p_value);
429  static int get_numeric_constant_value(Variant::Type p_type, const StringName& p_value);
430 
431  typedef String (*ObjectDeConstruct)(const Variant& p_object,void *ud);
432  typedef void (*ObjectConstruct)(const String& p_text,void *ud,Variant& r_value);
433 
434  String get_construct_string() const;
435  static void construct_from_string(const String& p_string,Variant& r_value,ObjectConstruct p_obj_construct=NULL,void *p_construct_ud=NULL);
436 
437  void operator=(const Variant& p_variant); // only this is enough for all the other types
438  Variant(const Variant& p_variant);
439  _FORCE_INLINE_ Variant() { type=NIL; }
440  _FORCE_INLINE_ ~Variant() { if (type!=Variant::NIL) clear(); }
441 
442 };
443 
444 //typedef Dictionary Dictionary; no
445 //typedef Array Array;
446 
447 
448 
449 Vector<Variant> varray();
450 Vector<Variant> varray(const Variant& p_arg1);
451 Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2);
452 Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3);
453 Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3,const Variant& p_arg4);
454 Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3,const Variant& p_arg4,const Variant& p_arg5);
455 
457 
458  static _FORCE_INLINE_ uint32_t hash(const Variant &p_variant) { return p_variant.hash(); }
459 };
460 
461 
462 Variant::ObjData& Variant::_get_obj() {
463 
464  return *reinterpret_cast<ObjData*>(&_data._mem[0]);
465 }
466 
467 const Variant::ObjData& Variant::_get_obj() const {
468 
469  return *reinterpret_cast<const ObjData*>(&_data._mem[0]);
470 }
471 
472 #endif
Definition: ref_ptr.h:40
Definition: array.h:38
Definition: variant.h:74
Definition: path_db.h:41
Definition: quat.h:40
Definition: node.h:42
Definition: aabb.h:43
Definition: matrix3.h:38
Definition: color.h:37
Definition: object.h:104
Definition: math_2d.h:204
Definition: image.h:47
Definition: variant.h:456
Definition: string_db.h:48
Definition: vector3.h:38
Definition: transform.h:38
Definition: input_event.h:263
Definition: list.h:44
Definition: rid.h:47
Definition: dictionary.h:42
Definition: math_2d.h:65
Definition: plane.h:35
Definition: math_2d.h:554
Definition: ip_address.h:34
Definition: object.h:127
Definition: simple_type.h:35
Definition: control.h:47
Definition: ustring.h:64
Definition: object.h:317
Definition: variant_call.cpp:42
Definition: variant.h:379