Class FieldAccessor<T>
- Namespace
- CStructSharp
- Assembly
- CStructSharp.dll
One member path of a layout, resolved once and read many times: layout.GetAccessor<float>("reading.pos.x").
Reading a member by a path string parses the path and looks up every segment on each call; an accessor does
that work when it is created, so a read is a few array lookups (Get(StructValue)) or a decode at a
constant byte offset (Read(ReadOnlySpan<byte>, ReadOptions?), Get<T>(FieldAccessor<T>)).
public sealed class FieldAccessor<T>
Type Parameters
TThe type the member is read as, with the conversions of
StructValue.Get<T>andReadValue<T>.
- Inheritance
-
FieldAccessor<T>
- Inherited Members
Remarks
An accessor is immutable and can be shared by threads. It is a shortcut, never a different reader: whenever the prepared route does not apply to a value or an input - a struct assembled by the caller, a member that is absent, an input that is too short, limits that the shortcut cannot prove - the read is the one the path string would perform, with the same result and the same failure.
Properties
Path
Gets the path the accessor reads, starting at the root declaration (reading.pos.x).
public string Path { get; }
Property Value
Root
Gets the root declaration the path starts at.
public string Root { get; }
Property Value
Methods
Get(StructValue)
Reads the member from a parsed root struct: the same value, or the same failure, as
value.Get<T>(path below the root).
public T Get(StructValue value)
Parameters
valueStructValueA struct parsed as the accessor's root (any other struct works too, through the path string).
Returns
- T
The member.
Exceptions
- CStructPathException
The member is absent or the path does not apply to the value.
- CStructReadException
The member cannot be converted to
T.
Read(ReadOnlySpan<byte>, ReadOptions?)
Reads the member from bytes that start with the root: the same value, or the same failure, as
layout.ReadValue<T>(source, Path, options: options), decoded directly when the member has a fixed
offset and the input and limits provably allow it.
public T Read(ReadOnlySpan<byte> source, ReadOptions? options = null)
Parameters
sourceReadOnlySpan<byte>The bytes; the root starts at the first one.
optionsReadOptionsThe read options; null uses the documented defaults.
Returns
- T
The member.
Exceptions
- CStructPathException
The path cannot be resolved.
- CStructReadException
The bytes cannot be decoded or converted to
T.