ezEngine  Milestone 9
Variant.h
1 #pragma once
2 
3 #include <Foundation/Algorithm/HashingUtils.h>
4 #include <Foundation/Containers/DynamicArray.h>
5 #include <Foundation/Containers/HashTable.h>
6 #include <Foundation/Threading/AtomicInteger.h>
7 #include <Foundation/Types/Types.h>
8 #include <Foundation/Utilities/ConversionUtils.h>
10 
11 class ezReflectedClass;
12 class ezVariant;
13 struct ezTime;
14 class ezUuid;
15 struct ezStringView;
16 
20 
28 class EZ_FOUNDATION_DLL ezVariant
29 {
30 public:
32  struct Type
33  {
34  typedef ezUInt8 StorageType;
38  enum Enum
39  {
40  Invalid = 0,
41 
43  FirstStandardType = 1,
44  Bool,
45  Int8,
71  DataBuffer,
72  Time,
74  Uuid,
77  LastStandardType,
79 
80  FirstExtendedType = 64,
82  VariantDictionary,
83  ReflectedPointer,
87 
88  MAX_ENUM_VALUE = LastExtendedType,
89  Default = Invalid
90  };
91  };
92 
94  template <typename T>
96  {
97  enum
98  {
99  value = Type::Invalid,
100  forceSharing = false,
101  hasReflectedMembers = false
102  };
103 
104  typedef T StorageType;
105  };
106 
108  ezVariant(); // [tested]
109 
115  ezVariant(const ezVariant& other); // [tested]
116 
118  ezVariant(ezVariant&& other); // [tested]
119 
120  ezVariant(const bool& value);
121  ezVariant(const ezInt8& value);
122  ezVariant(const ezUInt8& value);
123  ezVariant(const ezInt16& value);
124  ezVariant(const ezUInt16& value);
125  ezVariant(const ezInt32& value);
126  ezVariant(const ezUInt32& value);
127  ezVariant(const ezInt64& value);
128  ezVariant(const ezUInt64& value);
129  ezVariant(const float& value);
130  ezVariant(const double& value);
131  ezVariant(const ezColor& value);
132  ezVariant(const ezVec2& value);
133  ezVariant(const ezVec3& value);
134  ezVariant(const ezVec4& value);
135  ezVariant(const ezVec2I32& value);
136  ezVariant(const ezVec3I32& value);
137  ezVariant(const ezVec4I32& value);
138  ezVariant(const ezVec2U32& value);
139  ezVariant(const ezVec3U32& value);
140  ezVariant(const ezVec4U32& value);
141  ezVariant(const ezQuat& value);
142  ezVariant(const ezMat3& value);
143  ezVariant(const ezMat4& value);
144  ezVariant(const ezTransform& value);
145  ezVariant(const char* value);
146  ezVariant(const ezString& value);
147  ezVariant(const ezUntrackedString& value);
148  ezVariant(const ezStringView& value);
149  ezVariant(const ezDataBuffer& value);
150  ezVariant(const ezTime& value);
151  ezVariant(const ezUuid& value);
152  ezVariant(const ezAngle& value);
153  ezVariant(const ezColorGammaUB& value);
154 
155  ezVariant(const ezVariantArray& value);
156  ezVariant(const ezVariantDictionary& value);
157  ezVariant(ezReflectedClass* value);
158  ezVariant(const ezReflectedClass* value);
159  ezVariant(void* value);
160  ezVariant(const void* value);
161 
163  ~ezVariant();
164 
166  void operator=(const ezVariant& other); // [tested]
167 
169  void operator=(ezVariant&& other); // [tested]
170 
174  template <typename T>
175  void operator=(const T& value); // [tested]
176 
183  bool operator==(const ezVariant& other) const; // [tested]
184 
186  bool operator!=(const ezVariant& other) const; // [tested]
187 
189  template <typename T>
190  bool operator==(const T& other) const; // [tested]
191 
193  template <typename T>
194  bool operator!=(const T& other) const; // [tested]
195 
197  bool IsValid() const; // [tested]
198 
202  bool IsNumber() const; // [tested]
203 
205  bool IsFloatingPoint() const; // [tested]
206 
211  template <typename T>
212  bool IsA() const; // [tested]
213 
215  Type::Enum GetType() const; // [tested]
216 
224  template <typename T>
225  const T& Get() const; // [tested]
226 
228  void* GetData(); // [tested]
229 
231  const void* GetData() const; // [tested]
232 
236  ezVariant operator[](ezUInt32 uiIndex) const; // [tested]
237 
241  ezVariant operator[](ezHashingUtils::StringWrapper szKey) const; // [tested]
242 
255  template <typename T>
256  bool CanConvertTo() const; // [tested]
257 
259  bool CanConvertTo(Type::Enum type) const; // [tested]
260 
267  template <typename T>
268  T ConvertTo(ezResult* out_pConversionStatus = nullptr) const; // [tested]
269 
271  ezVariant ConvertTo(Type::Enum type, ezResult* out_pConversionStatus = nullptr) const; // [tested]
272 
280  template <typename Functor>
281  static void DispatchTo(Functor& functor, Type::Enum type); // [tested]
282 
284  ezUInt64 ComputeHash(ezUInt64 uiSeed = 0) const;
285 
286 private:
287  friend class ezVariantHelper;
288  friend struct CompareFunc;
289 
290  struct SharedData
291  {
292  void* m_Ptr;
293  ezAtomicInteger32 m_uiRef;
294  EZ_ALWAYS_INLINE SharedData(void* ptr)
295  : m_Ptr(ptr)
296  , m_uiRef(1)
297  {
298  }
299  virtual ~SharedData() {}
300  };
301 
302  template <typename T>
303  class TypedSharedData : public SharedData
304  {
305  private:
306  T m_t;
307 
308  public:
309  EZ_ALWAYS_INLINE TypedSharedData(const T& value)
310  : SharedData(&m_t)
311  , m_t(value)
312  {
313  }
314  };
315 
316  union Data {
317  float f[4];
318  SharedData* shared;
319  } m_Data;
320 
321  ezUInt32 m_Type : 31;
322  ezUInt32 m_bIsShared : 1;
323 
324  template <typename T>
325  void InitInplace(const T& value);
326 
327  template <typename T>
328  void InitShared(const T& value);
329 
330  void Release();
331  void CopyFrom(const ezVariant& other);
332  void MoveFrom(ezVariant&& other);
333 
334  template <typename T>
335  T& Cast();
336 
337  template <typename T>
338  const T& Cast() const;
339 
340  static bool IsNumberStatic(ezUInt32 type);
341  static bool IsFloatingPointStatic(ezUInt32 type);
342 
343  template <typename T>
344  T ConvertNumber() const;
345 };
346 
348 
349 #include <Foundation/Types/Implementation/VariantHelper_inl.h>
350 #include <Foundation/Types/Implementation/VariantTypeDeduction_inl.h>
351 #include <Foundation/Types/Implementation/Variant_inl.h>
352 
The variant stores an ezMat3. A heap allocation is required to store this data type.
Definition: Variant.h:66
The variant stores a void pointer.
Definition: Variant.h:85
The variant stores an ezVec3.
Definition: Variant.h:57
Definition: VariantHelper_inl.h:2
The variant stores an ezMat4. A heap allocation is required to store this data type.
Definition: Variant.h:67
The variant stores an ezVec3U32.
Definition: Variant.h:63
The variant stores an array of ezVariant&#39;s. A heap allocation is required to store this data type...
Definition: Variant.h:81
ezColor represents and RGBA color in linear color space. Values are stored as float, allowing HDR values and full precision color modifications.
Definition: Color.h:42
Enum
This enum describes the type of data that is currently stored inside the variant. Note that changes t...
Definition: Variant.h:38
The variant stores an ezColor.
Definition: Variant.h:55
The variant stores an ezVec4I32.
Definition: Variant.h:61
The variant stores an ezVec4U32.
Definition: Variant.h:64
The variant stores an ezVec2I32.
Definition: Variant.h:59
The variant stores an ezUInt64.
Definition: Variant.h:52
The variant stores an ezAngle value.
Definition: Variant.h:75
The variant stores an ezStringView.
Definition: Variant.h:70
All classes that should be dynamically reflectable, need to be derived from this base class...
Definition: DynamicRTTI.h:67
The variant stores an ezColorGammaUB value.
Definition: Variant.h:76
The time class encapsulates a double value storing the time in seconds.
Definition: Time.h:10
The variant stores a bool.
Definition: Variant.h:44
The variant stores an ezUInt32.
Definition: Variant.h:50
A 4-component vector class.
Definition: Declarations.h:82
ezStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition: StringView.h:18
The variant stores an ezVec2U32.
Definition: Variant.h:62
The variant stores an ezTransform. A heap allocation is required to store this data type...
Definition: Variant.h:68
The variant stores an ezInt8.
Definition: Variant.h:45
The variant stores a double.
Definition: Variant.h:54
helper struct to wrap a string pointer
Definition: HashingUtils.h:12
The variant stores a string. A heap allocation is required to store this data type.
Definition: Variant.h:69
The variant stores an ezUuid value.
Definition: Variant.h:74
The variant stores an ezVec4.
Definition: Variant.h:58
The variant stores an ezVec2.
Definition: Variant.h:56
The variant stores an ezInt64.
Definition: Variant.h:51
The variant stores an ezInt16.
Definition: Variant.h:47
The variant stores a float.
Definition: Variant.h:53
The variant stores an ezUInt8.
Definition: Variant.h:46
The variant stores an ezVec3I32.
Definition: Variant.h:60
ezVariant is a class that can store different types of variables, which is useful in situations where...
Definition: Variant.h:28
Number of values for ezVariant::Type.
Definition: Variant.h:86
Float wrapper struct for a safe usage and conversions of angles.
Definition: Angle.h:8
The variant stores an ezQuat.
Definition: Variant.h:65
The variant stores an ezUInt16.
Definition: Variant.h:48
A 8bit per channel unsigned normalized (values interpreted as 0-1) color storage format that represen...
Definition: Color8UNorm.h:69
Default enum for returning failure or success, instead of using a bool.
Definition: Types.h:51
A helper struct to convert the C++ type, which is passed as the template argument, into one of the ezVariant::Type enum values.
Definition: Variant.h:95
The variant stores an ezInt32.
Definition: Variant.h:49
This data type is the abstraction for 128-bit Uuid (also known as GUID) instances.
Definition: Uuid.h:10
This enum describes the type of data that is currently stored inside the variant. ...
Definition: Variant.h:32