cardidi 0315862527 Add some basic features of this toolkit.
feat: Logg was almost done.
fix: Some impossible call was fixed in UnityObjectWarp.cs
feat: Boxing<T> was force to unbox by explicit cast operator.
2024-05-10 00:55:42 +08:00

62 lines
1.5 KiB
C#

using System;
using UnityEngine.Assertions;
namespace Ca2d.Toolkit
{
/// <summary>
/// An utility structure which helps you to create an optional field in Unity Editor or a <see cref="Nullable"/>
/// alternative.
/// </summary>
/// <typeparam name="T">The type which will be optional.</typeparam>
[Serializable]
public struct Option<T>
{
/// <summary>
/// Is option set to fill.
/// </summary>
public bool Enabled;
/// <summary>
/// Value inside of option
/// </summary>
public T Value;
public T ValueOrDefault()
{
return Enabled ? Value : default;
}
public T ValueOrDefault(T def)
{
return Enabled ? Value : def;
}
public T ValueOrDefault(Func<T> def)
{
Assert.IsNotNull(def);
return Enabled ? Value : def();
}
public T ValueOrDefault<TSelector>(TSelector src, Func<TSelector, T> def)
{
Assert.IsNotNull(def);
return Enabled ? Value : def(src);
}
public Option(T value)
{
Enabled = true;
Value = value;
}
public static implicit operator T(Option<T> wrapper)
{
return wrapper.Value;
}
public static implicit operator Option<T>(T value)
{
return new Option<T>(value);
}
}
}