3PL_WARNING_DISABLE_MSVC(4702)
7 m_uiType = Type::Invalid;
14PL_WARNING_DISABLE_CLANG("-Wunused-local-typedef")
15PL_WARNING_DISABLE_GCC("-Wunused-local-typedefs")
24 MoveFrom(std::move(other));
170 const constexpr bool forceSharing = TypeDeduction<T>::forceSharing;
171 const constexpr bool inlineSized =
sizeof(T) <= InlinedStruct::DataSize;
173 InitTypedObject(value,
plTraitInt < (!forceSharing && inlineSized && isPOD) ? 1 : 0 > ());
179 constexpr bool bla = !std::is_same<T, void>::value;
181 InitTypedPointer(
const_cast<T*
>(value), plGetStaticRTTI<T>());
186 InitTypedPointer(value, pType);
208 MoveFrom(std::move(other));
225 return ConvertNumber<double>() ==
static_cast<double>(other);
234 return ConvertNumber<plInt64>() ==
static_cast<plInt64
>(other);
240 if constexpr (std::is_same_v<T, plHashedString>)
244 return Cast<plTempHashedString>() == other;
247 else if constexpr (std::is_same_v<T, plTempHashedString>)
251 return Cast<plHashedString>() == other;
254 else if constexpr (std::is_same_v<T, plStringView>)
258 return Cast<plString>().GetView() == other;
261 else if constexpr (std::is_same_v<T, plString>)
265 return Cast<plStringView>() == other.GetView();
269 using StorageType =
typename TypeDeduction<T>::StorageType;
270 PL_ASSERT_DEV(
IsA<StorageType>(),
"Stored type '{0}' does not match comparison type '{1}'", m_uiType, TypeDeduction<T>::value);
271 return Cast<StorageType>() == other;
281 return IsNumberStatic(m_uiType);
286 return IsFloatingPointStatic(m_uiType);
291 return IsStringStatic(m_uiType);
296 return IsHashedStringStatic(m_uiType);
302 return m_uiType == TypeDeduction<T>::value;
308 if (m_uiType == TypeDeduction<T>::value)
312 if constexpr (std::is_same<T, void*>::value || std::is_same<T, const void*>::value)
316 else if (ptr.m_pType)
319 const plRTTI* pType = plGetStaticRTTI<NonPointerT>();
320 return IsDerivedFrom(ptr.m_pType, pType);
322 else if (!ptr.m_pObject)
334 return m_uiType == TypeDeduction<T>::value;
341 if (m_uiType == TypeDeduction<T>::value)
345 return IsDerivedFrom(pType, plGetStaticRTTI<NonRefT>());
359 PL_ASSERT_DEV(
IsA<T>(),
"Stored type '{0}' does not match requested type '{1}'", m_uiType, TypeDeduction<T>::value);
366 PL_ASSERT_DEV(
IsA<T>(),
"Stored type '{0}' does not match requested type '{1}'", m_uiType, TypeDeduction<T>::value);
373 PL_ASSERT_DEV(
IsA<T>(),
"Stored type '{0}' does not match requested type '{1}'", m_uiType, TypeDeduction<T>::value);
380 PL_ASSERT_DEV(m_uiType == TypeDeduction<T>::value,
"Stored type '{0}' does not match requested type '{1}'", m_uiType, TypeDeduction<T>::value);
385PL_ALWAYS_INLINE T& plVariant::GetWritable()
388 return const_cast<T&
>(
Get<T>());
392PL_ALWAYS_INLINE T plVariant::GetWritable()
395 return const_cast<T
>(
Get<T>());
399PL_ALWAYS_INLINE T& plVariant::GetWritable()
402 return const_cast<T&
>(
Get<T>());
409 return Cast<plTypedPointer>().m_pObject;
411 return m_bIsShared ? m_Data.shared->m_Ptr : &m_Data;
425 if (out_pConversionStatus !=
nullptr)
426 *out_pConversionStatus = PL_FAILURE;
433 if (out_pConversionStatus !=
nullptr)
434 *out_pConversionStatus = PL_SUCCESS;
440 bool bSuccessful =
true;
441 plVariantHelper::To(*
this, result, bSuccessful);
443 if (out_pConversionStatus !=
nullptr)
444 *out_pConversionStatus = bSuccessful ? PL_SUCCESS : PL_FAILURE;
453PL_FORCE_INLINE
void plVariant::InitInplace(
const T& value)
456 static_assert(
plGetTypeClass<T>::value <= plTypeIsMemRelocatable::value,
"in place data needs to be POD or mem relocatable");
457 static_assert(
sizeof(T) <=
sizeof(m_Data),
"value of this type is too big to bestored inline in a Variant");
465PL_FORCE_INLINE
void plVariant::InitTypedObject(
const T& value,
plTraitInt<0>)
467 using StorageType =
typename TypeDeduction<T>::StorageType;
469 static_assert((
sizeof(StorageType) >
sizeof(InlinedStruct::DataSize)) || TypeDeduction<T>::forceSharing,
"Value should be inplace instead.");
470 static_assert(TypeDeduction<T>::value ==
Type::TypedObject,
"value of this type cannot be stored in a Variant");
471 const plRTTI* pType = plGetStaticRTTI<T>();
472 m_Data.shared = PL_DEFAULT_NEW(TypedSharedData<StorageType>, value, pType);
478PL_FORCE_INLINE
void plVariant::InitTypedObject(
const T& value,
plTraitInt<1>)
480 using StorageType =
typename TypeDeduction<T>::StorageType;
481 static_assert((
sizeof(StorageType) <= InlinedStruct::DataSize) && !TypeDeduction<T>::forceSharing,
"Value can't be stored inplace.");
482 static_assert(TypeDeduction<T>::value ==
Type::TypedObject,
"value of this type cannot be stored in a Variant");
485 m_Data.inlined.m_pType = plGetStaticRTTI<T>();
490inline void plVariant::Release()
494 if (m_Data.shared->m_uiRef.
Decrement() == 0)
496 PL_DEFAULT_DELETE(m_Data.shared);
501inline void plVariant::CopyFrom(
const plVariant& other)
503 m_uiType = other.m_uiType;
504 m_bIsShared = other.m_bIsShared;
508 m_Data.shared = other.m_Data.shared;
513 m_Data = other.m_Data;
517PL_ALWAYS_INLINE
void plVariant::MoveFrom(
plVariant&& other)
519 m_uiType = other.m_uiType;
520 m_bIsShared = other.m_bIsShared;
521 m_Data = other.m_Data;
524 other.m_bIsShared =
false;
525 other.m_Data.shared =
nullptr;
529const T& plVariant::Cast()
const
532 static_assert(validType,
"Invalid Cast, can only cast to storage type");
534 return m_bIsShared ? *
static_cast<const T*
>(m_Data.shared->m_Ptr) : *reinterpret_cast<const T*>(&m_Data);
538T plVariant::Cast()
const
543 PL_IGNORE_UNUSED(pType);
545 if constexpr (!std::is_same<T, void*>::value && !std::is_same<T, const void*>::value)
547 PL_ASSERT_DEV(pType ==
nullptr || IsDerivedFrom(pType, plGetStaticRTTI<NonRefPtrT>()),
"Object of type '{0}' does not derive from '{}'", GetTypeName(pType), GetTypeName(plGetStaticRTTI<NonRefPtrT>()));
549 return static_cast<T
>(ptr.m_pObject);
553const T plVariant::Cast()
const
562const T& plVariant::Cast()
const
565 PL_IGNORE_UNUSED(pType);
567 PL_ASSERT_DEV(IsDerivedFrom(pType, plGetStaticRTTI<NonRefT>()),
"Object of type '{0}' does not derive from '{}'", GetTypeName(pType), GetTypeName(plGetStaticRTTI<NonRefT>()));
569 return m_bIsShared ? *
static_cast<const T*
>(m_Data.shared->m_Ptr) : *reinterpret_cast<const T*>(&m_Data);
572PL_ALWAYS_INLINE
bool plVariant::IsNumberStatic(plUInt32 type)
577PL_ALWAYS_INLINE
bool plVariant::IsFloatingPointStatic(plUInt32 type)
582PL_ALWAYS_INLINE
bool plVariant::IsStringStatic(plUInt32 type)
587PL_ALWAYS_INLINE
bool plVariant::IsHashedStringStatic(plUInt32 type)
592PL_ALWAYS_INLINE
bool plVariant::IsVector2Static(plUInt32 type)
597PL_ALWAYS_INLINE
bool plVariant::IsVector3Static(plUInt32 type)
602PL_ALWAYS_INLINE
bool plVariant::IsVector4Static(plUInt32 type)
608T plVariant::ConvertNumber()
const
613 return static_cast<T
>(Cast<bool>());
615 return static_cast<T
>(Cast<plInt8>());
617 return static_cast<T
>(Cast<plUInt8>());
619 return static_cast<T
>(Cast<plInt16>());
621 return static_cast<T
>(Cast<plUInt16>());
623 return static_cast<T
>(Cast<plInt32>());
625 return static_cast<T
>(Cast<plUInt32>());
627 return static_cast<T
>(Cast<plInt64>());
629 return static_cast<T
>(Cast<plUInt64>());
631 return static_cast<T
>(Cast<float>());
633 return static_cast<T
>(Cast<double>());
636 PL_REPORT_FAILURE(
"Variant is not a number");
643 PL_ALWAYS_INLINE
static plUInt32 Hash(
const plVariant& value)
646 return (plUInt32)uiHash;
Float wrapper struct for a safe usage and conversions of angles.
Definition Angle.h:10
T Increment()
Increments the internal value and returns the incremented value.
Definition AtomicInteger_inl.h:35
T Decrement()
Decrements the internal value and returns the decremented value.
Definition AtomicInteger_inl.h:41
A 8bit per channel unsigned normalized (values interpreted as 0-1) color storage format that represen...
Definition Color8UNorm.h:99
plColor represents an RGBA color in linear color space. Values are stored as float,...
Definition Color.h:44
This class is optimized to take nearly no memory (sizeof(void*)) and to allow very fast checks whethe...
Definition HashedString.h:25
static void CopyConstruct(Destination *pDestination, const Source ©, size_t uiCount=1)
Constructs uiCount objects of type T in a raw buffer at pDestination, by creating uiCount copies of c...
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
A class to use together with plHashedString for quick comparisons with temporary strings that need no...
Definition HashedString.h:151
This data type is the abstraction for 128-bit Uuid (also known as GUID) instances.
Definition Uuid.h:11
plVariant is a class that can store different types of variables, which is useful in situations where...
Definition Variant.h:44
const plRTTI * GetReflectedType() const
Returns the plRTTI type of the held value. For TypedPointer and TypedObject this will return the type...
Definition Variant.cpp:593
plVariant()
Initializes the variant to be 'Invalid'.
Definition Variant_inl.h:5
bool IsHashedString() const
Returns whether the stored type is a hashed string (plHashedString or plTempHashedString).
Definition Variant_inl.h:294
bool IsValid() const
Returns whether this variant stores any other type than 'Invalid'.
Definition Variant_inl.h:274
bool operator==(const plVariant &other) const
Will compare the value of this variant to that of other.
Definition Variant.cpp:349
bool IsNumber() const
Returns whether the stored type is numerical type either integer or floating point.
Definition Variant_inl.h:279
~plVariant()
If necessary, this will deallocate any heap memory that is not in use any more.
Definition Variant_inl.h:189
const void * GetData() const
Returns a const void* to the internal data. For TypedPointer and TypedObject this will return a point...
Definition Variant_inl.h:405
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
plUInt64 ComputeHash(plUInt64 uiSeed=0) const
Computes the hash value of the stored data. Returns uiSeed (unchanged) for an invalid Variant.
Definition Variant.cpp:532
bool IsString() const
Returns whether the stored type is a string (plString or plStringView).
Definition Variant_inl.h:289
bool IsFloatingPoint() const
Returns whether the stored type is floating point (float or double).
Definition Variant_inl.h:284
Type::Enum GetType() const
Returns the exact plVariant::Type value.
Definition Variant_inl.h:351
bool CanConvertTo() const
Returns whether the stored type can generally be converted to the desired type.
void operator=(const plVariant &other)
Copies the data from the other variant into this one.
Definition Variant_inl.h:194
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.
Static Conversion Test.
Definition TypeTraits.h:73
If there is an % operator which takes a plTypeIsMemRelocatable and returns a CompileTimeTrueType T is...
Definition TypeTraits.h:67
Helper struct to calculate the Hash of different types.
Definition HashingUtils.h:75
If there is an % operator which takes a TypeIsPod and returns a CompileTimeTrueType T is Pod....
Definition TypeTraits.h:43
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
Type traits.
Definition TypeTraits.h:12
typename std::remove_const< typename std::remove_reference< T >::type >::type NonConstReferenceType
removes reference and const qualifier
Definition TypeTraits.h:218
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
Defines a reference to an immutable object owned by an plVariant.
Definition Variant.h:24
A typed raw pointer.
Definition TypedPointer.h:13
@ CustomTypeCast
Custom object types.
Definition VariantType.h:90
@ PointerCast
Any cast to T*.
Definition VariantType.h:88
@ DirectCast
A standard type.
Definition VariantType.h:87
@ TypedObject
plTypedObject cast. Needed because at no point does and plVariant ever store a plTypedObject so it ca...
Definition VariantType.h:89
A helper struct to convert the C++ type, which is passed as the template argument,...
Definition VariantType.h:97
Enum
This enum describes the type of data that is currently stored inside the variant. Note that changes t...
Definition VariantType.h:26
@ UInt8
The variant stores an plUInt8.
Definition VariantType.h:33
@ Vector3I
The variant stores an plVec3I32.
Definition VariantType.h:47
@ Int8
The variant stores an plInt8.
Definition VariantType.h:32
@ TempHashedString
The variant stores an plTempHashedString value.
Definition VariantType.h:64
@ Vector2U
The variant stores an plVec2U32.
Definition VariantType.h:49
@ Bool
The variant stores a bool.
Definition VariantType.h:31
@ Int32
The variant stores an plInt32.
Definition VariantType.h:36
@ Vector3
The variant stores an plVec3.
Definition VariantType.h:44
@ Vector4I
The variant stores an plVec4I32.
Definition VariantType.h:48
@ String
The variant stores a string. A heap allocation is required to store this data type.
Definition VariantType.h:56
@ Double
The variant stores a double.
Definition VariantType.h:41
@ 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
@ UInt16
The variant stores an plUInt16.
Definition VariantType.h:35
@ Float
The variant stores a float.
Definition VariantType.h:40
@ Vector2I
The variant stores an plVec2I32.
Definition VariantType.h:46
@ StringView
The variant stores an plStringView.
Definition VariantType.h:57
@ Vector3U
The variant stores an plVec3U32.
Definition VariantType.h:50
@ HashedString
The variant stores an plHashedString value.
Definition VariantType.h:63
@ Vector2
The variant stores an plVec2.
Definition VariantType.h:43
@ UInt32
The variant stores an plUInt32.
Definition VariantType.h:37
@ Vector4U
The variant stores an plVec4U32.
Definition VariantType.h:51
@ Invalid
The variant stores no (valid) data at the moment.
Definition VariantType.h:27
@ Int16
The variant stores an plInt16.
Definition VariantType.h:34
@ UInt64
The variant stores an plUInt64.
Definition VariantType.h:39
@ Vector4
The variant stores an plVec4.
Definition VariantType.h:45
@ TypedObject
The variant stores an plTypedObject value. Reflected type and data queries will match the object....
Definition VariantType.h:72
@ Int64
The variant stores an plInt64.
Definition VariantType.h:38