-
Notifications
You must be signed in to change notification settings - Fork 16
/
protocolstructure.cpp
3158 lines (2511 loc) · 105 KB
/
protocolstructure.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
#include "protocolstructure.h"
#include "protocolstructuremodule.h"
#include "protocolparser.h"
#include "protocolfield.h"
#include <string>
/*!
* Construct a protocol structure
* \param parse points to the global protocol parser that owns everything
* \param parent is the hierarchical name of the object that owns this object.
* \param support are the protocol support details
*/
ProtocolStructure::ProtocolStructure(ProtocolParser* parse, std::string parent, ProtocolSupport supported) :
Encodable(parse, parent, supported),
numbitfieldgroupbytes(0),
bitfields(false),
usestempencodebitfields(false),
usestempencodelongbitfields(false),
usestempdecodebitfields(false),
usestempdecodelongbitfields(false),
needsEncodeIterator(false),
needsDecodeIterator(false),
needsInitIterator(false),
needsVerifyIterator(false),
needs2ndEncodeIterator(false),
needs2ndDecodeIterator(false),
needs2ndInitIterator(false),
needs2ndVerifyIterator(false),
defaults(false),
hidden(false),
neverOmit(false),
hasinit(supported.language == ProtocolSupport::cpp_language),
hasverify(false),
encode(true),
decode(true),
compare(false),
print(false),
mapEncode(false),
redefines(nullptr)
{
// List of attributes understood by ProtocolStructure
attriblist = {"name", "title", "array", "variableArray", "array2d", "variable2dArray", "dependsOn", "comment", "hidden", "neverOmit", "limitOnEncode", "dbctx", "dbcrx"};
}
/*!
* Reset all data to defaults
*/
void ProtocolStructure::clear(void)
{
Encodable::clear();
// Delete any encodable objects in our list
for(std::size_t i = 0; i < encodables.size(); i++)
{
if(encodables[i])
{
delete encodables[i];
encodables[i] = NULL;
}
}
// Empty the list itself
encodables.clear();
// Objects in this list are owned by others, we just clear it, don't delete the objects
enumList.clear();
// The rest of the metadata
numbitfieldgroupbytes = 0;
bitfields = false;
usestempencodebitfields = false;
usestempencodelongbitfields = false;
usestempdecodebitfields = false;
usestempdecodelongbitfields = false;
needsEncodeIterator = false;
needsDecodeIterator = false;
needsInitIterator = false;
needsVerifyIterator = false;
needs2ndEncodeIterator = false;
needs2ndDecodeIterator = false;
needs2ndInitIterator = false;
needs2ndVerifyIterator = false;
defaults = false;
hidden = false;
neverOmit = false;
hasinit = (support.language == ProtocolSupport::cpp_language);
hasverify = false;
encode = decode = true;
print = compare = mapEncode = false;
structName.clear();
redefines = nullptr;
}// ProtocolStructure::clear
/*!
* Parse the DOM data for this structure
*/
void ProtocolStructure::parse(bool nocode)
{
if(e == nullptr)
return;
const XMLAttribute* map = e->FirstAttribute();
// All the attribute we care about
name = ProtocolParser::getAttribute("name", map, "_unknown");
title = ProtocolParser::getAttribute("title", map);
array = ProtocolParser::getAttribute("array", map);
variableArray = ProtocolParser::getAttribute("variableArray", map);
dependsOn = ProtocolParser::getAttribute("dependsOn", map);
dependsOnValue = ProtocolParser::getAttribute("dependsOnValue", map);
dependsOnCompare = ProtocolParser::getAttribute("dependsOnCompare", map);
comment = ProtocolParser::reflowComment(ProtocolParser::getAttribute("comment", map));
hidden = ProtocolParser::isFieldSet("hidden", map);
neverOmit = ProtocolParser::isFieldSet("neverOmit", map);
/*
* This logic is handled by my parents. This may not be the correct thing
* to do: someone might create a structure as a sub of another structure
* and hide it. In which case this won't be omitted. Not sure how best to
* deal with that case.
if(hidden && !neverOmit && support.omitIfHidden)
{
std::cout << "Skipping code output for hidden structure " << getHierarchicalName() << std::endl;
clear();
return;
}
*/
if(title.empty())
title = name;
// This will propagate to any of the children we create
if(ProtocolParser::isFieldSet("limitOnEncode", map))
support.limitonencode = true;
else if(ProtocolParser::isFieldClear("limitOnEncode", map))
support.limitonencode = false;
testAndWarnAttributes(map);
// for now the typename is derived from the name
structName = typeName = support.prefix + name + support.typeSuffix;
// We can't have a variable array length without an array
if(array.empty() && !variableArray.empty())
{
emitWarning("must specify array length to specify variable array length");
variableArray.clear();
}
if(!dependsOn.empty() && !variableArray.empty())
{
emitWarning("variable length arrays cannot also use dependsOn");
dependsOn.clear();
}
if(!dependsOnValue.empty() && dependsOn.empty())
{
emitWarning("dependsOnValue does not make sense unless dependsOn is defined");
dependsOnValue.clear();
}
if(!dependsOnCompare.empty() && dependsOnValue.empty())
{
emitWarning("dependsOnCompare does not make sense unless dependsOnValue is defined");
dependsOnCompare.clear();
}
else if(dependsOnCompare.empty() && !dependsOnValue.empty())
{
// This is not a warning, it is expected
dependsOnCompare = "==";
}
// Check to make sure we did not step on any keywords
checkAgainstKeywords();
// Get any enumerations
parseEnumerations(e, nocode);
// At this point a structure cannot be default, null, or reserved.
parseChildren(e);
// Sum the length of all the children
EncodedLength length;
for(std::size_t i = 0; i < encodables.size(); i++)
{
length.addToLength(encodables.at(i)->encodedLength);
}
// Account for array, variable array, and depends on
encodedLength.clear();
encodedLength.addToLength(length, array, !variableArray.empty(), !dependsOn.empty());
}// ProtocolStructure::parse
/*!
* Return the string used to declare this encodable as part of a structure.
* This includes the spacing, typename, name, semicolon, comment, and linefeed
* \return the declaration string for this encodable
*/
std::string ProtocolStructure::getDeclaration(void) const
{
std::string output = TAB_IN + "" + typeName + " " + name;
if(array.empty())
output += ";";
else if(array2d.empty())
output += "[" + array + "];";
else
output += "[" + array + "][" + array2d + "]";
if(!comment.empty())
output += " //!< " + comment;
output += "\n";
return output;
}// ProtocolStructure::getDeclaration
/*!
* Return the string that is used to encode *this* structure
* \param bitcount points to the running count of bits in a bitfields and should persist between calls, ignored
* \param isStructureMember is true if this encodable is accessed by structure pointer
* \return the string to add to the source to encode this structure
*/
std::string ProtocolStructure::getEncodeString(int* bitcount, bool isStructureMember) const
{
(void)bitcount;
std::string output;
std::string access = getEncodeFieldAccess(isStructureMember);
std::string spacing = TAB_IN;
if(!comment.empty())
output += spacing + "// " + comment + "\n";
if(!dependsOn.empty())
{
output += spacing + "if(" + getEncodeFieldAccess(isStructureMember, dependsOn);
if(!dependsOnValue.empty())
output += " " + dependsOnCompare + " " + dependsOnValue;
output += ")\n" + spacing + "{\n";
spacing += TAB_IN;
}
// Array handling
output += getEncodeArrayIterationCode(spacing, isStructureMember);
// Spacing for arrays
if(isArray())
{
spacing += TAB_IN;
if(is2dArray())
spacing += TAB_IN;
}
// The actual encode function
if(support.language == ProtocolSupport::c_language)
output += spacing + "encode" + typeName + "(_pg_data, &_pg_byteindex, " + access + ");\n";
else
{
// Note that if we are an array the dereferencing is done by the array operator
if(isStructureMember || isArray())
output += spacing + access + ".encode(_pg_data, &_pg_byteindex);\n";
else
output += spacing + access + "->encode(_pg_data, &_pg_byteindex);\n";
}
// Close the depends on block
if(!dependsOn.empty())
output += TAB_IN + "}\n";
return output;
}// ProtocolStructure::getEncodeString
/*!
* Return the string that is used to decode this structure
* \param bitcount points to the running count of bits in a bitfields and should persist between calls
* \param isStructureMember is true if this encodable is accessed by structure pointer
* \return the string to add to the source to decode this structure
*/
std::string ProtocolStructure::getDecodeString(int* bitcount, bool isStructureMember, bool defaultEnabled) const
{
(void)bitcount;
(void)defaultEnabled;
std::string output;
std::string access = getEncodeFieldAccess(isStructureMember);
std::string spacing = TAB_IN;
if(!comment.empty())
output += spacing + "// " + comment + "\n";
if(!dependsOn.empty())
{
output += spacing + "if(" + getDecodeFieldAccess(isStructureMember, dependsOn);
if(!dependsOnValue.empty())
output += " " + dependsOnCompare + " " + dependsOnValue;
output += ")\n" + spacing + "{\n";
spacing += TAB_IN;
}
// Array handling
output += getDecodeArrayIterationCode(spacing, isStructureMember);
// Spacing for arrays
if(isArray())
{
spacing += TAB_IN;
if(is2dArray())
spacing += TAB_IN;
}
if(support.language == ProtocolSupport::c_language)
{
output += spacing + "if(decode" + typeName + "(_pg_data, &_pg_byteindex, " + access + ") == 0)\n";
output += spacing + TAB_IN + "return 0;\n";
}
else
{
// Note that if we are an array the dereferencing is done by the array operator
if(isStructureMember || isArray())
output += spacing + "if(" + access + ".decode(_pg_data, &_pg_byteindex) == false)\n";
else
output += spacing + "if(" + access + "->decode(_pg_data, &_pg_byteindex) == false)\n";
output += spacing + TAB_IN + "return false;\n";
}
if(!dependsOn.empty())
output += TAB_IN + "}\n";
return output;
}// ProtocolStructure::getDecodeString
/*!
* Get the code which verifies this structure
* \return the code to put in the source file
*/
std::string ProtocolStructure::getVerifyString(void) const
{
std::string output;
std::string access = name;
std::string spacing = TAB_IN;
if(!hasverify)
return output;
if(!comment.empty())
output += spacing + "// " + comment + "\n";
// Do not call getDecodeArrayIterationCode() because we explicity don't handle variable length arrays here
if(isArray())
{
output += spacing + "for(_pg_i = 0; _pg_i < " + array + "; _pg_i++)\n";
spacing += TAB_IN;
if(is2dArray())
{
output += spacing + "for(_pg_j = 0; _pg_j < " + array2d + "; _pg_j++)\n";
spacing += TAB_IN;
}
}
if(support.language == ProtocolSupport::c_language)
{
output += spacing + "if(verify" + typeName + "(" + getDecodeFieldAccess(true) + ") == 0)\n";
output += spacing + TAB_IN + "_pg_good = 0;\n";
}
else
{
output += spacing + "if(" + getDecodeFieldAccess(true) + ".verify() == false)\n";
output += spacing + TAB_IN + "_pg_good = false;\n";
}
return output;
}// ProtocolStructure::getVerifyString
/*!
* Get the code which sets this structure member to initial values
* \return the code to put in the source file
*/
std::string ProtocolStructure::getSetInitialValueString(bool isStructureMember) const
{
std::string output;
std::string spacing = TAB_IN;
// We only need this function if we are C language, C++ classes initialize themselves
if((!hasinit) || (support.language != ProtocolSupport::c_language))
return output;
if(!comment.empty())
output += spacing + "// " + comment + "\n";
// Do not call getDecodeArrayIterationCode() because we explicity don't handle variable length arrays here
if(isArray())
{
output += spacing + "for(_pg_i = 0; _pg_i < " + array + "; _pg_i++)\n";
spacing += TAB_IN;
if(is2dArray())
{
output += spacing + "for(_pg_j = 0; _pg_j < " + array2d + "; _pg_j++)\n";
spacing += TAB_IN;
}
}
output += spacing + "init" + typeName + "(" + getDecodeFieldAccess(isStructureMember) + ");\n";
return output;
}// ProtocolStructure::getSetInitialValueString
//! Return the strings that #define initial and variable values
std::string ProtocolStructure::getInitialAndVerifyDefines(bool includeComment) const
{
std::string output;
for(std::size_t i = 0; i < encodables.size(); i++)
{
// Children's outputs do not have comments, just the top level stuff
output += encodables.at(i)->getInitialAndVerifyDefines(false);
}
// I don't want to output this comment if there are no values being commented,
// that's why I insert the comment after the #defines are output
if(!output.empty() && includeComment)
{
output.insert(0, "// Initial and verify values for " + name + "\n");
}
return output;
}// ProtocolStructure::getInitialAndVerifyDefines
/*!
* Get the string which identifies this encodable in a CAN DBC file.
* \param prename is the name of the previous structure that this may be a member of.
* \param bitcount the start bit of this encoding (0 being the first bit in the message).
* \return the string which starts with SG_ and identifies this signal.
*/
std::string ProtocolStructure::getDBCSignalString(std::string prename, int* bitcount) const
{
std::string output;
// bitcount is the true start bit (0 being the first data bit in the message)
if((*bitcount) < 0)
(*bitcount) = 0;
for(std::size_t i = 0; i < encodables.size(); i++)
output += encodables.at(i)->getDBCSignalString(prename, bitcount);
return output;
}// ProtocolStructure::getDBCSignalString
/*!
* Get the comment string for this encodable in a DBC file
* \param prename is the name of the previous structure that this may be a member of.
* \param ID is the message ID that this encodable belongs to
* \return the string which starts with "CM_ SG_" and comments this signal.
*/
std::string ProtocolStructure::getDBCSignalComment(std::string prename, uint32_t ID) const
{
std::string output;
for(std::size_t i = 0; i < encodables.size(); i++)
output += encodables.at(i)->getDBCSignalComment(prename, ID);
return output;
}// ProtocolStructure::getDBCSignalComment
/*!
* Get the string which comments this encodables enumerations in a CAN DBC file
* \param prename is the name of the previous structure that this may be a member of.
* \param ID is the message ID that this encodable belongs to
* \return the string which starts with "BA_ "FieldType" SG_" and documents this enumeration.
*/
std::string ProtocolStructure::getDBCSignalEnum(std::string prename, uint32_t ID) const
{
std::string output;
for(std::size_t i = 0; i < encodables.size(); i++)
output += encodables.at(i)->getDBCSignalEnum(prename, ID);
return output;
}// ProtocolStructure::getDBCSignalEnum
/*!
* Get the string used for comparing this field.
* \return the function string, which may be empty
*/
std::string ProtocolStructure::getComparisonString(void) const
{
std::string output;
std::string access1, access2;
// We must have parameters that we decode to do a comparison
if(!compare || (getNumberOfDecodeParameters() == 0))
return output;
std::string spacing = TAB_IN;
if(!comment.empty())
output += spacing + "// " + comment + "\n";
/// TODO: obey variable array length limits?
if(support.language == ProtocolSupport::c_language)
{
// The dereference of the array gets us back to the object, but we need the pointer
access1 = "&_pg_user1->" + name;
access2 = "&_pg_user2->" + name;
}
else
{
access1 = name;
access2 = "&_pg_user->" + name;
}
if(isArray())
{
output += spacing + "for(_pg_i = 0; _pg_i < " + array + "; _pg_i++)\n";
spacing += TAB_IN;
access1 += "[_pg_i]";
access2 += "[_pg_i]";
if(is2dArray())
{
access1 += "[_pg_j]";
access2 += "[_pg_j]";
output += spacing + "for(_pg_j = 0; _pg_j < " + array2d + "; _pg_j++)\n";
spacing += TAB_IN;
}// if 2D array of structures
}// if array of structures
if(support.language == ProtocolSupport::c_language)
output += spacing + "_pg_report += compare" + typeName + "(_pg_prename + \":" + name + "\"";
else
output += spacing + "_pg_report += " + access1 + ".compare(_pg_prename + \":" + name + "\"";
if(isArray())
output += " + \"[\" + std::to_string(_pg_i) + \"]\"";
if(is2dArray())
output += " + \"[\" + std::to_string(_pg_j) + \"]\"";
if(support.language == ProtocolSupport::c_language)
output += ", " + access1 + ", " + access2 + ");\n";
else
output += ", " + access2 + ");\n";
return output;
}// ProtocolStructure::getComparisonString
/*!
* Get the string used for printing this field as text.
* \return the print string, which may be empty
*/
std::string ProtocolStructure::getTextPrintString(void) const
{
std::string output;
std::string access;
std::string spacing = TAB_IN;
// We must parameters that we decode to do a print out
if(!print || (getNumberOfDecodeParameters() == 0))
return output;
if(!comment.empty())
output += spacing + "// " + comment + "\n";
output += getEncodeArrayIterationCode(spacing, true);
if(isArray())
{
spacing += TAB_IN;
if(is2dArray())
spacing += TAB_IN;
}
if(support.language == ProtocolSupport::c_language)
output += spacing + "_pg_report += textPrint" + typeName + "(_pg_prename + \":" + name + "\"";
else
output += spacing + "_pg_report += " + getEncodeFieldAccess(true) + ".textPrint(_pg_prename + \":" + name + "\"";
if(isArray())
output += " + \"[\" + std::to_string(_pg_i) + \"]\"";
if(is2dArray())
output += " + \"[\" + std::to_string(_pg_j) + \"]\"";
if(support.language == ProtocolSupport::c_language)
output += ", " + getEncodeFieldAccess(true);
output += ");\n";
return output;
}// ProtocolStructure::getTextPrintString
/*!
* Get the string used for reading this field from text.
* \return the read string, which may be empty
*/
std::string ProtocolStructure::getTextReadString(void) const
{
std::string output;
std::string access;
std::string spacing = TAB_IN;
// We must parameters that we decode to do a print out
if(!print || (getNumberOfDecodeParameters() == 0))
return output;
if(!comment.empty())
output += spacing + "// " + comment + "\n";
output += getEncodeArrayIterationCode(spacing, true);
if(isArray())
{
spacing += TAB_IN;
if(is2dArray())
spacing += TAB_IN;
}
if(support.language == ProtocolSupport::c_language)
output += spacing + "_pg_fieldcount += textRead" + typeName + "(_pg_prename + \":" + name + "\"";
else
output += spacing + "_pg_fieldcount += " + getEncodeFieldAccess(true) + ".textRead(_pg_prename + \":" + name + "\"";
if(isArray())
output += " + \"[\" + std::to_string(_pg_i) + \"]\"";
if(is2dArray())
output += " + \"[\" + std::to_string(_pg_j) + \"]\"";
output += ", _pg_source";
if(support.language == ProtocolSupport::c_language)
output += ", " + getEncodeFieldAccess(true);
output += ");\n";
return output;
}// ProtocolStructure::getTextReadString
/*!
* Return the string used for encoding this field to a map
* \return the encode string, which may be empty
*/
std::string ProtocolStructure::getMapEncodeString(void) const
{
std::string output;
std::string spacing = TAB_IN;
// We must parameters that we decode to do a print out
if(!mapEncode || (getNumberOfDecodeParameters() == 0))
return output;
if(!comment.empty())
output += spacing + "// " + comment + "\n";
std::string key = "\":" + name + "\"";
output += getEncodeArrayIterationCode(spacing, true);
if(isArray())
{
spacing += TAB_IN;
key += " + \"[\" + QString::number(_pg_i) + \"]\"";
if(is2dArray())
{
spacing += TAB_IN;
key += " + \"[\" + QString::number(_pg_j) + \"]\"";
}
}
if(support.language == ProtocolSupport::c_language)
output += spacing + "mapEncode" + typeName + "(_pg_prename + " + key + ", _pg_map, " + getEncodeFieldAccess(true);
else
output += spacing + getEncodeFieldAccess(true) + ".mapEncode(_pg_prename + " + key + ", _pg_map";
output += ");\n";
return output;
}// ProtocolStructure::getMapEncodeString
/*!
* Get the string used for decoding this field from a map.
* \return the map decode string, which may be empty
*/
std::string ProtocolStructure::getMapDecodeString(void) const
{
std::string output;
std::string spacing = TAB_IN;
// We must parameters that we decode to do a print out
if(!mapEncode || (getNumberOfDecodeParameters() == 0))
return output;
if(!comment.empty())
output += spacing + "// " + comment + "\n";
std::string key = "\":" + name + "\"";
output += getDecodeArrayIterationCode(spacing, true);
if(isArray())
{
spacing += TAB_IN;
key += " + \"[\" + QString::number(_pg_i) + \"]\"";
if(is2dArray())
{
spacing += TAB_IN;
key += " + \"[\" + QString::number(_pg_j) + \"]\"";
}
}
if(support.language == ProtocolSupport::c_language)
output += spacing + "mapDecode" + typeName + "(_pg_prename + " + key + ", _pg_map, " + getDecodeFieldAccess(true);
else
output += spacing + getDecodeFieldAccess(true) + ".mapDecode(_pg_prename + " + key + ", _pg_map";
output += ");\n";
return output;
}// ProtocolStructure::getMapDecodeString
/*!
* Parse all enumerations which are direct children of a DomNode
* \param node is parent node.
* \param nocode is used to prevent code outputs.
*/
void ProtocolStructure::parseEnumerations(const XMLNode* node, bool nocode)
{
for(const XMLElement* child = node->FirstChildElement(); child != nullptr; child = child->NextSiblingElement())
{
if(contains(child->Name(), "enum"))
{
const EnumCreator* Enum = parser->parseEnumeration(getHierarchicalName(), child, nocode);
if(Enum != nullptr)
{
// Inform this enumeration what it's header is
enumList.push_back(Enum);
}
}
}// for all my enum tags
}// ProtocolStructure::parseEnumerations
/*!
* Parse the DOM data for the children of this structure
* \param field is the DOM data for this structure
*/
void ProtocolStructure::parseChildren(const XMLElement* field)
{
Encodable* prevEncodable = NULL;
// Make encodables out of them, and add to our list
for(const XMLElement* child = field->FirstChildElement(); child != nullptr; child = child->NextSiblingElement())
{
Encodable* encodable = generateEncodable(parser, getHierarchicalName(), support, child);
if(encodable != NULL)
{
// If the encodable is null, then none of the metadata
// matters, its not going to end up in the output
if(!encodable->isNotEncoded())
{
ProtocolField* field = dynamic_cast<ProtocolField*>(encodable);
if(field != NULL)
{
// Let the new encodable know about the preceding one
field->setPreviousEncodable(prevEncodable);
if(field->overridesPreviousEncodable())
{
std::size_t prev;
for(prev = 0; prev < encodables.size(); prev++)
{
if(field->getOverriddenTypeData(dynamic_cast<ProtocolField*>(encodables.at(prev))))
break;
}
if(prev >= encodables.size())
{
field->emitWarning("override failed, could not find previous field");
delete encodable;
continue;
}
}// if encodable does override operation
// Track our metadata
if(field->usesBitfields())
{
field->getBitfieldGroupNumBytes(&numbitfieldgroupbytes);
bitfields = true;
if(field->usesEncodeTempBitfield())
usestempencodebitfields = true;
if(field->usesEncodeTempLongBitfield())
usestempencodelongbitfields = true;
if(field->usesDecodeTempBitfield())
usestempdecodebitfields = true;
if(field->usesDecodeTempLongBitfield())
usestempdecodelongbitfields = true;
}
if(field->usesEncodeIterator())
needsEncodeIterator = true;
if(field->usesDecodeIterator())
needsDecodeIterator = true;
if(field->usesInitIterator())
needsInitIterator = true;
if(field->usesVerifyIterator())
needsVerifyIterator = true;
if(field->uses2ndEncodeIterator())
needs2ndEncodeIterator = true;
if(field->uses2ndDecodeIterator())
needs2ndDecodeIterator = true;
if(field->uses2ndInitIterator())
needs2ndInitIterator = true;
if(field->uses2ndVerifyIterator())
needs2ndVerifyIterator = true;
if(field->usesDefaults())
defaults = true;
else if(defaults && field->invalidatesPreviousDefault())
{
// Check defaults. If a previous field was defaulted but this
// field is not, then we have to terminate the previous default,
// only the last fields can have defaults
for(std::size_t j = 0; j < encodables.size(); j++)
{
if(encodables[j]->usesDefaults())
{
encodables[j]->clearDefaults();
encodables[j]->emitWarning("default value ignored, field is followed by non-default");
}
}
defaults = false;
}
}// if this encodable is a field
else
{
// Structures can be arrays as well.
if(encodable->isArray())
{
needsDecodeIterator = needsEncodeIterator = true;
needsInitIterator = encodable->hasInit();
needsVerifyIterator = encodable->hasVerify();
}
if(encodable->is2dArray())
{
needs2ndDecodeIterator = needs2ndEncodeIterator = true;
needs2ndInitIterator = encodable->hasInit();
needs2ndVerifyIterator = encodable->hasVerify();
}
}// else if this encodable is not a field
// Handle the variable array case. We have to make sure that the referenced variable exists
if(!encodable->variableArray.empty())
{
std::size_t prev;
for(prev = 0; prev < encodables.size(); prev++)
{
Encodable* previous = encodables.at(prev);
if(previous == NULL)
continue;
// It has to be a named variable that is both in memory and encoded
if(previous->isNotEncoded() || previous->isNotInMemory())
continue;
// It has to be a primitive, and it cannot be an array itself
if(!previous->isPrimitive() && !previous->isArray())
continue;
// Now check to see if this previously defined encodable is our variable
if(previous->name == encodable->variableArray)
break;
}
if(prev >= encodables.size())
{
encodable->emitWarning("variable length array ignored, failed to find length variable");
encodable->variableArray.clear();
}
}// if this is a variable length array
// Handle the variable 2d array case. We have to make sure that the referenced variable exists
if(!encodable->variable2dArray.empty())
{
std::size_t prev;
for(prev = 0; prev < encodables.size(); prev++)
{
Encodable* previous = encodables.at(prev);
if(previous == NULL)
continue;
// It has to be a named variable that is both in memory and encoded
if(previous->isNotEncoded() || previous->isNotInMemory())
continue;
// It has to be a primitive, and it cannot be an array itself
if(!previous->isPrimitive() && !previous->isArray())
continue;
// Now check to see if this previously defined encodable is our variable
if(previous->name == encodable->variable2dArray)
break;
}
if(prev >= encodables.size())
{
encodable->emitWarning("variable 2d length array ignored, failed to find 2d length variable");
encodable->variable2dArray.clear();
}
}// if this is a 2d variable length array
// Handle the dependsOn case. We have to make sure that the referenced variable exists
if(!encodable->dependsOn.empty())
{
if(encodable->isBitfield())
{
encodable->emitWarning("bitfields cannot use dependsOn");
encodable->dependsOn.clear();
}
else
{
std::size_t prev;
for(prev = 0; prev < encodables.size(); prev++)
{
Encodable* previous = encodables.at(prev);
if(previous == NULL)
continue;
// It has to be a named variable that is both in memory and encoded
if(previous->isNotEncoded() || previous->isNotInMemory())
continue;
// It has to be a primitive, and it cannot be an array itself
if(!previous->isPrimitive() && !previous->isArray())
continue;