IMapAccessor
IAccessor └── IMapAccessor └── IMapAccessor<Container> └── IMapAccessor<Container, Key, Value>
IMapAccessor describes writers that access a map. Map can exist as class, struct, memory-span or stream.
/// <summary>Map accessor</summary>
public interface IMapAccessor : IAccessor, IDefaultConstructorAccessor, IConstructorAccessor, IDeconstructorAccessor, IContainerAccessor
{
/// <summary></summary>
Type KeyType { get; set; }
/// <summary></summary>
Type ValueType { get; set; }
/// <summary>Get reference to value</summary>
IWriter Referer { get; set; }
/// <summary>Remove at key</summary>
IWriter Remove { get; set; }
/// <summary>Get value at key.</summary>
IWriter Get { get; set; }
/// <summary>Set value at key.</summary>
IWriter Set { get; set; }
/// <summary>Clear list.</summary>
IWriterFrom<Avalanche.Void> Clear { get; set; }
/// <summary>Count.</summary>
IWriterTo<long> Count { get; set; }
}
IMapAccessor<Map> and IMapAccessor<Map, Key, Value> describes accessor for dictionary types.
/// <summary>Map accessor of <typeparamref name="Map"/>.</summary>
public interface IMapAccessor<Map, Key, Value> : IMapAccessor<Map>, IMapAccessorKeyOf<Key>, IMapAccessorValueOf<Value>, IConstructorAccessor<Map, IEnumerable<KeyValuePair<Key, Value>>>, IDeconstructorAccessor<Map, IEnumerable<KeyValuePair<Key, Value>>>
{
/// <summary>Get reference to value</summary>
new IWriter Referer { get; set; }
/// <summary>Remove at key</summary>
new IWriterBase<Key, Map> Remove { get; set; }
/// <summary>Get value at key.</summary>
new IWriterBase<(Map, Key), Value> Get { get; set; }
/// <summary>Set value at key.</summary>
new IWriterBase<(Key, Value), Map> Set { get; set; }
/// <summary>Clear list.</summary>
new IWriterBase<Avalanche.Void, Map> Clear { get; set; }
/// <summary>Count.</summary>
new IWriterBase<Map, long> Count { get; set; }
}
IMapVisitor and IMapVisitorT are visitor interfaces for IAccessorBase.Accept(IAccessorVisitor).
/// <summary>Base interface for map visitor</summary>
public interface IMapVisitor : IAccessorVisitor
{
/// <summary>Try visit <paramref name="mapAccessor"/>.</summary>
/// <returns>true to continue visitation</returns>
bool VisitMapAccessor(IMapAccessor mapAccessor);
}
/// <summary>Base interface for map visitor</summary>
public interface IMapVisitorT : IAccessorVisitor
{
/// <summary>Try visit <paramref name="mapAccessor"/>.</summary>
/// <returns>true to continue visitation</returns>
bool VisitMapAccessor<Map, Key, Value>(IMapAccessor<Map, Key, Value> mapAccessor);
}
MapAccessor
AccessorBase └── MapAccessor └── MapAccessor<Container> └── MapAccessor<Container, Key, Value>
MapAccessor<Map, Key, Value> is the default implementation.
IMapAccessor<Dictionary<string, int>, string, int> mapAccessor = new MapAccessor<Dictionary<string, int>, string, int>()
{
Constructor = null!,
Deconstructor = null!,
DefaultConstructor = null!,
Referer = null!,
Remove = null!,
Get = null!,
Set = null!,
Clear = null!,
Count = null!,
IsAssigned = null!,
Unassign = null!
}.SetReadOnly();
MapAccessor.Create(Type, Type, Type) also constructs IMapAccessor<Map, Key, Value>. There is a setter method for each field.
IMapAccessor<Dictionary<string, int>, string, int> mapAccessor =
(IMapAccessor<Dictionary<string, int>, string, int>)
MapAccessor.Create(typeof(Dictionary<string, int>), typeof(string), typeof(int))
.SetConstructor(null!)
.SetDeconstructor(null!)
.SetDefaultConstructor(null!)
.SetReferer(null!)
.SetRemove(null!)
.SetGet(null!)
.SetSet(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 NetMap(typeof(Dictionary<string, int>));
// Create request
IRequestFor<IAccessor> request = new AccessorRequest(target);
// Issue request
IMapAccessor<Dictionary<string, int>, string, int> accessor = service.GetRequired<IRequest, IMapAccessor<Dictionary<string, int>, string, int>>(request);
MapAccessorRequest(target) requests for IMapAccessor<Map, Key, Value>.
// Create service
IService service = Services.Create(NetAccessorHandlers.Instance);
// Create container target
IContainerTarget target = new NetMap(typeof(Dictionary<string, int>));
// Create request
IRequestFor<IMapAccessor> request = new MapAccessorRequest(target);
// Issue request
IMapAccessor<Dictionary<string, int>, string, int> accessor = service.GetRequired<IRequest, IMapAccessor<Dictionary<string, int>, string, int>>(request);
MapAccessorRequest 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 NetMap(typeof(Dictionary<string, int>));
// Create request
IRequestFor<IMapAccessor> request = new MapAccessorRequest
{
Constructor = new ConstructorRequest(target),
Deconstructor = new DeconstructorRequest(target),
DefaultConstructor = new DefaultConstructorRequest(target),
Remove = new RemoveRequest(target),
Get = new GetRequest(target),
Set = new SetRequest(target),
Clear = new ClearRequest(target),
Count = new CountRequest(target),
IsAssigned = new IsAssignedRequest(target),
Unassign = new UnassignRequest(target),
ContainerType = new ContainerTypeRequest(target),
KeyType = new KeyTypeRequest(target),
ValueType = new ValueTypeRequest(target),
Referer = new RefererRequest(target),
};
// Issue request
IMapAccessor<Dictionary<string, int>, string, int> accessor = service.GetRequired<IRequest, IMapAccessor<Dictionary<string, int>, string, int>>(request);
Usage
.DefaultConstructor creates a map with default value.
Dictionary<string, int> map = accessor.DefaultConstructor.Read();
.Constructor creates a map from enumerable of elements.
Dictionary<string, int> map = accessor.Constructor.ReadAs<IEnumerable<KeyValuePair<string, int>>, Dictionary<string, int>>(new Dictionary<string, int> { { "A", 1 }, { "B", 2 }, { "C", 3 } });
.Deconstructor reads values as an enumerable.
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
IEnumerable<KeyValuePair<string, int>> deconstruction = accessor.Deconstructor.Read(map);
.Remove removes an element at index.
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
accessor.Remove.Write("B", map);
.Get reads an element at index.
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
int value = accessor.Get.Read((map, "B"));
.Set assigns an element at index.
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
accessor.Set.Write(("D", 10), map);
.Clear removes all elements.
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
accessor.Clear.Write(default, map);
.Count returns the number of elements.
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
long count = accessor.Count.Read(map);
.IsAssigned tests whether a map is assigned in container.
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
bool nonNull = accessor.IsAssigned.Read(map);
.Unassign removes map at container.
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
accessor.Unassign.Write(default, ref map);
.Referer get a reference to element.
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
ref int element = ref ((IReferer<(Dictionary<string, int>, string), int>)accessor.Referer).GetReference((map, "B"));
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 mapaccessor
{
public static void Run()
{
{
// <01>
IMapAccessor<Dictionary<string, int>, string, int> mapAccessor = new MapAccessor<Dictionary<string, int>, string, int>()
{
Constructor = null!,
Deconstructor = null!,
DefaultConstructor = null!,
Referer = null!,
Remove = null!,
Get = null!,
Set = null!,
Clear = null!,
Count = null!,
IsAssigned = null!,
Unassign = null!
}.SetReadOnly();
// </01>
}
{
// <02>
IMapAccessor<Dictionary<string, int>, string, int> mapAccessor =
(IMapAccessor<Dictionary<string, int>, string, int>)
MapAccessor.Create(typeof(Dictionary<string, int>), typeof(string), typeof(int))
.SetConstructor(null!)
.SetDeconstructor(null!)
.SetDefaultConstructor(null!)
.SetReferer(null!)
.SetRemove(null!)
.SetGet(null!)
.SetSet(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 NetMap(typeof(Dictionary<string, int>));
// Create request
IRequestFor<IAccessor> request = new AccessorRequest(target);
// Issue request
IMapAccessor<Dictionary<string, int>, string, int> accessor = service.GetRequired<IRequest, IMapAccessor<Dictionary<string, int>, string, int>>(request);
// </10>
}
{
// <11>
// Create service
IService service = Services.Create(NetAccessorHandlers.Instance);
// Create container target
IContainerTarget target = new NetMap(typeof(Dictionary<string, int>));
// Create request
IRequestFor<IMapAccessor> request = new MapAccessorRequest(target);
// Issue request
IMapAccessor<Dictionary<string, int>, string, int> accessor = service.GetRequired<IRequest, IMapAccessor<Dictionary<string, int>, string, int>>(request);
// </11>
}
{
// <12>
// Create service
IService service = Services.Create(NetAccessorHandlers.Instance);
// Create container target
IContainerTarget target = new NetMap(typeof(Dictionary<string, int>));
// Create request
IRequestFor<IMapAccessor> request = new MapAccessorRequest
{
Constructor = new ConstructorRequest(target),
Deconstructor = new DeconstructorRequest(target),
DefaultConstructor = new DefaultConstructorRequest(target),
Remove = new RemoveRequest(target),
Get = new GetRequest(target),
Set = new SetRequest(target),
Clear = new ClearRequest(target),
Count = new CountRequest(target),
IsAssigned = new IsAssignedRequest(target),
Unassign = new UnassignRequest(target),
ContainerType = new ContainerTypeRequest(target),
KeyType = new KeyTypeRequest(target),
ValueType = new ValueTypeRequest(target),
Referer = new RefererRequest(target),
};
// Issue request
IMapAccessor<Dictionary<string, int>, string, int> accessor = service.GetRequired<IRequest, IMapAccessor<Dictionary<string, int>, string, int>>(request);
// </12>
}
{
IMapAccessor<Dictionary<string, int>, string, int> accessor = Services.Create(NetAccessorHandlers.Instance).GetRequiredService<IMapAccessor<Dictionary<string, int>, string, int>>();
{
// <51>
Dictionary<string, int> map = accessor.DefaultConstructor.Read();
// </51>
}
{
// <52>
Dictionary<string, int> map = accessor.Constructor.ReadAs<IEnumerable<KeyValuePair<string, int>>, Dictionary<string, int>>(new Dictionary<string, int> { { "A", 1 }, { "B", 2 }, { "C", 3 } });
// </52>
}
{
// <53>
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
IEnumerable<KeyValuePair<string, int>> deconstruction = accessor.Deconstructor.Read(map);
// </53>
}
{
// <56>
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
accessor.Remove.Write("B", map);
// </56>
}
{
// <57>
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
int value = accessor.Get.Read((map, "B"));
// </57>
}
{
// <58>
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
accessor.Set.Write(("D", 10), map);
// </58>
}
{
// <60>
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
accessor.Clear.Write(default, map);
// </60>
}
{
// <61>
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
long count = accessor.Count.Read(map);
// </61>
}
{
// <62>
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
bool nonNull = accessor.IsAssigned.Read(map);
// </62>
}
{
// <63>
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
accessor.Unassign.Write(default, ref map);
// </63>
}
{
// <64>
Dictionary<string, int> map = new Dictionary<string, int> { { "A", 7 }, { "B", 8 }, { "C", 9 } };
ref int element = ref ((IReferer<(Dictionary<string, int>, string), int>)accessor.Referer).GetReference((map, "B"));
// </64>
}
}
}
}