IListAccessor
IListAccessor describes writers that access a list. List can exist as class, struct, memory-span or stream.
/// <summary>List accessor</summary>
public interface IListAccessor : IAccessorBase, IDefaultConstructorAccessor, IConstructorAccessor, IDeconstructorAccessor
{
/// <summary></summary>
Type ListType { get; set; }
/// <summary></summary>
Type ElementType { get; set; }
/// <summary>Get reference to element</summary>
IWriterBase Referer { get; set; }
/// <summary>Add element</summary>
IWriterBase Add { get; set; }
/// <summary>Insert element at index</summary>
IWriterBase Insert { get; set; }
/// <summary>Remove element at index</summary>
IWriterBase RemoveAt { get; set; }
/// <summary>Get element at index.</summary>
IWriterBase Get { get; set; }
/// <summary>Set element at index.</summary>
IWriterBase Set { get; set; }
/// <summary>Find index of element.</summary>
IWriterBase IndexOf { get; set; }
/// <summary>Clear list.</summary>
IWriterFrom<Avalanche.Void> Clear { get; set; }
/// <summary>Count.</summary>
IWriterTo<long> Count { get; set; }
}
IListAccessor<L> and IListAccessor<L, E> describes accessor for container type L and element E.
/// <summary>Indicates that is list accessor of specific types</summary>
public interface IListAccessorT : IListAccessor, IAccessorT, IDefaultConstructorAccessorT { }
/// <summary>Indicates <typeparamref name="List"/> type.</summary>
public interface IListAccessor<List> : IListAccessorT, IAccessor<List>, IDefaultConstructorAccessor<List>, IConstructorAccessor<List>, IDeconstructorAccessor<List> { }
/// <summary>Indicates <typeparamref name="Element"/> type.</summary>
public interface IListAccessorOf<Element> : IListAccessorT { }
/// <summary>List accessor of <typeparamref name="List"/>.</summary>
public interface IListAccessor<List, Element> : IListAccessor<List>, IListAccessorOf<Element>, IConstructorAccessor<List, IEnumerable<Element>>, IDeconstructorAccessor<List, IEnumerable<Element>>
{
/// <summary>Get reference to element. <![CDATA[IRefererBase<(List, int), Element>]]> for IList classes, IWriter for memories and streams.</summary>
new IWriterBase Referer { get; set; }
/// <summary>Add element</summary>
new IWriterBase<Element, List> Add { get; set; }
/// <summary>Insert element at index</summary>
new IWriterBase<(Element, long), List> Insert { get; set; }
/// <summary>Remove element at index</summary>
new IWriterBase<long, List> RemoveAt { get; set; }
/// <summary>Get element at index.</summary>
new IWriterBase<(List, long), Element> Get { get; set; }
/// <summary>Set element at index.</summary>
new IWriterBase<(Element, long), List> Set { get; set; }
/// <summary>Find index of element.</summary>
new IWriterBase<(List, Element), long> IndexOf { get; set; }
/// <summary>Clear list.</summary>
new IWriterBase<Avalanche.Void, List> Clear { get; set; }
/// <summary>Count.</summary>
new IWriterBase<List, long> Count { get; set; }
}
IListVisitor and IListVisitorT are visitor interfaces for IAccessorBase.Accept(IAccessorVisitor).
/// <summary>Base interface for list visitor</summary>
public interface IListVisitor : IAccessorVisitor
{
/// <summary>Try visit <paramref name="listAccessor"/>.</summary>
/// <returns>true to continue visitation</returns>
bool VisitListAccessor(IListAccessor listAccessor);
}
/// <summary>Base interface for list visitor</summary>
public interface IListVisitorT : IAccessorVisitor
{
/// <summary>Try visit <paramref name="listAccessor"/>.</summary>
/// <returns>true to continue visitation</returns>
bool VisitListAccessor<List, Element>(IListAccessor<List, Element> listAccessor);
}
ListAccessor
ListAccessor<L, E> is the default implementation.
IListAccessor accessor = new ListAccessor<List<int>, int>()
{
Constructor = null!,
Deconstructor = null!,
DefaultConstructor = null!,
Referer = null!,
Add = null!,
Insert = null!,
RemoveAt = null!,
Get = null!,
Set = null!,
IndexOf = null!,
Clear = null!,
Count = null!
}.SetReadOnly();
ListAccessor.Create(Type, Type) also constructs IListAccessor<L, E>. There is a setter method for each field.
IListAccessor<List<int>, int> accessor =
(IListAccessor<List<int>, int>)
ListAccessor.Create(typeof(List<int>), typeof(int))
.SetConstructor(null!)
.SetDeconstructor(null!)
.SetDefaultConstructor(null!)
.SetReferer(null!)
.SetAdd(null!)
.SetInsert(null!)
.SetRemoveAt(null!)
.SetGet(null!)
.SetSet(null!)
.SetIndexOf(null!)
.SetClear(null!)
.SetCount(null!)
.SetReadOnly();
ListAccessorRequest
ListAccessorRequest.Create(Type) creates a request for implementation to IListAccessor<L>.
// Create request
ListAccessorRequest request = ListAccessorRequest.Create(typeof(List<int>));
// Issue request
IListAccessor<List<int>, int> accessor =
AccessorServices.Instance
.GetRequired<ListAccessorRequest, IListAccessor<List<int>, int>>(request);
IService.GetListAccessor<L>() extension method creates and executes ListAccessorRequest.
IListAccessor<List<int>> accessor = AccessorServices.Instance.GetListAccessor<List<int>>();
If dependency injection is used, ListAccessorRequest can be issued with service request to IListAccessor<L>.
IListAccessor<List<int>> accessor =
AccessorServices.Instance
.GetRequiredService<IListAccessor<List<int>>>();
Usage
.DefaultConstructor is a writer that creates list with default value.
List<int> list = accessor.DefaultConstructor.Read();
.Constructor is a writer that creates list from enumerable.
List<int> list = accessor.Constructor.ReadAs<IEnumerable<int>, List<int>>(new int[] { 1, 2, 3 });
.Deconstructor is a writer that reads values as a enumerable.
IEnumerable<int> deconstruction = accessor.Deconstructor.ReadAs<List<int>, IEnumerable<int>>(myList);
.Add is writer that adds an element
List<int> myList = new List<int>();
accessor.Add.WriteAs(10, myList);
If value typed list is on stack, it is refered with 'ref' pointer.
IListAccessor<StructList4<int>> accessor = AccessorServices.Instance.GetRequiredService<IListAccessor<StructList4<int>>>();
StructList4<int> valueList = new StructList4<int>();
accessor.Add.WriteAs(10, ref valueList);
If value typed list is in heap, it can be refered with adapted writer and boxed 'object'.
IListAccessor<StructList4<int>> accessor = AccessorServices.Instance.GetRequiredService<IListAccessor<StructList4<int>>>();
object boxedList = new StructList4<int>();
accessor.Add.TypeCast<int, object>().WriteAs(10, boxedList);
Full Example
Full example
using System.Collections.Generic;
using Avalanche.Accessor;
using Avalanche.Service;
using Avalanche.Utilities;
using Avalanche.Writer;
using Microsoft.Extensions.DependencyInjection;
public class listaccessor
{
public static void Run()
{
{
// <01>
IListAccessor accessor = new ListAccessor<List<int>, int>()
{
Constructor = null!,
Deconstructor = null!,
DefaultConstructor = null!,
Referer = null!,
Add = null!,
Insert = null!,
RemoveAt = null!,
Get = null!,
Set = null!,
IndexOf = null!,
Clear = null!,
Count = null!
}.SetReadOnly();
// </01>
}
{
// <02>
IListAccessor<List<int>, int> accessor =
(IListAccessor<List<int>, int>)
ListAccessor.Create(typeof(List<int>), typeof(int))
.SetConstructor(null!)
.SetDeconstructor(null!)
.SetDefaultConstructor(null!)
.SetReferer(null!)
.SetAdd(null!)
.SetInsert(null!)
.SetRemoveAt(null!)
.SetGet(null!)
.SetSet(null!)
.SetIndexOf(null!)
.SetClear(null!)
.SetCount(null!)
.SetReadOnly();
// </02>
}
{
// <11>
// Create request
ListAccessorRequest request = ListAccessorRequest.Create(typeof(List<int>));
// Issue request
IListAccessor<List<int>, int> accessor =
AccessorServices.Instance
.GetRequired<ListAccessorRequest, IListAccessor<List<int>, int>>(request);
// </11>
}
{
// <13>
IListAccessor<List<int>> accessor = AccessorServices.Instance.GetListAccessor<List<int>>();
// </13>
}
{
// <14>
IListAccessor<List<int>> accessor =
AccessorServices.Instance
.GetRequiredService<IListAccessor<List<int>>>();
// </14>
}
{
IListAccessor<List<int>> accessor = AccessorServices.Instance.GetRequiredService<IListAccessor<List<int>>>();
// <21>
List<int> list = accessor.DefaultConstructor.Read();
// </21>
}
{
IListAccessor<List<int>> accessor = AccessorServices.Instance.GetRequiredService<IListAccessor<List<int>>>();
// <22>
List<int> list = accessor.Constructor.ReadAs<IEnumerable<int>, List<int>>(new int[] { 1, 2, 3 });
// </22>
}
{
IListAccessor<List<int>> accessor = AccessorServices.Instance.GetRequiredService<IListAccessor<List<int>>>();
List<int> myList = new List<int> { 7, 8, 9 };
// <23>
IEnumerable<int> deconstruction = accessor.Deconstructor.ReadAs<List<int>, IEnumerable<int>>(myList);
// </23>
}
{
IListAccessor<List<int>> accessor = AccessorServices.Instance.GetRequiredService<IListAccessor<List<int>>>();
// <24>
List<int> myList = new List<int>();
accessor.Add.WriteAs(10, myList);
// </24>
}
{
// <25>
IListAccessor<StructList4<int>> accessor = AccessorServices.Instance.GetRequiredService<IListAccessor<StructList4<int>>>();
StructList4<int> valueList = new StructList4<int>();
accessor.Add.WriteAs(10, ref valueList);
// </25>
}
{
// <26>
IListAccessor<StructList4<int>> accessor = AccessorServices.Instance.GetRequiredService<IListAccessor<StructList4<int>>>();
object boxedList = new StructList4<int>();
accessor.Add.TypeCast<int, object>().WriteAs(10, boxedList);
// </26>
}
}
}