/
StreamerFormats.cs
347 lines (277 loc) · 12.8 KB
/
StreamerFormats.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*<FILE_LICENSE>
* NFX (.NET Framework Extension) Unistack Library
* Copyright 2003-2018 Agnicore Inc. portions ITAdapter Corp. Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
</FILE_LICENSE>*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.IO;
using System.Reflection;
namespace NFX.IO
{
/// <summary>
/// Describes a format - apair of readers/writers along with their capabilities - what types format supports natively
/// </summary>
public abstract class StreamerFormat
{
public abstract Type ReaderType { get; }
public abstract Type WriterType { get; }
/// <summary>
/// Makes new reader instance
/// </summary>
public abstract ReadingStreamer GetReadingStreamer(Encoding encoding = null);
/// <summary>
/// Makes new writer instance
/// </summary>
public abstract WritingStreamer GetWritingStreamer(Encoding encoding = null);
/// <summary>
/// Returns true when the supplied type is natively supported by format
/// </summary>
public abstract bool IsTypeSupported(Type t);
/// <summary>
/// Returns true when the supplied ref type is natively supported by format
/// </summary>
public abstract bool IsRefTypeSupported(Type t);
/// <summary>
/// Returns a method info for reading a certain value type for this format or null if type is not supported
/// </summary>
public abstract MethodInfo GetReadMethodForType(Type t);
/// <summary>
/// Returns a method info for reading a certain ref type for this format or null if type is not supported
/// </summary>
public abstract MethodInfo GetReadMethodForRefType(Type t);
/// <summary>
/// Returns a method info for writing a certain value type for this format or null if type is not supported
/// </summary>
public abstract MethodInfo GetWriteMethodForType(Type t);
/// <summary>
/// Returns a method info for writing a certain ref type for this format or null if type is not supported
/// </summary>
public abstract MethodInfo GetWriteMethodForRefType(Type t);
}
/// <summary>
/// Describes a format - apair of readers/writers along with their capabilities.
/// Developers may derive new formats that support custom serialization schemes
/// </summary>
public abstract class StreamerFormat<TReader, TWriter> : StreamerFormat where TReader : ReadingStreamer
where TWriter : WritingStreamer
{
//.ctor
protected StreamerFormat()
{
m_ReadMethods = new Dictionary<Type,MethodInfo>();
m_WriteMethods = new Dictionary<Type,MethodInfo>();
m_ReadMethodsRefT = new Dictionary<Type,MethodInfo>();
m_WriteMethodsRefT = new Dictionary<Type,MethodInfo>();
var t = this.WriterType;//typeof(TWriter);
var wMethods = t.GetMethods(BindingFlags.Instance | BindingFlags.Public)
.Where(mi => mi.Name.Equals("Write") && //do not localize
mi.ReturnType==typeof(void) &&
mi.GetParameters().Length==1);
foreach(var mi in wMethods)
{
var pt = mi.GetParameters()[0].ParameterType;
if (pt.IsValueType || pt==typeof(string))//string is treated like a value type
m_WriteMethods.Add(pt, mi);
else
m_WriteMethodsRefT.Add(pt, mi);
}
t = this.ReaderType;//typeof(TReader);
var rMethods = t.GetMethods(BindingFlags.Instance | BindingFlags.Public)
.Where(mi => mi.Name.StartsWith("Read") && mi.ReturnType!=typeof(void) && !mi.IsConstructor);//do not localize
foreach(var mi in rMethods)
{
var rt = mi.ReturnType;
if (rt.IsValueType || rt==typeof(string))//string is treated like a value type
m_ReadMethods.Add(rt, mi);
else
m_ReadMethodsRefT.Add(rt, mi);
}
compileActions();
}
private Dictionary<Type, MethodInfo> m_ReadMethods;
private Dictionary<Type, MethodInfo> m_WriteMethods;
private Dictionary<Type, Func<TReader, object>> m_ReadActions;
private Dictionary<Type, Action<TWriter, object>> m_WriteActions;
private Dictionary<Type, MethodInfo> m_ReadMethodsRefT;
private Dictionary<Type, MethodInfo> m_WriteMethodsRefT;
private Dictionary<Type, Func<TReader, object>> m_ReadActionsRefT;
private Dictionary<Type, Action<TWriter, object>> m_WriteActionsRefT;
/// <summary>
/// Makes new reader instance
/// </summary>
public sealed override ReadingStreamer GetReadingStreamer(Encoding encoding = null)
{
return MakeReadingStreamer(encoding);
}
/// <summary>
/// Makes new writer instance
/// </summary>
public sealed override WritingStreamer GetWritingStreamer(Encoding encoding = null)
{
return MakeWritingStreamer(encoding);
}
/// <summary>
/// Makes new reader instance
/// </summary>
public abstract TReader MakeReadingStreamer(Encoding encoding = null);
/// <summary>
/// Makes new writer instance
/// </summary>
public abstract TWriter MakeWritingStreamer(Encoding encoding = null);
/// <summary>
/// Returns true when the supplied value type is natively supported by format, that is - when this format
/// can directly write instances of this type without reflection/complex graph walk.
/// </summary>
public override bool IsTypeSupported(Type t)
{
return m_WriteMethods.ContainsKey(t);
}
/// <summary>
/// Returns true when the supplied reference type is natively supported by format, that is - when this format
/// can directly write instances of this type without reflection/complex graph walk.
/// </summary>
public override bool IsRefTypeSupported(Type t)
{
return m_WriteMethodsRefT.ContainsKey(t);
}
/// <summary>
/// Returns a method info for reading a certain value type for this format or null if this type is not directly supported.
/// Use IsTypeSupported(type) to see if the type is native to this format.
/// </summary>
public sealed override MethodInfo GetReadMethodForType(Type t)
{
MethodInfo result = null;
if (m_ReadMethods.TryGetValue(t, out result)) return result;
return null;
}
/// <summary>
/// Returns a method info for reading a certain reference type for this format or null if this type is not directly supported.
/// Use IsRefTypeSupported(type) to see if the ref type is native to this format.
/// </summary>
public sealed override MethodInfo GetReadMethodForRefType(Type t)
{
MethodInfo result = null;
if (m_ReadMethodsRefT.TryGetValue(t, out result)) return result;
return null;
}
/// <summary>
/// Returns a method info for writing a certain value type for this format.
/// Use IsTypeSupported(type) to see if the type is native to this format.
/// </summary>
public sealed override MethodInfo GetWriteMethodForType(Type t)
{
MethodInfo result = null;
if (m_WriteMethods.TryGetValue(t, out result)) return result;
return null;
}
/// <summary>
/// Returns a method info for writing a certain ref type for this format.
/// Use IsRefTypeSupported(type) to see if the type is native to this format.
/// </summary>
public sealed override MethodInfo GetWriteMethodForRefType(Type t)
{
MethodInfo result = null;
if (m_WriteMethodsRefT.TryGetValue(t, out result)) return result;
return null;
}
/// <summary>
/// Returns a function that reads the specified value type and returns it as object
/// </summary>
public Func<TReader, object> GetReadActionForType(Type t)
{
Func<TReader, object> result = null;
if (m_ReadActions.TryGetValue(t, out result)) return result;
return null;
}
/// <summary>
/// Returns a function that reads the specified ref type and returns it as object
/// </summary>
public Func<TReader, object> GetReadActionForRefType(Type t)
{
Func<TReader, object> result = null;
if (m_ReadActionsRefT.TryGetValue(t, out result)) return result;
return null;
}
/// <summary>
/// Returns an action that writes the value of the specified value type
/// </summary>
public Action<TWriter, object> GetWriteActionForType(Type t)
{
Action<TWriter, object> result = null;
if (m_WriteActions.TryGetValue(t, out result)) return result;
return null;
}
/// <summary>
/// Returns an action that writes the value of the specified ref type
/// </summary>
public Action<TWriter, object> GetWriteActionForRefType(Type t)
{
Action<TWriter, object> result = null;
if (m_WriteActionsRefT.TryGetValue(t, out result)) return result;
return null;
}
private void compileActions()
{
m_ReadActions = new Dictionary<Type,Func<TReader,object>>();
m_WriteActions = new Dictionary<Type,Action<TWriter,object>>();
m_ReadActionsRefT = new Dictionary<Type,Func<TReader,object>>();
m_WriteActionsRefT = new Dictionary<Type,Action<TWriter,object>>();
foreach(var rkvp in m_ReadMethods)
{
var returnType = rkvp.Key;
var mi = rkvp.Value;
m_ReadActions.Add(returnType, compileReader(returnType, mi));
}
foreach(var rkvp in m_ReadMethodsRefT)
{
var returnType = rkvp.Key;
var mi = rkvp.Value;
m_ReadActionsRefT.Add(returnType, compileReader(returnType, mi));
}
foreach(var wkvp in m_WriteMethods)
{
var argType = wkvp.Key;
var mi = wkvp.Value;
m_WriteActions.Add(argType, compileWriter(argType, mi));
}
foreach(var wkvp in m_WriteMethodsRefT)
{
var argType = wkvp.Key;
var mi = wkvp.Value;
m_WriteActionsRefT.Add(argType, compileWriter(argType, mi));
}
}
private Func<TReader, object> compileReader(Type tp, MethodInfo miRead)
{
var pReadingStreamer = Expression.Parameter(typeof(TReader));
return Expression.Lambda<Func<TReader, object>>(
Expression.Convert( Expression.Call(pReadingStreamer, miRead), typeof(object) ),
pReadingStreamer).Compile();
}
private Action<TWriter, object> compileWriter(Type tp, MethodInfo miWrite)
{
var pWritingStreamer = Expression.Parameter(typeof(TWriter));
var pValue = Expression.Parameter(typeof(object));
return Expression.Lambda<Action<TWriter, object>>(
Expression.Call(pWritingStreamer, miWrite,
Expression.Convert( pValue, tp)
),
pWritingStreamer, pValue).Compile();
}
}
}