-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettableOnce.cs
47 lines (39 loc) · 1.23 KB
/
SettableOnce.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MarkusSecundus.Util
{
/// <summary>
/// Proxy pointing to a value that can be written once and read arbitrarily.
/// </summary>
/// <typeparam name="T">
/// Type of contained value
/// </typeparam>
public sealed class SettableOnce<T>
{
private T _value;
/// <summary>
/// Value this proxy points to
/// </summary>
public T Value
{
get => _value;
set
{
if (IsSet)
throw new InvalidOperationException($"Value was already set to {_value}");
_value = value;
IsSet = true;
}
}
/// <summary>
/// Whether the value has already been set
/// </summary>
public bool IsSet { get; private set; } = false;
public override bool Equals(object obj) => obj is SettableOnce<T> w && IsSet == w.IsSet && (!IsSet || Equals(Value, w.Value));
public override int GetHashCode() => IsSet ? Value.GetHashCode() : 0;
public override string ToString() => IsSet ? "<nil>" : ""+Value;
}
}