2019-06-12 21:56:55 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Capnp
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// ListDeserializer specialization for List(T) when T is a known struct (i.e. a list of fixed-width composites).
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ListOfStructsDeserializer: ListDeserializer, IReadOnlyList<DeserializerState>
|
|
|
|
|
{
|
2020-02-09 13:49:21 +01:00
|
|
|
|
internal ListOfStructsDeserializer(in DeserializerState context):
|
|
|
|
|
base(context)
|
2019-06-12 21:56:55 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Always returns <code>ListKind.ListOfStructs</code>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public override ListKind Kind => ListKind.ListOfStructs;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the deserializer state at given index.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="index">Element index</param>
|
|
|
|
|
/// <returns>Element deserializer state</returns>
|
|
|
|
|
/// <exception cref="IndexOutOfRangeException"><paramref name="index"/> is out of range.</exception>
|
|
|
|
|
/// <exception cref="DeserializationException">Traversal limit reached</exception>
|
|
|
|
|
public DeserializerState this[int index]
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (index < 0 || index >= Count)
|
|
|
|
|
throw new IndexOutOfRangeException();
|
|
|
|
|
|
|
|
|
|
int stride = State.StructDataCount + State.StructPtrCount;
|
|
|
|
|
|
|
|
|
|
var state = State;
|
|
|
|
|
// the “traversal limit” should count a list of zero-sized elements as if each element were one word instead.
|
|
|
|
|
state.IncrementBytesTraversed(checked(8u * (uint)(stride == 0 ? 1 : stride)));
|
|
|
|
|
state.Offset = checked(state.Offset + 1 + index * stride);
|
|
|
|
|
state.Kind = ObjectKind.Struct;
|
|
|
|
|
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts this list to a different representation by applying an element selector function.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">Target type after applying the selector function</typeparam>
|
|
|
|
|
/// <param name="cons">The selector function</param>
|
|
|
|
|
/// <returns>The new list representation</returns>
|
|
|
|
|
public override IReadOnlyList<T> Cast<T>(Func<DeserializerState, T> cons)
|
|
|
|
|
{
|
|
|
|
|
return this.LazyListSelect(cons);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerable<DeserializerState> Enumerate()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < Count; i++)
|
|
|
|
|
{
|
|
|
|
|
yield return this[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-07-12 21:48:01 +02:00
|
|
|
|
/// Implements <see cref="IEnumerable{DeserializerState}"/>.
|
2019-06-12 21:56:55 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerator<DeserializerState> GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
return Enumerate().GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
return GetEnumerator();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-11 17:56:12 +01:00
|
|
|
|
}
|