libs.capnproto-dotnetcore_R.../Capnp.Net.Runtime/ListOfPrimitivesDeserializer.cs

234 lines
9.1 KiB
C#
Raw Permalink Normal View History

2019-06-12 21:56:55 +02:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace Capnp
{
/// <summary>
2020-02-17 21:23:44 +01:00
/// ListDeserializer specialization for unmanaged primitive types (including enum).
2019-06-12 21:56:55 +02:00
/// </summary>
/// <typeparam name="T">List element type</typeparam>
public class ListOfPrimitivesDeserializer<T>: ListDeserializer, IReadOnlyList<T>
2020-02-09 13:49:21 +01:00
where T: unmanaged
2019-06-12 21:56:55 +02:00
{
class ListOfULongAsStructView<U> : IReadOnlyList<U>
{
readonly ListOfPrimitivesDeserializer<ulong> _lpd;
readonly Func<DeserializerState, U> _sel;
public ListOfULongAsStructView(ListOfPrimitivesDeserializer<ulong> lpd, Func<DeserializerState, U> sel)
{
_lpd = lpd;
_sel = sel;
}
public U this[int index]
{
get
{
var state = _lpd.State;
if (index < 0 || index >= _lpd.Count)
2020-04-11 15:48:02 +02:00
throw new IndexOutOfRangeException();
2019-06-12 21:56:55 +02:00
state.Offset += index;
state.Kind = ObjectKind.Struct;
state.StructDataCount = 1;
state.StructPtrCount = 0;
return _sel(state);
}
}
public int Count => _lpd.Count;
IEnumerable<U> Enumerate()
{
var state = _lpd.State;
state.Kind = ObjectKind.Struct;
state.StructDataCount = 1;
state.StructPtrCount = 0;
for (int i = 0; i < Count; i++)
{
yield return _sel(state);
++state.Offset;
}
}
public IEnumerator<U> GetEnumerator()
{
return Enumerate().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
readonly ListKind _kind;
2020-02-09 13:49:21 +01:00
internal ListOfPrimitivesDeserializer(in DeserializerState state, ListKind kind) :
base(state)
2019-06-12 21:56:55 +02:00
{
_kind = kind;
}
/// <summary>
/// One of ListOfBytes, ListOfShorts, ListOfInts, ListOfLongs.
/// </summary>
public override ListKind Kind => _kind;
2020-02-17 21:23:44 +01:00
/// <summary>
/// Retrieves the underlying memory span of this object
/// </summary>
public ReadOnlySpan<T> Span => MemoryMarshal.Cast<ulong, T>(State.CurrentSegment.Slice(State.Offset)).Slice(0, Count);
2019-06-12 21:56:55 +02:00
/// <summary>
/// Returns the element at given index.
/// </summary>
/// <param name="index">Element index</param>
/// <returns>Element value</returns>
/// <exception cref="IndexOutOfRangeException"><paramref name="index"/> is out of range.</exception>
2020-02-17 21:23:44 +01:00
public T this[int index] => Span[index];
2019-06-12 21:56:55 +02:00
2020-02-09 13:49:21 +01:00
ListOfPrimitivesDeserializer<U> PrimitiveCast<U>() where U: unmanaged
2019-06-12 21:56:55 +02:00
{
if (Marshal.SizeOf<U>() != Marshal.SizeOf<T>())
throw new NotSupportedException("Source and target types have different sizes, cannot cast");
2020-02-09 13:49:21 +01:00
return new ListOfPrimitivesDeserializer<U>(State, Kind);
2019-06-12 21:56:55 +02:00
}
/// <summary>
/// Always throws <see cref="NotSupportedException"/> because this specialization can never represent a List(bool).
/// </summary>
public override IReadOnlyList<bool> CastBool() => throw new NotSupportedException("Cannot cast to list of bits");
/// <summary>
/// Attempts to interpret this instance as List(UInt8).
/// </summary>
/// <returns>The desired representation</returns>
/// <exception cref="NotSupportedException">Element size is different from 1 byte.</exception>
public override IReadOnlyList<byte> CastByte() => PrimitiveCast<byte>();
/// <summary>
/// Attempts to interpret this instance as List(Int8).
/// </summary>
/// <returns>The desired representation</returns>
/// <exception cref="NotSupportedException">Element size is different from 1 byte.</exception>
public override IReadOnlyList<sbyte> CastSByte() => PrimitiveCast<sbyte>();
/// <summary>
/// Attempts to interpret this instance as List(UInt16).
/// </summary>
/// <returns>The desired representation</returns>
/// <exception cref="NotSupportedException">Element size is different from 2 bytes.</exception>
public override IReadOnlyList<ushort> CastUShort() => PrimitiveCast<ushort>();
/// <summary>
/// Attempts to interpret this instance as List(Int16).
/// </summary>
/// <returns>The desired representation</returns>
/// <exception cref="NotSupportedException">Element size is different from 2 bytes.</exception>
public override IReadOnlyList<short> CastShort() => PrimitiveCast<short>();
/// <summary>
/// Attempts to interpret this instance as List(UInt32).
/// </summary>
/// <returns>The desired representation</returns>
/// <exception cref="NotSupportedException">Element size is different from 4 bytes.</exception>
public override IReadOnlyList<uint> CastUInt() => PrimitiveCast<uint>();
/// <summary>
/// Attempts to interpret this instance as List(Int32).
/// </summary>
/// <returns>The desired representation</returns>
/// <exception cref="NotSupportedException">Element size is different from 4 bytes.</exception>
public override IReadOnlyList<int> CastInt() => PrimitiveCast<int>();
/// <summary>
/// Attempts to interpret this instance as List(UInt64).
/// </summary>
/// <returns>The desired representation</returns>
/// <exception cref="NotSupportedException">Element size is different from 8 bytes.</exception>
public override IReadOnlyList<ulong> CastULong() => PrimitiveCast<ulong>();
/// <summary>
/// Attempts to interpret this instance as List(Int64).
/// </summary>
/// <returns>The desired representation</returns>
/// <exception cref="NotSupportedException">Element size is different from 8 bytes.</exception>
public override IReadOnlyList<long> CastLong() => PrimitiveCast<long>();
/// <summary>
/// Attempts to interpret this instance as List(Float32).
/// </summary>
/// <returns>The desired representation</returns>
/// <exception cref="NotSupportedException">Element size is different from 4 bytes.</exception>
public override IReadOnlyList<float> CastFloat() => PrimitiveCast<float>();
/// <summary>
/// Attempts to interpret this instance as List(Float64).
/// </summary>
/// <returns>The desired representation</returns>
/// <exception cref="NotSupportedException">Element size is different from 8 bytes.</exception>
public override IReadOnlyList<double> CastDouble() => PrimitiveCast<double>();
/// <summary>
/// Attempts to interpret this instance as List(U) whereby U is a struct, applying a selector function to each element.
/// </summary>
/// <param name="cons">Selector function</param>
/// <returns>The desired representation</returns>
public override IReadOnlyList<U> Cast<U>(Func<DeserializerState, U> cons)
{
switch (Marshal.SizeOf<T>())
{
case 1: return PrimitiveCast<byte>().LazyListSelect(x => cons(DeserializerState.MakeValueState(x)));
case 2: return PrimitiveCast<ushort>().LazyListSelect(x => cons(DeserializerState.MakeValueState(x)));
case 4: return PrimitiveCast<uint>().LazyListSelect(x => cons(DeserializerState.MakeValueState(x)));
case 8: return new ListOfULongAsStructView<U>(PrimitiveCast<ulong>(), cons);
default:
throw new InvalidProgramException("This program path should not be reachable");
}
}
/// <summary>
/// Attempts to interpret this instance as Text and returns the string representation.
/// </summary>
/// <returns>The decoded string</returns>
/// <exception cref="NotSupportedException">Element size is different from 1 byte.</exception>
public override string CastText()
{
2020-02-17 21:23:44 +01:00
var utf8Bytes = PrimitiveCast<byte>().Span;
2019-06-12 21:56:55 +02:00
if (utf8Bytes.Length == 0) return string.Empty;
var utf8GytesNoZterm = utf8Bytes.Slice(0, utf8Bytes.Length - 1);
2019-06-22 18:43:30 -04:00
return Encoding.UTF8.GetString(utf8GytesNoZterm.ToArray());
2019-06-12 21:56:55 +02:00
}
IEnumerable<T> Enumerate()
{
for (int i = 0; i < Count; i++)
yield return this[i];
}
/// <summary>
/// Implements <see cref="IEnumerable{T}"/>.
/// </summary>
/// <returns></returns>
public IEnumerator<T> GetEnumerator()
{
return Enumerate().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
2020-01-11 17:56:12 +01:00
}