using System.Collections;
using System.Diagnostics.CodeAnalysis;
namespace PkmnLib.Dynamic.ScriptHandling;
///
/// A holder class for a script. This is used so we can cache a list of these, and iterate over them, even when
/// the underlying script changes.
///
public class ScriptContainer : IEnumerable
{
///
public ScriptContainer()
{
}
///
public ScriptContainer(Script script)
{
Script = script;
}
///
/// Whether this container is empty.
///
[MemberNotNullWhen(false, nameof(ScriptHandling.Script))]
public bool IsEmpty => Script is null;
///
/// The script in this container.
///
public Script? Script { get; private set; }
///
public IEnumerator GetEnumerator()
{
yield return this;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
///
/// Assigns a new script to this container. If there was a script already, it is removed.
///
///
public void Set(Script script)
{
if (Script is not null)
{
Script.OnRemove();
Script.MarkForDeletion();
}
Script = script;
}
///
/// Removes the script from this container.
///
public void Clear()
{
if (Script is not null)
{
Script.OnRemove();
Script.MarkForDeletion();
}
Script = null;
}
}