2019-06-16 01:55:35 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace Encompass
|
|
|
|
{
|
|
|
|
internal class MessageManager
|
|
|
|
{
|
2019-06-20 17:46:15 +00:00
|
|
|
private readonly Dictionary<Type, List<IMessage>> messageTypeToMessages = new Dictionary<Type, List<IMessage>>();
|
2019-06-16 01:55:35 +00:00
|
|
|
|
|
|
|
internal void AddMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
|
|
|
|
{
|
|
|
|
if (!messageTypeToMessages.ContainsKey(typeof(TMessage)))
|
|
|
|
{
|
|
|
|
messageTypeToMessages.Add(typeof(TMessage), new List<IMessage>());
|
|
|
|
}
|
2019-06-17 01:03:57 +00:00
|
|
|
|
|
|
|
messageTypeToMessages[typeof(TMessage)].Add(message);
|
2019-06-16 01:55:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal void ClearMessages()
|
|
|
|
{
|
|
|
|
foreach (var entry in messageTypeToMessages)
|
|
|
|
{
|
|
|
|
entry.Value.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal IEnumerable<TMessage> GetMessagesByType<TMessage>() where TMessage : struct, IMessage
|
|
|
|
{
|
2019-06-22 00:23:52 +00:00
|
|
|
return messageTypeToMessages.ContainsKey(typeof(TMessage)) ?
|
|
|
|
messageTypeToMessages[typeof(TMessage)].Cast<TMessage>() :
|
|
|
|
Enumerable.Empty<TMessage>();
|
2019-06-16 01:55:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|