1
+ //------------------------------------------------------------------------------
2
+ // <auto-generated>
3
+ // This code was generated by https://github.com/StefH/AnyOf.
4
+ //
5
+ // Changes to this file may cause incorrect behavior and will be lost if
6
+ // the code is regenerated.
7
+ // </auto-generated>
8
+ //------------------------------------------------------------------------------
9
+
10
+ #pragma warning disable CS1591
11
+
12
+ using System ;
13
+ using System . Diagnostics ;
14
+ using System . Collections . Generic ;
15
+
16
+ namespace AnyOfTypes
17
+ {
18
+ [ DebuggerDisplay ( "{_thisType}, AnyOfType = {_currentType}; Type = {_currentValueType?.Name}; Value = '{ToString()}'" ) ]
19
+ internal struct AnyOf < TFirst , TSecond , TThird > : IEquatable < AnyOf < TFirst , TSecond , TThird > >
20
+ {
21
+ private readonly string _thisType => $ "AnyOf<{ typeof ( TFirst ) . Name } , { typeof ( TSecond ) . Name } , { typeof ( TThird ) . Name } >";
22
+ private readonly int _numberOfTypes ;
23
+ private readonly object _currentValue ;
24
+ private readonly Type _currentValueType ;
25
+ private readonly AnyOfType _currentType ;
26
+
27
+ private readonly TFirst _first ;
28
+ private readonly TSecond _second ;
29
+ private readonly TThird _third ;
30
+
31
+ public readonly AnyOfType [ ] AnyOfTypes => new [ ] { AnyOfType . First , AnyOfType . Second , AnyOfType . Third } ;
32
+ public readonly Type [ ] Types => new [ ] { typeof ( TFirst ) , typeof ( TSecond ) , typeof ( TThird ) } ;
33
+ public bool IsUndefined => _currentType == AnyOfType . Undefined ;
34
+ public bool IsFirst => _currentType == AnyOfType . First ;
35
+ public bool IsSecond => _currentType == AnyOfType . Second ;
36
+ public bool IsThird => _currentType == AnyOfType . Third ;
37
+
38
+ public static implicit operator AnyOf < TFirst , TSecond , TThird > ( TFirst value ) => new AnyOf < TFirst , TSecond , TThird > ( value ) ;
39
+
40
+ public static implicit operator TFirst ( AnyOf < TFirst , TSecond , TThird > @this ) => @this . First ;
41
+
42
+ public AnyOf ( TFirst value )
43
+ {
44
+ _numberOfTypes = 3 ;
45
+ _currentType = AnyOfType . First ;
46
+ _currentValue = value ;
47
+ _currentValueType = typeof ( TFirst ) ;
48
+ _first = value ;
49
+ _second = default ;
50
+ _third = default ;
51
+ }
52
+
53
+ public TFirst First
54
+ {
55
+ get
56
+ {
57
+ Validate ( AnyOfType . First ) ;
58
+ return _first ;
59
+ }
60
+ }
61
+
62
+ public static implicit operator AnyOf < TFirst , TSecond , TThird > ( TSecond value ) => new AnyOf < TFirst , TSecond , TThird > ( value ) ;
63
+
64
+ public static implicit operator TSecond ( AnyOf < TFirst , TSecond , TThird > @this ) => @this . Second ;
65
+
66
+ public AnyOf ( TSecond value )
67
+ {
68
+ _numberOfTypes = 3 ;
69
+ _currentType = AnyOfType . Second ;
70
+ _currentValue = value ;
71
+ _currentValueType = typeof ( TSecond ) ;
72
+ _second = value ;
73
+ _first = default ;
74
+ _third = default ;
75
+ }
76
+
77
+ public TSecond Second
78
+ {
79
+ get
80
+ {
81
+ Validate ( AnyOfType . Second ) ;
82
+ return _second ;
83
+ }
84
+ }
85
+
86
+ public static implicit operator AnyOf < TFirst , TSecond , TThird > ( TThird value ) => new AnyOf < TFirst , TSecond , TThird > ( value ) ;
87
+
88
+ public static implicit operator TThird ( AnyOf < TFirst , TSecond , TThird > @this ) => @this . Third ;
89
+
90
+ public AnyOf ( TThird value )
91
+ {
92
+ _numberOfTypes = 3 ;
93
+ _currentType = AnyOfType . Third ;
94
+ _currentValue = value ;
95
+ _currentValueType = typeof ( TThird ) ;
96
+ _third = value ;
97
+ _first = default ;
98
+ _second = default ;
99
+ }
100
+
101
+ public TThird Third
102
+ {
103
+ get
104
+ {
105
+ Validate ( AnyOfType . Third ) ;
106
+ return _third ;
107
+ }
108
+ }
109
+
110
+ private void Validate ( AnyOfType desiredType )
111
+ {
112
+ if ( desiredType != _currentType )
113
+ {
114
+ throw new InvalidOperationException ( $ "Attempting to get { desiredType } when { _currentType } is set") ;
115
+ }
116
+ }
117
+
118
+ public AnyOfType CurrentType
119
+ {
120
+ get
121
+ {
122
+ return _currentType ;
123
+ }
124
+ }
125
+
126
+ public object CurrentValue
127
+ {
128
+ get
129
+ {
130
+ return _currentValue ;
131
+ }
132
+ }
133
+
134
+ public Type CurrentValueType
135
+ {
136
+ get
137
+ {
138
+ return _currentValueType ;
139
+ }
140
+ }
141
+
142
+ public override int GetHashCode ( )
143
+ {
144
+ var fields = new object [ ]
145
+ {
146
+ _numberOfTypes ,
147
+ _currentValue ,
148
+ _currentType ,
149
+ _first ,
150
+ _second ,
151
+ _third ,
152
+ typeof ( TFirst ) ,
153
+ typeof ( TSecond ) ,
154
+ typeof ( TThird ) ,
155
+ } ;
156
+ return HashCodeCalculator . GetHashCode ( fields ) ;
157
+ }
158
+
159
+ public bool Equals ( AnyOf < TFirst , TSecond , TThird > other )
160
+ {
161
+ return _currentType == other . _currentType &&
162
+ _numberOfTypes == other . _numberOfTypes &&
163
+ EqualityComparer < object > . Default . Equals ( _currentValue , other . _currentValue ) &&
164
+ EqualityComparer < TFirst > . Default . Equals ( _first , other . _first ) &&
165
+ EqualityComparer < TSecond > . Default . Equals ( _second , other . _second ) &&
166
+ EqualityComparer < TThird > . Default . Equals ( _third , other . _third ) ;
167
+ }
168
+
169
+ public static bool operator == ( AnyOf < TFirst , TSecond , TThird > obj1 , AnyOf < TFirst , TSecond , TThird > obj2 )
170
+ {
171
+ return EqualityComparer < AnyOf < TFirst , TSecond , TThird > > . Default . Equals ( obj1 , obj2 ) ;
172
+ }
173
+
174
+ public static bool operator != ( AnyOf < TFirst , TSecond , TThird > obj1 , AnyOf < TFirst , TSecond , TThird > obj2 )
175
+ {
176
+ return ! ( obj1 == obj2 ) ;
177
+ }
178
+
179
+ public override bool Equals ( object obj )
180
+ {
181
+ return obj is AnyOf < TFirst , TSecond , TThird > o && Equals ( o ) ;
182
+ }
183
+
184
+ public override string ToString ( )
185
+ {
186
+ return IsUndefined ? null : $ "{ _currentValue } ";
187
+ }
188
+ }
189
+ }
0 commit comments