Plasma Engine  2.0
Loading...
Searching...
No Matches
VisualScriptData_inl.h
1
2PL_FORCE_INLINE void plVisualScriptDataDescription::CheckOffset(DataOffset dataOffset, const plRTTI* pType) const
3{
4#if PL_ENABLED(PL_COMPILE_FOR_DEBUG)
5 auto givenDataType = dataOffset.GetType();
6 auto& offsetAndCount = m_PerTypeInfo[givenDataType];
7 PL_ASSERT_DEBUG(offsetAndCount.m_uiCount > 0, "Invalid data offset");
8 const plUInt32 uiLastOffset = offsetAndCount.m_uiStartOffset + (offsetAndCount.m_uiCount - 1) * plVisualScriptDataType::GetStorageSize(givenDataType);
9 PL_ASSERT_DEBUG(dataOffset.m_uiByteOffset >= offsetAndCount.m_uiStartOffset && dataOffset.m_uiByteOffset <= uiLastOffset, "Invalid data offset");
10
11 if (pType != nullptr)
12 {
13 auto expectedDataType = plVisualScriptDataType::FromRtti(pType);
14 PL_ASSERT_DEBUG(expectedDataType == givenDataType, "Data type mismatch, expected '{}'({}) but got '{}'", plVisualScriptDataType::GetName(expectedDataType), pType->GetTypeName(), plVisualScriptDataType::GetName(givenDataType));
15 }
16#endif
17}
18
19PL_FORCE_INLINE plVisualScriptDataDescription::DataOffset plVisualScriptDataDescription::GetOffset(plVisualScriptDataType::Enum dataType, plUInt32 uiIndex, DataOffset::Source::Enum source) const
20{
21 auto& offsetAndCount = m_PerTypeInfo[dataType];
22 plUInt32 uiByteOffset = plInvalidIndex;
23 if (uiIndex < offsetAndCount.m_uiCount)
24 {
25 uiByteOffset = offsetAndCount.m_uiStartOffset + uiIndex * plVisualScriptDataType::GetStorageSize(dataType);
26 }
27
28 return DataOffset(uiByteOffset, dataType, source);
29}
30
32
33PL_ALWAYS_INLINE bool plVisualScriptDataStorage::IsAllocated() const
34{
35 return m_Storage.GetByteBlobPtr().IsEmpty() == false;
36}
37
38template <typename T>
39const T& plVisualScriptDataStorage::GetData(DataOffset dataOffset) const
40{
41 static_assert(!std::is_pointer<T>::value && !std::is_same<T, plTypedPointer>::value, "Use GetPointerData instead");
42
43 m_pDesc->CheckOffset(dataOffset, plGetStaticRTTI<T>());
44
45 return *reinterpret_cast<const T*>(m_Storage.GetByteBlobPtr().GetPtr() + dataOffset.m_uiByteOffset);
46}
47
48template <typename T>
49T& plVisualScriptDataStorage::GetWritableData(DataOffset dataOffset)
50{
51 static_assert(!std::is_pointer<T>::value && !std::is_same<T, plTypedPointer>::value, "Use GetPointerData instead");
52
53 m_pDesc->CheckOffset(dataOffset, plGetStaticRTTI<T>());
54
55 return *reinterpret_cast<T*>(m_Storage.GetByteBlobPtr().GetPtr() + dataOffset.m_uiByteOffset);
56}
57
58template <typename T>
59void plVisualScriptDataStorage::SetData(DataOffset dataOffset, const T& value)
60{
61 static_assert(!std::is_pointer<T>::value, "Use SetPointerData instead");
62
63 if (dataOffset.m_uiByteOffset < m_Storage.GetByteBlobPtr().GetCount())
64 {
65 m_pDesc->CheckOffset(dataOffset, plGetStaticRTTI<T>());
66
67 auto pData = m_Storage.GetByteBlobPtr().GetPtr() + dataOffset.m_uiByteOffset;
68
69 if constexpr (std::is_same<T, plGameObjectHandle>::value)
70 {
71 auto& storedHandle = *reinterpret_cast<plVisualScriptGameObjectHandle*>(pData);
72 storedHandle.AssignHandle(value);
73 }
74 else if constexpr (std::is_same<T, plComponentHandle>::value)
75 {
76 auto& storedHandle = *reinterpret_cast<plVisualScriptComponentHandle*>(pData);
77 storedHandle.AssignHandle(value);
78 }
79 else if constexpr (std::is_same<T, plStringView>::value)
80 {
81 *reinterpret_cast<plString*>(pData) = value;
82 }
83 else
84 {
85 *reinterpret_cast<T*>(pData) = value;
86 }
87 }
88}
89
90template <typename T>
91void plVisualScriptDataStorage::SetPointerData(DataOffset dataOffset, T ptr, const plRTTI* pType, plUInt32 uiExecutionCounter)
92{
93 static_assert(std::is_pointer<T>::value);
94
95 if (dataOffset.m_uiByteOffset < m_Storage.GetByteBlobPtr().GetCount())
96 {
97 auto pData = m_Storage.GetByteBlobPtr().GetPtr() + dataOffset.m_uiByteOffset;
98
99 if constexpr (std::is_same<T, plGameObject*>::value)
100 {
101 m_pDesc->CheckOffset(dataOffset, plGetStaticRTTI<plGameObject>());
102
103 auto& storedHandle = *reinterpret_cast<plVisualScriptGameObjectHandle*>(pData);
104 storedHandle.AssignPtr(ptr, uiExecutionCounter);
105 }
106 else if constexpr (std::is_same<T, plComponent*>::value)
107 {
108 m_pDesc->CheckOffset(dataOffset, plGetStaticRTTI<plComponent>());
109
110 auto& storedHandle = *reinterpret_cast<plVisualScriptComponentHandle*>(pData);
111 storedHandle.AssignPtr(ptr, uiExecutionCounter);
112 }
113 else
114 {
115 PL_ASSERT_DEBUG(!pType || pType->IsDerivedFrom<plComponent>() == false, "Component type '{}' is stored as typed pointer, cast to plComponent first to ensure correct storage", pType->GetTypeName());
116
117 m_pDesc->CheckOffset(dataOffset, pType);
118
119 auto& typedPointer = *reinterpret_cast<plTypedPointer*>(pData);
120 typedPointer.m_pObject = ptr;
121 typedPointer.m_pType = pType;
122 }
123 }
124}
125
127
128inline plResult plVisualScriptInstanceData::Serialize(plStreamWriter& inout_stream) const
129{
130 PL_SUCCEED_OR_RETURN(m_DataOffset.Serialize(inout_stream));
131 inout_stream << m_DefaultValue;
132 return PL_SUCCESS;
133}
134
135inline plResult plVisualScriptInstanceData::Deserialize(plStreamReader& inout_stream)
136{
137 PL_SUCCEED_OR_RETURN(m_DataOffset.Deserialize(inout_stream));
138 inout_stream >> m_DefaultValue;
139 return PL_SUCCESS;
140}
plByteBlobPtr GetByteBlobPtr()
Returns a blob pointer to the blob data, or an empty blob pointer if the blob is empty.
Definition Blob.h:366
PL_ALWAYS_INLINE PointerType GetPtr() const
Returns the pointer to the array.
Definition Blob.h:80
PL_ALWAYS_INLINE bool IsEmpty() const
Returns whether the array is empty.
Definition Blob.h:92
PL_ALWAYS_INLINE plUInt64 GetCount() const
Returns the number of elements in the array.
Definition Blob.h:95
Base class of all component types.
Definition Component.h:25
This class holds information about reflected types. Each instance represents one type that is known t...
Definition RTTI.h:30
PL_ALWAYS_INLINE plStringView GetTypeName() const
Returns the name of this type.
Definition RTTI.h:48
PL_ALWAYS_INLINE bool IsDerivedFrom(const plRTTI *pBaseType) const
Returns true if this type is derived from the given type (or of the same type).
Definition RTTI.h:60
Interface for binary in (read) streams.
Definition Stream.h:22
Interface for binary out (write) streams.
Definition Stream.h:107
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
A typed raw pointer.
Definition TypedPointer.h:13
Definition VisualScriptDataType.h:99
Definition VisualScriptData.h:10
Definition VisualScriptDataType.h:76