forked from fbudin69500/QTGUI
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mgen.cpp
291 lines (224 loc) · 8.34 KB
/
mgen.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
#include "mgen.h"
//constructor
MGen::MGen( MapType & hmap ,
std::string directory ,
std::string filename ,
std::string prefix
)
:hmap(hmap),filename(prefix+filename),directory(directory)
{
m_prefix = prefix ;
m_indent = " " ;
}
//destructor
MGen::~MGen()
{
}
//METHODS
//Outputs the hmap into the Model.cpp
void MGen::generateClass()
{
std::ofstream headerstream;
QString headerFileName = QDir(this->directory.c_str()).filePath((this->filename+".h").c_str());
headerstream.open(headerFileName.toStdString().c_str(),std::ios::out);
qDebug()<<headerFileName;
try{
if(headerstream.is_open()) {
genHeader(headerstream);
}
}catch(...) {
std::cout<<"Output to model cpp file failed"<<std::endl;
}
headerstream.close();
std::ofstream sourcestream;
QString sourceFileName = QDir(this->directory.c_str()).filePath((this->filename+".cpp").c_str());
sourcestream.open(sourceFileName.toStdString().c_str(),std::ios::out);
try{
if(sourcestream.is_open()) {
genSource(sourcestream);
}
}catch(...) {
std::cout<<"Output to model cpp file failed"<<std::endl;
}
sourcestream.close();
}
void MGen::genHeader(std::ofstream & headerstream) {
headerstream<<"#ifndef "+this->filename+"_H"<<std::endl;
headerstream<<"#define "+this->filename+"_H"<<std::endl<<std::endl;
//generate the includes
genIncludes(headerstream);
//print class keyword and the class name
headerstream << "class "<<this->filename<<" {"<<std::endl;
//generate the field
genField(headerstream);
//make getter and setters public
headerstream <<std::endl<< "public: "<<std::endl<<std::endl;
headerstream << m_indent << this->filename << "();"<<std::endl;
genGetSet(headerstream,true);
headerstream<<"};"<<std::endl<<std::endl;
headerstream<<"#endif"<<std::endl;
}
void MGen::genSource(std::ofstream & sourcestream)
{
sourcestream<<"#include \""+this->filename+".h\""<<std::endl<<std::endl;
genConstructor(sourcestream);
genGetSet(sourcestream,false);
}
void MGen::genConstructor(std::ofstream &sourcestream)
{
//get iterator from begin
MapType::const_iterator it=hmap.begin();
sourcestream << m_indent <<this->filename + "::" + this->filename + "()" << std::endl ;
sourcestream << m_indent << "{" << std::endl ;
//while the point did not hit the end of the map
while(it != hmap.end())
{
if( (it->second).compare( "QString" ) == 0 )
{
sourcestream << m_indent <<m_indent <<it->first.second.toStdString() << " = \"\" ;" << std::endl ;
}
else if( (it->second).compare( "double" ) == 0 )
{
sourcestream << m_indent <<m_indent <<it->first.second.toStdString() << " = 0.0 ;" << std::endl ;
}
else if( (it->second).compare( "int" ) == 0 )
{
sourcestream << m_indent <<m_indent <<it->first.second.toStdString() << " = 0 ;" << std::endl ;
}
else if( (it->second).compare( "bool" ) == 0 )
{
sourcestream << m_indent <<m_indent <<it->first.second.toStdString() << " = false ;" << std::endl ;
}
else
{
sourcestream << m_indent <<m_indent << "// " << it->first.second.toStdString() << " : " << it->second.toStdString() << " does not require any initialization" << std::endl ;
}
it++;
}
sourcestream << "}" << std::endl << std::endl ;
}
void MGen::AdditionalIncludeLibraries( std::ofstream & headerstream , QString headerName )
{
if( usedHeader.find( headerName ) == usedHeader.end() ) //try to find the header first
{
headerstream << "#include <" << headerName.toStdString() << ">" << std::endl ; // put it into the header
usedHeader.insert( headerName ) ; //put into set
}
}
void MGen::genIncludes(std::ofstream & headerstream)
{
//get iterator from begin
MapType::const_iterator it=hmap.begin();
while(it != hmap.end())
{
//compare the value type to the header QString for now, see if both are same string, aka when compare returns 0
if( (it->second).compare( "QString" ) == 0 )
{
AdditionalIncludeLibraries( headerstream , "QString" ) ;
}
else if( (it->second).compare( "std::map<std::pair<unsigned long,QString>,bool>" ) == 0 )
{
AdditionalIncludeLibraries( headerstream , "QString" ) ;
AdditionalIncludeLibraries( headerstream , "map" ) ;
AdditionalIncludeLibraries( headerstream , "utility" ) ;
}
else if( (it->second).compare( "std::vector<std::vector<QString> >" ) == 0 )
{
AdditionalIncludeLibraries( headerstream , "QString" ) ;
AdditionalIncludeLibraries( headerstream , "vector" ) ;
AdditionalIncludeLibraries( headerstream , "stdexcept" ) ;
}
it++;
}
headerstream << std::endl ;
}
//generates fields
void MGen::genField(std::ofstream & headerstream)
{
//get iterator from begin
MapType::const_iterator it=hmap.begin();
while(it != hmap.end())
{
headerstream<<m_indent<<it->second.toStdString()<<" "
<<it->first.second.toStdString()<<";"<<std::endl;
it++;
}
}
void MGen::genGetSet(std::ofstream & headerstream,bool headeronly)
{
//get iterator from begin
MapType::const_iterator it=hmap.begin();
//while the point did not hit the end of the map
while(it != hmap.end()) {
//getter
genGet(it,headerstream,headeronly);
//setter
genSet(it,headerstream,headeronly);
it++;
}
}
void MGen::genGet(MapType::const_iterator & it, std::ofstream & stream,bool headeronly)
{
//generate Type getVarName();
stream<<m_indent<<it->second.toStdString()<<" "<<(headeronly?"":this->filename+"::")
<<"get"
<<it->first.second.toStdString()<<"()";
//Now depending on whether we are generating header or not
if(headeronly)
{
stream<<";"<<std::endl;
//exit, cause no need to generate the rest unless we are messing with source
return;
}
else
{
stream<<"{"<<std::endl;
}
//generate body of getter
stream<<m_indent<<m_indent<<"return "<<
it->first.second.toStdString()<<";"<<std::endl;
//end it
stream<<m_indent<<"}"<<std::endl;
}
void MGen::genSet(MapType::const_iterator & it, std::ofstream & stream, bool headeronly)
{
//generate Type setVarName(Type a);
stream<<m_indent<<"void "<<(headeronly?"":this->filename+"::")
<<"set"
<<it->first.second.toStdString()<<"("
<<it->second.toStdString()
<<" a"
<<")";
//Now depending on whether we are generating header or not
if(headeronly)
{
stream<<";"<<std::endl<<std::endl;
return;
}
else
{
stream<<"{"<<std::endl;
}
//generate body of setter
// If the input is a tableview, then it has to have the same number of elements on each line
if( (it->second).compare( "std::vector<std::vector<QString> >" ) == 0 )
{
stream<<m_indent<<m_indent<<"if( !a.empty() )"<<std::endl;
stream<<m_indent<<m_indent<<"{"<<std::endl;
stream<<m_indent<<m_indent<<m_indent<<"std::vector<std::vector<QString> >::iterator it = a.begin(); "<<std::endl;
stream<<m_indent<<m_indent<<m_indent<<"std::vector<std::vector<QString> >::size_type first_size = a[0].size();"<<std::endl;
stream<<m_indent<<m_indent<<m_indent<<"for( ; it != a.end() ; ++it )"<<std::endl;
stream<<m_indent<<m_indent<<m_indent<<"{"<<std::endl;
stream<<m_indent<<m_indent<<m_indent<<m_indent<<"if( it->size() != first_size )"<<std::endl;
stream<<m_indent<<m_indent<<m_indent<<m_indent<<"{"<<std::endl;
stream<<m_indent<<m_indent<<m_indent<<m_indent<<m_indent<<"throw std::runtime_error(\"Failed: All rows do not have the same size\");"<<std::endl;
stream<<m_indent<<m_indent<<m_indent<<m_indent<<"}"<<std::endl;
stream<<m_indent<<m_indent<<m_indent<<"}"<<std::endl;
stream<<m_indent<<m_indent<<"}"<<std::endl;
}
stream<<m_indent<<m_indent<<
it->first.second.toStdString()<<"=a;"
<<std::endl;
//end it
stream<<m_indent<<"}"<<std::endl;
}