You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.6 KiB
60 lines
1.6 KiB
using System; |
|
using Unity.Collections; |
|
using Unity.Collections.LowLevel.Unsafe; |
|
|
|
namespace GameCore.LowLevel |
|
{ |
|
public unsafe struct NativeDataContainer<T> : IDisposable where T : unmanaged |
|
{ |
|
[NativeDisableUnsafePtrRestriction] |
|
internal UnsafeDataContainer* m_UnsafeDataPtr; |
|
internal AllocatorManager.AllocatorHandle m_Allocator; |
|
|
|
public bool IsCreated => m_UnsafeDataPtr != null; |
|
|
|
public NativeDataContainer(AllocatorManager.AllocatorHandle allocator) |
|
{ |
|
m_UnsafeDataPtr = AllocatorManager.Allocate<UnsafeDataContainer>(allocator); |
|
*m_UnsafeDataPtr = new UnsafeDataContainer(UnsafeUtility.SizeOf<T>(), allocator); |
|
|
|
m_Allocator = allocator; |
|
} |
|
|
|
public void Dispose() |
|
{ |
|
if (!IsCreated) |
|
{ |
|
return; |
|
} |
|
|
|
m_UnsafeDataPtr->Dispose(); |
|
AllocatorManager.Free(m_Allocator, m_UnsafeDataPtr); |
|
m_UnsafeDataPtr = null; |
|
} |
|
|
|
public bool HasValue(int index) |
|
{ |
|
return m_UnsafeDataPtr->HasValue(index); |
|
} |
|
|
|
public bool Free(int index) |
|
{ |
|
return m_UnsafeDataPtr->Free(index); |
|
} |
|
|
|
public int Alloc(UnsafeDataContainer.AllocOptions options = UnsafeDataContainer.AllocOptions.None) |
|
{ |
|
return m_UnsafeDataPtr->Alloc(options); |
|
} |
|
|
|
public ref T ElementAt(int index) |
|
{ |
|
return ref m_UnsafeDataPtr->ElementAt<T>(index); |
|
} |
|
|
|
public void Clear() |
|
{ |
|
m_UnsafeDataPtr->Clear(); |
|
} |
|
} |
|
} |