Create generic ThreadSafeValue
This commit is contained in:
@@ -10,7 +10,7 @@ namespace Meade.net.UnitTests
|
|||||||
public void When_Assigned_ThenValueIsSame(bool value)
|
public void When_Assigned_ThenValueIsSame(bool value)
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
ThreadSafeBool sut = value;
|
ThreadSafeValue<bool> sut = value;
|
||||||
|
|
||||||
// when
|
// when
|
||||||
bool actual = sut;
|
bool actual = sut;
|
||||||
@@ -26,7 +26,7 @@ namespace Meade.net.UnitTests
|
|||||||
public void When_SetValue_ThenValueIsUpdated(bool initialValue, bool setValue)
|
public void When_SetValue_ThenValueIsUpdated(bool initialValue, bool setValue)
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
ThreadSafeBool sut = initialValue;
|
ThreadSafeValue<bool> sut = initialValue;
|
||||||
|
|
||||||
// when
|
// when
|
||||||
sut.Set(setValue);
|
sut.Set(setValue);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace Meade.net.UnitTests
|
|||||||
public void When_Assigned_ThenValueIsSame(DateTime value)
|
public void When_Assigned_ThenValueIsSame(DateTime value)
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
ThreadSafeDateTime sut = value;
|
ThreadSafeValue<DateTime> sut = value;
|
||||||
|
|
||||||
// when
|
// when
|
||||||
DateTime actual = sut;
|
DateTime actual = sut;
|
||||||
@@ -25,7 +25,7 @@ namespace Meade.net.UnitTests
|
|||||||
public void When_SetValue_ThenValueIsUpdated(DateTime initialValue, DateTime setValue)
|
public void When_SetValue_ThenValueIsUpdated(DateTime initialValue, DateTime setValue)
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
ThreadSafeDateTime sut = initialValue;
|
ThreadSafeValue<DateTime> sut = initialValue;
|
||||||
|
|
||||||
// when
|
// when
|
||||||
sut.Set(setValue);
|
sut.Set(setValue);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace Meade.net.UnitTests
|
|||||||
public void When_Assigned_ThenValueIsSame(PierSide value)
|
public void When_Assigned_ThenValueIsSame(PierSide value)
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
ThreadSafeEnum<PierSide> sut = value;
|
ThreadSafeValue<PierSide> sut = value;
|
||||||
|
|
||||||
// when
|
// when
|
||||||
PierSide actual = sut;
|
PierSide actual = sut;
|
||||||
@@ -33,7 +33,7 @@ namespace Meade.net.UnitTests
|
|||||||
public void When_SetValue_ThenValueIsUpdated(PierSide initialValue, PierSide setValue)
|
public void When_SetValue_ThenValueIsUpdated(PierSide initialValue, PierSide setValue)
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
ThreadSafeEnum<PierSide> sut = initialValue;
|
ThreadSafeValue<PierSide> sut = initialValue;
|
||||||
|
|
||||||
// when
|
// when
|
||||||
sut.Set(setValue);
|
sut.Set(setValue);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace Meade.net.UnitTests
|
|||||||
public void When_Assigned_ThenValueIsSame(double? value)
|
public void When_Assigned_ThenValueIsSame(double? value)
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
ThreadSafeNullableDouble sut = value;
|
ThreadSafeValue<double?> sut = value;
|
||||||
|
|
||||||
// when
|
// when
|
||||||
double? actual = sut;
|
double? actual = sut;
|
||||||
@@ -32,7 +32,7 @@ namespace Meade.net.UnitTests
|
|||||||
public void When_SetValue_ThenValueIsUpdated(double? initialValue, double? setValue)
|
public void When_SetValue_ThenValueIsUpdated(double? initialValue, double? setValue)
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
ThreadSafeNullableDouble sut = initialValue;
|
ThreadSafeValue<double?> sut = initialValue;
|
||||||
|
|
||||||
// when
|
// when
|
||||||
sut.Set(setValue);
|
sut.Set(setValue);
|
||||||
|
|||||||
@@ -146,10 +146,7 @@
|
|||||||
<Compile Include="ProfileProperties.cs" />
|
<Compile Include="ProfileProperties.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="TelescopeList.cs" />
|
<Compile Include="TelescopeList.cs" />
|
||||||
<Compile Include="ThreadSafeBool.cs" />
|
<Compile Include="ThreadSafeValue.cs" />
|
||||||
<Compile Include="ThreadSafeDateTime.cs" />
|
|
||||||
<Compile Include="ThreadSafeEnum.cs" />
|
|
||||||
<Compile Include="ThreadSafeNullableDouble.cs" />
|
|
||||||
<Compile Include="Win32Utilities.cs" />
|
<Compile Include="Win32Utilities.cs" />
|
||||||
<Compile Include="Wrapper\IProfileWrapper.cs" />
|
<Compile Include="Wrapper\IProfileWrapper.cs" />
|
||||||
<Compile Include="Wrapper\SharedResourcesWrapper.cs" />
|
<Compile Include="Wrapper\SharedResourcesWrapper.cs" />
|
||||||
|
|||||||
@@ -502,7 +502,7 @@ namespace ASCOM.Meade.net
|
|||||||
ParkedPosition = parkedPosition;
|
ParkedPosition = parkedPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readonly ThreadSafeBool _isParked = false;
|
private static readonly ThreadSafeValue<bool> _isParked = false;
|
||||||
public static bool IsParked
|
public static bool IsParked
|
||||||
{
|
{
|
||||||
get => _isParked;
|
get => _isParked;
|
||||||
@@ -516,7 +516,7 @@ namespace ASCOM.Meade.net
|
|||||||
private set => Interlocked.Exchange(ref _parkedPosition, value);
|
private set => Interlocked.Exchange(ref _parkedPosition, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readonly ThreadSafeEnum<PierSide> _sideOfPier = PierSide.pierUnknown;
|
private static readonly ThreadSafeValue<PierSide> _sideOfPier = PierSide.pierUnknown;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Start with <see cref="PierSide.pierUnknown"/>.
|
/// Start with <see cref="PierSide.pierUnknown"/>.
|
||||||
/// As we do not know the physical declination axis position, we have to keep track manually.
|
/// As we do not know the physical declination axis position, we have to keep track manually.
|
||||||
@@ -527,14 +527,14 @@ namespace ASCOM.Meade.net
|
|||||||
internal set => _sideOfPier.Set(value);
|
internal set => _sideOfPier.Set(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readonly ThreadSafeNullableDouble _targetRightAscension = null as double?;
|
private static readonly ThreadSafeValue<double?> _targetRightAscension = null as double?;
|
||||||
public static double? TargetRightAscension
|
public static double? TargetRightAscension
|
||||||
{
|
{
|
||||||
get => _targetRightAscension;
|
get => _targetRightAscension;
|
||||||
internal set => _targetRightAscension.Set(value);
|
internal set => _targetRightAscension.Set(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readonly ThreadSafeNullableDouble _targetDeclination = null as double?;
|
private static readonly ThreadSafeValue<double?> _targetDeclination = null as double?;
|
||||||
public static double? TargetDeclination
|
public static double? TargetDeclination
|
||||||
{
|
{
|
||||||
get => _targetDeclination;
|
get => _targetDeclination;
|
||||||
@@ -548,28 +548,28 @@ namespace ASCOM.Meade.net
|
|||||||
internal set => Interlocked.Exchange(ref _slewSettleTime, value);
|
internal set => Interlocked.Exchange(ref _slewSettleTime, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readonly ThreadSafeBool _isLongFormat = false;
|
private static readonly ThreadSafeValue<bool> _isLongFormat = false;
|
||||||
public static bool IsLongFormat
|
public static bool IsLongFormat
|
||||||
{
|
{
|
||||||
get => _isLongFormat;
|
get => _isLongFormat;
|
||||||
internal set => _isLongFormat.Set(value);
|
internal set => _isLongFormat.Set(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readonly ThreadSafeBool _movingPrimary = false;
|
private static readonly ThreadSafeValue<bool> _movingPrimary = false;
|
||||||
public static bool MovingPrimary
|
public static bool MovingPrimary
|
||||||
{
|
{
|
||||||
get => _movingPrimary;
|
get => _movingPrimary;
|
||||||
internal set => _movingPrimary.Set(value);
|
internal set => _movingPrimary.Set(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readonly ThreadSafeBool _movingSecondary = false;
|
private static readonly ThreadSafeValue<bool> _movingSecondary = false;
|
||||||
public static bool MovingSecondary
|
public static bool MovingSecondary
|
||||||
{
|
{
|
||||||
get => _movingSecondary;
|
get => _movingSecondary;
|
||||||
internal set => _movingSecondary.Set(value);
|
internal set => _movingSecondary.Set(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static readonly ThreadSafeDateTime _earliestNonSlewingTime = DateTime.MinValue;
|
private static readonly ThreadSafeValue<DateTime> _earliestNonSlewingTime = DateTime.MinValue;
|
||||||
public static DateTime EarliestNonSlewingTime
|
public static DateTime EarliestNonSlewingTime
|
||||||
{
|
{
|
||||||
get => _earliestNonSlewingTime;
|
get => _earliestNonSlewingTime;
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace ASCOM.Meade.net
|
|
||||||
{
|
|
||||||
public class ThreadSafeBool
|
|
||||||
{
|
|
||||||
private object _value;
|
|
||||||
|
|
||||||
public ThreadSafeBool(in bool value) => _value = value;
|
|
||||||
|
|
||||||
public void Set(in bool value) => Interlocked.Exchange(ref _value, value);
|
|
||||||
|
|
||||||
public static implicit operator ThreadSafeBool(in bool value) => new ThreadSafeBool(value);
|
|
||||||
|
|
||||||
public static implicit operator bool(ThreadSafeBool @this) => (bool)@this._value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace ASCOM.Meade.net
|
|
||||||
{
|
|
||||||
public class ThreadSafeDateTime
|
|
||||||
{
|
|
||||||
private long _value;
|
|
||||||
|
|
||||||
public ThreadSafeDateTime(in DateTime value) => _value = DateTimeToLong(value);
|
|
||||||
|
|
||||||
public void Set(in DateTime value) => Interlocked.Exchange(ref _value, DateTimeToLong(value));
|
|
||||||
|
|
||||||
private static long DateTimeToLong(in DateTime value) => value.ToUniversalTime().Ticks;
|
|
||||||
|
|
||||||
public static implicit operator ThreadSafeDateTime(in DateTime value) => new ThreadSafeDateTime(value);
|
|
||||||
|
|
||||||
public static implicit operator DateTime(ThreadSafeDateTime @this) => new DateTime(Interlocked.Read(ref @this._value), DateTimeKind.Utc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace ASCOM.Meade.net
|
|
||||||
{
|
|
||||||
public class ThreadSafeEnum<T>
|
|
||||||
where T: struct, Enum
|
|
||||||
{
|
|
||||||
private long _value;
|
|
||||||
|
|
||||||
public ThreadSafeEnum(T value) => _value = EnumToLong(value);
|
|
||||||
|
|
||||||
public void Set(T value) => Interlocked.Exchange(ref _value, EnumToLong(value));
|
|
||||||
|
|
||||||
private static long EnumToLong(T value) => Convert.ToInt64(value);
|
|
||||||
|
|
||||||
public static implicit operator ThreadSafeEnum<T>(T value) => new ThreadSafeEnum<T>(value);
|
|
||||||
|
|
||||||
public static implicit operator T(ThreadSafeEnum<T> @this) => (T) Enum.ToObject(typeof(T), Interlocked.Read(ref @this._value));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace ASCOM.Meade.net
|
|
||||||
{
|
|
||||||
public class ThreadSafeNullableDouble
|
|
||||||
{
|
|
||||||
private long _value;
|
|
||||||
|
|
||||||
public ThreadSafeNullableDouble(in double? value) => _value = NullableDoubleToLong(value);
|
|
||||||
|
|
||||||
public void Set(in double? value) => Interlocked.Exchange(ref _value, NullableDoubleToLong(value));
|
|
||||||
|
|
||||||
private static long NullableDoubleToLong(in double? value) => BitConverter.DoubleToInt64Bits(value ?? double.NaN);
|
|
||||||
|
|
||||||
public static implicit operator ThreadSafeNullableDouble(in double? value) => new ThreadSafeNullableDouble(value);
|
|
||||||
|
|
||||||
public static implicit operator double?(ThreadSafeNullableDouble @this)
|
|
||||||
{
|
|
||||||
var doubleValue = BitConverter.Int64BitsToDouble(Interlocked.Read(ref @this._value));
|
|
||||||
return double.IsNaN(doubleValue) ? null as double? : doubleValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
using JetBrains.Annotations;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace ASCOM.Meade.net
|
||||||
|
{
|
||||||
|
public class ThreadSafeValue<T>
|
||||||
|
{
|
||||||
|
private object _value;
|
||||||
|
|
||||||
|
public ThreadSafeValue(in T value) => _value = value;
|
||||||
|
|
||||||
|
public void Set(in T value) => Interlocked.Exchange(ref _value, value);
|
||||||
|
|
||||||
|
public static implicit operator ThreadSafeValue<T>(in T value) => new ThreadSafeValue<T>(value);
|
||||||
|
|
||||||
|
public static implicit operator T([NotNull] ThreadSafeValue<T> @this) => (T)(@this?._value ?? default);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user