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

57 lines
1.5 KiB
C#

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 Ptr;
public T Current {
get => Get();
set => Set(value);
}
public Pointer(IGetSet<long, T> target, long pointer = 0)
{
Target = target;
Ptr = pointer;
}
public Pointer<T> Branch(long delta = 0)
{
return new Pointer<T>(Target, Ptr + delta);
}
/// <summary>
/// Returns the next item from <see cref="Target"/> and increases the pointer by 1.
/// </summary>
public T Next() => Target.Get(Ptr++);
/// <summary>
/// Sets the value of <see cref="Target"/> at <paramref name="position"/>.
/// </summary>
public void Set(long position, T value) => Target.Set(position, value);
/// <summary>
/// Sets the value of <see cref="Target"/> at the current <see cref="Ptr"/> location.
/// </summary>
public void Set(T value) => Set(Ptr, value);
/// <summary>
/// Gets the value of <see cref="Target"/> at <paramref name="position"/>.
/// </summary>
public T Get(long position) => Target.Get(position);
/// <summary>
/// Gets the value of <see cref="Target"/> at the current <see cref="Ptr"/> location.
/// </summary>
public T Get() => Get(Ptr);
public override string ToString()
{
return $"&{Ptr:X4}={Current}";
}
}