Introduce State<T> for Enums

This commit is contained in:
ig 2023-03-09 14:01:57 +01:00
parent a66d547291
commit 9d753a8471
2 changed files with 35 additions and 1 deletions

View File

@ -34,4 +34,11 @@ public readonly struct State : IReadOnlyList<String>
public Int32 Count => Values.Count;
public String this[Int32 index] => Values[index];
}
public static State<T> From<T>(T t) where T : Enum
{
return new State<T>(t);
}
}

View File

@ -0,0 +1,27 @@
using System.Collections;
namespace InnovEnergy.Lib.Units;
public readonly struct State<T> : IReadOnlyList<T> where T:Enum
{
public IReadOnlyList<T> Values { get; }
public State(IReadOnlyList<T> values) => Values = values;
public State(params T[] values) : this((IReadOnlyList<T>)values){}
public State(params State<T>[] states) : this((IReadOnlyList<T>)states.SelectMany(s => s.Values).ToList()){}
public static implicit operator State<T>(T s) => new((IReadOnlyList<T>)s);
public static implicit operator State<T>(List<T> s) => new((IReadOnlyList<T>)s);
public static implicit operator State<T>(T[] s) => new((IReadOnlyList<T>)s);
public static State<T> operator |(State<T> left, State<T> right) => new State<T>(left, right);
public IEnumerator<T> GetEnumerator() => Values.GetEnumerator();
public override String ToString() => String.Join("; ", Values);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public Int32 Count => Values.Count;
public T this[Int32 index] => Values[index];
}