qamp/Qrakhen.Qamp.Core/Values/Objects/Outer.cs

41 lines
1.1 KiB
C#

using Qrakhen.Qamp.Core.Collections;
namespace Qrakhen.Qamp.Core.Values.Objects;
using T = ValueType;
/// <summary>
/// Wrapper to access values that are spread around the stack inside contexts
/// </summary>
public class Outer(Pointer<Value> target) : Obj(T.Outer)
{
private Value _stored = Value.Void;
public Pointer<Value> Target = target;
public bool IsClosed { get; private set; }
/// <summary>
/// Dynamic getter & setter that returns or writes to either the target value directly,
/// or the stored value if this <see cref="Outer"/> has been closed.
/// </summary>
public Value Value
{
get => IsClosed ? _stored : Target!.Get();
set {
if (IsClosed)
_stored = value;
else
Target.Set(value);
}
}
public Value Close()
{
if (IsClosed)
throw new Exception($"meh, something not right here (cant close already closed outer)");
_stored = Target!.Get();
IsClosed = true;
return _stored;
}
}