MoonTools.ECS/src/MessageStorage.cs

64 lines
1.1 KiB
C#
Raw Normal View History

2022-04-08 05:52:03 +00:00
using System;
using MoonTools.ECS.Collections;
2022-03-07 07:17:05 +00:00
2023-11-03 19:40:26 +00:00
namespace MoonTools.ECS;
public class MessageStorage : IDisposable
2022-03-07 07:17:05 +00:00
{
2023-11-03 19:40:26 +00:00
private NativeArray Messages;
private bool IsDisposed;
public MessageStorage(int elementSize)
2022-03-07 07:17:05 +00:00
{
2023-11-03 19:40:26 +00:00
Messages = new NativeArray(elementSize);
2022-03-07 07:17:05 +00:00
}
2023-11-03 19:40:26 +00:00
public void Add<T>(in T message) where T : unmanaged
2022-03-07 07:17:05 +00:00
{
2023-11-03 19:40:26 +00:00
Messages.Append(message);
}
2022-04-08 05:52:03 +00:00
2023-11-03 19:40:26 +00:00
public bool Some()
{
return Messages.Count > 0;
}
2022-03-18 19:50:59 +00:00
2023-11-03 19:40:26 +00:00
public ReadOnlySpan<T> All<T>() where T : unmanaged
{
return Messages.ToSpan<T>();
}
2022-03-18 19:50:59 +00:00
2023-11-03 19:40:26 +00:00
public T First<T>() where T : unmanaged
{
return Messages.Get<T>(0);
}
2022-03-18 19:50:59 +00:00
2023-11-03 19:40:26 +00:00
public void Clear()
{
Messages.Clear();
}
2023-11-03 19:40:26 +00:00
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
2023-11-03 19:40:26 +00:00
Messages.Dispose();
IsDisposed = true;
}
2023-11-03 19:40:26 +00:00
}
2023-11-03 19:40:26 +00:00
// ~MessageStorage()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
2023-11-03 19:40:26 +00:00
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
2022-03-07 07:17:05 +00:00
}
}