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

50 lines
1.4 KiB
C#

namespace Qrakhen.Qamp.Core.Values.Objects;
public class List(IEnumerable<Value> data) : ItemProvider<int>(ValueType.List)
{
public List<Value> Data = [..data];
public void Add(Value value)
{
Data.Add(value);
}
public void Remove(Value index)
{
int _index = ExtractIndex(index);
AssertValidAccesor(_index);
Data.RemoveAt(_index);
}
protected override void InnerSet(int index, Value value)
{
Data[index] = value;
}
protected override Value InnerGet(int index)
{
return Data[index];
}
protected override void AssertValidAccesor(int index)
{
if (index < 0 || index > Data.Count)
throw new ItemProviderException($"Can not index '{index}' of list, as the index is outside its boundaries.", this);
}
protected override int ExtractIndex(Value value)
{
int index;
if (value.IsSigned)
index = (int)value.Signed;
else if (value.IsUnsigned)
index = (int)value.Unsigned;
else
throw new ItemProviderException($"Can not use {value} as an accessor, as it is not an integer.", this);
return index;
}
public override string ToString() => ToString(true);
public string ToString(bool detail) => detail ? $":[{string.Join(", ", Data)}]" : $"List[{Data.Count}]";
}