using System; using System.Collections; using System.Collections.Generic; namespace Capnp { /// /// ListDeserializer specialization for List(T) when T is unknown (generic), List(Data), List(Text), and List(List(...)). /// public class ListOfPointersDeserializer: ListDeserializer, IReadOnlyList { internal ListOfPointersDeserializer(in DeserializerState state) : base(state) { } /// /// Always ListKind.ListOfPointers /// public override ListKind Kind => ListKind.ListOfPointers; /// /// Gets the DeserializerState representing the element at given index. /// /// Element index /// DeserializerState representing the element at given index public DeserializerState this[int index] { get { if (index < 0 || index >= Count) throw new IndexOutOfRangeException(); var state = State; state.DecodePointer(index); return state; } } IEnumerable Enumerate() { for (int i = 0; i < Count; i++) yield return this[i]; } /// /// Implements . /// public IEnumerator GetEnumerator() { return Enumerate().GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } /// /// Applies a selector function to each element. /// /// Element target type /// Selector function /// The desired representation public override IReadOnlyList Cast(Func cons) { return this.LazyListSelect(cons); } /// /// Interprets this instance as List(List(...)). /// /// The desired representation. Since it is evaluated lazily, type conflicts will not happen before accessing the resulting list's elements. public override IReadOnlyList CastList() { return this.LazyListSelect(d => d.RequireList()); } /// /// Interprets this instance as a list of capabilities. /// /// Capability interface /// The desired representation. Since it is evaluated lazily, type conflicts will not happen before accessing the resulting list's elements. public override IReadOnlyList CastCapList() { return State.RequireCapList(); } } }