-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathgenerator_kotlin.cpp
8905 lines (7679 loc) · 306 KB
/
generator_kotlin.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_kotlin.cpp
\brief Fast binary encoding Kotlin generator implementation
\author Ivan Shynkarenka
\date 03.10.2018
\copyright MIT License
*/
#include "generator_kotlin.h"
namespace FBE {
void GeneratorKotlin::Generate(const std::shared_ptr<Package>& package)
{
std::string domain = (package->domain && !package->domain->empty()) ? (*package->domain + ".") : "";
GenerateFBEPackage(domain, "fbe");
GenerateFBEUUIDGenerator(domain, "fbe");
GenerateFBEBuffer(domain, "fbe");
GenerateFBEModel(domain, "fbe");
GenerateFBEFieldModel(domain, "fbe");
GenerateFBEFieldModel(domain, "fbe", "Boolean", "Boolean", "", "1", "false");
GenerateFBEFieldModel(domain, "fbe", "Byte", "Byte", "", "1", "0.toByte()");
GenerateFBEFieldModel(domain, "fbe", "Char", "Char", ".code", "1", "'\\u0000'");
GenerateFBEFieldModel(domain, "fbe", "WChar", "Char", ".code", "4", "'\\u0000'");
GenerateFBEFieldModel(domain, "fbe", "Int8", "Byte", "", "1", "0.toByte()");
GenerateFBEFieldModel(domain, "fbe", "UInt8", "UByte", "", "1", "0.toUByte()");
GenerateFBEFieldModel(domain, "fbe", "Int16", "Short", "", "2", "0.toShort()");
GenerateFBEFieldModel(domain, "fbe", "UInt16", "UShort", "", "2", "0.toUShort()");
GenerateFBEFieldModel(domain, "fbe", "Int32", "Int", "", "4", "0");
GenerateFBEFieldModel(domain, "fbe", "UInt32", "UInt", "", "4", "0u");
GenerateFBEFieldModel(domain, "fbe", "Int64", "Long", "", "8", "0L");
GenerateFBEFieldModel(domain, "fbe", "UInt64", "ULong", "", "8", "0uL");
GenerateFBEFieldModel(domain, "fbe", "Float", "Float", "", "4", "0.0f");
GenerateFBEFieldModel(domain, "fbe", "Double", "Double", "", "8", "0.0");
GenerateFBEFieldModel(domain, "fbe", "UUID", "java.util.UUID", "", "16", "UUIDGenerator.nil()");
GenerateFBEFieldModelDecimal(domain, "fbe");
if (Version() < 8)
GenerateFBEFieldModelDate(domain, "fbe");
else
GenerateFBEFieldModelTimestamp(domain, "fbe");
GenerateFBEFieldModelBytes(domain, "fbe");
GenerateFBEFieldModelString(domain, "fbe");
if (Final())
{
GenerateFBESize(domain, "fbe");
GenerateFBEFinalModel(domain, "fbe");
GenerateFBEFinalModel(domain, "fbe", "Boolean", "Boolean", "", "1", "false");
GenerateFBEFinalModel(domain, "fbe", "Byte", "Byte", "", "1", "0.toByte()");
GenerateFBEFinalModel(domain, "fbe", "Char", "Char", ".code", "1", "'\\u0000'");
GenerateFBEFinalModel(domain, "fbe", "WChar", "Char", ".code", "4", "'\\u0000'");
GenerateFBEFinalModel(domain, "fbe", "Int8", "Byte", "", "1", "0.toByte()");
GenerateFBEFinalModel(domain, "fbe", "UInt8", "UByte", "", "1", "0.toUByte()");
GenerateFBEFinalModel(domain, "fbe", "Int16", "Short", "", "2", "0.toShort()");
GenerateFBEFinalModel(domain, "fbe", "UInt16", "UShort", "", "2", "0.toUShort()");
GenerateFBEFinalModel(domain, "fbe", "Int32", "Int", "", "4", "0");
GenerateFBEFinalModel(domain, "fbe", "UInt32", "UInt", "", "4", "0u");
GenerateFBEFinalModel(domain, "fbe", "Int64", "Long", "", "8", "0L");
GenerateFBEFinalModel(domain, "fbe", "UInt64", "ULong", "", "8", "0uL");
GenerateFBEFinalModel(domain, "fbe", "Float", "Float", "", "4", "0.0f");
GenerateFBEFinalModel(domain, "fbe", "Double", "Double", "", "8", "0.0");
GenerateFBEFinalModel(domain, "fbe", "UUID", "java.util.UUID", "", "16", "UUIDGenerator.nil()");
GenerateFBEFinalModelDecimal(domain, "fbe");
if (Version() < 8)
GenerateFBEFinalModelDate(domain, "fbe");
else
GenerateFBEFinalModelTimestamp(domain, "fbe");
GenerateFBEFinalModelBytes(domain, "fbe");
GenerateFBEFinalModelString(domain, "fbe");
}
if (Proto())
{
GenerateFBESender(domain, "fbe");
GenerateFBESenderListener(domain, "fbe");
GenerateFBEReceiver(domain, "fbe");
GenerateFBEReceiverListener(domain, "fbe");
GenerateFBEClient(domain, "fbe");
GenerateFBEClientListener(domain, "fbe");
}
if (JSON())
GenerateFBEJson(domain, "fbe");
GeneratePackage(package);
}
void GeneratorKotlin::GenerateHeader(const std::string& source)
{
std::string code = R"CODE(//------------------------------------------------------------------------------
// Automatically generated by the Fast Binary Encoding compiler, do not modify!
// https://github.com/chronoxor/FastBinaryEncoding
// Source: _INPUT_
// FBE version: _VERSION_
//------------------------------------------------------------------------------
@file:Suppress("UnusedImport", "unused")
)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 GeneratorKotlin::GenerateFooter()
{
}
void GeneratorKotlin::GenerateImports(const std::string& domain, const std::string& package)
{
// Generate package name
WriteLine();
WriteLineIndent("package " + domain + package);
}
void GeneratorKotlin::GenerateImports(const std::shared_ptr<Package>& p)
{
std::string domain = (p->domain && !p->domain->empty()) ? (*p->domain + ".") : "";
// Generate common import
GenerateImports(domain, *p->name);
}
void GeneratorKotlin::GenerateFBEPackage(const std::string& domain, const std::string& package)
{
CppCommon::Path path = CppCommon::Path(_output) / CreatePackagePath(domain, package);
// Create FBE package path
CppCommon::Directory::CreateTree(path);
}
void GeneratorKotlin::GenerateFBEUUIDGenerator(const std::string& domain, const std::string& package)
{
CppCommon::Path path = CppCommon::Path(_output) / CreatePackagePath(domain, package);
// Generate the file
CppCommon::Path file = path / "UUIDGenerator.kt";
WriteBegin();
// Generate headers
GenerateHeader("FBE");
GenerateImports(domain, package);
std::string code = R"CODE(
// Fast Binary Encoding UUID generator
object UUIDGenerator
{
// Gregorian epoch
private const val GregorianEpoch = 0xFFFFF4E2F964AC00uL
// Kotlin constants workaround
private val Sign = java.lang.Long.parseUnsignedLong("8000000000000000", 16)
private val Low = java.lang.Long.parseUnsignedLong("00000000FFFFFFFF", 16)
private val Mid = java.lang.Long.parseUnsignedLong("0000FFFF00000000", 16)
private val High = java.lang.Long.parseUnsignedLong("FFFF000000000000", 16)
// Lock and random generator
private val lock = Object()
private val generator = java.util.Random()
// Node & clock sequence bytes
private val node = makeNode()
private var nodeAndClockSequence = makeNodeAndClockSequence()
// Last UUID generated timestamp
private var last = GregorianEpoch
private fun makeNode(): ULong = generator.nextLong().toULong() or 0x0000010000000000uL
private fun makeNodeAndClockSequence(): ULong
{
val clock = generator.nextLong().toULong()
var lsb: ULong = 0u
// Variant (2 bits)
lsb = lsb or 0x8000000000000000uL
// Clock sequence (14 bits)
lsb = lsb or ((clock and 0x0000000000003FFFuL) shl 48)
// 6 bytes
lsb = lsb or node
return lsb
}
// Generate nil UUID0 (all bits set to zero)
fun nil(): java.util.UUID = java.util.UUID(0, 0)
// Generate sequential UUID1 (time based version)
fun sequential(): java.util.UUID
{
val now = System.currentTimeMillis().toULong()
// Generate new clock sequence bytes to get rid of UUID duplicates
synchronized(lock)
{
if (now <= last)
nodeAndClockSequence = makeNodeAndClockSequence()
last = now
}
val nanosSince = (now - GregorianEpoch) * 10000u
var msb = 0uL
msb = msb or (0x00000000FFFFFFFFuL and nanosSince).shl(32)
msb = msb or (0x0000FFFF00000000uL and nanosSince).shr(16)
msb = msb or (0xFFFF000000000000uL and nanosSince).shr(48)
// Sets the version to 1
msb = msb or 0x0000000000001000uL
return java.util.UUID(msb.toLong(), nodeAndClockSequence.toLong())
}
// Generate random UUID4 (randomly or pseudo-randomly generated version)
fun random(): java.util.UUID = java.util.UUID.randomUUID()
}
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
// Generate footer
GenerateFooter();
// Store the file
WriteEnd();
Store(file);
}
void GeneratorKotlin::GenerateFBEBuffer(const std::string& domain, const std::string& package)
{
CppCommon::Path path = CppCommon::Path(_output) / CreatePackagePath(domain, package);
// Generate the file
CppCommon::Path file = path / "Buffer.kt";
WriteBegin();
// Generate headers
GenerateHeader("FBE");
GenerateImports(domain, package);
std::string code = R"CODE(
// Fast Binary Encoding buffer based on dynamic byte array
class Buffer
{
// Is the buffer empty?
val empty: Boolean
get() = size == 0L
// Get bytes memory buffer
var data = ByteArray(0)
private set
// Get bytes memory buffer capacity
val capacity: Long
get() = data.size.toLong()
// Get bytes memory buffer size
var size: Long = 0
private set
// Get bytes memory buffer offset
var offset: Long = 0
private set
// Initialize a new expandable buffer with zero capacity
constructor()
// Initialize a new expandable buffer with the given capacity
constructor(capacity: Long) { attach(capacity) }
// Initialize a new buffer based on the specified byte array
constructor(buffer: ByteArray) { attach(buffer) }
// Initialize a new buffer based on the specified region (offset) of a byte array
constructor(buffer: ByteArray, offset: Long) { attach(buffer, offset) }
// Initialize a new buffer based on the specified region (size and offset) of a byte array
constructor(buffer: ByteArray, size: Long, offset: Long) { attach(buffer, size, offset) }
// Attach memory buffer methods
fun attach() { data = ByteArray(0); size = 0; offset = 0 }
fun attach(capacity: Long) { data = ByteArray(capacity.toInt()); size = 0; offset = 0 }
fun attach(buffer: ByteArray) { data = buffer; size = buffer.size.toLong(); offset = 0 }
fun attach(buffer: ByteArray, offset: Long) { data = buffer; size = buffer.size.toLong(); this.offset = offset }
fun attach(buffer: ByteArray, size: Long, offset: Long) { data = buffer; this.size = size; this.offset = offset }
// Allocate memory in the current buffer and return offset to the allocated memory block
fun allocate(size: Long): Long
{
assert(size >= 0) { "Invalid allocation size!" }
if (size < 0)
throw IllegalArgumentException("Invalid allocation size!")
val offset = this.size
// Calculate a new buffer size
val total = this.size + size
if (total <= data.size)
{
this.size = total
return offset
}
val data = ByteArray(kotlin.math.max(total, 2L * this.data.size).toInt())
System.arraycopy(this.data, 0, data, 0, this.size.toInt())
this.data = data
this.size = total
return offset
}
// Remove some memory of the given size from the current buffer
fun remove(offset: Long, size: Long)
{
assert((offset + size) <= this.size) { "Invalid offset & size!" }
if ((offset + size) > this.size)
throw IllegalArgumentException("Invalid offset & size!")
System.arraycopy(data, (offset + size).toInt(), data, offset.toInt(), (this.size - size - offset).toInt())
this.size -= size
if (this.offset >= offset + size)
this.offset -= size
else if (this.offset >= offset)
{
this.offset -= this.offset - offset
if (this.offset > this.size)
this.offset = this.size
}
}
// Reserve memory of the given capacity in the current buffer
fun reserve(capacity: Long)
{
assert(capacity >= 0) { "Invalid reserve capacity!" }
if (capacity < 0)
throw IllegalArgumentException("Invalid reserve capacity!")
if (capacity > data.size)
{
val data = ByteArray(kotlin.math.max(capacity, 2L * this.data.size).toInt())
System.arraycopy(this.data, 0, data, 0, size.toInt())
this.data = data
}
}
// Resize the current buffer
fun resize(size: Long)
{
reserve(size)
this.size = size
if (offset > this.size)
offset = this.size
}
// Reset the current buffer and its offset
fun reset()
{
size = 0
offset = 0
}
// Shift the current buffer offset
fun shift(offset: Long) { this.offset += offset }
// Unshift the current buffer offset
fun unshift(offset: Long) { this.offset -= offset }
companion object
{
// Buffer I/O methods
fun readBoolean(buffer: ByteArray, offset: Long): Boolean
{
val index = offset.toInt()
return buffer[index].toInt() != 0
}
fun readByte(buffer: ByteArray, offset: Long): Byte
{
val index = offset.toInt()
return buffer[index]
}
fun readChar(buffer: ByteArray, offset: Long): Char
{
return readInt8(buffer, offset).toInt().toChar()
}
fun readWChar(buffer: ByteArray, offset: Long): Char
{
return readInt32(buffer, offset).toChar()
}
fun readInt8(buffer: ByteArray, offset: Long): Byte
{
val index = offset.toInt()
return buffer[index]
}
fun readUInt8(buffer: ByteArray, offset: Long): UByte
{
val index = offset.toInt()
return buffer[index].toUByte()
}
fun readInt16(buffer: ByteArray, offset: Long): Short
{
val index = offset.toInt()
return (((buffer[index + 0].toInt() and 0xFF) shl 0) or
((buffer[index + 1].toInt() and 0xFF) shl 8)).toShort()
}
fun readUInt16(buffer: ByteArray, offset: Long): UShort
{
val index = offset.toInt()
return (((buffer[index + 0].toUInt() and 0xFFu) shl 0) or
((buffer[index + 1].toUInt() and 0xFFu) shl 8)).toUShort()
}
fun readInt32(buffer: ByteArray, offset: Long): Int
{
val index = offset.toInt()
return ((buffer[index + 0].toInt() and 0xFF) shl 0) or
((buffer[index + 1].toInt() and 0xFF) shl 8) or
((buffer[index + 2].toInt() and 0xFF) shl 16) or
((buffer[index + 3].toInt() and 0xFF) shl 24)
}
fun readUInt32(buffer: ByteArray, offset: Long): UInt
{
val index = offset.toInt()
return ((buffer[index + 0].toUInt() and 0xFFu) shl 0) or
((buffer[index + 1].toUInt() and 0xFFu) shl 8) or
((buffer[index + 2].toUInt() and 0xFFu) shl 16) or
((buffer[index + 3].toUInt() and 0xFFu) shl 24)
}
fun readInt64(buffer: ByteArray, offset: Long): Long
{
val index = offset.toInt()
return ((buffer[index + 0].toLong() and 0xFF) shl 0) or
((buffer[index + 1].toLong() and 0xFF) shl 8) or
((buffer[index + 2].toLong() and 0xFF) shl 16) or
((buffer[index + 3].toLong() and 0xFF) shl 24) or
((buffer[index + 4].toLong() and 0xFF) shl 32) or
((buffer[index + 5].toLong() and 0xFF) shl 40) or
((buffer[index + 6].toLong() and 0xFF) shl 48) or
((buffer[index + 7].toLong() and 0xFF) shl 56)
}
fun readUInt64(buffer: ByteArray, offset: Long): ULong
{
val index = offset.toInt()
return ((buffer[index + 0].toULong() and 0xFFu) shl 0) or
((buffer[index + 1].toULong() and 0xFFu) shl 8) or
((buffer[index + 2].toULong() and 0xFFu) shl 16) or
((buffer[index + 3].toULong() and 0xFFu) shl 24) or
((buffer[index + 4].toULong() and 0xFFu) shl 32) or
((buffer[index + 5].toULong() and 0xFFu) shl 40) or
((buffer[index + 6].toULong() and 0xFFu) shl 48) or
((buffer[index + 7].toULong() and 0xFFu) shl 56)
}
private fun readInt64BE(buffer: ByteArray, offset: Long): Long
{
val index = offset.toInt()
return ((buffer[index + 0].toLong() and 0xFF) shl 56) or
((buffer[index + 1].toLong() and 0xFF) shl 48) or
((buffer[index + 2].toLong() and 0xFF) shl 40) or
((buffer[index + 3].toLong() and 0xFF) shl 32) or
((buffer[index + 4].toLong() and 0xFF) shl 24) or
((buffer[index + 5].toLong() and 0xFF) shl 16) or
((buffer[index + 6].toLong() and 0xFF) shl 8) or
((buffer[index + 7].toLong() and 0xFF) shl 0)
}
fun readFloat(buffer: ByteArray, offset: Long): Float
{
val bits = readInt32(buffer, offset)
return java.lang.Float.intBitsToFloat(bits)
}
fun readDouble(buffer: ByteArray, offset: Long): Double
{
val bits = readInt64(buffer, offset)
return java.lang.Double.longBitsToDouble(bits)
}
fun readBytes(buffer: ByteArray, offset: Long, size: Long): ByteArray
{
val result = ByteArray(size.toInt())
System.arraycopy(buffer, offset.toInt(), result, 0, size.toInt())
return result
}
fun readString(buffer: ByteArray, offset: Long, size: Long): String
{
return String(buffer, offset.toInt(), size.toInt(), java.nio.charset.StandardCharsets.UTF_8)
}
fun readUUID(buffer: ByteArray, offset: Long): java.util.UUID
{
return java.util.UUID(readInt64BE(buffer, offset), readInt64BE(buffer, offset + 8))
}
fun write(buffer: ByteArray, offset: Long, value: Boolean)
{
buffer[offset.toInt()] = (if (value) 1 else 0).toByte()
}
fun write(buffer: ByteArray, offset: Long, value: Byte)
{
buffer[offset.toInt()] = value
}
fun write(buffer: ByteArray, offset: Long, value: UByte)
{
buffer[offset.toInt()] = value.toByte()
}
fun write(buffer: ByteArray, offset: Long, value: Short)
{
buffer[offset.toInt() + 0] = (value.toInt() shr 0).toByte()
buffer[offset.toInt() + 1] = (value.toInt() shr 8).toByte()
}
fun write(buffer: ByteArray, offset: Long, value: UShort)
{
buffer[offset.toInt() + 0] = (value.toUInt() shr 0).toByte()
buffer[offset.toInt() + 1] = (value.toUInt() shr 8).toByte()
}
fun write(buffer: ByteArray, offset: Long, value: Int)
{
buffer[offset.toInt() + 0] = (value shr 0).toByte()
buffer[offset.toInt() + 1] = (value shr 8).toByte()
buffer[offset.toInt() + 2] = (value shr 16).toByte()
buffer[offset.toInt() + 3] = (value shr 24).toByte()
}
fun write(buffer: ByteArray, offset: Long, value: UInt)
{
buffer[offset.toInt() + 0] = (value shr 0).toByte()
buffer[offset.toInt() + 1] = (value shr 8).toByte()
buffer[offset.toInt() + 2] = (value shr 16).toByte()
buffer[offset.toInt() + 3] = (value shr 24).toByte()
}
fun write(buffer: ByteArray, offset: Long, value: Long)
{
buffer[offset.toInt() + 0] = (value shr 0).toByte()
buffer[offset.toInt() + 1] = (value shr 8).toByte()
buffer[offset.toInt() + 2] = (value shr 16).toByte()
buffer[offset.toInt() + 3] = (value shr 24).toByte()
buffer[offset.toInt() + 4] = (value shr 32).toByte()
buffer[offset.toInt() + 5] = (value shr 40).toByte()
buffer[offset.toInt() + 6] = (value shr 48).toByte()
buffer[offset.toInt() + 7] = (value shr 56).toByte()
}
fun write(buffer: ByteArray, offset: Long, value: ULong)
{
buffer[offset.toInt() + 0] = (value shr 0).toByte()
buffer[offset.toInt() + 1] = (value shr 8).toByte()
buffer[offset.toInt() + 2] = (value shr 16).toByte()
buffer[offset.toInt() + 3] = (value shr 24).toByte()
buffer[offset.toInt() + 4] = (value shr 32).toByte()
buffer[offset.toInt() + 5] = (value shr 40).toByte()
buffer[offset.toInt() + 6] = (value shr 48).toByte()
buffer[offset.toInt() + 7] = (value shr 56).toByte()
}
private fun writeBE(buffer: ByteArray, offset: Long, value: Long)
{
buffer[offset.toInt() + 0] = (value shr 56).toByte()
buffer[offset.toInt() + 1] = (value shr 48).toByte()
buffer[offset.toInt() + 2] = (value shr 40).toByte()
buffer[offset.toInt() + 3] = (value shr 32).toByte()
buffer[offset.toInt() + 4] = (value shr 24).toByte()
buffer[offset.toInt() + 5] = (value shr 16).toByte()
buffer[offset.toInt() + 6] = (value shr 8).toByte()
buffer[offset.toInt() + 7] = (value shr 0).toByte()
}
fun write(buffer: ByteArray, offset: Long, value: Float)
{
val bits = java.lang.Float.floatToIntBits(value)
write(buffer, offset, bits)
}
fun write(buffer: ByteArray, offset: Long, value: Double)
{
val bits = java.lang.Double.doubleToLongBits(value)
write(buffer, offset, bits)
}
fun write(buffer: ByteArray, offset: Long, value: ByteArray)
{
System.arraycopy(value, 0, buffer, offset.toInt(), value.size)
}
fun write(buffer: ByteArray, offset: Long, value: ByteArray, valueOffset: Long, valueSize: Long)
{
System.arraycopy(value, valueOffset.toInt(), buffer, offset.toInt(), valueSize.toInt())
}
fun write(buffer: ByteArray, offset: Long, value: Byte, valueCount: Long)
{
for (i in 0 until valueCount)
buffer[(offset + i).toInt()] = value
}
fun write(buffer: ByteArray, offset: Long, value: java.util.UUID)
{
writeBE(buffer, offset, value.mostSignificantBits)
writeBE(buffer, offset + 8, value.leastSignificantBits)
}
}
}
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
// Generate footer
GenerateFooter();
// Store the file
WriteEnd();
Store(file);
}
void GeneratorKotlin::GenerateFBEModel(const std::string& domain, const std::string& package)
{
CppCommon::Path path = CppCommon::Path(_output) / CreatePackagePath(domain, package);
// Generate the file
CppCommon::Path file = path / "Model.kt";
WriteBegin();
// Generate headers
GenerateHeader("FBE");
GenerateImports(domain, package);
std::string code = R"CODE(
// Fast Binary Encoding base model
open class Model
{
// Get bytes buffer
var buffer = Buffer()
private set
// Initialize a new model
protected constructor()
protected constructor(buffer: Buffer) { this.buffer = buffer }
// Attach memory buffer methods
fun attach() { buffer.attach() }
fun attach(capacity: Long) { buffer.attach(capacity) }
fun attach(buffer: ByteArray) { this.buffer.attach(buffer) }
fun attach(buffer: ByteArray, offset: Long) { this.buffer.attach(buffer, offset) }
fun attach(buffer: ByteArray, size: Long, offset: Long) { this.buffer.attach(buffer, size, offset) }
fun attach(buffer: Buffer) { this.buffer.attach(buffer.data, buffer.size, buffer.offset) }
fun attach(buffer: Buffer, offset: Long) { this.buffer.attach(buffer.data, buffer.size, offset) }
// Model buffer operations
fun allocate(size: Long): Long { return buffer.allocate(size) }
fun remove(offset: Long, size: Long) { buffer.remove(offset, size) }
fun reserve(capacity: Long) { buffer.reserve(capacity) }
fun resize(size: Long) { buffer.resize(size) }
fun reset() { buffer.reset() }
fun shift(offset: Long) { buffer.shift(offset) }
fun unshift(offset: Long) { buffer.unshift(offset) }
// Buffer I/O methods
protected fun readUInt32(offset: Long): UInt { return Buffer.readUInt32(buffer.data, buffer.offset + offset) }
protected fun write(offset: Long, value: UInt) { Buffer.write(buffer.data, buffer.offset + offset, value) }
}
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
// Generate footer
GenerateFooter();
// Store the file
WriteEnd();
Store(file);
}
void GeneratorKotlin::GenerateFBEFieldModel(const std::string& domain, const std::string& package)
{
CppCommon::Path path = CppCommon::Path(_output) / CreatePackagePath(domain, package);
// Generate the file
CppCommon::Path file = path / "FieldModel.kt";
WriteBegin();
// Generate headers
GenerateHeader("FBE");
GenerateImports(domain, package);
std::string code = R"CODE(
// Fast Binary Encoding base field model
@Suppress("MemberVisibilityCanBePrivate")
abstract class FieldModel protected constructor(protected var _buffer: Buffer, protected var _offset: Long)
{
// Field offset
var fbeOffset: Long
get() = _offset
set(value) { _offset = value }
// Field size
open val fbeSize: Long = 0
// Field extra size
open val fbeExtra: Long = 0
// Shift the current field offset
fun fbeShift(size: Long) { _offset += size }
// Unshift the current field offset
fun fbeUnshift(size: Long) { _offset -= size }
// Check if the value is valid
open fun verify(): Boolean = true
// Buffer I/O methods
protected fun readBoolean(offset: Long): Boolean { return Buffer.readBoolean(_buffer.data, _buffer.offset + offset) }
protected fun readByte(offset: Long): Byte { return Buffer.readByte(_buffer.data, _buffer.offset + offset) }
protected fun readChar(offset: Long): Char { return Buffer.readChar(_buffer.data, _buffer.offset + offset) }
protected fun readWChar(offset: Long): Char { return Buffer.readWChar(_buffer.data, _buffer.offset + offset) }
protected fun readInt8(offset: Long): Byte { return Buffer.readInt8(_buffer.data, _buffer.offset + offset) }
protected fun readUInt8(offset: Long): UByte { return Buffer.readUInt8(_buffer.data, _buffer.offset + offset) }
protected fun readInt16(offset: Long): Short { return Buffer.readInt16(_buffer.data, _buffer.offset + offset) }
protected fun readUInt16(offset: Long): UShort { return Buffer.readUInt16(_buffer.data, _buffer.offset + offset) }
protected fun readInt32(offset: Long): Int { return Buffer.readInt32(_buffer.data, _buffer.offset + offset) }
protected fun readUInt32(offset: Long): UInt { return Buffer.readUInt32(_buffer.data, _buffer.offset + offset) }
protected fun readInt64(offset: Long): Long { return Buffer.readInt64(_buffer.data, _buffer.offset + offset) }
protected fun readUInt64(offset: Long): ULong { return Buffer.readUInt64(_buffer.data, _buffer.offset + offset) }
protected fun readFloat(offset: Long): Float { return Buffer.readFloat(_buffer.data, _buffer.offset + offset) }
protected fun readDouble(offset: Long): Double { return Buffer.readDouble(_buffer.data, _buffer.offset + offset) }
protected fun readBytes(offset: Long, size: Long): ByteArray { return Buffer.readBytes(_buffer.data, _buffer.offset + offset, size) }
protected fun readString(offset: Long, size: Long): String { return Buffer.readString(_buffer.data, _buffer.offset + offset, size) }
protected fun readUUID(offset: Long): java.util.UUID { return Buffer.readUUID(_buffer.data, _buffer.offset + offset) }
protected fun write(offset: Long, value: Boolean) { Buffer.write(_buffer.data, _buffer.offset + offset, value) }
protected fun write(offset: Long, value: Byte) { Buffer.write(_buffer.data, _buffer.offset + offset, value) }
protected fun write(offset: Long, value: UByte) { Buffer.write(_buffer.data, _buffer.offset + offset, value) }
protected fun write(offset: Long, value: Short) { Buffer.write(_buffer.data, _buffer.offset + offset, value) }
protected fun write(offset: Long, value: UShort) { Buffer.write(_buffer.data, _buffer.offset + offset, value) }
protected fun write(offset: Long, value: Int) { Buffer.write(_buffer.data, _buffer.offset + offset, value) }
protected fun write(offset: Long, value: UInt) { Buffer.write(_buffer.data, _buffer.offset + offset, value) }
protected fun write(offset: Long, value: Long) { Buffer.write(_buffer.data, _buffer.offset + offset, value) }
protected fun write(offset: Long, value: ULong) { Buffer.write(_buffer.data, _buffer.offset + offset, value) }
protected fun write(offset: Long, value: Float) { Buffer.write(_buffer.data, _buffer.offset + offset, value) }
protected fun write(offset: Long, value: Double) { Buffer.write(_buffer.data, _buffer.offset + offset, value) }
protected fun write(offset: Long, value: ByteArray) { Buffer.write(_buffer.data, _buffer.offset + offset, value) }
protected fun write(offset: Long, value: ByteArray, valueOffset: Long, valueSize: Long) { Buffer.write(_buffer.data, _buffer.offset + offset, value, valueOffset, valueSize) }
protected fun write(offset: Long, value: Byte, valueCount: Long) { Buffer.write(_buffer.data, _buffer.offset + offset, value, valueCount) }
protected fun write(offset: Long, value: java.util.UUID) { Buffer.write(_buffer.data, _buffer.offset + offset, value) }
}
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
// Generate footer
GenerateFooter();
// Store the file
WriteEnd();
Store(file);
}
void GeneratorKotlin::GenerateFBEFieldModel(const std::string& domain, const std::string& package, const std::string& name, const std::string& type, const std::string& base, const std::string& size, const std::string& defaults)
{
CppCommon::Path path = CppCommon::Path(_output) / CreatePackagePath(domain, package);
// Generate the file
CppCommon::Path file = path / ("FieldModel" + name + ".kt");
WriteBegin();
// Generate headers
GenerateHeader("FBE");
GenerateImports(domain, package);
std::string code = R"CODE(
// Fast Binary Encoding _TYPE_ field model
class FieldModel_NAME_(buffer: Buffer, offset: Long) : FieldModel(buffer, offset)
{
// Field size
override val fbeSize: Long = _SIZE_
// Get the value
fun get(defaults: _TYPE_ = _DEFAULTS_): _TYPE_
{
if ((_buffer.offset + fbeOffset + fbeSize) > _buffer.size)
return defaults
return read_NAME_(fbeOffset)
}
// Set the value
fun set(value: _TYPE_)
{
assert((_buffer.offset + fbeOffset + fbeSize) <= _buffer.size) { "Model is broken!" }
if ((_buffer.offset + fbeOffset + fbeSize) > _buffer.size)
return
write(fbeOffset, value_BASE_)
}
}
)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);
// Generate footer
GenerateFooter();
// Store the file
WriteEnd();
Store(file);
}
void GeneratorKotlin::GenerateFBEFieldModelDecimal(const std::string& domain, const std::string& package)
{
CppCommon::Path path = CppCommon::Path(_output) / CreatePackagePath(domain, package);
// Generate the file
CppCommon::Path file = path / "FieldModelDecimal.kt";
WriteBegin();
// Generate headers
GenerateHeader("FBE");
GenerateImports(domain, package);
std::string code = R"CODE(
// Fast Binary Encoding decimal field model
class FieldModelDecimal(buffer: Buffer, offset: Long) : FieldModel(buffer, offset)
{
// Field size
override val fbeSize: Long = 16
// Get the decimal value
fun get(defaults: java.math.BigDecimal = java.math.BigDecimal.valueOf(0L)): java.math.BigDecimal
{
if ((_buffer.offset + fbeOffset + fbeSize) > _buffer.size)
return defaults
val magnitude = readBytes(fbeOffset, 12)
val scale = readByte(fbeOffset + 14).toInt()
val signum = if (readByte(fbeOffset + 15) < 0) -1 else 1
// Reverse magnitude
for (i in 0 until (magnitude.size / 2))
{
val temp = magnitude[i]
magnitude[i] = magnitude[magnitude.size - i - 1]
magnitude[magnitude.size - i - 1] = temp
}
val unscaled = java.math.BigInteger(signum, magnitude)
return java.math.BigDecimal(unscaled, scale)
}
// Set the decimal value
fun set(value: java.math.BigDecimal)
{
assert((_buffer.offset + fbeOffset + fbeSize) <= _buffer.size) { "Model is broken!" }
if ((_buffer.offset + fbeOffset + fbeSize) > _buffer.size)
return
// Get unscaled absolute value
val unscaled = value.abs().unscaledValue()
val bitLength = unscaled.bitLength()
if ((bitLength < 0) || (bitLength > 96))
{
// Value too big for .NET Decimal (bit length is limited to [0, 96])
write(fbeOffset, 0.toByte(), fbeSize)
return
}
// Get byte array
val unscaledBytes = unscaled.toByteArray()
// Get scale
val scale = value.scale()
if ((scale < 0) || (scale > 28))
{
// Value scale exceeds .NET Decimal limit of [0, 28]
write(fbeOffset, 0.toByte(), fbeSize)
return
}
// Write unscaled value to bytes 0-11
var index = 0
var i = unscaledBytes.size - 1
while ((i >= 0) && (index < 12))
{
write(fbeOffset + index, unscaledBytes[i])
i--
index++
}
// Fill remaining bytes with zeros
while (index < 14)
{
write(fbeOffset + index, 0.toByte())
index++
}
// Write scale at byte 14
write(fbeOffset + 14, scale.toByte())
// Write signum at byte 15
write(fbeOffset + 15, (if (value.signum() < 0) -128 else 0).toByte())
}
}
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
// Generate footer
GenerateFooter();
// Store the file
WriteEnd();
Store(file);
}
void GeneratorKotlin::GenerateFBEFieldModelDate(const std::string& domain, const std::string& package)
{
CppCommon::Path path = CppCommon::Path(_output) / CreatePackagePath(domain, package);
// Generate the file
CppCommon::Path file = path / "FieldModelDate.kt";
WriteBegin();
// Generate headers
GenerateHeader("FBE");
GenerateImports(domain, package);
std::string code = R"CODE(
// Fast Binary Encoding date field model
class FieldModelDate(buffer: Buffer, offset: Long) : FieldModel(buffer, offset)
{
// Field size
override val fbeSize: Long = 8
// Get the date value
fun get(defaults: java.util.Date = java.util.Date(0)): java.util.Date
{
if ((_buffer.offset + fbeOffset + fbeSize) > _buffer.size)
return defaults
val nanoseconds = readInt64(fbeOffset)
return java.util.Date(nanoseconds / 1000000)
}
// Set the date value
fun set(value: java.util.Date)
{
assert((_buffer.offset + fbeOffset + fbeSize) <= _buffer.size) { "Model is broken!" }
if ((_buffer.offset + fbeOffset + fbeSize) > _buffer.size)
return
val nanoseconds = value.time * 1000000
write(fbeOffset, nanoseconds.toULong())
}
}
)CODE";
// Prepare code template
code = std::regex_replace(code, std::regex("\n"), EndLine());
Write(code);
// Generate footer
GenerateFooter();
// Store the file
WriteEnd();
Store(file);
}
void GeneratorKotlin::GenerateFBEFieldModelTimestamp(const std::string& domain, const std::string& package)
{
CppCommon::Path path = CppCommon::Path(_output) / CreatePackagePath(domain, package);
// Generate the file
CppCommon::Path file = path / "FieldModelTimestamp.kt";