-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathgenerator_csharp.cpp
8732 lines (7657 loc) · 324 KB
/
generator_csharp.cpp
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
\file generator_csharp.cpp
\brief Fast binary encoding C# generator implementation
\author Ivan Shynkarenka
\date 20.04.2018
\copyright MIT License
*/
#include "generator_csharp.h"
namespace FBE {
void GeneratorCSharp::Generate(const std::shared_ptr<Package>& package)
{
std::string domain = (package->domain && !package->domain->empty()) ? (*package->domain + ".") : "";
GeneratePackage(domain, package);
}
void GeneratorCSharp::GenerateHeader(const std::string& source)
{
std::string code = R"CODE(//------------------------------------------------------------------------------
// <auto-generated>
// Automatically generated by the Fast Binary Encoding compiler, do not modify!
// https://github.com/chronoxor/FastBinaryEncoding
// Source: _INPUT_
// FBE version: _VERSION_
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("_INPUT_"), source);
code = std::regex_replace(code, std::regex("_VERSION_"), version);
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
}
void GeneratorCSharp::GenerateFooter()
{
}
void GeneratorCSharp::GenerateImports()
{
// Generate common imports
WriteLine();
WriteLineIndent("using System;");
WriteLineIndent("using System.Collections.Generic;");
WriteLineIndent("using System.Diagnostics;");
WriteLineIndent("using System.Globalization;");
WriteLineIndent("using System.IO;");
WriteLineIndent("using System.Linq;");
WriteLineIndent("using System.Numerics;");
WriteLineIndent("using System.Runtime.Serialization;");
WriteLineIndent("using System.Text;");
WriteLineIndent("using System.Threading;");
WriteLineIndent("using System.Threading.Tasks;");
if (JSON())
{
WriteLineIndent("#if UTF8JSON");
WriteLineIndent("using Utf8Json;");
WriteLineIndent("using Utf8Json.Resolvers;");
WriteLineIndent("#elif NEWTONSOFTJSON");
WriteLineIndent("using Newtonsoft.Json;");
WriteLineIndent("#else");
WriteLineIndent("using System.Text.Json;");
WriteLineIndent("using System.Text.Json.Serialization;");
WriteLineIndent("#endif");
}
}
void GeneratorCSharp::GenerateImports(const std::string& domain, const std::shared_ptr<Package>& p)
{
// Generate common imports
GenerateImports();
// Generate packages import
if (p->import)
{
WriteLine();
for (auto import : p->import->imports)
WriteLineIndent("using " + domain + *import + ";");
}
}
void GeneratorCSharp::GenerateFBEUuidGenerator()
{
std::string code = R"CODE(
// Fast Binary Encoding UUID generator
public static class UuidGenerator
{
// Gregorian epoch
private static readonly DateTime GregorianEpoch = new DateTime(1582, 10, 15, 0, 0, 0, DateTimeKind.Utc);
// Lock and random generator
private static readonly object Lock = new object();
private static readonly Random Generator = new Random();
// Node & clock sequence bytes
private static readonly byte[] NodeBytes;
private static readonly byte[] ClockSequenceBytes;
// Last UUID generated timestamp
private static DateTime _last = DateTime.UtcNow;
static UuidGenerator()
{
NodeBytes = new byte[6];
Generator.NextBytes(NodeBytes);
ClockSequenceBytes = new byte[2];
Generator.NextBytes(ClockSequenceBytes);
}
// Generate nil UUID0 (all bits set to zero)
public static Guid Nil() { return new Guid(); }
// Generate sequential UUID1 (time based version)
public static Guid Sequential()
{
var now = DateTime.UtcNow;
// Generate new clock sequence bytes to get rid of UUID duplicates
lock (Lock)
{
if (now <= _last)
Generator.NextBytes(ClockSequenceBytes);
_last = now;
}
long ticks = (now - GregorianEpoch).Ticks;
Span<byte> guid = stackalloc byte[16];
byte[] timestamp = BitConverter.GetBytes(ticks);
// Copy node
for (int i = 0; i < Math.Min(6, NodeBytes.Length); i++)
guid[10 + i] = NodeBytes[i];
// Copy clock sequence
for (int i = 0; i < Math.Min(2, ClockSequenceBytes.Length); i++)
guid[8 + i] = ClockSequenceBytes[i];
// Copy timestamp
for (int i = 0; i < Math.Min(8, timestamp.Length); i++)
guid[i] = timestamp[i];
// Set the variant
guid[8] &= 0x3F;
guid[8] |= 0x80;
// Set the version
guid[7] &= 0x0F;
guid[7] |= 0x10;
return new Guid(guid);
}
// Generate random UUID4 (randomly or pseudo-randomly generated version)
public static Guid Random() { return Guid.NewGuid(); }
}
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
}
void GeneratorCSharp::GenerateFBEBuffer()
{
std::string code = R"CODE(
// Fast Binary Encoding dynamic bytes buffer
public class Buffer
{
private byte[] _data;
private long _size;
private long _offset;
// Is the buffer empty?
public bool IsEmpty => (_data == null) || (_size == 0);
// Bytes memory buffer
public byte[] Data => _data;
// Bytes memory buffer capacity
public long Capacity => _data.Length;
// Bytes memory buffer size
public long Size => _size;
// Bytes memory buffer offset
public long Offset => _offset;
// Initialize a new expandable buffer with zero capacity
public Buffer() { Attach(); }
// Initialize a new expandable buffer with the given capacity
public Buffer(long capacity) { Attach(capacity); }
// Initialize a new buffer based on the specified byte array
public Buffer(byte[] buffer) { Attach(buffer); }
// Initialize a new buffer based on the specified region (offset) of a byte array
public Buffer(byte[] buffer, long offset) { Attach(buffer, offset); }
// Initialize a new buffer based on the specified region (size and offset) of a byte array
public Buffer(byte[] buffer, long size, long offset) { Attach(buffer, size, offset); }
#region Attach memory buffer methods
public void Attach() { _data = new byte[0]; _size = 0; _offset = 0; }
public void Attach(long capacity) { _data = new byte[capacity]; _size = 0; _offset = 0; }
public void Attach(byte[] buffer) { _data = buffer; _size = buffer.Length; _offset = 0; }
public void Attach(byte[] buffer, long offset) { _data = buffer; _size = buffer.Length; _offset = offset; }
public void Attach(byte[] buffer, long size, long offset) { _data = buffer; _size = size; _offset = offset; }
#endregion
#region Memory buffer methods
// Allocate memory in the current buffer and return offset to the allocated memory block
public long Allocate(long size)
{
Debug.Assert((size >= 0), "Invalid allocation size!");
if (size < 0)
throw new ArgumentException("Invalid allocation size!", nameof(size));
long offset = Size;
// Calculate a new buffer size
long total = _size + size;
if (total <= Capacity)
{
_size = total;
return offset;
}
byte[] data = new byte[Math.Max(total, 2 * Capacity)];
Array.Copy(_data, 0, data, 0, _size);
_data = data;
_size = total;
return offset;
}
// Remove some memory of the given size from the current buffer
public void Remove(long offset, long size)
{
Debug.Assert(((offset + size) <= Size), "Invalid offset & size!");
if ((offset + size) > Size)
throw new ArgumentException("Invalid offset & size!", nameof(offset));
Array.Copy(_data, offset + size, _data, offset, _size - size - offset);
_size -= size;
if (_offset >= (offset + size))
_offset -= size;
else if (_offset >= offset)
{
_offset -= _offset - offset;
if (_offset > Size)
_offset = Size;
}
}
// Reserve memory of the given capacity in the current buffer
public void Reserve(long capacity)
{
Debug.Assert((capacity >= 0), "Invalid reserve capacity!");
if (capacity < 0)
throw new ArgumentException("Invalid reserve capacity!", nameof(capacity));
if (capacity > Capacity)
{
byte[] data = new byte[Math.Max(capacity, 2 * Capacity)];
Array.Copy(_data, 0, data, 0, _size);
_data = data;
}
}
// Resize the current buffer
public void Resize(long size)
{
Reserve(size);
_size = size;
if (_offset > _size)
_offset = _size;
}
// Reset the current buffer and its offset
public void Reset()
{
_size = 0;
_offset = 0;
}
// Shift the current buffer offset
public void Shift(long offset) { _offset += offset; }
// Unshift the current buffer offset
public void Unshift(long offset) { _offset -= offset; }
#endregion
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
code = R"CODE(
#region Buffer I/O methods
public static bool ReadBool(byte[] buffer, long offset)
{
return buffer[offset] != 0;
}
public static byte ReadByte(byte[] buffer, long offset)
{
return buffer[offset];
}
public static char ReadChar(byte[] buffer, long offset)
{
return (char)ReadUInt8(buffer, offset);
}
public static char ReadWChar(byte[] buffer, long offset)
{
return (char)ReadUInt32(buffer, offset);
}
public static sbyte ReadInt8(byte[] buffer, long offset)
{
return (sbyte)buffer[offset];
}
public static byte ReadUInt8(byte[] buffer, long offset)
{
return buffer[offset];
}
public static short ReadInt16(byte[] buffer, long offset)
{
return (short)(buffer[offset + 0] | (buffer[offset + 1] << 8));
}
public static ushort ReadUInt16(byte[] buffer, long offset)
{
return (ushort)(buffer[offset + 0] | (buffer[offset + 1] << 8));
}
public static int ReadInt32(byte[] buffer, long offset)
{
return (buffer[offset + 0] << 0)|
(buffer[offset + 1] << 8)|
(buffer[offset + 2] << 16)|
(buffer[offset + 3] << 24);
}
public static uint ReadUInt32(byte[] buffer, long offset)
{
return ((uint)buffer[offset + 0] << 0)|
((uint)buffer[offset + 1] << 8)|
((uint)buffer[offset + 2] << 16)|
((uint)buffer[offset + 3] << 24);
}
public static long ReadInt64(byte[] buffer, long offset)
{
return ((long)buffer[offset + 0] << 0)|
((long)buffer[offset + 1] << 8)|
((long)buffer[offset + 2] << 16)|
((long)buffer[offset + 3] << 24)|
((long)buffer[offset + 4] << 32)|
((long)buffer[offset + 5] << 40)|
((long)buffer[offset + 6] << 48)|
((long)buffer[offset + 7] << 56);
}
public static ulong ReadUInt64(byte[] buffer, long offset)
{
return ((ulong)buffer[offset + 0] << 0)|
((ulong)buffer[offset + 1] << 8)|
((ulong)buffer[offset + 2] << 16)|
((ulong)buffer[offset + 3] << 24)|
((ulong)buffer[offset + 4] << 32)|
((ulong)buffer[offset + 5] << 40)|
((ulong)buffer[offset + 6] << 48)|
((ulong)buffer[offset + 7] << 56);
}
public static ulong ReadUInt64Guid(byte[] buffer, long offset)
{
return ((ulong)buffer[offset + 0] << 24)|
((ulong)buffer[offset + 1] << 16)|
((ulong)buffer[offset + 2] << 8)|
((ulong)buffer[offset + 3] << 0)|
((ulong)buffer[offset + 4] << 40)|
((ulong)buffer[offset + 5] << 32)|
((ulong)buffer[offset + 6] << 56)|
((ulong)buffer[offset + 7] << 48);
}
public static float ReadFloat(byte[] buffer, long offset)
{
var bits = default(FloatUnion);
bits.UIntData = ReadUInt32(buffer, offset);
return bits.FloatData;
}
public static double ReadDouble(byte[] buffer, long offset)
{
var bits = default(DoubleUnion);
bits.ULongData = ReadUInt64(buffer, offset);
return bits.DoubleData;
}
public static decimal ReadDecimal(byte[] buffer, long offset)
{
var bits = default(DecimalUnion);
bits.UIntLow = ReadUInt32(buffer, offset);
bits.UIntMid = ReadUInt32(buffer, offset + 4);
bits.UIntHigh = ReadUInt32(buffer, offset + 8);
bits.UIntFlags = ReadUInt32(buffer, offset + 12);
return bits.DecimalData;
}
public static byte[] ReadBytes(byte[] buffer, long offset, long size)
{
byte[] result = new byte[size];
Array.Copy(buffer, offset, result, 0, (int)size);
return result;
}
public static string ReadString(byte[] buffer, long offset, long size)
{
return Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
}
public static Guid ReadUUID(byte[] buffer, long offset)
{
var bits = default(GuidUnion);
bits.ULongHigh = ReadUInt64Guid(buffer, offset);
bits.ULongLow = ReadUInt64(buffer, offset + 8);
return bits.GuidData;
}
public static void Write(byte[] buffer, long offset, bool value)
{
buffer[offset] = (byte)(value ? 1 : 0);
}
public static void Write(byte[] buffer, long offset, sbyte value)
{
buffer[offset] = (byte)value;
}
public static void Write(byte[] buffer, long offset, byte value)
{
buffer[offset] = value;
}
public static void Write(byte[] buffer, long offset, short value)
{
buffer[offset + 0] = (byte)(value >> 0);
buffer[offset + 1] = (byte)(value >> 8);
}
public static void Write(byte[] buffer, long offset, ushort value)
{
buffer[offset + 0] = (byte)(value >> 0);
buffer[offset + 1] = (byte)(value >> 8);
}
public static void Write(byte[] buffer, long offset, int value)
{
buffer[offset + 0] = (byte)(value >> 0);
buffer[offset + 1] = (byte)(value >> 8);
buffer[offset + 2] = (byte)(value >> 16);
buffer[offset + 3] = (byte)(value >> 24);
}
public static void Write(byte[] buffer, long offset, uint value)
{
buffer[offset + 0] = (byte)(value >> 0);
buffer[offset + 1] = (byte)(value >> 8);
buffer[offset + 2] = (byte)(value >> 16);
buffer[offset + 3] = (byte)(value >> 24);
}
public static void Write(byte[] buffer, long offset, long value)
{
buffer[offset + 0] = (byte)(value >> 0);
buffer[offset + 1] = (byte)(value >> 8);
buffer[offset + 2] = (byte)(value >> 16);
buffer[offset + 3] = (byte)(value >> 24);
buffer[offset + 4] = (byte)(value >> 32);
buffer[offset + 5] = (byte)(value >> 40);
buffer[offset + 6] = (byte)(value >> 48);
buffer[offset + 7] = (byte)(value >> 56);
}
public static void Write(byte[] buffer, long offset, ulong value)
{
buffer[offset + 0] = (byte)(value >> 0);
buffer[offset + 1] = (byte)(value >> 8);
buffer[offset + 2] = (byte)(value >> 16);
buffer[offset + 3] = (byte)(value >> 24);
buffer[offset + 4] = (byte)(value >> 32);
buffer[offset + 5] = (byte)(value >> 40);
buffer[offset + 6] = (byte)(value >> 48);
buffer[offset + 7] = (byte)(value >> 56);
}
public static void WriteGuid(byte[] buffer, long offset, ulong value)
{
buffer[offset + 0] = (byte)(value >> 24);
buffer[offset + 1] = (byte)(value >> 16);
buffer[offset + 2] = (byte)(value >> 8);
buffer[offset + 3] = (byte)(value >> 0);
buffer[offset + 4] = (byte)(value >> 40);
buffer[offset + 5] = (byte)(value >> 32);
buffer[offset + 6] = (byte)(value >> 56);
buffer[offset + 7] = (byte)(value >> 48);
}
public static void Write(byte[] buffer, long offset, float value)
{
var bits = default(FloatUnion);
bits.FloatData = value;
Write(buffer, offset, bits.UIntData);
}
public static void Write(byte[] buffer, long offset, double value)
{
var bits = default(DoubleUnion);
bits.DoubleData = value;
Write(buffer, offset, bits.ULongData);
}
public static void Write(byte[] buffer, long offset, decimal value)
{
var bits = default(DecimalUnion);
bits.DecimalData = value;
Write(buffer, offset, bits.UIntLow);
Write(buffer, offset + 4, bits.UIntMid);
Write(buffer, offset + 8, bits.UIntHigh);
Write(buffer, offset + 12, bits.UIntFlags);
}
public static void Write(byte[] buffer, long offset, byte[] value)
{
Array.Copy(value, 0, buffer, offset, value.Length);
}
public static void Write(byte[] buffer, long offset, byte[] value, long valueOffset, long valueSize)
{
Array.Copy(value, valueOffset, buffer, offset, valueSize);
}
public static void Write(byte[] buffer, long offset, byte value, long valueCount)
{
for (long i = 0; i < valueCount; i++)
buffer[offset + i] = value;
}
public static long Write(byte[] buffer, long offset, string value)
{
return Encoding.UTF8.GetBytes(value, 0, value.Length, buffer, (int)offset);
}
public static void Write(byte[] buffer, long offset, Guid value)
{
var bits = default(GuidUnion);
bits.GuidData = value;
WriteGuid(buffer, offset, bits.ULongHigh);
Write(buffer, offset + 8, bits.ULongLow);
}
#endregion
#region Utilities
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)]
private struct FloatUnion
{
[System.Runtime.InteropServices.FieldOffset(0)]
public uint UIntData;
[System.Runtime.InteropServices.FieldOffset(0)]
public float FloatData;
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)]
private struct DoubleUnion
{
[System.Runtime.InteropServices.FieldOffset(0)]
public ulong ULongData;
[System.Runtime.InteropServices.FieldOffset(0)]
public double DoubleData;
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)]
private struct DecimalUnion
{
[System.Runtime.InteropServices.FieldOffset(0)]
public uint UIntFlags;
[System.Runtime.InteropServices.FieldOffset(4)]
public uint UIntHigh;
[System.Runtime.InteropServices.FieldOffset(8)]
public uint UIntLow;
[System.Runtime.InteropServices.FieldOffset(12)]
public uint UIntMid;
[System.Runtime.InteropServices.FieldOffset(0)]
public decimal DecimalData;
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)]
private struct GuidUnion
{
[System.Runtime.InteropServices.FieldOffset(0)]
public ulong ULongHigh;
[System.Runtime.InteropServices.FieldOffset(8)]
public ulong ULongLow;
[System.Runtime.InteropServices.FieldOffset(0)]
public Guid GuidData;
}
#endregion
}
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
}
void GeneratorCSharp::GenerateFBEBaseModel()
{
std::string code = R"CODE(
// Fast Binary Encoding base model
public class Model
{
// Bytes buffer
public Buffer Buffer { get; }
protected Model() { Buffer = new Buffer(); }
protected Model(Buffer buffer) { Buffer = buffer; }
#region Attach memory buffer methods
public void Attach() { Buffer.Attach(); }
public void Attach(long capacity) { Buffer.Attach(capacity); }
public void Attach(byte[] buffer) { Buffer.Attach(buffer); }
public void Attach(byte[] buffer, long offset) { Buffer.Attach(buffer, offset); }
public void Attach(byte[] buffer, long size, long offset) { Buffer.Attach(buffer, size, offset); }
public void Attach(Buffer buffer) { Buffer.Attach(buffer.Data, buffer.Size, buffer.Offset); }
public void Attach(Buffer buffer, long offset) { Buffer.Attach(buffer.Data, buffer.Size, offset); }
#endregion
#region Memory buffer methods
public long Allocate(long size) { return Buffer.Allocate(size); }
public void Remove(long offset, long size) { Buffer.Remove(offset, size); }
public void Reserve(long capacity) { Buffer.Reserve(capacity); }
public void Resize(long size) { Buffer.Resize(size); }
public void Reset() { Buffer.Reset(); }
public void Shift(long offset) { Buffer.Shift(offset); }
public void Unshift(long offset) { Buffer.Unshift(offset); }
#endregion
#region Buffer I/O methods
protected uint ReadUInt32(long offset) { return Buffer.ReadUInt32(Buffer.Data, Buffer.Offset + offset); }
protected void Write(long offset, uint value) { Buffer.Write(Buffer.Data, Buffer.Offset + offset, value); }
#endregion
}
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
}
void GeneratorCSharp::GenerateFBEFieldModelBase()
{
std::string code = R"CODE(
// Fast Binary Encoding base types enumeration
public enum BaseTypes
{
BOOL,
BYTE,
BYTES,
CHAR,
WCHAR,
INT8,
UINT8,
INT16,
UINT16,
INT32,
UINT32,
INT64,
UINT64,
FLOAT,
DOUBLE,
DECIMAL,
UUID,
STRING,
TIMESTAMP
}
// Fast Binary Encoding base field model
public abstract class FieldModelBase
{
protected Buffer _buffer;
protected long _offset;
protected FieldModelBase(Buffer buffer, long offset)
{
_buffer = buffer;
_offset = offset;
}
// Get the field offset
public long FBEOffset { get => _offset; set => _offset = value; }
// Get the field size
public virtual long FBESize => 0;
// Get the field extra size
public virtual long FBEExtra => 0;
// Shift the current field offset
public void FBEShift(long size) { _offset += size; }
// Unshift the current field offset
public void FBEUnshift(long size) { _offset -= size; }
#region Buffer I/O methods
protected bool ReadBool(long offset) { return Buffer.ReadBool(_buffer.Data, _buffer.Offset + offset); }
protected byte ReadByte(long offset) { return Buffer.ReadByte(_buffer.Data, _buffer.Offset + offset); }
protected char ReadChar(long offset) { return Buffer.ReadChar(_buffer.Data, _buffer.Offset + offset); }
protected char ReadWChar(long offset) { return Buffer.ReadWChar(_buffer.Data, _buffer.Offset + offset); }
protected sbyte ReadInt8(long offset) { return Buffer.ReadInt8(_buffer.Data, _buffer.Offset + offset); }
protected byte ReadUInt8(long offset) { return Buffer.ReadUInt8(_buffer.Data, _buffer.Offset + offset); }
protected short ReadInt16(long offset) { return Buffer.ReadInt16(_buffer.Data, _buffer.Offset + offset); }
protected ushort ReadUInt16(long offset) { return Buffer.ReadUInt16(_buffer.Data, _buffer.Offset + offset); }
protected int ReadInt32(long offset) { return Buffer.ReadInt32(_buffer.Data, _buffer.Offset + offset); }
protected uint ReadUInt32(long offset) { return Buffer.ReadUInt32(_buffer.Data, _buffer.Offset + offset); }
protected long ReadInt64(long offset) { return Buffer.ReadInt64(_buffer.Data, _buffer.Offset + offset); }
protected ulong ReadUInt64(long offset) { return Buffer.ReadUInt64(_buffer.Data, _buffer.Offset + offset); }
protected float ReadFloat(long offset) { return Buffer.ReadFloat(_buffer.Data, _buffer.Offset + offset); }
protected double ReadDouble(long offset) { return Buffer.ReadDouble(_buffer.Data, _buffer.Offset + offset); }
protected decimal ReadDecimal(long offset) { return Buffer.ReadDecimal(_buffer.Data, _buffer.Offset + offset); }
protected byte[] ReadBytes(long offset, long size) { return Buffer.ReadBytes(_buffer.Data, _buffer.Offset + offset, size); }
protected string ReadString(long offset, long size) { return Buffer.ReadString(_buffer.Data, _buffer.Offset + offset, size); }
protected Guid ReadUUID(long offset) { return Buffer.ReadUUID(_buffer.Data, _buffer.Offset + offset); }
protected void Write(long offset, bool value) { Buffer.Write(_buffer.Data, _buffer.Offset + offset, value); }
protected void Write(long offset, sbyte value) { Buffer.Write(_buffer.Data, _buffer.Offset + offset, value); }
protected void Write(long offset, byte value) { Buffer.Write(_buffer.Data, _buffer.Offset + offset, value); }
protected void Write(long offset, short value) { Buffer.Write(_buffer.Data, _buffer.Offset + offset, value); }
protected void Write(long offset, ushort value) { Buffer.Write(_buffer.Data, _buffer.Offset + offset, value); }
protected void Write(long offset, int value) { Buffer.Write(_buffer.Data, _buffer.Offset + offset, value); }
protected void Write(long offset, uint value) { Buffer.Write(_buffer.Data, _buffer.Offset + offset, value); }
protected void Write(long offset, long value) { Buffer.Write(_buffer.Data, _buffer.Offset + offset, value); }
protected void Write(long offset, ulong value) { Buffer.Write(_buffer.Data, _buffer.Offset + offset, value); }
protected void Write(long offset, float value) { Buffer.Write(_buffer.Data, _buffer.Offset + offset, value); }
protected void Write(long offset, double value) { Buffer.Write(_buffer.Data, _buffer.Offset + offset, value); }
protected void Write(long offset, decimal value) { Buffer.Write(_buffer.Data, _buffer.Offset + offset, value); }
protected void Write(long offset, byte[] value) { Buffer.Write(_buffer.Data, _buffer.Offset + offset, value); }
protected void Write(long offset, byte[] value, long valueOffset, long valueSize) { Buffer.Write(_buffer.Data, _buffer.Offset + offset, value, valueOffset, valueSize); }
protected void Write(long offset, byte value, long valueCount) { Buffer.Write(_buffer.Data, _buffer.Offset + offset, value, valueCount); }
protected long Write(long offset, string value) { return Buffer.Write(_buffer.Data, _buffer.Offset + offset, value); }
protected void Write(long offset, Guid value) { Buffer.Write(_buffer.Data, _buffer.Offset + offset, value); }
#endregion
}
// Fast Binary Encoding field model value type
public abstract class FieldModelValueType<T> : FieldModelBase
where T : struct
{
protected FieldModelValueType(Buffer buffer, long offset) : base(buffer, offset) {}
// Clone the field model
public abstract FieldModelValueType<T> Clone();
// Check if the value is valid
public virtual bool Verify() { return true; }
// Get the value
public abstract void Get(out T value);
public abstract void Get(out T value, T defaults);
// Set the value
public abstract void Set(T value);
// Create field model of the given type
public static FieldModelValueType<T> CreateFieldModel(BaseTypes type, Buffer buffer, long offset)
{
switch (type)
{
case BaseTypes.BOOL:
return new FieldModelBool(buffer, offset) as FieldModelValueType<T>;
case BaseTypes.BYTE:
return new FieldModelByte(buffer, offset) as FieldModelValueType<T>;
case BaseTypes.CHAR:
return new FieldModelChar(buffer, offset) as FieldModelValueType<T>;
case BaseTypes.WCHAR:
return new FieldModelWChar(buffer, offset) as FieldModelValueType<T>;
case BaseTypes.INT8:
return new FieldModelInt8(buffer, offset) as FieldModelValueType<T>;
case BaseTypes.UINT8:
return new FieldModelUInt8(buffer, offset) as FieldModelValueType<T>;
case BaseTypes.INT16:
return new FieldModelInt16(buffer, offset) as FieldModelValueType<T>;
case BaseTypes.UINT16:
return new FieldModelUInt16(buffer, offset) as FieldModelValueType<T>;
case BaseTypes.INT32:
return new FieldModelInt32(buffer, offset) as FieldModelValueType<T>;
case BaseTypes.UINT32:
return new FieldModelUInt32(buffer, offset) as FieldModelValueType<T>;
case BaseTypes.INT64:
return new FieldModelInt64(buffer, offset) as FieldModelValueType<T>;
case BaseTypes.UINT64:
return new FieldModelUInt64(buffer, offset) as FieldModelValueType<T>;
case BaseTypes.FLOAT:
return new FieldModelFloat(buffer, offset) as FieldModelValueType<T>;
case BaseTypes.DOUBLE:
return new FieldModelDouble(buffer, offset) as FieldModelValueType<T>;
case BaseTypes.DECIMAL:
return new FieldModelDecimal(buffer, offset) as FieldModelValueType<T>;
case BaseTypes.UUID:
return new FieldModelUUID(buffer, offset) as FieldModelValueType<T>;
case BaseTypes.TIMESTAMP:
return new FieldModelTimestamp(buffer, offset) as FieldModelValueType<T>;
default:
Debug.Assert(false, "Unknown type!");
return null;
}
}
}
// Fast Binary Encoding field model reference type
public abstract class FieldModelReferenceType<T> : FieldModelBase
where T : class
{
protected FieldModelReferenceType(Buffer buffer, long offset) : base(buffer, offset) {}
// Clone the field model
public abstract FieldModelReferenceType<T> Clone();
// Check if the value is valid
public virtual bool Verify() { return true; }
// Get the value
public abstract void Get(out T value);
public abstract void Get(out T value, T defaults);
// Set the value
public abstract void Set(T value);
// Create field model of the given type
public static FieldModelReferenceType<T> CreateFieldModel(BaseTypes type, Buffer buffer, long offset)
{
switch (type)
{
case BaseTypes.BYTES:
return new FieldModelBytes(buffer, offset) as FieldModelReferenceType<T>;
case BaseTypes.STRING:
return new FieldModelString(buffer, offset) as FieldModelReferenceType<T>;
default:
Debug.Assert(false, "Unknown type!");
return null;
}
}
}
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
}
void GeneratorCSharp::GenerateFBEFieldModel(const std::string& name, const std::string& type, const std::string& base, const std::string& size, const std::string& defaults)
{
std::string code = R"CODE(
// Fast Binary Encoding _TYPE_ field model
public class FieldModel_NAME_ : FieldModelValueType<_TYPE_>
{
public FieldModel_NAME_(Buffer buffer, long offset) : base(buffer, offset) {}
// Get the field size
public override long FBESize => _SIZE_;
// Clone the field model
public override FieldModelValueType<_TYPE_> Clone() { return new FieldModel_NAME_(_buffer, _offset); }
// Get the value
public override void Get(out _TYPE_ value) { Get(out value, _DEFAULTS_); }
public override void Get(out _TYPE_ value, _TYPE_ defaults)
{
if ((_buffer.Offset + FBEOffset + FBESize) > _buffer.Size)
{
value = defaults;
return;
}
value = Read_NAME_(FBEOffset);
}
// Set the value
public override void Set(_TYPE_ value)
{
Debug.Assert(((_buffer.Offset + FBEOffset + FBESize) <= _buffer.Size), "Model is broken!");
if ((_buffer.Offset + FBEOffset + FBESize) > _buffer.Size)
return;
Write(FBEOffset, _BASE_value);
}
}
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("_NAME_"), name);
code = std::regex_replace(code, std::regex("_TYPE_"), type);
code = std::regex_replace(code, std::regex("_BASE_"), base);
code = std::regex_replace(code, std::regex("_SIZE_"), size);
code = std::regex_replace(code, std::regex("_DEFAULTS_"), defaults);
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
}
void GeneratorCSharp::GenerateFBEFieldModelTimestamp()
{
std::string code = R"CODE(
// Fast Binary Encoding timestamp field model
public class FieldModelTimestamp : FieldModelValueType<DateTime>
{
private const long UnixEpoch = 621355968000000000;
public FieldModelTimestamp(Buffer buffer, long offset) : base(buffer, offset) {}
// Get the field size
public override long FBESize => 8;
// Clone the field model
public override FieldModelValueType<DateTime> Clone() { return new FieldModelTimestamp(_buffer, _offset); }
// Get the timestamp value
public override void Get(out DateTime value) { Get(out value, new DateTime(UnixEpoch, DateTimeKind.Utc)); }
public override void Get(out DateTime value, DateTime defaults)
{
if ((_buffer.Offset + FBEOffset + FBESize) > _buffer.Size)
{
value = defaults;
return;
}
ulong ticks = ReadUInt64(FBEOffset) / 100;
value = new DateTime((long)(UnixEpoch + ticks), DateTimeKind.Utc);
}
// Set the timestamp value
public override void Set(DateTime value)
{
Debug.Assert(((_buffer.Offset + FBEOffset + FBESize) <= _buffer.Size), "Model is broken!");
if ((_buffer.Offset + FBEOffset + FBESize) > _buffer.Size)
return;
ulong nanoseconds = (ulong)((value.Ticks - UnixEpoch) * 100);
Write(FBEOffset, nanoseconds);
}
}
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
}
void GeneratorCSharp::GenerateFBEFieldModelBytes()
{
std::string code = R"CODE(
// Fast Binary Encoding bytes field model
public class FieldModelBytes : FieldModelReferenceType<MemoryStream>
{
public FieldModelBytes(Buffer buffer, long offset) : base(buffer, offset) {}
// Get the field size
public override long FBESize => 4;
// Get the field extra size
public override long FBEExtra
{
get
{
if ((_buffer.Offset + FBEOffset + FBESize) > _buffer.Size)
return 0;