using System;
using System.Collections;
using System.Collections.Generic;
namespace Capnp
{
///
/// ListDeserializer specialization for a list of capabilities.
///
/// Capability interface
public class ListOfCapsDeserializer : ListDeserializer, IReadOnlyList
where T: class
{
internal ListOfCapsDeserializer(in DeserializerState state) : base(state)
{
Rpc.CapabilityReflection.ValidateCapabilityInterface(typeof(T));
}
///
/// Returns the capability at given index.
///
/// Element index
/// The capability at given index (in terms of its proxy instance)
public T this[int index]
{
get
{
if (index < 0 || index >= Count)
throw new IndexOutOfRangeException();
return (Rpc.CapabilityReflection.CreateProxy(State.DecodeCapPointer(index)) as T)!;
}
}
///
/// Always ListKind.ListOfPointers
///
public override ListKind Kind => ListKind.ListOfPointers;
///
/// Always throws , since it is not intended to convert a capability list to anything else.
///
public override IReadOnlyList Cast(Func cons)
{
throw new NotSupportedException("Cannot cast a list of capabilities to anything else");
}
IEnumerable Enumerate()
{
int count = Count;
for (int i = 0; i < count; i++)
{
yield return this[i];
}
}
///
/// Implements .
///
///
public IEnumerator GetEnumerator()
{
return Enumerate().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}