Plasma Engine  2.0
Loading...
Searching...
No Matches
Casts.h
1#pragma once
2
4
5#include <Foundation/Reflection/Implementation/DynamicRTTI.h>
6
7PL_WARNING_PUSH()
8PL_WARNING_DISABLE_CLANG("-Wunused-local-typedef")
9PL_WARNING_DISABLE_GCC("-Wunused-local-typedefs")
10
14template <typename T>
15PL_ALWAYS_INLINE T plStaticCast(plReflectedClass* pObject)
16{
17 using NonPointerT = typename plTypeTraits<T>::NonPointerType;
18 PL_ASSERT_DEV(pObject == nullptr || pObject->IsInstanceOf<NonPointerT>(), "Invalid static cast: Object of type '{0}' is not an instance of '{1}'",
19 pObject->GetDynamicRTTI()->GetTypeName(), plGetStaticRTTI<NonPointerT>()->GetTypeName());
20 return static_cast<T>(pObject);
21}
22
26template <typename T>
27PL_ALWAYS_INLINE T plStaticCast(const plReflectedClass* pObject)
28{
29 using NonPointerT = typename plTypeTraits<T>::NonConstReferencePointerType;
30 PL_ASSERT_DEV(pObject == nullptr || pObject->IsInstanceOf<NonPointerT>(), "Invalid static cast: Object of type '{0}' is not an instance of '{1}'",
31 pObject->GetDynamicRTTI()->GetTypeName(), plGetStaticRTTI<NonPointerT>()->GetTypeName());
32 return static_cast<T>(pObject);
33}
34
38template <typename T>
39PL_ALWAYS_INLINE T plStaticCast(plReflectedClass& in_object)
40{
41 using NonReferenceT = typename plTypeTraits<T>::NonReferenceType;
42 PL_ASSERT_DEV(in_object.IsInstanceOf<NonReferenceT>(), "Invalid static cast: Object of type '{0}' is not an instance of '{1}'",
43 in_object.GetDynamicRTTI()->GetTypeName(), plGetStaticRTTI<NonReferenceT>()->GetTypeName());
44 return static_cast<T>(in_object);
45}
46
50template <typename T>
51PL_ALWAYS_INLINE T plStaticCast(const plReflectedClass& object)
52{
53 using NonReferenceT = typename plTypeTraits<T>::NonConstReferenceType;
54 PL_ASSERT_DEV(object.IsInstanceOf<NonReferenceT>(), "Invalid static cast: Object of type '{0}' is not an instance of '{1}'",
55 object.GetDynamicRTTI()->GetTypeName(), plGetStaticRTTI<NonReferenceT>()->GetTypeName());
56 return static_cast<T>(object);
57}
58
62template <typename T>
63PL_ALWAYS_INLINE T plDynamicCast(plReflectedClass* pObject)
64{
65 if (pObject)
66 {
67 using NonPointerT = typename plTypeTraits<T>::NonPointerType;
68 if (pObject->IsInstanceOf<NonPointerT>())
69 {
70 return static_cast<T>(pObject);
71 }
72 }
73 return nullptr;
74}
75
79template <typename T>
80PL_ALWAYS_INLINE T plDynamicCast(const plReflectedClass* pObject)
81{
82 if (pObject)
83 {
84 using NonPointerT = typename plTypeTraits<T>::NonConstReferencePointerType;
85 if (pObject->IsInstanceOf<NonPointerT>())
86 {
87 return static_cast<T>(pObject);
88 }
89 }
90 return nullptr;
91}
92
93PL_WARNING_POP()
PL_ALWAYS_INLINE plStringView GetTypeName() const
Returns the name of this type.
Definition RTTI.h:48
All classes that should be dynamically reflectable, need to be derived from this base class.
Definition DynamicRTTI.h:86
bool IsInstanceOf(const plRTTI *pType) const
Returns whether the type of this instance is of the given type or derived from it.
Definition DynamicRTTI.cpp:6
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
typename std::remove_pointer< T >::type NonPointerType
removes pointer
Definition TypeTraits.h:215
typename std::remove_reference< T >::type NonReferenceType
removes reference
Definition TypeTraits.h:212