add SettingsValueOption

This commit is contained in:
Pilzinsel64
2025-04-25 09:47:36 +02:00
parent e6340b4894
commit ec22181750
3 changed files with 82 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
using Newtonsoft.Json;
namespace Pilz.Configuration;
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(SettingsValueOptionJsonConverter))]
public class SettingsValueOption<T> : ISettingsValueOptionValueAccessor where T : struct
{
protected T? value;
public T DefaultValue { get; }
public virtual bool IsDefault => value == null;
public SettingsValueOption()
{
}
public SettingsValueOption(T value)
{
this.value = DefaultValue = value;
}
public virtual T Value
{
get => value ?? DefaultValue;
set => this.value = value;
}
public static implicit operator T(SettingsValueOption<T> option)
{
return option.Value;
}
object ISettingsValueOptionValueAccessor.ValueRaw
{
get => value;
set => this.value = (T?)value;
}
public virtual void Reset()
{
value = null;
}
public override string ToString()
{
return Value.ToString();
}
}