IListAccessor
IAccessor └── IListAccessor └── IListAccessor<Container> └── IListAccessor<Container, Element>
IListAccessor provides an access to containers that hold lists. List can exist as class, struct, json, yaml, memory-span or stream.
/// <summary>List accessor</summary>
public interface IListAccessor : IAccessor, IDefaultConstructorAccessor, IConstructorAccessor, IDeconstructorAccessor, IContainerAccessor
{
/// <summary></summary>
Type ElementType { get; set; }
/// <summary>Get reference to element</summary>
IWriter Referer { get; set; }
/// <summary>Add element</summary>
IWriter Add { get; set; }
/// <summary>Insert element at index</summary>
IWriter Insert { get; set; }
/// <summary>Remove element at index</summary>
IWriter RemoveAt { get; set; }
/// <summary>Get element at index.</summary>
IWriter Get { get; set; }
/// <summary>Set element at index.</summary>
IWriter Set { get; set; }
/// <summary>Find index of element.</summary>
IWriter IndexOf { get; set; }
/// <summary>Clear list.</summary>
IWriterFrom<Avalanche.Void> Clear { get; set; }
/// <summary>Count.</summary>
IWriterTo<long> Count { get; set; }
}
IListAccessor<List> and IListAccessor<List, Element> describes accessor for container type List and element Element.
/// <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 IWriter 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
AccessorBase └── ListAccessor └── ListAccessor<Container> └── ListAccessor<Container, Element>
ListAccessor<List, Element> is the default implementation.
IListAccessor accessor = new ListAccessor<List<int>, int>()
{
Constructor = null!,
Deconstructor = null!,
DefaultConstructor = null!,
Add = null!,
Insert = null!,
RemoveAt = null!,
Get = null!,
Set = null!,
Referer = null!,
IndexOf = null!,
Clear = null!,
Count = null!,
IsAssigned = null!,
Unassign = null!
}.SetReadOnly();
ListAccessor.Create(containerType, elementType) constructs IListAccessor<List, Element> with run-time type arguments. 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!)
.SetAdd(null!)
.SetInsert(null!)
.SetRemoveAt(null!)
.SetGet(null!)
.SetSet(null!)
.SetReferer(null!)
.SetIndexOf(null!)
.SetClear(null!)
.SetCount(null!)
.SetIsAssigned(null!)
.SetUnassign(null!)
.SetReadOnly();
Query
AccessorRequest(target) requests for IAccessor depending on type target.
// Create service
IService service = Services.Create(NetAccessorHandlers.Instance);
// Create container target
IContainerTarget target = new NetList(typeof(List<int>));
// Create request
IRequestFor<IAccessor> request = new AccessorRequest(target);
// Issue request
IListAccessor<List<int>, int> accessor = service.GetRequired<IRequest, IListAccessor<List<int>, int>>(request);
ListAccessorRequest(target) requests for IListAccessor<List, Element>.
// Create service
IService service = Services.Create(NetAccessorHandlers.Instance);
// Create container target
IContainerTarget target = new NetList(typeof(List<int>));
// Create request
IRequestFor<IListAccessor> request = new ListAccessorRequest(target);
// Issue request
IListAccessor<List<int>, int> accessor = service.GetRequired<IRequest, IListAccessor<List<int>, int>>(request);
ListAccessorRequest is specifically a request for specific writers. Each can be assigned with explicit customization.
// Create service
IService service = Services.Create(NetAccessorHandlers.Instance);
// Create container target
IContainerTarget target = new NetList(typeof(List<int>));
// Create request
IRequestFor<IListAccessor> request = new ListAccessorRequest
{
Constructor = new ConstructorRequest(target),
Deconstructor = new DeconstructorRequest(target),
DefaultConstructor = new DefaultConstructorRequest(target),
Add = new AddRequest(target),
Insert = new InsertRequest(target),
RemoveAt = new RemoveAtRequest(target),
Get = new GetRequest(target),
Set = new SetRequest(target),
IndexOf = new IndexOfRequest(target),
Clear = new ClearRequest(target),
Count = new CountRequest(target),
IsAssigned = new IsAssignedRequest(target),
Unassign = new UnassignRequest(target),
ContainerType = new ContainerTypeRequest(target),
ElementType = new ElementTypeRequest(target),
Referer = new RefererRequest(target),
};
// Issue request
IListAccessor<List<int>, int> accessor = service.GetRequired<IRequest, IListAccessor<List<int>, int>>(request);
Usage
.DefaultConstructor creates a list with default value.
List<int> list = accessor.DefaultConstructor.Read();
.Constructor creates a list from enumerable of elements.
List<int> list = accessor.Constructor.ReadAs<IEnumerable<int>, List<int>>(new int[] { 1, 2, 3 });
.Deconstructor reads values as an enumerable.
List<int> list = new List<int> { 7, 8, 9 };
IEnumerable<int> deconstruction = accessor.Deconstructor.Read(list);
.Add adds an element.
List<int> list = new List<int>();
accessor.Add.Write(10, list);
.Insert inserts an element.
// Create list
List<int> list = new List<int> { 1, 2, 3 };
// Insert element to list
accessor.Insert.Write((100, 1L), list);
.RemoveAt removes an element at index.
// Create list
List<int> list = new List<int> { 1, 2, 3 };
// Remove at index
accessor.RemoveAt.Write(0L, list);
.Get reads an element at index.
List<int> list = new List<int> { 1, 2, 3 };
int value = accessor.Get.Read((list, 0L));
.Set assigns an element at index.
List<int> list = new List<int> { 1, 2, 3 };
accessor.Set.Write((10, 0L), list);
.IndexOf searches for an element.
List<int> list = new List<int> { 1, 2, 3 };
long index = accessor.IndexOf.Read((list, 2));
.Clear removes all elements.
List<int> list = new List<int> { 1, 2, 3 };
accessor.Clear.Write(default, list);
.Count returns the number of elements.
List<int> list = new List<int> { 1, 2, 3 };
long count = accessor.Count.Read(list);
.IsAssigned tests whether a list is assigned in container.
List<int> list = new List<int> { 1, 2, 3 };
bool nonNull = accessor.IsAssigned.Read(list);
.Unassign removes list at container.
List<int> list = new List<int> { 1, 2, 3 };
accessor.Unassign.Write(default, ref list);
.Referer get a reference to element.
List<int> list = new List<int> { 1, 2, 3 };
ref int element = ref ((IReferer<(List<int>, long), int>)accessor.Referer).GetReference((list, 1L));
Notes
A ref must be used when accessing value-typed container.
// Create accessor
IListAccessor<StructList4<int>, int> accessor =
Services.Create(NetAccessorHandlers.Instance)
.GetRequiredService<IListAccessor<StructList4<int>, int>>();
// Create value-typed list into stack
var list = new StructList4<int> { 1, 2, 3 };
// Add an element into value-typed list in stack
accessor.Add.Write(4, ref list);
Boxed value-types can be accessed as object.
// Create accessor
IListAccessor<StructList4<int>, int> accessor =
Services.Create(NetAccessorHandlers.Instance)
.GetRequiredService<IListAccessor<StructList4<int>, int>>();
// Create value-typed list into heap
object list = new StructList4<int> { 1, 2, 3 };
// Add an element into value-typed list in heap
accessor.Add.TypeCast<int, object>().Write(4, list);
Full Example
Full example
using System;
using System.Collections.Generic;
using Avalanche.Accessor;
using Avalanche.DataType;
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!,
Add = null!,
Insert = null!,
RemoveAt = null!,
Get = null!,
Set = null!,
Referer = null!,
IndexOf = null!,
Clear = null!,
Count = null!,
IsAssigned = null!,
Unassign = 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!)
.SetAdd(null!)
.SetInsert(null!)
.SetRemoveAt(null!)
.SetGet(null!)
.SetSet(null!)
.SetReferer(null!)
.SetIndexOf(null!)
.SetClear(null!)
.SetCount(null!)
.SetIsAssigned(null!)
.SetUnassign(null!)
.SetReadOnly();
// </02>
}
{
// <10>
// Create service
IService service = Services.Create(NetAccessorHandlers.Instance);
// Create container target
IContainerTarget target = new NetList(typeof(List<int>));
// Create request
IRequestFor<IAccessor> request = new AccessorRequest(target);
// Issue request
IListAccessor<List<int>, int> accessor = service.GetRequired<IRequest, IListAccessor<List<int>, int>>(request);
// </10>
}
{
// <11>
// Create service
IService service = Services.Create(NetAccessorHandlers.Instance);
// Create container target
IContainerTarget target = new NetList(typeof(List<int>));
// Create request
IRequestFor<IListAccessor> request = new ListAccessorRequest(target);
// Issue request
IListAccessor<List<int>, int> accessor = service.GetRequired<IRequest, IListAccessor<List<int>, int>>(request);
// </11>
}
{
// <12>
// Create service
IService service = Services.Create(NetAccessorHandlers.Instance);
// Create container target
IContainerTarget target = new NetList(typeof(List<int>));
// Create request
IRequestFor<IListAccessor> request = new ListAccessorRequest
{
Constructor = new ConstructorRequest(target),
Deconstructor = new DeconstructorRequest(target),
DefaultConstructor = new DefaultConstructorRequest(target),
Add = new AddRequest(target),
Insert = new InsertRequest(target),
RemoveAt = new RemoveAtRequest(target),
Get = new GetRequest(target),
Set = new SetRequest(target),
IndexOf = new IndexOfRequest(target),
Clear = new ClearRequest(target),
Count = new CountRequest(target),
IsAssigned = new IsAssignedRequest(target),
Unassign = new UnassignRequest(target),
ContainerType = new ContainerTypeRequest(target),
ElementType = new ElementTypeRequest(target),
Referer = new RefererRequest(target),
};
// Issue request
IListAccessor<List<int>, int> accessor = service.GetRequired<IRequest, IListAccessor<List<int>, int>>(request);
// </12>
}
{
IListAccessor<List<int>, int> accessor = Services.Create(NetAccessorHandlers.Instance).GetRequiredService<IListAccessor<List<int>, int>>();
{
// <51>
List<int> list = accessor.DefaultConstructor.Read();
// </51>
}
{
// <52>
List<int> list = accessor.Constructor.ReadAs<IEnumerable<int>, List<int>>(new int[] { 1, 2, 3 });
// </52>
}
{
// <53>
List<int> list = new List<int> { 7, 8, 9 };
IEnumerable<int> deconstruction = accessor.Deconstructor.Read(list);
// </53>
}
{
// <54>
List<int> list = new List<int>();
accessor.Add.Write(10, list);
// </54>
}
{
// <55>
// Create list
List<int> list = new List<int> { 1, 2, 3 };
// Insert element to list
accessor.Insert.Write((100, 1L), list);
// </55>
}
{
// <56>
// Create list
List<int> list = new List<int> { 1, 2, 3 };
// Remove at index
accessor.RemoveAt.Write(0L, list);
// </56>
}
{
// <57>
List<int> list = new List<int> { 1, 2, 3 };
int value = accessor.Get.Read((list, 0L));
// </57>
}
{
// <58>
List<int> list = new List<int> { 1, 2, 3 };
accessor.Set.Write((10, 0L), list);
// </58>
}
{
// <59>
List<int> list = new List<int> { 1, 2, 3 };
long index = accessor.IndexOf.Read((list, 2));
// </59>
}
{
// <60>
List<int> list = new List<int> { 1, 2, 3 };
accessor.Clear.Write(default, list);
// </60>
}
{
// <61>
List<int> list = new List<int> { 1, 2, 3 };
long count = accessor.Count.Read(list);
// </61>
}
{
// <62>
List<int> list = new List<int> { 1, 2, 3 };
bool nonNull = accessor.IsAssigned.Read(list);
// </62>
}
{
// <63>
List<int> list = new List<int> { 1, 2, 3 };
accessor.Unassign.Write(default, ref list);
// </63>
}
{
// <64>
List<int> list = new List<int> { 1, 2, 3 };
ref int element = ref ((IReferer<(List<int>, long), int>)accessor.Referer).GetReference((list, 1L));
// </64>
}
}
{
// <71>
// Create accessor
IListAccessor<StructList4<int>, int> accessor =
Services.Create(NetAccessorHandlers.Instance)
.GetRequiredService<IListAccessor<StructList4<int>, int>>();
// Create value-typed list into stack
var list = new StructList4<int> { 1, 2, 3 };
// Add an element into value-typed list in stack
accessor.Add.Write(4, ref list);
// </71>
}
{
// <72>
// Create accessor
IListAccessor<StructList4<int>, int> accessor =
Services.Create(NetAccessorHandlers.Instance)
.GetRequiredService<IListAccessor<StructList4<int>, int>>();
// Create value-typed list into heap
object list = new StructList4<int> { 1, 2, 3 };
// Add an element into value-typed list in heap
accessor.Add.TypeCast<int, object>().Write(4, list);
// </72>
}
{
// <73>
// Create accessor
IListAccessor<IList<int>, int> accessor =
Services.Create(NetAccessorHandlers.Instance)
.GetRequiredService<IListAccessor<IList<int>, int>>();
// Create IList<int>
int[] list = { 1, 2, 3 };
// Add an element into value-typed list in heap
long count = accessor.Count.Read(list);
// </73>
}
}
}