1515
1616#include " WiresharkFloatField.h"
1717
18+ #include " Wireshark.h"
1819#include " WiresharkGenerator.h"
1920
21+ #include " commsdsl/gen/strings.h"
22+ #include " commsdsl/gen/util.h"
23+
24+ #include < cassert>
25+ #include < cstddef>
26+ #include < cmath>
27+ #include < type_traits>
28+
29+ namespace strings = commsdsl::gen::strings;
30+ namespace util = commsdsl::gen::util;
31+
2032namespace commsdsl2wireshark
2133{
2234
@@ -35,4 +47,113 @@ bool WiresharkFloatField::genPrepareImpl()
3547 return true ;
3648}
3749
50+ std::string WiresharkFloatField::wiresharkFieldRegistrationImpl (const WiresharkField* refField) const
51+ {
52+ static const std::string Templ =
53+ " #^#SPECIALS#$#\n "
54+ " local #^#OBJ_NAME#$# = #^#CREATE_FUNC#$#(ProtoField.#^#TYPE#$#(\" #^#REF_NAME#$#\" , #^#DISP_NAME#$#, #^#SPECIALS_NAME#$#, #^#DESC#$#))\n "
55+ ;
56+
57+ util::GenReplacementMap repl = {
58+ {" SPECIALS" , wiresharkSpecialsInternal (refField)},
59+ {" OBJ_NAME" , wiresharkFieldObjName (refField)},
60+ {" CREATE_FUNC" , Wireshark::wiresharkCreateFieldFuncName (WiresharkGenerator::wiresharkCast (genGenerator ()))},
61+ {" TYPE" , wiresharkFloatTypeInternal ()},
62+ {" REF_NAME" , wiresharkFieldRefName (refField)},
63+ {" DISP_NAME" , wiresharkFieldNameVarNameStr (refField)},
64+ {" SPECIALS_NAME" , strings::genNilStr ()},
65+ {" DESC" , wiresharkFieldDescriptionStr (refField)},
66+ };
67+
68+ if (!repl[" SPECIALS" ].empty ()) {
69+ repl[" SPECIALS_NAME" ] = repl[" OBJ_NAME" ] + strings::genValsSuffixStr ();
70+ }
71+
72+ assert (!repl[" TYPE" ].empty ());
73+ return util::genProcessTemplate (Templ, repl);
74+ }
75+
76+ std::string WiresharkFloatField::wiresharkSpecialsInternal (const WiresharkField* refField) const
77+ {
78+ auto & specials = genSpecialsSortedByValue ();
79+ if (specials.empty ()) {
80+ return strings::genEmptyString ();
81+ }
82+
83+ util::GenStringsList elems;
84+ for (auto & s : specials) {
85+ if (std::isnan (s.second .m_value )) {
86+ // Known limitation: the NaN comparing to NaN will return false, so it cannot really be found in map
87+ continue ;
88+ }
89+
90+ if (!genGenerator ().genDoesElementExist (s.second .m_sinceVersion , s.second .m_deprecatedSince , true )) {
91+ continue ;
92+ }
93+
94+ static const std::string Templ =
95+ " [#^#VAL#$#] = \" #^#NAME#$#\" " ;
96+
97+ util::GenReplacementMap repl = {
98+ {" NAME" , util::genDisplayName (s.second .m_displayName , s.first )},
99+ {" VAL" , wiresharkSpecialValStrInternal (s.second .m_value )},
100+ };
101+
102+ elems.push_back (util::genProcessTemplate (Templ, repl));
103+ }
104+
105+ if (elems.empty ()) {
106+ return strings::genEmptyString ();
107+ }
108+
109+ static const std::string Templ =
110+ " local #^#NAME#$##^#SUFFIX#$# = {\n "
111+ " #^#ELEMS#$#\n "
112+ " }\n "
113+ ;
114+
115+ util::GenReplacementMap repl = {
116+ {" NAME" , wiresharkFieldObjName (refField)},
117+ {" SUFFIX" , strings::genValsSuffixStr ()},
118+ {" ELEMS" , util::genStrListToString (elems, " ,\n " , " " )},
119+ };
120+
121+ return util::genProcessTemplate (Templ, repl);
122+ }
123+
124+ std::string WiresharkFloatField::wiresharkSpecialValStrInternal (double val) const
125+ {
126+ assert (!std::isnan (val)); // Should be filtered out earlier
127+
128+ if (!std::isinf (val)) {
129+ return std::to_string (val);
130+ }
131+
132+ if (val < 0 ) {
133+ return " 1/0" ;
134+ }
135+
136+ return " -1/0" ;
137+ }
138+
139+ const std::string& WiresharkFloatField::wiresharkFloatTypeInternal () const
140+ {
141+ static const std::string Map[] = {
142+ /* Float */ " float" ,
143+ /* Double */ " double" ,
144+ };
145+ static const std::size_t MapSize = std::extent_v<decltype (Map)>;
146+ static_assert (MapSize == static_cast <unsigned >(ParseFloatField::ParseType::NumOfValues));
147+
148+ auto parseObj = genFloatFieldParseObj ();
149+ auto idx = static_cast <unsigned >(parseObj.parseType ());
150+ if (MapSize <= idx) {
151+ [[maybe_unused]] static constexpr bool Should_not_happen = false ;
152+ assert (Should_not_happen);
153+ idx = static_cast <decltype (idx)>(ParseFloatField::ParseType::Double);
154+ }
155+
156+ return Map[idx];
157+ }
158+
38159} // namespace commsdsl2wireshark
0 commit comments