laatzen/Common/CommonConsole/Value.cs
2023-09-04 12:32:29 +02:00

184 lines
3.9 KiB
C#

namespace CommonConsole
{
using System;
using System.Collections.Generic;
using System.Linq;
public abstract class Value
{
public virtual object Current { get; set; }
public virtual byte[] Buffer { get; set; }
public static implicit operator Value(string valueName)
{
try
{
var valueType = Type.GetType(
typeName: $"CommonConsole.{valueName}",
throwOnError: false,
ignoreCase: true);
var instance = Activator.CreateInstance(valueType);
if (instance is Value value)
{
return value;
}
}
catch (Exception e)
{
// TODO: Handle exceptions.
}
return default(Value);
}
public static IEnumerable<string> ValueTypes
=> typeof(Value)
.Assembly
.GetTypes()
.Where(x => typeof(Value).IsAssignableFrom(x) && !x.IsAbstract)
.Select(x => x.Name);
}
public class Value<T> : Value
{
protected T value;
}
public class bool_t : Value<bool>
{
public override object Current
{
get => this.value;
set => this.value = (bool)value;
}
}
public class rpc : Value<int>
{
public override object Current
{
get => this.value;
set => this.value = (int)value;
}
}
public class @string : Value<string>
{
public override object Current
{
get => this.value;
set => this.value = (string)value;
}
}
public class time_t : Value<int>
{
public override object Current
{
get => this.value;
set => this.value = (int)value;
}
}
public class status_t : Value<byte>
{
public override object Current
{
get => this.value;
set => this.value = (byte)value;
}
}
public class enum8 : Value<byte>
{
public override object Current
{
get => this.value;
set => this.value = (byte)value;
}
}
public class uint8_t : Value<byte>
{
public override object Current
{
get => this.value;
set => this.value = (byte)value;
}
}
public class uint16_t : Value<ushort>
{
public override object Current
{
get => this.value;
set => this.value = (ushort)value;
}
}
public class uint32_t : Value<uint>
{
public override object Current
{
get => this.value;
set => this.value = (uint)value;
}
}
public class uint64_t : Value<ulong>
{
public override object Current
{
get => this.value;
set => this.value = (ulong)value;
}
}
public class uint96_t : Value<string>
{
public override object Current
{
get => this.value;
set => this.value = (string)value;
}
}
public class int8_t : Value<sbyte>
{
public override object Current
{
get => this.value;
set => this.value = (sbyte)value;
}
}
public class int16_t : Value<short>
{
public override object Current
{
get => this.value;
set => this.value = (short)value;
}
}
public class int32_t : Value<int>
{
public override object Current
{
get => this.value;
set => this.value = (int)value;
}
}
public class int64_t : Value<long>
{
public override object Current
{
get => this.value;
set => this.value = (long)value;
}
}
}