namespace OECS;
///
/// Internal store for pending interrupts. Owned by
/// and injected into so the handler's default Execute
/// can access it.
///
internal class InterruptStore
{
private readonly Dictionary _interrupts = new();
///
/// Stores an interrupt. Only one interrupt of a given type may be pending.
///
public void Add(T interrupt) where T : struct, IInterrupt
{
var type = typeof(T);
if (_interrupts.ContainsKey(type))
throw new InvalidOperationException(
$"An interrupt of type {type.Name} is already pending.");
_interrupts[type] = interrupt;
}
///
/// Tries to retrieve a pending interrupt of type .
///
public bool TryGet(out T interrupt) where T : struct, IInterrupt
{
if (_interrupts.TryGetValue(typeof(T), out var boxed))
{
interrupt = (T)boxed;
return true;
}
interrupt = default;
return false;
}
///
/// Removes the pending interrupt of type .
/// No-op if none is pending.
///
public bool Remove() where T : struct, IInterrupt
{
return _interrupts.Remove(typeof(T));
}
///
/// True if any interrupt is currently pending.
///
public bool HasAny => _interrupts.Count > 0;
///
/// True if an interrupt of type is pending.
///
public bool Has() where T : struct, IInterrupt
{
return _interrupts.ContainsKey(typeof(T));
}
///
/// Clears all pending interrupts. Used during world reset or serialization load.
///
internal void Clear()
{
_interrupts.Clear();
}
}