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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using Google.ProtocolBuffers.Descriptors;
//Disable CS3011: only CLS-compliant members can be abstract
#pragma warning disable 3011
namespace Google.ProtocolBuffers.Serialization
{
/// <summary>
/// Provides a base class for writers that performs some basic type dispatching
/// </summary>
public abstract class AbstractWriter : ICodedOutputStream, IDisposable
{
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
GC.SuppressFinalize(this);
Flush();
Dispose(true);
}
/// <summary>
/// Completes any pending write operations
/// </summary>
public virtual void Flush()
{
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
protected virtual void Dispose(bool disposing)
{
}
/// <summary>
/// Writes the message to the the formatted stream.
/// </summary>
public abstract void WriteMessage(IMessageLite message);
/// <summary>
/// Used to write any nessary root-message preamble. After this call you can call
/// IMessageLite.MergeTo(...) and complete the message with a call to WriteMessageEnd().
/// These three calls are identical to just calling WriteMessage(message);
/// </summary>
/// <example>
/// AbstractWriter writer;
/// writer.WriteMessageStart();
/// message.WriteTo(writer);
/// writer.WriteMessageEnd();
/// // ... or, but not both ...
/// writer.WriteMessage(message);
/// </example>
public abstract void WriteMessageStart();
/// <summary>
/// Used to complete a root-message previously started with a call to WriteMessageStart()
/// </summary>
public abstract void WriteMessageEnd();
/// <summary>
/// Writes a Boolean value
/// </summary>
protected abstract void Write(string field, Boolean value);
/// <summary>
/// Writes a Int32 value
/// </summary>
protected abstract void Write(string field, Int32 value);
/// <summary>
/// Writes a UInt32 value
/// </summary>
[CLSCompliant(false)]
protected abstract void Write(string field, UInt32 value);
/// <summary>
/// Writes a Int64 value
/// </summary>
protected abstract void Write(string field, Int64 value);
/// <summary>
/// Writes a UInt64 value
/// </summary>
[CLSCompliant(false)]
protected abstract void Write(string field, UInt64 value);
/// <summary>
/// Writes a Single value
/// </summary>
protected abstract void Write(string field, Single value);
/// <summary>
/// Writes a Double value
/// </summary>
protected abstract void Write(string field, Double value);
/// <summary>
/// Writes a String value
/// </summary>
protected abstract void Write(string field, String value);
/// <summary>
/// Writes a set of bytes
/// </summary>
protected abstract void Write(string field, ByteString value);
/// <summary>
/// Writes a message or group as a field
/// </summary>
protected abstract void WriteMessageOrGroup(string field, IMessageLite message);
/// <summary>
/// Writes a System.Enum by the numeric and textual value
/// </summary>
protected abstract void WriteEnum(string field, int number, string name);
/// <summary>
/// Writes a field of the type determined by field.FieldType
/// </summary>
protected virtual void WriteField(FieldType fieldType, string field, object value)
{
switch (fieldType)
{
case FieldType.Bool:
Write(field, (bool) value);
break;
case FieldType.Int64:
case FieldType.SInt64:
case FieldType.SFixed64:
Write(field, (long) value);
break;
case FieldType.UInt64:
case FieldType.Fixed64:
Write(field, (ulong) value);
break;
case FieldType.Int32:
case FieldType.SInt32:
case FieldType.SFixed32:
Write(field, (int) value);
break;
case FieldType.UInt32:
case FieldType.Fixed32:
Write(field, (uint) value);
break;
case FieldType.Float:
Write(field, (float) value);
break;
case FieldType.Double:
Write(field, (double) value);
break;
case FieldType.String:
Write(field, (string) value);
break;
case FieldType.Bytes:
Write(field, (ByteString) value);
break;
case FieldType.Group:
WriteMessageOrGroup(field, (IMessageLite) value);
break;
case FieldType.Message:
WriteMessageOrGroup(field, (IMessageLite) value);
break;
case FieldType.Enum:
{
if (value is IEnumLite)
{
WriteEnum(field, ((IEnumLite) value).Number, ((IEnumLite) value).Name);
}
else if (value is IConvertible)
{
WriteEnum(field, ((IConvertible) value).ToInt32(CultureInfo.InvariantCulture),
((IConvertible) value).ToString(CultureInfo.InvariantCulture));
}
else
{
throw new ArgumentException("Expected an Enum type for field " + field);
}
break;
}
default:
throw InvalidProtocolBufferException.InvalidTag();
}
}
/// <summary>
/// Writes an array of field values
/// </summary>
protected virtual void WriteArray(FieldType fieldType, string field, IEnumerable items)
{
foreach (object obj in items)
{
WriteField(fieldType, field, obj);
}
}
/// <summary>
/// Writes a numeric unknown field of wire type: Fixed32, Fixed64, or Variant
/// </summary>
[CLSCompliant(false)]
protected virtual void WriteUnknown(WireFormat.WireType wireType, int fieldNumber, ulong value)
{
}
/// <summary>
/// Writes an unknown field, Expect WireType of GroupStart or LengthPrefix
/// </summary>
[CLSCompliant(false)]
protected virtual void WriteUnknown(WireFormat.WireType wireType, int fieldNumber, ByteString value)
{
}
#region ICodedOutputStream Members
void ICodedOutputStream.WriteUnknownGroup(int fieldNumber, IMessageLite value)
{
}
void ICodedOutputStream.WriteUnknownBytes(int fieldNumber, ByteString value)
{
}
void ICodedOutputStream.WriteUnknownField(int fieldNumber, WireFormat.WireType type, ulong value)
{
}
void ICodedOutputStream.WriteMessageSetExtension(int fieldNumber, string fieldName, IMessageLite value)
{
}
void ICodedOutputStream.WriteMessageSetExtension(int fieldNumber, string fieldName, ByteString value)
{
}
void ICodedOutputStream.WriteField(FieldType fieldType, int fieldNumber, string fieldName, object value)
{
WriteField(fieldType, fieldName, value);
}
void ICodedOutputStream.WriteDouble(int fieldNumber, string fieldName, double value)
{
Write(fieldName, value);
}
void ICodedOutputStream.WriteFloat(int fieldNumber, string fieldName, float value)
{
Write(fieldName, value);
}
void ICodedOutputStream.WriteUInt64(int fieldNumber, string fieldName, ulong value)
{
Write(fieldName, value);
}
void ICodedOutputStream.WriteInt64(int fieldNumber, string fieldName, long value)
{
Write(fieldName, value);
}
void ICodedOutputStream.WriteInt32(int fieldNumber, string fieldName, int value)
{
Write(fieldName, value);
}
void ICodedOutputStream.WriteFixed64(int fieldNumber, string fieldName, ulong value)
{
Write(fieldName, value);
}
void ICodedOutputStream.WriteFixed32(int fieldNumber, string fieldName, uint value)
{
Write(fieldName, value);
}
void ICodedOutputStream.WriteBool(int fieldNumber, string fieldName, bool value)
{
Write(fieldName, value);
}
void ICodedOutputStream.WriteString(int fieldNumber, string fieldName, string value)
{
Write(fieldName, value);
}
void ICodedOutputStream.WriteGroup(int fieldNumber, string fieldName, IMessageLite value)
{
WriteMessageOrGroup(fieldName, value);
}
void ICodedOutputStream.WriteMessage(int fieldNumber, string fieldName, IMessageLite value)
{
WriteMessageOrGroup(fieldName, value);
}
void ICodedOutputStream.WriteBytes(int fieldNumber, string fieldName, ByteString value)
{
Write(fieldName, value);
}
void ICodedOutputStream.WriteUInt32(int fieldNumber, string fieldName, uint value)
{
Write(fieldName, value);
}
void ICodedOutputStream.WriteEnum(int fieldNumber, string fieldName, int value, object rawValue)
{
WriteEnum(fieldName, value, rawValue.ToString());
}
void ICodedOutputStream.WriteSFixed32(int fieldNumber, string fieldName, int value)
{
Write(fieldName, value);
}
void ICodedOutputStream.WriteSFixed64(int fieldNumber, string fieldName, long value)
{
Write(fieldName, value);
}
void ICodedOutputStream.WriteSInt32(int fieldNumber, string fieldName, int value)
{
Write(fieldName, value);
}
void ICodedOutputStream.WriteSInt64(int fieldNumber, string fieldName, long value)
{
Write(fieldName, value);
}
void ICodedOutputStream.WriteArray(FieldType fieldType, int fieldNumber, string fieldName, IEnumerable list)
{
WriteArray(fieldType, fieldName, list);
}
void ICodedOutputStream.WriteGroupArray<T>(int fieldNumber, string fieldName, IEnumerable<T> list)
{
WriteArray(FieldType.Group, fieldName, list);
}
void ICodedOutputStream.WriteMessageArray<T>(int fieldNumber, string fieldName, IEnumerable<T> list)
{
WriteArray(FieldType.Message, fieldName, list);
}
void ICodedOutputStream.WriteStringArray(int fieldNumber, string fieldName, IEnumerable<string> list)
{
WriteArray(FieldType.String, fieldName, list);
}
void ICodedOutputStream.WriteBytesArray(int fieldNumber, string fieldName, IEnumerable<ByteString> list)
{
WriteArray(FieldType.Bytes, fieldName, list);
}
void ICodedOutputStream.WriteBoolArray(int fieldNumber, string fieldName, IEnumerable<bool> list)
{
WriteArray(FieldType.Bool, fieldName, list);
}
void ICodedOutputStream.WriteInt32Array(int fieldNumber, string fieldName, IEnumerable<int> list)
{
WriteArray(FieldType.Int32, fieldName, list);
}
void ICodedOutputStream.WriteSInt32Array(int fieldNumber, string fieldName, IEnumerable<int> list)
{
WriteArray(FieldType.SInt32, fieldName, list);
}
void ICodedOutputStream.WriteUInt32Array(int fieldNumber, string fieldName, IEnumerable<uint> list)
{
WriteArray(FieldType.UInt32, fieldName, list);
}
void ICodedOutputStream.WriteFixed32Array(int fieldNumber, string fieldName, IEnumerable<uint> list)
{
WriteArray(FieldType.Fixed32, fieldName, list);
}
void ICodedOutputStream.WriteSFixed32Array(int fieldNumber, string fieldName, IEnumerable<int> list)
{
WriteArray(FieldType.SFixed32, fieldName, list);
}
void ICodedOutputStream.WriteInt64Array(int fieldNumber, string fieldName, IEnumerable<long> list)
{
WriteArray(FieldType.Int64, fieldName, list);
}
void ICodedOutputStream.WriteSInt64Array(int fieldNumber, string fieldName, IEnumerable<long> list)
{
WriteArray(FieldType.SInt64, fieldName, list);
}
void ICodedOutputStream.WriteUInt64Array(int fieldNumber, string fieldName, IEnumerable<ulong> list)
{
WriteArray(FieldType.UInt64, fieldName, list);
}
void ICodedOutputStream.WriteFixed64Array(int fieldNumber, string fieldName, IEnumerable<ulong> list)
{
WriteArray(FieldType.Fixed64, fieldName, list);
}
void ICodedOutputStream.WriteSFixed64Array(int fieldNumber, string fieldName, IEnumerable<long> list)
{
WriteArray(FieldType.SFixed64, fieldName, list);
}
void ICodedOutputStream.WriteDoubleArray(int fieldNumber, string fieldName, IEnumerable<double> list)
{
WriteArray(FieldType.Double, fieldName, list);
}
void ICodedOutputStream.WriteFloatArray(int fieldNumber, string fieldName, IEnumerable<float> list)
{
WriteArray(FieldType.Float, fieldName, list);
}
void ICodedOutputStream.WriteEnumArray<T>(int fieldNumber, string fieldName, IEnumerable<T> list)
{
WriteArray(FieldType.Enum, fieldName, list);
}
void ICodedOutputStream.WritePackedArray(FieldType fieldType, int fieldNumber, string fieldName,
IEnumerable list)
{
WriteArray(fieldType, fieldName, list);
}
void ICodedOutputStream.WritePackedBoolArray(int fieldNumber, string fieldName, int computedSize,
IEnumerable<bool> list)
{
WriteArray(FieldType.Bool, fieldName, list);
}
void ICodedOutputStream.WritePackedInt32Array(int fieldNumber, string fieldName, int computedSize,
IEnumerable<int> list)
{
WriteArray(FieldType.Int32, fieldName, list);
}
void ICodedOutputStream.WritePackedSInt32Array(int fieldNumber, string fieldName, int computedSize,
IEnumerable<int> list)
{
WriteArray(FieldType.SInt32, fieldName, list);
}
void ICodedOutputStream.WritePackedUInt32Array(int fieldNumber, string fieldName, int computedSize,
IEnumerable<uint> list)
{
WriteArray(FieldType.UInt32, fieldName, list);
}
void ICodedOutputStream.WritePackedFixed32Array(int fieldNumber, string fieldName, int computedSize,
IEnumerable<uint> list)
{
WriteArray(FieldType.Fixed32, fieldName, list);
}
void ICodedOutputStream.WritePackedSFixed32Array(int fieldNumber, string fieldName, int computedSize,
IEnumerable<int> list)
{
WriteArray(FieldType.SFixed32, fieldName, list);
}
void ICodedOutputStream.WritePackedInt64Array(int fieldNumber, string fieldName, int computedSize,
IEnumerable<long> list)
{
WriteArray(FieldType.Int64, fieldName, list);
}
void ICodedOutputStream.WritePackedSInt64Array(int fieldNumber, string fieldName, int computedSize,
IEnumerable<long> list)
{
WriteArray(FieldType.SInt64, fieldName, list);
}
void ICodedOutputStream.WritePackedUInt64Array(int fieldNumber, string fieldName, int computedSize,
IEnumerable<ulong> list)
{
WriteArray(FieldType.UInt64, fieldName, list);
}
void ICodedOutputStream.WritePackedFixed64Array(int fieldNumber, string fieldName, int computedSize,
IEnumerable<ulong> list)
{
WriteArray(FieldType.Fixed64, fieldName, list);
}
void ICodedOutputStream.WritePackedSFixed64Array(int fieldNumber, string fieldName, int computedSize,
IEnumerable<long> list)
{
WriteArray(FieldType.SFixed64, fieldName, list);
}
void ICodedOutputStream.WritePackedDoubleArray(int fieldNumber, string fieldName, int computedSize,
IEnumerable<double> list)
{
WriteArray(FieldType.Double, fieldName, list);
}
void ICodedOutputStream.WritePackedFloatArray(int fieldNumber, string fieldName, int computedSize,
IEnumerable<float> list)
{
WriteArray(FieldType.Float, fieldName, list);
}
void ICodedOutputStream.WritePackedEnumArray<T>(int fieldNumber, string fieldName, int computedSize,
IEnumerable<T> list)
{
WriteArray(FieldType.Enum, fieldName, list);
}
#endregion
}
}
|