IMapAccessor
IMapAccessor describes writers that access a map. Map can exist as class, struct, memory-span or stream.
/// <summary>Map accessor</summary>
public interface IMapAccessor : IAccessorBase, IDefaultConstructorAccessor, IConstructorAccessor, IDeconstructorAccessor
{
/// <summary></summary>
Type MapType { get; set; }
/// <summary></summary>
Type KeyType { get; set; }
/// <summary></summary>
Type ValueType { get; set; }
/// <summary>Get reference to value</summary>
IWriterBase Referer { get; set; }
/// <summary>Remove at key</summary>
IWriterBase Remove { get; set; }
/// <summary>Get value at key.</summary>
IWriterBase Get { get; set; }
/// <summary>Set value at key.</summary>
IWriterBase Set { get; set; }
/// <summary>Clear list.</summary>
IWriterFrom<Avalanche.Void> Clear { get; set; }
/// <summary>Count.</summary>
IWriterTo<long> Count { get; set; }
}
IMapAccessor<M> and IMapAccessor<M, K, V> describes accessor for container type M, key K and value V.
/// <summary>Indicates that is map accessor of specific types.</summary>
public interface IMapAccessorT : IMapAccessor, IAccessorT, IDefaultConstructorAccessorT { }
/// <summary>Indicates <typeparamref name="Map"/> type.</summary>
public interface IMapAccessor<Map> : IMapAccessorT, IAccessor<Map>, IDefaultConstructorAccessor<Map>, IConstructorAccessor<Map>, IDeconstructorAccessor<Map> { }
/// <summary>Indicates <typeparamref name="Key"/> type.</summary>
public interface IMapAccessorKeyOf<Key> : IMapAccessorT { }
/// <summary>Indicates <typeparamref name="Value"/> type.</summary>
public interface IMapAccessorValueOf<Value> : IMapAccessorT { }
/// <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 IWriterBase 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
MapAccessor<M, K, V> 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!
}.SetReadOnly();
MapAccessor.Create(Type, Type, Type) also constructs IMapAccessor<M, K, V>. 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!)
.SetReadOnly();
MapAccessorRequest
MapAccessorRequest.Create(Type) creates a request for implementation to IMapAccessor<M>.
// Create request
MapAccessorRequest request = MapAccessorRequest.Create(typeof(Dictionary<string, int>));
// Issue request
IMapAccessor<Dictionary<string, int>, string, int> accessor =
AccessorServices.Instance
.GetRequired<MapAccessorRequest, IMapAccessor<Dictionary<string, int>, string, int>>(request);
IService.GetMapAccessor<M>() extension method creates and executes MapAccessorRequest.
IMapAccessor<Dictionary<string, int>> accessor =
AccessorServices.Instance
.GetMapAccessor<Dictionary<string, int>>();
If dependency injection is used, MapAccessorRequest can be issued with service request to IMapAccessor<M>.
IMapAccessor<Dictionary<string, int>> accessor =
AccessorServices.Instance
.GetRequiredService<IMapAccessor<Dictionary<string, int>>>();
Usage
.DefaultConstructor is a writer that creates map with default value.
Dictionary<string, int> map = accessor.DefaultConstructor.Read();
.Constructor is a writer that creates map from enumerable.
KeyValuePair<string, int>[] data = new[] { new KeyValuePair<string, int>("Hello", 753) };
Dictionary<string, int> map = accessor
.Constructor
.ReadAs<IEnumerable<KeyValuePair<string, int>>, Dictionary<string, int>>(data);
.Deconstructor is a writer that reads values as a enumerable.
IEnumerable<KeyValuePair<string, int>> deconstruction =
accessor
.Deconstructor
.ReadAs<Dictionary<string, int>, IEnumerable<KeyValuePair<string, int>>>(myMap);
.Set is writer that assigns an element.
IMapAccessor<Dictionary<string, int>, string, int> accessor =
AccessorServices.Instance
.GetRequiredService<IMapAccessor<Dictionary<string, int>, string, int>>();
Dictionary<string, int> map = new Dictionary<string, int>();
accessor.Set.Write(("ABC", 123), map);
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 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!
}.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!)
.SetReadOnly();
// </02>
}
{
// <11>
// Create request
MapAccessorRequest request = MapAccessorRequest.Create(typeof(Dictionary<string, int>));
// Issue request
IMapAccessor<Dictionary<string, int>, string, int> accessor =
AccessorServices.Instance
.GetRequired<MapAccessorRequest, IMapAccessor<Dictionary<string, int>, string, int>>(request);
// </11>
}
{
// <13>
IMapAccessor<Dictionary<string, int>> accessor =
AccessorServices.Instance
.GetMapAccessor<Dictionary<string, int>>();
// </13>
}
{
// <14>
IMapAccessor<Dictionary<string, int>> accessor =
AccessorServices.Instance
.GetRequiredService<IMapAccessor<Dictionary<string, int>>>();
// </14>
}
{
IMapAccessor<Dictionary<string, int>> accessor = AccessorServices.Instance.GetRequiredService<IMapAccessor<Dictionary<string, int>>>();
// <21>
Dictionary<string, int> map = accessor.DefaultConstructor.Read();
// </21>
}
{
IMapAccessor<Dictionary<string, int>> accessor = AccessorServices.Instance.GetRequiredService<IMapAccessor<Dictionary<string, int>>>();
// <22>
KeyValuePair<string, int>[] data = new[] { new KeyValuePair<string, int>("Hello", 753) };
Dictionary<string, int> map = accessor
.Constructor
.ReadAs<IEnumerable<KeyValuePair<string, int>>, Dictionary<string, int>>(data);
// </22>
}
{
IMapAccessor<Dictionary<string, int>> accessor =
AccessorServices.Instance
.GetRequiredService<IMapAccessor<Dictionary<string, int>>>();
Dictionary<string, int> myMap = new Dictionary<string, int> { { "World", 444 } };
// <23>
IEnumerable<KeyValuePair<string, int>> deconstruction =
accessor
.Deconstructor
.ReadAs<Dictionary<string, int>, IEnumerable<KeyValuePair<string, int>>>(myMap);
// </23>
}
{
// <24>
IMapAccessor<Dictionary<string, int>, string, int> accessor =
AccessorServices.Instance
.GetRequiredService<IMapAccessor<Dictionary<string, int>, string, int>>();
Dictionary<string, int> map = new Dictionary<string, int>();
accessor.Set.Write(("ABC", 123), map);
// </24>
}
}
}