This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
Option.cs
170 lines (148 loc) · 4.83 KB
/
Option.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
namespace Improbable.Gdk.Core
{
/// <summary>
/// An implementation of Option which is compatible with Unity's ECS.
/// </summary>
/// <remarks>
/// This is required because bool is not blittable by default.
/// </remarks>
/// <typeparam name="T">The contained type in the Option.</typeparam>
public struct Option<T> : IEquatable<Option<T>>
{
public static readonly Option<T> Empty = new Option<T>();
/// <summary>
/// True if the Option contains a value, false if not.
/// </summary>
public BlittableBool HasValue { get; }
private readonly T value;
/// <summary>
/// Returns the value contained inside the Option.
/// </summary>
/// <exception cref="CalledValueOnEmptyOptionException">
/// Thrown if the Option is empty.
/// </exception>
public T Value
{
get
{
if (!HasValue)
{
throw new CalledValueOnEmptyOptionException("Called Value on empty Option.");
}
return value;
}
}
/// <summary>
/// Constructor for an option.
/// </summary>
/// <param name="value">
/// The value to be contained in the option
/// </param>
/// <exception cref="CreatedOptionWithNullPayloadException">
/// Thrown if the value parameter is null.
/// </exception>
public Option(T value)
{
if (!typeof(T).IsValueType && value == null)
{
throw new CreatedOptionWithNullPayloadException("Options may not have null payloads.");
}
HasValue = true;
this.value = value;
}
/// <summary>
/// Attempts to get the value contained within the Option.
/// </summary>
/// <param name="outValue">
/// When this method returns, contains the value contained within the Option if the Option is non-empty,
/// otherwise the default value for the type of the outValue parameter.
/// </param>
/// <returns>
/// A bool indicating success.
/// </returns>
public bool TryGetValue(out T outValue)
{
if (!HasValue)
{
outValue = default(T);
return false;
}
outValue = value;
return true;
}
/// <summary>
/// Gets the value within the Option or the provided default value if the Option is empty.
/// </summary>
/// <param name="defaultValue">
/// The default value to return if the Option is empty.
/// </param>
/// <returns>
/// The value contained within the Option or the provided value.
/// </returns>
public T GetValueOrDefault(T defaultValue)
{
return HasValue ? value : defaultValue;
}
public override bool Equals(object other)
{
return other is Option<T> option && Equals(option);
}
public bool Equals(Option<T> other)
{
if (!HasValue && !other.HasValue)
{
return true;
}
if (HasValue && other.HasValue)
{
return value.Equals(other.Value);
}
return false;
}
public static bool operator ==(Option<T> a, Option<T> b)
{
return a.Equals(b);
}
public static bool operator !=(Option<T> a, Option<T> b)
{
return !a.Equals(b);
}
public override int GetHashCode()
{
return HasValue ? value.GetHashCode() : 0;
}
public override string ToString()
{
return HasValue ? $"Option[{value.ToString()}]" : "Option.Empty";
}
public static implicit operator Option<T>(T value)
{
return new Option<T>(value);
}
public static implicit operator T(Option<T> option)
{
return option.Value;
}
}
/// <summary>
/// Represents an error that occurs when an Option is created with an invalid initial state.
/// </summary>
public class CreatedOptionWithNullPayloadException : Exception
{
public CreatedOptionWithNullPayloadException(string message)
: base(message)
{
}
}
/// <summary>
/// Represents an error when an Option's contained value is attempted to be accessed when the option is empty.
/// </summary>
public class CalledValueOnEmptyOptionException : Exception
{
public CalledValueOnEmptyOptionException(string message)
: base(message)
{
}
}
}