mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-02 05:58:18 +00:00
C# support for directly reading and writting to memory other than byte[]. For example, ByteBuffer can be initialized with a custom allocator which uses shared memory / memory mapped files. (#4886)
Public access to the backing buffer uses Span<T> instead of ArraySegment<T>. Writing to the buffer now supports Span<T> in addition to T[]. To maintain backwards compatibility ENABLE_SPAN_T must be defined.
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
e1f48ad35a
commit
d0321df8cf
@@ -78,6 +78,23 @@ namespace FlatBuffers
|
||||
return offset + bb.GetInt(offset) + sizeof(int); // data starts after the length
|
||||
}
|
||||
|
||||
#if ENABLE_SPAN_T
|
||||
// Get the data of a vector whoses offset is stored at "offset" in this object as an
|
||||
// Spant<byte>. If the vector is not present in the ByteBuffer,
|
||||
// then an empty span will be returned.
|
||||
public Span<byte> __vector_as_span(int offset)
|
||||
{
|
||||
var o = this.__offset(offset);
|
||||
if (0 == o)
|
||||
{
|
||||
return new Span<byte>();
|
||||
}
|
||||
|
||||
var pos = this.__vector(o);
|
||||
var len = this.__vector_len(o);
|
||||
return bb.ToSpan(pos, len);
|
||||
}
|
||||
#else
|
||||
// Get the data of a vector whoses offset is stored at "offset" in this object as an
|
||||
// ArraySegment<byte>. If the vector is not present in the ByteBuffer,
|
||||
// then a null value will be returned.
|
||||
@@ -93,6 +110,7 @@ namespace FlatBuffers
|
||||
var len = this.__vector_len(o);
|
||||
return bb.ToArraySegment(pos, len);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Get the data of a vector whoses offset is stored at "offset" in this object as an
|
||||
// T[]. If the vector is not present in the ByteBuffer, then a null value will be
|
||||
|
||||
Reference in New Issue
Block a user