Plasma Engine  2.0
Loading...
Searching...
No Matches
VariantAdapter.h
1#pragma once
2
3#include <Foundation/Types/Variant.h>
4
5template <typename T>
7{
8 using Type = T;
9 using RttiType = T;
10};
11
12template <typename T>
14{
15 using Type = plEnum<T>;
16 using RttiType = T;
17};
18
19template <typename T>
21{
22 using Type = plBitflags<T>;
23 using RttiType = T;
24};
25
26template <typename T>
28{
31};
32
33template <>
34struct plCleanType<const char*>
35{
36 using Type = const char*;
37 using RttiType = const char*;
38};
39
41
42template <typename T>
44{
45 enum
46 {
47 value = false,
48 };
49};
50
51template <typename T>
52struct plIsOutParam<T&>
53{
54 enum
55 {
56 value = !std::is_const<typename plTypeTraits<T>::NonReferencePointerType>::value,
57 };
58};
59
60template <typename T>
61struct plIsOutParam<T*>
62{
63 enum
64 {
65 value = !std::is_const<typename plTypeTraits<T>::NonReferencePointerType>::value,
66 };
67};
68
70
72template <class T, class C = typename plCleanType<T>::Type>
74{
75 enum
76 {
78 };
79};
80
81template <class T>
83{
84 enum
85 {
86 value = true,
87 };
88};
89
91
93template <class T, class C = typename plCleanType<T>::Type>
101
102template <class T>
104{
105 enum
106 {
107 value = true,
108 };
109};
110
114template <class T,
115 class C = typename plCleanType<T>::Type,
116 int VALUE_TYPE = plIsValueType<T>::value>
118{
119 using RealType = typename plTypeTraits<T>::NonConstReferencePointerType;
121 : m_value(value)
122 {
123 }
124
125 void operator=(RealType* rhs) { m_value = rhs; }
126 void operator=(RealType&& rhs)
127 {
128 if (m_value.IsValid())
129 *m_value.Get<RealType*>() = rhs;
130 }
131 plVariant& m_value;
132};
133
134template <class T, class S>
136{
137 using RealType = typename plTypeTraits<T>::NonConstReferencePointerType;
139 : m_value(value)
140 {
141 }
142
143 void operator=(plEnum<S>&& rhs) { m_value = static_cast<plInt64>(rhs.GetValue()); }
144
145 plVariant& m_value;
146};
147
148template <class T, class S>
150{
151 using RealType = typename plTypeTraits<T>::NonConstReferencePointerType;
153 : m_value(value)
154 {
155 }
156
157 void operator=(plBitflags<S>&& rhs) { m_value = static_cast<plInt64>(rhs.GetValue()); }
158
159 plVariant& m_value;
160};
161
162template <class T, class C>
164{
165 using RealType = typename plTypeTraits<T>::NonConstReferencePointerType;
167 : m_value(value)
168 {
169 }
170
171 void operator=(T&& rhs) { m_value = rhs; }
172
173 plVariant& m_value;
174};
175
176template <class T>
178{
180 : m_value(value)
181 {
182 }
183
184 void operator=(T&& rhs) { m_value = rhs; }
185
186 plVariant& m_value;
187};
188
189template <class T>
191{
193 : m_value(value)
194 {
195 }
196
197 void operator=(T&& rhs) { m_value = rhs; }
198
199 plVariant& m_value;
200};
201
203
206template <class T,
207 class C = typename plCleanType<T>::Type,
208 int VALUE_TYPE = plIsValueType<T>::value,
209 int OUT_PARAM = plIsOutParam<T>::value>
211{
212 using RealType = typename plTypeTraits<T>::NonConstReferencePointerType;
213
215 : m_value(value)
216 {
217 }
218
219 operator RealType&() { return *m_value.Get<RealType*>(); }
220
221 operator RealType*() { return m_value.IsValid() ? m_value.Get<RealType*>() : nullptr; }
222
223 plVariant& m_value;
224};
225
226template <class T, class S>
227struct plVariantAdapter<T, plEnum<S>, 0, 0>
228{
229 using RealType = typename plTypeTraits<T>::NonConstReferencePointerType;
231 : m_value(value)
232 {
233 if (m_value.IsValid())
234 m_realValue = static_cast<typename S::Enum>(m_value.ConvertTo<plInt64>());
235 }
236
237 operator const plEnum<S>&() { return m_realValue; }
238 operator const plEnum<S>*() { return m_value.IsValid() ? &m_realValue : nullptr; }
239
240 plVariant& m_value;
241 plEnum<S> m_realValue;
242};
243
244template <class T, class S>
245struct plVariantAdapter<T, plEnum<S>, 0, 1>
246{
247 using RealType = typename plTypeTraits<T>::NonConstReferencePointerType;
249 : m_value(value)
250 {
251 if (m_value.IsValid())
252 m_realValue = static_cast<typename S::Enum>(m_value.ConvertTo<plInt64>());
253 }
255 {
256 if (m_value.IsValid())
257 m_value = static_cast<plInt64>(m_realValue.GetValue());
258 }
259
260 operator plEnum<S>&() { return m_realValue; }
261 operator plEnum<S>*() { return m_value.IsValid() ? &m_realValue : nullptr; }
262
263 plVariant& m_value;
264 plEnum<S> m_realValue;
265};
266
267template <class T, class S>
268struct plVariantAdapter<T, plBitflags<S>, 0, 0>
269{
270 using RealType = typename plTypeTraits<T>::NonConstReferencePointerType;
272 : m_value(value)
273 {
274 if (m_value.IsValid())
275 m_realValue.SetValue(static_cast<typename S::StorageType>(m_value.ConvertTo<plInt64>()));
276 }
277
278 operator const plBitflags<S>&() { return m_realValue; }
279 operator const plBitflags<S>*() { return m_value.IsValid() ? &m_realValue : nullptr; }
280
281 plVariant& m_value;
282 plBitflags<S> m_realValue;
283};
284
285template <class T, class S>
286struct plVariantAdapter<T, plBitflags<S>, 0, 1>
287{
288 using RealType = typename plTypeTraits<T>::NonConstReferencePointerType;
290 : m_value(value)
291 {
292 if (m_value.IsValid())
293 m_realValue.SetValue(static_cast<typename S::StorageType>(m_value.ConvertTo<plInt64>()));
294 }
296 {
297 if (m_value.IsValid())
298 m_value = static_cast<plInt64>(m_realValue.GetValue());
299 }
300
301 operator plBitflags<S>&() { return m_realValue; }
302 operator plBitflags<S>*() { return m_value.IsValid() ? &m_realValue : nullptr; }
303
304 plVariant& m_value;
305 plBitflags<S> m_realValue;
306};
307
308template <class T, class C>
309struct plVariantAdapter<T, C, 1, 0>
310{
311 using RealType = typename plTypeTraits<T>::NonConstReferencePointerType;
313 : m_value(value)
314 {
315 }
316
317 operator const C&()
318 {
320 {
321 if (m_value.GetType() == plVariantType::TypedPointer)
322 return *m_value.Get<RealType*>();
323 }
324 return m_value.Get<RealType>();
325 }
326
327 operator const C*()
328 {
330 {
331 if (m_value.GetType() == plVariantType::TypedPointer)
332 return m_value.IsValid() ? m_value.Get<RealType*>() : nullptr;
333 }
334 return m_value.IsValid() ? &m_value.Get<RealType>() : nullptr;
335 }
336
337 plVariant& m_value;
338};
339
340template <class T, class C>
341struct plVariantAdapter<T, C, 1, 1>
342{
343 using RealType = typename plTypeTraits<T>::NonConstReferencePointerType;
345 : m_value(value)
346 {
347 // We ignore the return value here instead const_cast the Get<> result to profit from the Get methods runtime type checks.
348 m_value.GetWriteAccess();
349 }
350
351 operator C&()
352 {
353 if (m_value.GetType() == plVariantType::TypedPointer)
354 return *m_value.Get<RealType*>();
355 else
356 return const_cast<RealType&>(m_value.Get<RealType>());
357 }
358 operator C*()
359 {
360 if (m_value.GetType() == plVariantType::TypedPointer)
361 return m_value.IsValid() ? m_value.Get<RealType*>() : nullptr;
362 else
363 return m_value.IsValid() ? &const_cast<RealType&>(m_value.Get<RealType>()) : nullptr;
364 }
365
366 plVariant& m_value;
367};
368
369template <class T>
371{
373 : m_value(value)
374 {
375 }
376
377 operator const plVariant&() { return m_value; }
378 operator const plVariant*() { return &m_value; }
379
380 plVariant& m_value;
381};
382
383template <class T>
385{
387 : m_value(value)
388 {
389 }
390
391 operator plVariant&() { return m_value; }
392 operator plVariant*() { return &m_value; }
393
394 plVariant& m_value;
395};
396
397template <class T>
399{
401 : m_value(value)
402 {
403 }
404
405 operator const plVariantArray&() { return m_value.Get<plVariantArray>(); }
406 operator const plVariantArray*() { return m_value.IsValid() ? &m_value.Get<plVariantArray>() : nullptr; }
407
408 plVariant& m_value;
409};
410
411template <class T>
413{
415 : m_value(value)
416 {
417 }
418
419 operator plVariantArray&() { return m_value.GetWritable<plVariantArray>(); }
420 operator plVariantArray*() { return m_value.IsValid() ? &m_value.GetWritable<plVariantArray>() : nullptr; }
421
422 plVariant& m_value;
423};
424
425template <class T>
427{
429 : m_value(value)
430 {
431 }
432
433 operator const plVariantDictionary&() { return m_value.Get<plVariantDictionary>(); }
434 operator const plVariantDictionary*() { return m_value.IsValid() ? &m_value.Get<plVariantDictionary>() : nullptr; }
435
436 plVariant& m_value;
437};
438
439template <class T>
441{
443 : m_value(value)
444 {
445 }
446
447 operator plVariantDictionary&() { return m_value.GetWritable<plVariantDictionary>(); }
448 operator plVariantDictionary*() { return m_value.IsValid() ? &m_value.GetWritable<plVariantDictionary>() : nullptr; }
449
450 plVariant& m_value;
451};
452
453template <>
454struct plVariantAdapter<const char*, const char*, 1, 0>
455{
457 : m_value(value)
458 {
459 }
460
461 operator const char*() { return m_value.IsValid() ? m_value.Get<plString>().GetData() : nullptr; }
462
463 plVariant& m_value;
464};
465
466template <class T>
468{
470 : m_value(value)
471 {
472 }
473
474 operator const plStringView() { return m_value.IsA<plStringView>() ? m_value.Get<plStringView>() : m_value.Get<plString>().GetView(); }
475
476 plVariant& m_value;
477};
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
plVariant is a class that can store different types of variables, which is useful in situations where...
Definition Variant.h:44
bool IsValid() const
Returns whether this variant stores any other type than 'Invalid'.
Definition Variant_inl.h:274
T ConvertTo(plResult *out_pConversionStatus=nullptr) const
Tries to convert the stored value to the given type. The optional status parameter can be used to che...
Definition Variant_inl.h:421
plTypedPointer GetWriteAccess()
Returns an writable plTypedPointer to the internal data. If the data is currently shared a clone will...
Definition Variant.cpp:391
Type::Enum GetType() const
Returns the exact plVariant::Type value.
Definition Variant_inl.h:351
const T & Get() const
Returns the variants value as the provided type.
bool IsA() const
Returns whether the stored type is exactly the given type.
The plBitflags class allows you to work with type-safe bitflags.
Definition Bitflags.h:82
Definition VariantAdapter.h:7
Definition VariantAdapter.h:28
A custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition Enum.h:37
Definition VariantAdapter.h:44
Used to determine if the given type is a build-in standard variant type.
Definition VariantAdapter.h:74
Used to determine if the given type can be stored by value inside an plVariant (either standard type ...
Definition VariantAdapter.h:95
typename std::remove_const< typename std::remove_reference< typename std::remove_pointer< T >::type >::type >::type NonConstReferencePointerType
removes reference, const and pointer qualifier Note that this removes the const and reference of the ...
Definition TypeTraits.h:225
Used to implicitly retrieve any value from an plVariant to be used as a function argument using the a...
Definition VariantAdapter.h:211
Used to automatically assign any value to an plVariant using the assignment rules outlined in plAbstr...
Definition VariantAdapter.h:118
@ CustomTypeCast
Custom object types.
Definition VariantType.h:90
A helper struct to convert the C++ type, which is passed as the template argument,...
Definition VariantType.h:97
@ TypedPointer
The variant stores an plTypedPointer value. Reflected type and data queries will match the pointed to...
Definition VariantType.h:71
@ FirstStandardType
*** Types that are flagged as 'StandardTypes' (see DetermineTypeFlags) ***
Definition VariantType.h:30