Plasma Engine  2.0
Loading...
Searching...
No Matches
StateMachineInstanceData.h
1#pragma once
2
3#include <GameEngine/GameEngineDLL.h>
4
5#include <Foundation/Containers/Blob.h>
6#include <Foundation/Containers/SmallArray.h>
7#include <Foundation/Memory/InstanceDataAllocator.h>
8
9namespace plStateMachineInternal
10{
12 struct PL_GAMEENGINE_DLL Compound
13 {
14 PL_ALWAYS_INLINE plUInt32 GetBaseOffset() const { return m_InstanceDataOffsets.GetUserData<plUInt32>(); }
15 PL_ALWAYS_INLINE plUInt32 GetDataSize() const { return m_InstanceDataAllocator.GetTotalDataSize(); }
16
17 plSmallArray<plUInt32, 2> m_InstanceDataOffsets;
18 plInstanceDataAllocator m_InstanceDataAllocator;
19
21 {
22 const Compound* m_pOwner = nullptr;
23
25 {
26 if (m_pOwner != nullptr)
27 {
28 m_pOwner->m_InstanceDataAllocator.Destruct(GetBlobPtr());
29 }
30 }
31
32 PL_ALWAYS_INLINE plByteBlobPtr GetBlobPtr()
33 {
34 return plByteBlobPtr(plMemoryUtils::AddByteOffset(reinterpret_cast<plUInt8*>(this), m_pOwner->GetBaseOffset()), m_pOwner->GetDataSize());
35 }
36 };
37
38 PL_ALWAYS_INLINE void* GetSubInstanceData(InstanceData* pData, plUInt32 uiIndex) const
39 {
40 return pData != nullptr ? m_InstanceDataAllocator.GetInstanceData(pData->GetBlobPtr(), m_InstanceDataOffsets[uiIndex]) : nullptr;
41 }
42
43 PL_FORCE_INLINE void Initialize(InstanceData* pData) const
44 {
45 if (pData != nullptr && pData->m_pOwner == nullptr)
46 {
47 pData->m_pOwner = this;
48 m_InstanceDataAllocator.Construct(pData->GetBlobPtr());
49 }
50 }
51
52 template <typename T>
53 bool GetInstanceDataDesc(plArrayPtr<T*> subObjects, plInstanceDataDesc& out_desc)
54 {
55 m_InstanceDataOffsets.Clear();
56 m_InstanceDataAllocator.ClearDescs();
57
58 plUInt32 uiMaxAlignment = 0;
59
60 plInstanceDataDesc instanceDataDesc;
61 for (T* pSubObject : subObjects)
62 {
63 plUInt32 uiOffset = plInvalidIndex;
64 if (pSubObject->GetInstanceDataDesc(instanceDataDesc))
65 {
66 uiOffset = m_InstanceDataAllocator.AddDesc(instanceDataDesc);
67 uiMaxAlignment = plMath::Max(uiMaxAlignment, instanceDataDesc.m_uiTypeAlignment);
68 }
69 m_InstanceDataOffsets.PushBack(uiOffset);
70 }
71
72 if (uiMaxAlignment > 0)
73 {
74 out_desc.FillFromType<InstanceData>();
75 out_desc.m_ConstructorFunction = nullptr; // not needed, instance data is constructed on first OnEnter
76
77 plUInt32 uiBaseOffset = plMemoryUtils::AlignSize(out_desc.m_uiTypeSize, uiMaxAlignment);
78 m_InstanceDataOffsets.GetUserData<plUInt32>() = uiBaseOffset;
79
80 out_desc.m_uiTypeSize = uiBaseOffset + m_InstanceDataAllocator.GetTotalDataSize();
81 out_desc.m_uiTypeAlignment = plMath::Max(out_desc.m_uiTypeAlignment, uiMaxAlignment);
82
83 return true;
84 }
85
86 return false;
87 }
88 };
89} // namespace plStateMachineInternal
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
Helper class to manager instance data allocation, construction and destruction.
Definition InstanceDataAllocator.h:35
void ClearDescs()
Resets all internal state.
Definition InstanceDataAllocator.cpp:15
void Construct(plByteBlobPtr blobPtr) const
Constructs the instance data objects, within the pre-allocated memory block.
Definition InstanceDataAllocator.cpp:43
plUInt32 AddDesc(const plInstanceDataDesc &desc)
Adds the given desc to internal list of data that needs to be allocated and returns the byte offset.
Definition InstanceDataAllocator.cpp:5
plUInt32 GetTotalDataSize() const
The total size in bytes taken up by all instance data objects that were added.
Definition InstanceDataAllocator.h:56
void Destruct(plByteBlobPtr blobPtr) const
Destructs the instance data objects.
Definition InstanceDataAllocator.cpp:59
static PL_ALWAYS_INLINE void * GetInstanceData(const plByteBlobPtr &blobPtr, plUInt32 uiOffset)
Retrieves a void pointer to the instance data within the given blob at the given offset,...
Definition InstanceDataAllocator.h:59
static T * AddByteOffset(T *pPtr, std::ptrdiff_t offset)
Returns the address stored in ptr plus the given byte offset iOffset, cast to type T.
static T AlignSize(T uiSize, T uiAlignment)
Aligns the given size uiSize by rounding up to the next multiple of the size.
void Clear()
Clears the array.
Definition SmallArray_inl.h:222
Definition SmallArray.h:219
constexpr PL_ALWAYS_INLINE T Max(T f1, T f2)
Returns the greater value, f1 or f2.
Definition Math_inl.h:39
Structure to describe an instance data type.
Definition InstanceDataAllocator.h:17
Definition StateMachineInstanceData.h:21
Helper class to manage instance data for compound states or transitions.
Definition StateMachineInstanceData.h:13