forked from billvaglienti/ProtoGen
-
Notifications
You must be signed in to change notification settings - Fork 5
/
protocolcode.cpp
151 lines (119 loc) · 4.63 KB
/
protocolcode.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
#include "protocolcode.h"
#include "protocolparser.h"
#include <iostream>
/*!
* Construct a blank protocol field
* \param parse points to the global protocol parser that owns everything
* \param parent is the hierarchical name of the owning object
* \param supported indicates what the protocol can support
*/
ProtocolCode::ProtocolCode(ProtocolParser* parse, std::string parent, ProtocolSupport supported):
Encodable(parse, parent, supported)
{
attriblist = {"name", "encode", "decode", "encode_c", "decode_c", "encode_cpp", "decode_cpp", "encode_python", "decode_python", "comment", "include"};
}
//! Reset all data to defaults
void ProtocolCode::clear(void)
{
Encodable::clear();
encode.clear();
decode.clear();
encodecpp.clear();
decodecpp.clear();
encodepython.clear();
decodepython.clear();
include.clear();
}
/*!
* Parse the DOM to determine the details of this ProtocolCode
*/
void ProtocolCode::parse(void)
{
clear();
if(e == nullptr)
return;
const XMLAttribute* map = e->FirstAttribute();
// We use name as part of our debug outputs, so its good to have it first.
name = ProtocolParser::getAttribute("name", map, "_unknown");
encode = ProtocolParser::getAttribute("encode_c", map);
decode = ProtocolParser::getAttribute("decode_c", map);
encodecpp = ProtocolParser::getAttribute("encode_cpp", map);
decodecpp = ProtocolParser::getAttribute("decode_cpp", map);
encodepython = ProtocolParser::getAttribute("encode_python", map);
decodepython = ProtocolParser::getAttribute("decode_python", map);
comment = ProtocolParser::getAttribute("comment", map);
include = ProtocolParser::getAttribute("include", map);
if(encode.empty())
encode = ProtocolParser::getAttribute("encode", map);
if(decode.empty())
decode = ProtocolParser::getAttribute("decode", map);
testAndWarnAttributes(map);
}// ProtocolCode::parse
/*!
* Get the next lines(s) of source coded needed to add this code to the encode function
* \param isBigEndian should be true for big endian encoding, ignored.
* \param bitcount points to the running count of bits in a bitfields and
* should persist between calls, ignored.
* \param isStructureMember should be true if the left hand side is a
* member of a user structure, else the left hand side is a pointer
* to the inMemoryType, ignored.
* \return The string to add to the source file for this code.
*/
std::string ProtocolCode::getEncodeString(bool isBigEndian, int* bitcount, bool isStructureMember) const
{
(void)isBigEndian;
(void)bitcount;
(void)isStructureMember;
std::string output;
if((support.language == ProtocolSupport::c_language) && !encode.empty())
{
if(!comment.empty())
output += TAB_IN + "// " + comment + "\n";
output += TAB_IN + encode + "\n";
}
else if((support.language == ProtocolSupport::cpp_language) && !encodecpp.empty())
{
if(!comment.empty())
output += TAB_IN + "// " + comment + "\n";
output += TAB_IN + encodecpp + "\n";
}
return output;
}
/*!
* Get the next lines(s) of source coded needed to add this code to the decode function.
* \param isBigEndian should be true for big endian encoding, ignored.
* \param bitcount points to the running count of bits in a bitfields and
* should persist between calls, ignored.
* \param isStructureMember should be true if the left hand side is a
* member of a user structure, else the left hand side is a pointer
* to the inMemoryType, ignored.
* \param defaultEnabled should be true to handle defaults, ignored.
* \return The string to add to the source file for this code.
*/
std::string ProtocolCode::getDecodeString(bool isBigEndian, int* bitcount, bool isStructureMember, bool defaultEnabled) const
{
(void)isBigEndian;
(void)bitcount;
(void)isStructureMember;
(void)defaultEnabled;
std::string output;
if((support.language == ProtocolSupport::c_language) && !decode.empty())
{
if(!comment.empty())
output += TAB_IN + "// " + comment + "\n";
output += TAB_IN + decode + "\n";
}
else if((support.language == ProtocolSupport::cpp_language) && !decodecpp.empty())
{
if(!comment.empty())
output += TAB_IN + "// " + comment + "\n";
output += TAB_IN + decodecpp + "\n";
}
return output;
}
//! Return the include directives that go into source code needed for this encodable
void ProtocolCode::getSourceIncludeDirectives(std::vector<std::string>& list) const
{
if(!include.empty())
list.push_back(include);
}