-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrapidstruct.c3
More file actions
1585 lines (1361 loc) · 52.4 KB
/
rapidstruct.c3
File metadata and controls
1585 lines (1361 loc) · 52.4 KB
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
/*
Copyright (c) 2026 Noah McLean
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
module rapidstruct;
import std::core::mem;
faultdef RS_ARENA_OUT_OF_MEMORY, RS_UNDEFINED_SCHEMA_TAG, RS_SCHEMA_DEFINITION_MISMATCH, RS_FIELD_TOO_LONG, RS_STRUCT_UNDERFLOW;
<*
Simply an integer that represents a key that can be used instead of the tag String for accessing fields.
It refers to the exact same thing as a tag String, but bypasses the need to check String equivalence.
-1 indicates that something went wrong while adding a field to a schema
*>
typedef RS_SchemaKey = inline int;
alias Byte = char;
const uint EXPANSION_INCREMENT = 4096;
//********
//RS_Arena
//********
struct RS_Arena {
void* rootPtr;
void* nextPtr;
usz currentBytesAllocated;
usz size;
}
<*
This creates the arena and allocates memory of the specified size.
This returns the base memory address for the allocated memory, which may be null if there was a problem.
*>
fn void* initArena(RS_Arena* arena, usz size)
{
arena.rootPtr = malloc(size);
arena.nextPtr = arena.rootPtr;
arena.size = size;
return arena.rootPtr;
}
<*
Frees the memory in use by the arena
*>
fn void destroyArena(RS_Arena* arena)
{
free(arena.rootPtr);
}
//This is just to mantain 8 byte allignment to keep things simple
const usz ARENA_ALIGNMENT = 8;
fn usz _calcAlignmentCorrection(usz allocSize) @private
{
usz carry = ((usz) allocSize) % ARENA_ALIGNMENT;
if(carry == 0) {
return 0;
}
usz extraBump = ARENA_ALIGNMENT - carry;
return extraBump;
}
<*
Allocates the requested memory on the arena specified and returns the pointer.
Returns null if there is not enough space on the arena to accomodate the requested size.
*>
fn void* allocOnArena(RS_Arena* arena, usz size)
{
if(arena.currentBytesAllocated + size > arena.size) {
return null;
}
void* block = arena.nextPtr;
usz alignmentCorrection = _calcAlignmentCorrection(size);
arena.nextPtr = (void*) (((Byte*) block) + size) + alignmentCorrection;
arena.currentBytesAllocated += size;
return block;
}
<*
Fully resets the arena.
If you are using this in the context of RapidStruct and it is being used by an RS_Processor, you should instead call resetProcessorToMemBaseline()
*>
fn void resetArena(RS_Arena* arena)
{
arena.currentBytesAllocated = 0;
arena.nextPtr = arena.rootPtr;
}
//*********
//RS_Schema
//*********
enum RS_FieldType : int
{
BOOL,
BYTE,
SHORT,
INT,
LONG,
FLOAT,
DOUBLE,
STRING,
RAW,
STRUCT
}
struct RS_Schema {
RS_FieldType[256] fieldTypes;
String[256] fieldTags;
RS_Schema*[256] nestedSchemas;
int tagCount;
}
<*
Adds a field definition to this schema.
Do not added a nested struct definition here, instead use RS_Schema.addStructToSchema()
If you get past the contract, the returned RS_SchemaKey will be invalid (-1)
@require type != RS_FieldType.STRUCT : "Must use RS_Schema.addStructToSchema() for nested structs"
*>
fn RS_SchemaKey RS_Schema.addFieldToSchema(&this, String tag, RS_FieldType type)
{
if(type == RS_FieldType.STRUCT) {
//TODO: is there a better way to handle this?
//If you get past the contract somehow then do nothing as you need a schema with a struct.
return (RS_SchemaKey) -1;
}
//Check for duplicate tag definition
for(int i = 0; i < this.tagCount; i++) {
if(this.fieldTags[i] == tag) {
//TODO: Is this the best way to handle this?
//If you duplicate a tag, you overwrite it
this.fieldTypes[i] = type;
this.fieldTags[i] = tag;
return (RS_SchemaKey) i;
}
}
this.fieldTypes[this.tagCount] = type;
this.fieldTags[this.tagCount] = tag;
this.tagCount++;
return (RS_SchemaKey) this.tagCount - 1;
}
<*
Adds a nested struct field definition to this schema.
*>
fn RS_SchemaKey RS_Schema.addStructToSchema(&this, String tag, RS_Schema* nestedSchemaToAdd)
{
//Check for duplicate tag definition
for(int i = 0; i < this.tagCount; i++) {
if(this.fieldTags[i] == tag) {
//TODO: Is this the best way to handle this?
//If you duplicate a tag, you overwrite it
this.fieldTypes[i] = RS_FieldType.STRUCT;
this.fieldTags[i] = tag;
this.nestedSchemas[i] = nestedSchemaToAdd;
return (RS_SchemaKey) i;
}
}
this.fieldTypes[this.tagCount] = RS_FieldType.STRUCT;
this.fieldTags[this.tagCount] = tag;
this.nestedSchemas[this.tagCount] = nestedSchemaToAdd;
this.tagCount++;
return (RS_SchemaKey) this.tagCount - 1;
}
<*
Returns the key associated with a given tag.
Returns -1 if there is no definition for that tag.
*>
fn RS_SchemaKey RS_Schema.getSchemaKey(&this, String tag)
{
for(int i = 0; i < this.tagCount; i++) {
if(this.fieldTags[i] == tag) {
return (RS_SchemaKey) i;
}
}
return (RS_SchemaKey) -1;
}
//********
//RS_Field
//********
struct RS_Field {
union {
bool boolVal;
Byte byteVal;
ushort shortVal;
uint intVal;
ulong longVal;
float floatVal;
double doubleVal;
}
RS_FieldType type;
//This is only used if this field is actually a struct
RS_Struct* rs_struct;
//These are only used for strings, raw bytes, and serialized rs_structs
Byte* buffer;
uint bufferLength;
}
<*
Accesses this RS_Field as a BOOL and sets its value.
This RS_Field must be of type BOOL to use this method.
If you really want to access a RS_Struct as the wrong type, do so within the RS_Fields's union itself (I.e., .intVal, .floatVal, etc...).
@require this.type == RS_FieldType.BOOL : "RS_Field must be accessed as its own type"
*>
fn void RS_Field.putBool(&this, bool val)
{
this.boolVal = val;
}
<*
Accesses this RS_Field as a BYTE and sets its value.
This RS_Field must be of type BYTE to use this method.
If you really want to access a RS_Struct as the wrong type, do so within the RS_Fields's union itself (I.e., .intVal, .floatVal, etc...).
@require this.type == RS_FieldType.BYTE : "RS_Field must be accessed as its own type"
*>
fn void RS_Field.putByte(&this, Byte val)
{
this.byteVal = val;
}
<*
Accesses this RS_Field as a SHORT and sets its value.
This RS_Field must be of type SHORT to use this method.
If you really want to access a RS_Struct as the wrong type, do so within the RS_Fields's union itself (I.e., .intVal, .floatVal, etc...).
@require this.type == RS_FieldType.SHORT : "RS_Field must be accessed as its own type"
*>
fn void RS_Field.putShort(&this, ushort val)
{
this.shortVal = val;
}
<*
Accesses this RS_Field as an INT and sets its value.
This RS_Field must be of type INT to use this method.
If you really want to access a RS_Struct as the wrong type, do so within the RS_Fields's union itself (I.e., .intVal, .floatVal, etc...).
@require this.type == RS_FieldType.INT : "RS_Field must be accessed as its own type"
*>
fn void RS_Field.putInt(&this, uint val)
{
this.intVal = val;
}
<*
Accesses this RS_Field as a LONG and sets its value.
This RS_Field must be of type LONG to use this method.
If you really want to access a RS_Struct as the wrong type, do so within the RS_Fields's union itself (I.e., .intVal, .floatVal, etc...).
@require this.type == RS_FieldType.LONG : "RS_Field must be accessed as its own type"
*>
fn void RS_Field.putLong(&this, ulong val)
{
this.longVal = val;
}
<*
Accesses this RS_Field as a FLOAT and sets its value.
This RS_Field must be of type FLOAT to use this method.
If you really want to access a RS_Struct as the wrong type, do so within the RS_Fields's union itself (I.e., .intVal, .floatVal, etc...).
@require this.type == RS_FieldType.FLOAT : "RS_Field must be accessed as its own type"
*>
fn void RS_Field.putFloat(&this, float val)
{
this.floatVal = val;
}
<*
Accesses this RS_Field as a DOUBLE and sets its value.
This RS_Field must be of type DOUBLE to use this method.
If you really want to access a RS_Struct as the wrong type, do so within the RS_Fields's union itself (I.e., .intVal, .floatVal, etc...).
@require this.type == RS_FieldType.DOUBLE : "RS_Field must be accessed as its own type"
*>
fn void RS_Field.putDouble(&this, double val)
{
this.doubleVal = val;
}
<*
Accesses this RS_Field as a STRING and sets its value.
This RS_Field must be of type STRING to use this method.
This may fail if the arena runs out of memory.
@require this.type == RS_FieldType.STRING : "RS_Field must be accessed as its own type"
*>
fn void? RS_Field.putString(&this, String s, RS_Processor* proc)
{
Byte* buffer = (Byte*) allocOnArena(proc.arena, s.len);
if(!buffer) {
return RS_ARENA_OUT_OF_MEMORY~;
}
mem::copy(buffer, &s[0], s.len);
this.buffer = buffer;
this.bufferLength = (uint) s.len;
}
<*
Accesses this RS_Field as a raw byte array and sets its value.
Unlike all of the other accessor methods, this one does not care about what it's actual type is.
You can use this however you wish, but this will be useless if it is a primitive type.
This may fail if the arena runs out of memory.
*>
fn void? RS_Field.putBytes(&this, Byte* bytes, int length, RS_Processor* proc)
{
Byte* buffer = (Byte*) allocOnArena(proc.arena, length);
if(!buffer) {
return RS_ARENA_OUT_OF_MEMORY~;
}
mem::copy(buffer, bytes, length);
this.buffer = buffer;
this.bufferLength = length;
}
<*
Accesses this RS_Field as a STRUCT and sets its value.
This RS_Field must be of type STRUCT to use this method.
@require this.type == RS_FieldType.STRUCT : "RS_Field must be accessed as its own type"
*>
fn void RS_Field.putStruct(&this, RS_Struct* rs_struct)
{
this.rs_struct = rs_struct;
}
<*
Accesses this RS_Field as a BOOL and gets its value.
This RS_Field must be of type BOOL to use this method.
If you really want to access a RS_Struct as the wrong type, do so within the RS_Fields's union itself (I.e., .intVal, .floatVal, etc...).
@require this.type == RS_FieldType.BOOL : "RS_Field must be accessed as its own type"
*>
fn bool RS_Field.asBool(&this)
{
return this.boolVal;
}
<*
Accesses this RS_Field as a BYTE and gets its value.
This RS_Field must be of type BYTE to use this method.
If you really want to access a RS_Struct as the wrong type, do so within the RS_Fields's union itself (I.e., .intVal, .floatVal, etc...).
@require this.type == RS_FieldType.BYTE : "RS_Field must be accessed as its own type"
*>
fn Byte RS_Field.asByte(&this)
{
return this.byteVal;
}
<*
Accesses this RS_Field as a SHORT and gets its value.
This RS_Field must be of type SHORT to use this method.
If you really want to access a RS_Struct as the wrong type, do so within the RS_Fields's union itself (I.e., .intVal, .floatVal, etc...).
@require this.type == RS_FieldType.SHORT : "RS_Field must be accessed as its own type"
*>
fn ushort RS_Field.asShort(&this)
{
return this.shortVal;
}
<*
Accesses this RS_Field as a INT and gets its value.
This RS_Field must be of type INT to use this method.
If you really want to access a RS_Struct as the wrong type, do so within the RS_Fields's union itself (I.e., .intVal, .floatVal, etc...)
@require this.type == RS_FieldType.INT : "RS_Field must be accessed as its own type"
*>
fn uint RS_Field.asInt(&this)
{
return this.intVal;
}
<*
Accesses this RS_Field as a LONG and gets its value.
This RS_Field must be of type LONG to use this method.
If you really want to access a RS_Struct as the wrong type, do so within the RS_Fields's union itself (I.e., .intVal, .floatVal, etc...)
@require this.type == RS_FieldType.LONG : "RS_Field must be accessed as its own type"
*>
fn ulong RS_Field.asLong(&this)
{
return this.longVal;
}
<*
Accesses this RS_Field as a FLOAT and gets its value.
This RS_Field must be of type FLOAT to use this method.
If you really want to access a RS_Struct as the wrong type, do so within the RS_Fields's union itself (I.e., .intVal, .floatVal, etc...)
@require this.type == RS_FieldType.FLOAT : "RS_Field must be accessed as its own type"
*>
fn float RS_Field.asFloat(&this)
{
return this.floatVal;
}
<*
Accesses this RS_Field as a DOUBLE and gets its value.
This RS_Field must be of type DOUBLE to use this method.
If you really want to access a RS_Struct as the wrong type, do so within the RS_Fields's union itself (I.e., .intVal, .floatVal, etc...)
@require this.type == RS_FieldType.DOUBLE : "RS_Field must be accessed as its own type"
*>
fn double RS_Field.asDouble(&this)
{
return this.doubleVal;
}
<*
Accesses this RS_Field as a STRING and gets its value.
This RS_Field must be of type STRING to use this method.
If you really want to access a RS_Struct as the wrong type, do so within the RS_Fields's union itself (I.e., .intVal, .floatVal, etc...)
@require this.type == RS_FieldType.STRING : "RS_Field must be accessed as its own type"
*>
fn String RS_Field.asString(&this)
{
String s = (String) this.buffer[0 : this.bufferLength];
return s;
}
<*
Accesses this RS_Field as a raw byte array and gets its value.
Unlike all of the other accessor methods, this one does not care about what it's actual type is.
You can use this however you wish, but this will be useless if it is a primitive type.
*>
fn Byte* RS_Field.asBytes(&this)
{
return this.buffer;
}
<*
Accesses this RS_Field as a STRUCT and gets its value.
This RS_Field must be of type STRUCT to use this method.
@require this.type == RS_FieldType.STRUCT : "RS_Field must be accessed as its own type"
*>
fn RS_Struct* RS_Field.asStruct(&this)
{
return this.rs_struct;
}
//*********
//RS_Struct
//*********
struct RS_Struct {
RS_Schema *schema;
Byte *schemaKeys;
RS_Field** fields;
uint fieldCount;
uint fieldCapacity;
}
<*
Initializes this RS_Struct with the given schema and sets it to use an initial field capacity of 64.
One of the init functions must be called before use and before setStructMemory().
*>
fn void initStruct(RS_Struct* rs_struct, RS_Schema* schema)
{
rs_struct.schema = schema;
rs_struct.fieldCount = 0;
//Initial default capacity of 64
rs_struct.fieldCapacity = 64;
}
<*
Initializes this RS_Struct with the given schema and sets the initial field capacity.
One of the init functions must be called before use and before setStructMemory().
*>
fn void initStructWCapacity(RS_Struct* rs_struct, RS_Schema* schema, int initialFieldCapacity)
{
rs_struct.schema = schema;
rs_struct.fieldCount = 0;
rs_struct.fieldCapacity = initialFieldCapacity;
}
<*
Resets this RS_Struct's fieldCount to 0.
Retains the fieldCapacity as it previously was, but allocates fresh memory to hold those fields.
This must be called before use, and after calling one of the init methods.
This should also be called if RS_Processor.resetToBaseline() was called.
This may fail if the arena runs out of memory.
*>
fn void? setStructMemory(RS_Struct* rs_struct, RS_Processor* proc)
{
rs_struct.fieldCount = 0;
Byte* schemaKeyArray = (Byte*) allocOnArena(proc.arena, rs_struct.fieldCapacity);
RS_Field** fieldArray = (RS_Field**) allocOnArena(proc.arena, RS_Field*.sizeof * rs_struct.fieldCapacity);
if(!schemaKeyArray || !fieldArray) {
return RS_ARENA_OUT_OF_MEMORY~;
}
rs_struct.schemaKeys = schemaKeyArray;
rs_struct.fields = fieldArray;
}
<*
Internal function for an RS_Struct's field array.
This may fail if the arena runs out of memory.
*>
fn void? _struct_expandFieldArray(RS_Struct* rs_struct, RS_Processor* proc) @private
{
//Just a simple doubling
uint newfieldCapacity = rs_struct.fieldCapacity * 2;
Byte* newSchemaKeyArray = (Byte*) allocOnArena(proc.arena, newfieldCapacity);
RS_Field** newFieldArray = (RS_Field**) allocOnArena(proc.arena, RS_Field*.sizeof * newfieldCapacity);
if(!newSchemaKeyArray || !newFieldArray) {
return RS_ARENA_OUT_OF_MEMORY~;
}
mem::copy(newSchemaKeyArray, rs_struct.schemaKeys, rs_struct.fieldCount);
mem::copy(newFieldArray, rs_struct.fields, RS_Field*.sizeof * rs_struct.fieldCount);
rs_struct.schemaKeys = newSchemaKeyArray;
rs_struct.fields = newFieldArray;
rs_struct.fieldCapacity = newfieldCapacity;
}
<*
Internal function for taking a field and storing it on an RS_Struct's field array.
This may fail if the field array has to be expanded and the arena runs out of memory.
*>
fn void? _struct_storeField(RS_Struct* rs_struct, RS_Field* field, RS_SchemaKey schemaKey, RS_Processor* proc) @private
{
if(rs_struct.fieldCount == rs_struct.fieldCapacity) {
//Expand things here
_struct_expandFieldArray(rs_struct, proc)!;
}
rs_struct.fields[rs_struct.fieldCount] = field;
rs_struct.schemaKeys[rs_struct.fieldCount] = (Byte) schemaKey;
rs_struct.fieldCount++;
}
<*
Takes an RS_Field pointer and stores it on this RS_Struct's field array.
Again, only stores the pointer.
This may fail if the field array has to be expanded and the arena runs out of memory or a tag was passed that is undefined or of the wrong type.
*>
fn void? RS_Struct.add(&this, String tag, RS_Field* field, RS_Processor* proc)
{
RS_SchemaKey schemaKey = this.schema.getSchemaKey(tag);
this.addWithKey(schemaKey, field, proc)!;
}
<*
Takes an RS_Field pointer and stores it on this RS_Struct's field array.
Again, only stores the pointer.
This may fail if the field array has to be expanded and the arena runs out of memory or a key was passed that is undefined or of the wrong type.
*>
fn void? RS_Struct.addWithKey(&this, RS_SchemaKey schemaKey, RS_Field* field, RS_Processor* proc)
{
if(schemaKey == -1) {
return RS_UNDEFINED_SCHEMA_TAG~;
}
if(field.type != this.schema.fieldTypes[schemaKey]) {
return RS_SCHEMA_DEFINITION_MISMATCH~;
}
_struct_storeField(this, field, schemaKey, proc)!;
}
<*
Returns the first RS_Field* that matches the given tag.
Returns null if this RS_Struct does not contain any field with that tag.
*>
fn RS_Field* RS_Struct.get(&this, String tag)
{
RS_SchemaKey schemaKey = this.schema.getSchemaKey(tag);
return this.getWithKey(schemaKey);
}
<*
This is an alternative to get() in order to bypass String comparison.
Returns the first RS_Field* that matches the given key.
Returns null if this RS_Struct does not contain any field with that key.
*>
fn RS_Field* RS_Struct.getWithKey(&this, RS_SchemaKey schemaKey)
{
for(int i = 0; i < this.fieldCount; i++) {
if(this.schemaKeys[i] == schemaKey) {
return this.fields[i];
}
}
return null;
}
<*
Returns an RS_Field* array containing all RS_Fields that have then passed tag.
Will return an empty array if there are no fields with a matching tag.
This allocates the resulting array on the arena associated with the passed RS_Processor, and may fail if the arena runs out of memory.
*>
fn RS_Field*[]? RS_Struct.getAllWithTag(&this, String tag, RS_Processor* proc)
{
RS_SchemaKey schemaKey = this.schema.getSchemaKey(tag);
return this.getAllWithKey(schemaKey, proc)!;
}
<*
Returns an RS_Field* array containing all RS_Fields that have then passed tag.
Will return an empty array if there are no fields with a matching tag.
This allocates the resulting array on the arena associated with the passed RS_Processor, and may fail if the arena runs out of memory.
*>
fn RS_Field*[]? RS_Struct.getAllWithKey(&this, RS_SchemaKey schemaKey, RS_Processor* proc)
{
int count = 0;
for(int i = 0; i < this.fieldCount; i++) {
if(this.schemaKeys[i] == schemaKey) {
count++;
}
}
RS_Field** fieldArrayPtr = allocOnArena(proc.arena, (RS_Struct*.sizeof) * count);
if(!fieldArrayPtr) {
return RS_ARENA_OUT_OF_MEMORY~;
}
RS_Field*[] fieldArray = fieldArrayPtr[0 : count];
int putCount = 0;
for(int i = 0; i < this.fieldCount; i++) {
if(this.schemaKeys[i] == schemaKey) {
fieldArray[putCount] = this.fields[i];
putCount++;
}
}
return fieldArray;
}
<*
Returns true if there is at least one field in this RS_Struct that has a matching tag.
Returns false if there are none.
*>
fn bool RS_Struct.hasField(&this, String tag)
{
RS_SchemaKey schemaKey = this.schema.getSchemaKey(tag);
return this.hasFieldWithKey(schemaKey);
}
<*
Returns true if there is at least one field in this RS_Struct that has a matching key.
Returns false if there are none.
*>
fn bool RS_Struct.hasFieldWithKey(&this, RS_SchemaKey schemaKey)
{
for(int i = 0; i < this.fieldCount; i++) {
if(this.schemaKeys[i] == schemaKey) {
return true;
}
}
return false;
}
<*
Creates and adds a BOOL field with the given tag and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the tag is undefined or the tag belongs to a different type.
*>
fn void? RS_Struct.addBool(&this, String tag, bool val, RS_Processor* proc)
{
RS_SchemaKey schemaKey = this.schema.getSchemaKey(tag);
this.addBoolWithKey(schemaKey, val, proc)!;
}
<*
This is an alternative to addBool() in order to bypass String comparison.
Creates and adds a BOOL field with the given key and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the key is undefined or the key belongs to a different type.
*>
fn void? RS_Struct.addBoolWithKey(&this, RS_SchemaKey schemaKey, bool val, RS_Processor* proc)
{
if(schemaKey == -1) {
return RS_UNDEFINED_SCHEMA_TAG~;
}
if(this.schema.fieldTypes[schemaKey] != RS_FieldType.BOOL) {
return RS_SCHEMA_DEFINITION_MISMATCH~;
}
RS_Field* field = (RS_Field*) allocOnArena(proc.arena, RS_Field.sizeof);
if(!field) {
return RS_ARENA_OUT_OF_MEMORY~;
}
field.type = this.schema.fieldTypes[schemaKey];
field.putBool(val);
_struct_storeField(this, field, schemaKey, proc)!;
}
<*
Creates and adds a BYTE field with the given tag and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the tag is undefined or the tag belongs to a different type.
*>
fn void? RS_Struct.addByte(&this, String tag, Byte val, RS_Processor* proc)
{
RS_SchemaKey schemaKey = this.schema.getSchemaKey(tag);
this.addByteWithKey(schemaKey, val, proc)!;
}
<*
This is an alternative to addByte() in order to bypass String comparison.
Creates and adds a BYTE field with the given key and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the key is undefined or the key belongs to a different type.
*>
fn void? RS_Struct.addByteWithKey(&this, RS_SchemaKey schemaKey, Byte val, RS_Processor* proc)
{
if(schemaKey == -1) {
return RS_UNDEFINED_SCHEMA_TAG~;
}
if(this.schema.fieldTypes[schemaKey] != RS_FieldType.BYTE) {
return RS_SCHEMA_DEFINITION_MISMATCH~;
}
RS_Field* field = (RS_Field*) allocOnArena(proc.arena, RS_Field.sizeof);
if(!field) {
return RS_ARENA_OUT_OF_MEMORY~;
}
field.type = this.schema.fieldTypes[schemaKey];
field.putByte(val);
_struct_storeField(this, field, schemaKey, proc)!;
}
<*
Creates and adds a SHORT field with the given tag and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the tag is undefined or the tag belongs to a different type.
*>
fn void? RS_Struct.addShort(&this, String tag, ushort val, RS_Processor* proc)
{
RS_SchemaKey schemaKey = this.schema.getSchemaKey(tag);
this.addShortWithKey(schemaKey, val, proc)!;
}
<*
This is an alternative to addShort() in order to bypass String comparison.
Creates and adds a SHORT field with the given key and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the key is undefined or the key belongs to a different type.
*>
fn void? RS_Struct.addShortWithKey(&this, RS_SchemaKey schemaKey, ushort val, RS_Processor* proc)
{
if(schemaKey == -1) {
return RS_UNDEFINED_SCHEMA_TAG~;
}
if(this.schema.fieldTypes[schemaKey] != RS_FieldType.SHORT) {
return RS_SCHEMA_DEFINITION_MISMATCH~;
}
RS_Field* field = (RS_Field*) allocOnArena(proc.arena, RS_Field.sizeof);
if(!field) {
return RS_ARENA_OUT_OF_MEMORY~;
}
field.type = this.schema.fieldTypes[schemaKey];
field.putShort(val);
_struct_storeField(this, field, schemaKey, proc)!;
}
<*
Creates and adds an INT field with the given tag and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the tag is undefined or the tag belongs to a different type.
*>
fn void? RS_Struct.addInt(&this, String tag, uint val, RS_Processor* proc)
{
RS_SchemaKey schemaKey = this.schema.getSchemaKey(tag);
this.addIntWithKey(schemaKey, val, proc)!;
}
<*
This is an alternative to addInt() in order to bypass String comparison.
Creates and adds an INT field with the given key and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the key is undefined or the key belongs to a different type.
*>
fn void? RS_Struct.addIntWithKey(&this, RS_SchemaKey schemaKey, uint val, RS_Processor* proc)
{
if(schemaKey == -1) {
return RS_UNDEFINED_SCHEMA_TAG~;
}
if(this.schema.fieldTypes[schemaKey] != RS_FieldType.INT) {
return RS_SCHEMA_DEFINITION_MISMATCH~;
}
RS_Field* field = (RS_Field*) allocOnArena(proc.arena, RS_Field.sizeof);
if(!field) {
return RS_ARENA_OUT_OF_MEMORY~;
}
field.type = this.schema.fieldTypes[schemaKey];
field.putInt(val);
_struct_storeField(this, field, schemaKey, proc)!;
}
<*
Creates and adds a LONG field with the given tag and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the tag is undefined or the tag belongs to a different type.
*>
fn void? RS_Struct.addLong(&this, String tag, ulong val, RS_Processor* proc)
{
RS_SchemaKey schemaKey = this.schema.getSchemaKey(tag);
this.addLongWithKey(schemaKey, val, proc)!;
}
<*
This is an alternative to addLong() in order to bypass String comparison.
Creates and adds a LONG field with the given key and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the key is undefined or the key belongs to a different type.
*>
fn void? RS_Struct.addLongWithKey(&this, RS_SchemaKey schemaKey, ulong val, RS_Processor* proc)
{
if(schemaKey == -1) {
return RS_UNDEFINED_SCHEMA_TAG~;
}
if(this.schema.fieldTypes[schemaKey] != RS_FieldType.LONG) {
return RS_SCHEMA_DEFINITION_MISMATCH~;
}
RS_Field* field = (RS_Field*) allocOnArena(proc.arena, RS_Field.sizeof);
if(!field) {
return RS_ARENA_OUT_OF_MEMORY~;
}
field.type = this.schema.fieldTypes[schemaKey];
field.putLong(val);
_struct_storeField(this, field, schemaKey, proc)!;
}
<*
Creates and adds a FLOAT field with the given tag and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the tag is undefined or the tag belongs to a different type.
*>
fn void? RS_Struct.addFloat(&this, String tag, float val, RS_Processor* proc)
{
RS_SchemaKey schemaKey = this.schema.getSchemaKey(tag);
this.addFloatWithKey(schemaKey, val, proc)!;
}
<*
This is an alternative to addFloat() in order to bypass String comparison.
Creates and adds a FLOAT field with the given key and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the key is undefined or the key belongs to a different type.
*>
fn void? RS_Struct.addFloatWithKey(&this, RS_SchemaKey schemaKey, float val, RS_Processor* proc)
{
if(schemaKey == -1) {
return RS_UNDEFINED_SCHEMA_TAG~;
}
if(this.schema.fieldTypes[schemaKey] != RS_FieldType.FLOAT) {
return RS_SCHEMA_DEFINITION_MISMATCH~;
}
RS_Field* field = (RS_Field*) allocOnArena(proc.arena, RS_Field.sizeof);
if(!field) {
return RS_ARENA_OUT_OF_MEMORY~;
}
field.type = this.schema.fieldTypes[schemaKey];
field.putFloat(val);
_struct_storeField(this, field, schemaKey, proc)!;
}
<*
Creates and adds a DOUBLE field with the given tag and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the tag is undefined or the tag belongs to a different type.
*>
fn void? RS_Struct.addDouble(&this, String tag, double val, RS_Processor* proc)
{
RS_SchemaKey schemaKey = this.schema.getSchemaKey(tag);
this.addDoubleWithKey(schemaKey, val, proc)!;
}
<*
This is an alternative to addDouble() in order to bypass String comparison.
Creates and adds a DOUBLE field with the given key and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the key is undefined or the key belongs to a different type.
*>
fn void? RS_Struct.addDoubleWithKey(&this, RS_SchemaKey schemaKey, double val, RS_Processor* proc)
{
if(schemaKey == -1) {
return RS_UNDEFINED_SCHEMA_TAG~;
}
if(this.schema.fieldTypes[schemaKey] != RS_FieldType.DOUBLE) {
return RS_SCHEMA_DEFINITION_MISMATCH~;
}
RS_Field* field = (RS_Field*) allocOnArena(proc.arena, RS_Field.sizeof);
if(!field) {
return RS_ARENA_OUT_OF_MEMORY~;
}
field.type = this.schema.fieldTypes[schemaKey];
field.putDouble(val);
_struct_storeField(this, field, schemaKey, proc)!;
}
<*
Creates and adds a STRING field with the given tag and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the tag is undefined or the tag belongs to a different type.
*>
fn void? RS_Struct.addString(&this, String tag, String val, RS_Processor* proc)
{
RS_SchemaKey schemaKey = this.schema.getSchemaKey(tag);
this.addStringWithKey(schemaKey, val, proc)!;
}
<*
This is an alternative to addString() in order to bypass String comparison.
Creates and adds a STRING field with the given key and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the key is undefined or the key belongs to a different type.
*>
fn void? RS_Struct.addStringWithKey(&this, RS_SchemaKey schemaKey, String val, RS_Processor* proc)
{
if(schemaKey == -1) {
return RS_UNDEFINED_SCHEMA_TAG~;
}
if(this.schema.fieldTypes[schemaKey] != RS_FieldType.STRING) {
return RS_SCHEMA_DEFINITION_MISMATCH~;
}
RS_Field* field = (RS_Field*) allocOnArena(proc.arena, RS_Field.sizeof);
if(!field) {
return RS_ARENA_OUT_OF_MEMORY~;
}
field.type = this.schema.fieldTypes[schemaKey];
field.putString(val, proc)!;
_struct_storeField(this, field, schemaKey, proc)!;
}
<*
Creates and adds a RAW field with the given tag and value.
This may fail if there is not enough memory in the arena to support allocating a field or any other allocations relating to adding a field.
This also may fail if the tag is undefined or the tag belongs to a different type.
*>
fn void? RS_Struct.addBytes(&this, String tag, Byte* byteArray, uint arrayLength, RS_Processor* proc)
{
RS_SchemaKey schemaKey = this.schema.getSchemaKey(tag);
this.addBytesWithKey(schemaKey, byteArray, arrayLength, proc)!;
}
<*
This is an alternative to addBytes() in order to bypass String comparison.
Creates and adds a RAW field with the given key and value.