Innovenergy_trunk/csharp/Lib/Units/StateOfT.cs

27 lines
1.1 KiB
C#

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];
}