23. 1階層だけの抽象化
public abstract class Formatter<TTypeResolver, T>
where TTypeResolver : ITypeResolver, new()
{
public abstract int? GetLength();
public abstract int Serialize(ref byte[] bytes, int offset, T value);
public abstract T Deserialize(ref byte[] bytes, int offset, out int byteSize);
}
24. internal class Int32ArrayFormatter : Formatter<Int32[]>
{
public override int Serialize(ref byte[] bytes, int offset, Int32[] value)
{
var writeSize = value.Length * 4;
BinaryUtil.EnsureCapacity(ref bytes, offset, writeSize + 4);
BinaryUtil.WriteInt32Unsafe(ref bytes, offset, value.Length);
Buffer.BlockCopy(value, 0, bytes, offset + 4, writeSize);
return writeSize + 4;
}
}
// 例えばint[]の場合、普通はintの要素一個一個を処理するコードになる
for (int i = 0; i < values.Length; i++)
{
stream.Write(serialize(values[i]));
}