steam-deck-tools/CommonHelpers/TimedValue.cs
Kamil Trzciński 00a30cbf5c SteamController: Fix PS button and Gyro support for DS4
Make DS4 fields be readonly and optimise DS4 packets.
2023-02-11 14:40:09 +01:00

51 lines
1.1 KiB
C#

namespace CommonHelpers
{
public struct TimedValue<T> where T : struct
{
public T Value { get; }
public DateTime ExpiryDate { get; }
public bool Valid
{
get => !Expired;
}
public bool Expired
{
get => ExpiryDate < DateTime.UtcNow;
}
public TimedValue(T value, TimeSpan ts)
{
this.Value = value;
this.ExpiryDate = DateTime.UtcNow.Add(ts);
}
public TimedValue(T value, int timeoutMs)
: this(value, TimeSpan.FromMilliseconds(timeoutMs))
{
}
public bool TryGetValue(out T value)
{
value = this.Value;
return Valid;
}
public T? GetValue()
{
return Valid ? Value : null;
}
public T GetValueOrDefault(T defaultValue)
{
return Valid ? Value : defaultValue;
}
public static implicit operator T?(TimedValue<T> tv)
{
return tv.GetValue();
}
}
}