PkmnLib.NET/PkmnLib.Dynamic/ScriptHandling/ScriptContainer.cs

72 lines
1.7 KiB
C#

using System.Collections;
using System.Diagnostics.CodeAnalysis;
namespace PkmnLib.Dynamic.ScriptHandling;
/// <summary>
/// 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.
/// </summary>
public class ScriptContainer : IEnumerable<ScriptContainer>
{
/// <inheritdoc cref="ScriptContainer"/>
public ScriptContainer()
{
}
/// <inheritdoc cref="ScriptContainer"/>
public ScriptContainer(Script script)
{
Script = script;
}
/// <summary>
/// Whether this container is empty.
/// </summary>
[MemberNotNullWhen(false, nameof(ScriptHandling.Script))]
public bool IsEmpty => Script is null;
/// <summary>
/// The script in this container.
/// </summary>
public Script? Script { get; private set; }
/// <inheritdoc />
public IEnumerator<ScriptContainer> GetEnumerator()
{
yield return this;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
/// <summary>
/// Assigns a new script to this container. If there was a script already, it is removed.
/// </summary>
/// <param name="script"></param>
public void Set(Script script)
{
if (Script is not null)
{
Script.OnRemove();
Script.MarkForDeletion();
}
Script = script;
}
/// <summary>
/// Removes the script from this container.
/// </summary>
public void Clear()
{
if (Script is not null)
{
Script.OnRemove();
Script.MarkForDeletion();
}
Script = null;
}
}