/
InheritedTypeGridView.cs
35 lines (31 loc) · 1.27 KB
/
InheritedTypeGridView.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
using JetBrains.Annotations;
using SadRogue.Primitives.GridViews;
namespace GoRogue.GridViews
{
/// <summary>
/// Grid view that translates from T1 to T2, assuming T2 inherits from T1, by simply casting.
/// </summary>
/// <remarks>
/// This is useful to implement full co-variance/contra-variance with grid views, if you know that values that will be
/// put into the grid will be of a particular type. An exception will occur if casting a value to T2 fails.
/// </remarks>
/// <typeparam name="T1">Base type of grid view to implement.</typeparam>
/// <typeparam name="T2">Value inheriting from/implementing T1.</typeparam>
[PublicAPI]
public class InheritedTypeGridView<T1, T2> : SettableTranslationGridView<T1, T2>
where T1 : T2
{
/// <inheritdoc/>
public InheritedTypeGridView(ISettableGridView<T1> baseGrid)
: base(baseGrid)
{ }
/// <inheritdoc/>
public InheritedTypeGridView(ISettableGridView<T1> baseGrid, ISettableGridView<T2> overlay)
: base(baseGrid, overlay)
{ }
/// <inheritdoc/>
protected override T2 TranslateGet(T1 value) => value;
/// <inheritdoc/>
protected override T1 TranslateSet(T2 value) => (T1)value!;
}
}