using System;
using System.Threading;
namespace Swan.Threading {
///
/// Defines an atomic generic Enum.
///
/// The type of enum.
public sealed class AtomicEnum where T : struct, IConvertible {
private Int64 _backingValue;
///
/// Initializes a new instance of the class.
///
/// The initial value.
/// T must be an enumerated type.
public AtomicEnum(T initialValue) {
if(!Enum.IsDefined(typeof(T), initialValue)) {
throw new ArgumentException("T must be an enumerated type");
}
this.Value = initialValue;
}
///
/// Gets or sets the value.
///
public T Value {
get => (T)Enum.ToObject(typeof(T), this.BackingValue);
set => this.BackingValue = Convert.ToInt64(value);
}
private Int64 BackingValue {
get => Interlocked.Read(ref this._backingValue);
set => Interlocked.Exchange(ref this._backingValue, value);
}
}
}