qamp/Qrakhen.Qamp.Core/Collections/Pointer.cs

59 lines
1.6 KiB
C#

using Qrakhen.Qamp.Core.Collections.Abstractions;
namespace Qrakhen.Qamp.Core.Collections;
/// <summary>
/// Dynamic Pointer able to point to any object implementing the <see cref="IGetSet{long, TValue}"/> interface.
/// </summary>
public class Pointer<T>
{
protected IGetSet<long, T> Target;
public long Cursor;
public T Current {
get => Get();
set => Set(value);
}
public Pointer(IGetSet<long, T> target, long pointer = 0)
{
Target = target;
Cursor = pointer;
}
public Pointer<T> Branch(long delta = 0)
{
return new Pointer<T>(Target, Cursor + delta);
}
/// <summary>
/// Returns the next item from <see cref="Target"/> and increases the pointer by 1.
/// </summary>
public T Next() => Target.Get(Cursor++);
/// <summary>
/// Sets the value of <see cref="Target"/> RELATIVE from the current position (<paramref name="delta"/>).
/// </summary>
public void Set(long delta, T value) => Target.Set(Cursor + delta, value);
/// <summary>
/// Sets the value of <see cref="Target"/> at the current <see cref="Cursor"/> location.
/// </summary>
public void Set(T value) => Set(Cursor, value);
/// <summary>
/// Gets the value of <see cref="Target"/> RELATIVE from the current position (<paramref name="delta"/>).
/// </summary>
public T Get(long delta) => Target.Get(Cursor + delta);
/// <summary>
/// Gets the value of <see cref="Target"/> at the current <see cref="Cursor"/> location.
/// </summary>
public T Get() => Get(Cursor);
public override string ToString()
{
return $"&{Cursor:X4}={Current}";
}
}