Referer
IRefererBase is the root interface for consumers that get a reference to inner parts of objects.
/// <summary>Accessor referer that can return a reference to a value.</summary>
public interface IRefererBase : IWriterBase { }
IRefererBase<From, To> describes writers that get a 'ref' to specific object and value type.
/// <summary>Accessor referer that can return a reference to a value.</summary>
public interface IRefererBase<From, To> : IRefererBase, IRefererFrom<From>, IRefererTo<To>, IWriterTypes { }
/// <summary>Accessor referer that can return a reference to a value.</summary>
public interface IReferer<From, To> : IRefererBase<From, To> where From : notnull
{
/// <summary>Can get accessor reference</summary>
bool CanRefer { get; }
/// <summary>Get reference to accessor.</summary>
/// <param name="from"></param>
/// <returns>False if write is not supported with given arguments.</returns>
/// <exception cref="Exception">Unexpected error.</exception>
ref To GetReference(From from, IServiceProvider? context = null);
}
/// <summary>Accessor referer that can return a reference to a accessor by record reference.</summary>
public interface IRefererFromRef<From, To> : IRefererBase<From, To> where From : notnull
{
/// <summary>Can get accessor reference</summary>
bool CanRefer { get; }
/// <summary>Get reference to accessor.</summary>
/// <param name="from"></param>
/// <returns>False if write is not supported with given arguments.</returns>
/// <exception cref="Exception">Unexpected error.</exception>
ref To GetReference(ref From from, IServiceProvider? context = null);
}
IRefererBase is specialization of IWriterBase.
IWriterBase └── IRefererBase └── IRefererBase<From, To> ├── IReferer<From, To> └── IRefererFromRef<From, To>
IReferer<From, To> gets reference to inner content of class.
// Create label referer
IReferer<MyClass, string> referer =
DelegateWriter.Refer((MyClass myClass) => ref myClass.label);
// Create class
MyClass myClass = new MyClass("A", 1);
// Get reference to 'label'
ref string label = ref referer.GetReference(myClass);
IRefererFromRef<From, To> gets reference to inner content of struct.
// Create label referer
IRefererFromRef<MyStruct, string> referer =
DelegateWriter.Refer((ref MyStruct myStruct) => ref myStruct.label);
// Create struct
MyStruct myStruct = new MyStruct("A", 1);
// Get reference to 'label'
ref string label = ref referer.GetReference(ref myStruct);
.TypeCastFromObject() casts 'From' type to object. For structs takes reference into box.
// Create 'MyClass' label referer
IReferer<object, string> classReferer =
DelegateWriter.Refer((MyClass myClass) => ref myClass.label)
.TypeCastFromObject();
// Create 'MyStruct' label referer
IReferer<object, string> structReferer =
DelegateWriter.Refer((ref MyStruct myStruct) => ref myStruct.label)
.TypeCastFromObject();
// Create instances
object myClass = new MyClass("A", 1);
object myStruct = new MyStruct("A", 1);
// Get reference to 'label'
ref string classLabel = ref classReferer.GetReference(myClass);
ref string structLabel = ref structReferer.GetReference(myStruct);
Visitor
IRefererVisitor is the visitor interface for refer visitor implementations. Invoked from IWriterBase.Accept(IWriterVisitorBase).
/// <summary>Referer visitor</summary>
public interface IRefererVisitor : IWriterBaseVisitor
{
/// <summary>Try visit <paramref name="refererT"/>.</summary>
/// <returns>true to continue visitation</returns>
bool Visit<From, To>(IReferer<From, To> refererT) where From : notnull;
/// <summary>Try visit <paramref name="refererTFR"/>.</summary>
/// <returns>true to continue visitation</returns>
bool Visit<From, To>(IRefererFromRef<From, To> refererTFR) where From : notnull;
/// <summary>Try visit <paramref name="referer"/>.</summary>
/// <returns>true to continue visitation</returns>
bool Visit(IReferer referer);
/// <summary>Try visit <paramref name="refererFR"/>.</summary>
/// <returns>true to continue visitation</returns>
bool Visit(IRefererFromRef refererFR);
}
Full Example
Full example
using Avalanche.Writer;
class referer
{
public static void Run()
{
{
// <01>
// Create label referer
IReferer<MyClass, string> referer =
DelegateWriter.Refer((MyClass myClass) => ref myClass.label);
// Create class
MyClass myClass = new MyClass("A", 1);
// Get reference to 'label'
ref string label = ref referer.GetReference(myClass);
// </01>
object box = myClass;
IReferer<object, string> refererFromObject = (IReferer<object, string>)referer;
ref string label2 = ref refererFromObject.GetReference(box);
}
{
// <02>
// Create label referer
IRefererFromRef<MyStruct, string> referer =
DelegateWriter.Refer((ref MyStruct myStruct) => ref myStruct.label);
// Create struct
MyStruct myStruct = new MyStruct("A", 1);
// Get reference to 'label'
ref string label = ref referer.GetReference(ref myStruct);
// </02>
object box = myStruct;
IReferer<object, string> refererFromObject = (IReferer<object, string>)referer;
ref string label2 = ref refererFromObject.GetReference(box);
}
{
// <03>
// Create 'MyClass' label referer
IReferer<object, string> classReferer =
DelegateWriter.Refer((MyClass myClass) => ref myClass.label)
.TypeCastFromObject();
// Create 'MyStruct' label referer
IReferer<object, string> structReferer =
DelegateWriter.Refer((ref MyStruct myStruct) => ref myStruct.label)
.TypeCastFromObject();
// Create instances
object myClass = new MyClass("A", 1);
object myStruct = new MyStruct("A", 1);
// Get reference to 'label'
ref string classLabel = ref classReferer.GetReference(myClass);
ref string structLabel = ref structReferer.GetReference(myStruct);
// </03>
}
}
public record MyClass
{
public string label;
public int id;
public MyClass(string label, int id) { this.label = label; this.id = id; }
public void Print(string value) { }
}
public record struct MyStruct
{
public string label;
public int id;
public MyStruct(string label, int id) { this.label = label; this.id = id; }
public void Print(string value) { }
}
}