MoonTools.ECS/src/MessageDepot.cs

40 lines
847 B
C#
Raw Normal View History

2022-03-07 07:17:05 +00:00
namespace MoonTools.ECS;
internal class MessageDepot
{
private Dictionary<Type, MessageStorage> storages = new Dictionary<Type, MessageStorage>();
private MessageStorage<TMessage> Lookup<TMessage>() where TMessage : struct
{
if (!storages.ContainsKey(typeof(TMessage)))
{
storages.Add(typeof(TMessage), new MessageStorage<TMessage>());
}
return storages[typeof(TMessage)] as MessageStorage<TMessage>;
}
public void Add<TMessage>(in TMessage message) where TMessage : struct
{
Lookup<TMessage>().Add(message);
}
public bool Some<TMessage>() where TMessage : struct
{
return Lookup<TMessage>().Some();
}
public ReadOnlySpan<TMessage> Read<TMessage>() where TMessage : struct
{
return Lookup<TMessage>().All();
}
public void Clear()
{
foreach (var storage in storages.Values)
{
storage.Clear();
}
}
}