/
EnumFieldHandler.cs
314 lines (280 loc) · 11.6 KB
/
EnumFieldHandler.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BTDB.IL;
using BTDB.StreamLayer;
using System.Diagnostics;
namespace BTDB.FieldHandler
{
public class EnumFieldHandler : IFieldHandler
{
readonly byte[] _configuration;
readonly bool _signed;
Type _enumType;
public class EnumConfiguration
{
readonly bool _signed;
readonly bool _flags;
readonly string[] _names;
readonly ulong[] _values;
public EnumConfiguration(Type enumType)
{
_signed = IsSignedEnum(enumType);
_flags = IsFlagsEnum(enumType);
_names = enumType.GetEnumNames();
var members = enumType.GetMembers(BindingFlags.Static | BindingFlags.Public);
Debug.Assert(members.Length == _names.Length);
for (int i = 0; i < members.Length; i++)
{
var a = members[i].GetCustomAttributes<PersistedNameAttribute>().FirstOrDefault();
if (a != null) _names[i] = a.Name;
}
var undertype = enumType.GetEnumUnderlyingType();
var enumValues = enumType.GetEnumValues();
IEnumerable<ulong> enumValuesUlongs;
if (undertype == typeof(int)) enumValuesUlongs = enumValues.Cast<int>().Select(i => (ulong)i);
else if (undertype == typeof(uint)) enumValuesUlongs = enumValues.Cast<uint>().Select(i => (ulong)i);
else if (undertype == typeof(sbyte)) enumValuesUlongs = enumValues.Cast<sbyte>().Select(i => (ulong)i);
else if (undertype == typeof(byte)) enumValuesUlongs = enumValues.Cast<byte>().Select(i => (ulong)i);
else if (undertype == typeof(short)) enumValuesUlongs = enumValues.Cast<short>().Select(i => (ulong)i);
else if (undertype == typeof(ushort)) enumValuesUlongs = enumValues.Cast<ushort>().Select(i => (ulong)i);
else if (undertype == typeof(long)) enumValuesUlongs = enumValues.Cast<long>().Select(i => (ulong)i);
else enumValuesUlongs = enumValues.Cast<ulong>();
_values = enumValuesUlongs.ToArray();
}
public EnumConfiguration(byte[] configuration)
{
var reader = new ByteArrayReader(configuration);
var header = reader.ReadVUInt32();
_signed = (header & 1) != 0;
_flags = (header & 2) != 0;
var count = header >> 2;
_names = new string[count];
_values = new ulong[count];
for (int i = 0; i < count; i++) Names[i] = reader.ReadString();
if (_signed)
{
for (int i = 0; i < count; i++) Values[i] = (ulong)reader.ReadVInt64();
}
else
{
for (int i = 0; i < count; i++) Values[i] = reader.ReadVUInt64();
}
}
public bool Signed => _signed;
public bool Flags => _flags;
public string[] Names => _names;
public ulong[] Values => _values;
public byte[] ToConfiguration()
{
var writer = new ByteBufferWriter();
writer.WriteVUInt32((_signed ? 1u : 0) + (Flags ? 2u : 0) + 4u * (uint)Names.Length);
foreach (var name in Names)
{
writer.WriteString(name);
}
foreach (var value in Values)
{
if (_signed) writer.WriteVInt64((long)value);
else writer.WriteVUInt64(value);
}
return writer.Data.ToByteArray();
}
public Type ToType()
{
var builder = ILBuilder.Instance;
var literals = new Dictionary<string, object>();
for (var i = 0; i < Names.Length; i++)
{
if (_signed)
{
literals.Add(Names[i], (long)Values[i]);
}
else
{
literals.Add(Names[i], Values[i]);
}
}
return builder.NewEnum("EnumByFieldHandler", _signed ? typeof(long) : typeof(ulong), literals);
}
public static bool operator ==(EnumConfiguration left, EnumConfiguration right)
{
if (ReferenceEquals(left, right)) return true;
if (ReferenceEquals(left, null)) return false;
return left.Equals(right);
}
public static bool operator !=(EnumConfiguration left, EnumConfiguration right)
{
return !(left == right);
}
public bool Equals(EnumConfiguration other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return other._flags.Equals(_flags)
&& _names.SequenceEqual(other._names)
&& _values.SequenceEqual(other._values);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof(EnumConfiguration)) return false;
return Equals((EnumConfiguration)obj);
}
public override int GetHashCode()
{
unchecked
{
int result = _flags.GetHashCode();
result = (result * 397) ^ _names.GetHashCode();
result = (result * 397) ^ _values.GetHashCode();
return result;
}
}
public bool IsSubsetOf(EnumConfiguration targetCfg)
{
if (_flags != targetCfg._flags) return false;
var targetDict =
targetCfg.Names.Zip(targetCfg.Values, (k, v) => new KeyValuePair<string, ulong>(k, v))
.ToDictionary(p => p.Key, p => p.Value);
for (int i = 0; i < _names.Length; i++)
{
ulong targetValue;
if (!targetDict.TryGetValue(_names[i], out targetValue)) return false;
if (_values[i] != targetValue) return false;
}
return true;
}
public bool IsBinaryRepresentationSubsetOf(EnumConfiguration targetCfg)
{
var targetDict = targetCfg.Values.ToDictionary(v => v, _ => false);
return _values.All(v => targetDict.ContainsKey(v));
}
}
public EnumFieldHandler(Type enumType)
{
if (!IsCompatibleWith(enumType)) throw new ArgumentException("enumType");
_enumType = enumType;
var ec = new EnumConfiguration(enumType);
_signed = ec.Signed;
_configuration = ec.ToConfiguration();
}
public EnumFieldHandler(byte[] configuration)
{
_configuration = configuration;
var ec = new EnumConfiguration(configuration);
_signed = ec.Signed;
}
static bool IsSignedEnum(Type enumType)
{
return SignedFieldHandler.IsCompatibleWith(enumType.GetEnumUnderlyingType());
}
static bool IsFlagsEnum(Type type)
{
return type.GetCustomAttributes(typeof(FlagsAttribute), false).Length != 0;
}
public static string HandlerName => "Enum";
public string Name => HandlerName;
public byte[] Configuration => _configuration;
public static bool IsCompatibleWith(Type type)
{
if (!type.IsEnum) return false;
var enumUnderlyingType = type.GetEnumUnderlyingType();
return SignedFieldHandler.IsCompatibleWith(enumUnderlyingType) || UnsignedFieldHandler.IsCompatibleWith(enumUnderlyingType);
}
public Type HandledType()
{
return _enumType ?? (_enumType = new EnumConfiguration(_configuration).ToType());
}
public bool NeedsCtx()
{
return false;
}
public void Load(IILGen ilGenerator, Action<IILGen> pushReaderOrCtx)
{
pushReaderOrCtx(ilGenerator);
Type typeRead;
if (_signed)
{
ilGenerator.Call(() => default(AbstractBufferedReader).ReadVInt64());
typeRead = typeof(long);
}
else
{
ilGenerator.Call(() => default(AbstractBufferedReader).ReadVUInt64());
typeRead = typeof(ulong);
}
new DefaultTypeConvertorGenerator().GenerateConversion(typeRead, _enumType.GetEnumUnderlyingType())(ilGenerator);
}
public void Skip(IILGen ilGenerator, Action<IILGen> pushReaderOrCtx)
{
pushReaderOrCtx(ilGenerator);
if (_signed)
{
ilGenerator.Call(() => default(AbstractBufferedReader).SkipVInt64());
}
else
{
ilGenerator.Call(() => default(AbstractBufferedReader).SkipVUInt64());
}
}
public void Save(IILGen ilGenerator, Action<IILGen> pushWriterOrCtx, Action<IILGen> pushValue)
{
pushWriterOrCtx(ilGenerator);
pushValue(ilGenerator);
if (_signed)
{
ilGenerator
.ConvI8()
.Call(() => default(AbstractBufferedWriter).WriteVInt64(0));
}
else
{
ilGenerator
.ConvU8()
.Call(() => default(AbstractBufferedWriter).WriteVUInt64(0));
}
}
public IFieldHandler SpecializeLoadForType(Type type, IFieldHandler typeHandler)
{
if (typeHandler == this) return this;
var enumTypeHandler = typeHandler as EnumFieldHandler;
if (enumTypeHandler != null && _signed == enumTypeHandler._signed)
{
if (type.GetCustomAttributes(typeof (BinaryCompatibilityOnlyAttribute), false).Length != 0)
{
if (new EnumConfiguration(Configuration).IsBinaryRepresentationSubsetOf(new EnumConfiguration(enumTypeHandler.Configuration)))
return typeHandler;
}
else
{
if (new EnumConfiguration(Configuration).IsSubsetOf(new EnumConfiguration(enumTypeHandler.Configuration)))
return typeHandler;
}
}
if (_enumType == null && type.IsEnum)
{
if (_configuration.SequenceEqual(new EnumConfiguration(type).ToConfiguration()))
{
_enumType = type;
}
}
return this;
}
public IFieldHandler SpecializeSaveForType(Type type)
{
return SpecializeLoadForType(type, null);
}
public bool IsCompatibleWith(Type type, FieldHandlerOptions options)
{
return IsCompatibleWith(type);
}
public bool FreeContent(IILGen ilGenerator, Action<IILGen> pushReaderOrCtx)
{
Skip(ilGenerator, pushReaderOrCtx);
return false;
}
}
}