GCC Code Coverage Report


Directory: ./
File: tmp_project/PhoenixGenerator/src/PClassGenerator.cpp
Date: 2025-04-25 19:19:43
Exec Total Coverage
Lines: 189 194 97.4%
Branches: 150 181 82.9%

Line Branch Exec Source
1
2 /***************************************
3 Auteur : Pierre Aubert
4 Mail : pierre.aubert@lapp.in2p3.fr
5 Licence : CeCILL-C
6 ****************************************/
7
8 #include "PClassGenerator.h"
9 #include "header_generator.h"
10
11 ///Constructor of PClassGenerator
12
5/5
✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 8 taken 3 times.
✓ Branch 11 taken 3 times.
✓ Branch 14 taken 3 times.
3 PClassGenerator::PClassGenerator(){
13
1/1
✓ Branch 1 taken 3 times.
3 initialisationPClassGenerator();
14 3 }
15
16 ///Destructor of PClassGenerator
17 6 PClassGenerator::~PClassGenerator(){
18
19 }
20
21 ///Saves both files include and sources of the class
22 /** @param baseFileName : base of the files names to write
23 * @return true on success, false otherwise
24 */
25 3 bool PClassGenerator::saveFileImplDef(const PPath & baseFileName){
26
2/2
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
3 PPath baseToUse("");
27
4/4
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
3 if(baseFileName == ""){baseToUse = p_className;}
28
1/1
✓ Branch 1 taken 2 times.
2 else{baseToUse = baseFileName;}
29
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
3 if(baseToUse == ""){return false;}
30
5/5
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
4 PPath includeName(baseToUse + PPath(".h"));
31
5/5
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
4 PPath sourceName(baseToUse + PPath(".cpp"));
32
6/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 9 taken 1 times.
✓ Branch 12 taken 1 times.
2 if(p_useTemplate){sourceName = PString(baseToUse + "_impl.h");}
33
1/1
✓ Branch 1 taken 2 times.
2 bool b(saveFileDef(includeName, sourceName));
34
1/1
✓ Branch 1 taken 2 times.
2 b &= saveFileImpl(sourceName, includeName);
35 2 return b;
36 3 }
37
38 ///Saves class definition
39 /** @param fileNameInclude : file to write
40 * @param fileNameSource : file of source
41 * @return true on success, false otherwise
42 */
43 2 bool PClassGenerator::saveFileDef(const PPath & fileNameInclude, const PPath & fileNameSource){
44
1/1
✓ Branch 1 taken 2 times.
2 std::ofstream fs;
45
1/1
✓ Branch 2 taken 2 times.
2 fs.open(fileNameInclude.c_str());
46
2/3
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if(!fs.is_open()){
47 std::cerr << "PClassGenerator::saveFileDef : can't open file '" << fileNameInclude << "'" << std::endl;
48 return false;
49 }
50
1/1
✓ Branch 1 taken 2 times.
2 licenceSave(fs);
51
1/1
✓ Branch 1 taken 2 times.
2 PString macroDef(makeMultiIncludeDefineMacro(fileNameInclude));
52
3/3
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
2 fs << "#ifndef " << macroDef << std::endl;
53
5/5
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
2 fs << "#define " << macroDef << std::endl << std::endl << std::endl;
54
1/1
✓ Branch 1 taken 2 times.
2 bool b = saveClassDef(fs);
55
1/1
✓ Branch 1 taken 2 times.
2 saveTemplateEndDefinition(fs, fileNameSource);
56
5/5
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
2 fs << std::endl << std::endl << "#endif" << std::endl << std::endl;
57
1/1
✓ Branch 1 taken 2 times.
2 fs.close();
58 2 return b;
59 2 }
60
61 ///Saves class implementation
62 /** @param fileNameSource : file to write
63 * @param fileNameInclude : include file name
64 * @return true on success, false otherwise
65 */
66 2 bool PClassGenerator::saveFileImpl(const PPath & fileNameSource, const PPath & fileNameInclude){
67
1/1
✓ Branch 1 taken 2 times.
2 std::ofstream fs;
68
1/1
✓ Branch 2 taken 2 times.
2 fs.open(fileNameSource.c_str());
69
2/3
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if(!fs.is_open()){
70 std::cerr << "PClassGenerator::saveFileImpl : can't open file '" << fileNameSource << "'" << std::endl;
71 return false;
72 }
73
1/1
✓ Branch 1 taken 2 times.
2 licenceSave(fs);
74
1/1
✓ Branch 1 taken 2 times.
2 saveTemplateBeginImpl(fs, fileNameInclude);
75
6/6
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 16 taken 2 times.
2 fs << std::endl << "#include \"" << fileNameInclude << "\"" << std::endl << std::endl;
76
1/1
✓ Branch 1 taken 2 times.
2 bool b = saveClassImpl(fs);
77
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 fs << std::endl << std::endl;
78
1/1
✓ Branch 1 taken 2 times.
2 saveTemplateEndImpl(fs);
79
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 fs << std::endl << std::endl;
80
1/1
✓ Branch 1 taken 2 times.
2 fs.close();
81 2 return b;
82 2 }
83
84 ///Saves class definition
85 /** @param fs : file to write
86 * @return true on success, false otherwise
87 */
88 2 bool PClassGenerator::saveClassDef(std::ofstream & fs){
89
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if(p_classBrief != "") fs << "///@brief " << p_classBrief << std::endl;
90 2 saveTemplateDefinition(fs);
91 2 fs << "class " << p_className << "{" << std::endl;
92 2 fs << "\tpublic:" << std::endl;
93 2 saveConstructorDef(fs);
94 2 saveCopyContructorDef(fs);
95 2 saveDestructorDef(fs);
96
97 2 saveCopyEqualOperatorDef(fs);
98 2 fs << "\tprotected:" << std::endl;
99 2 saveCopyFunctionDef(fs);
100 2 fs << "\tprivate:" << std::endl;
101 2 saveInitialisationFunctionDef(fs);
102 2 fs << "};" << std::endl << std::endl;;
103 2 return true;
104 }
105
106 ///Saves class implementation
107 /** @param fs : file to write
108 * @return true on success, false otherwise
109 */
110 2 bool PClassGenerator::saveClassImpl(std::ofstream & fs){
111 2 saveConstructorImpl(fs);
112 2 saveCopyContructorImpl(fs);
113 2 saveDestructorImpl(fs);
114
115 2 saveCopyEqualOperatorImpl(fs);
116
117 2 saveCopyFunctionImpl(fs);
118
119 2 saveInitialisationFunctionImpl(fs);
120 2 return true;
121
122 }
123
124 ///Set the class name
125 /** @param className : class name
126 */
127 3 void PClassGenerator::setClassName(const PString & className){
128 3 p_className = className;
129 3 p_classNameSpace = className;
130 3 p_classTypeName = className;
131 3 }
132
133 ///Set the template definition
134 /** @param templateDef : template definition
135 */
136 3 void PClassGenerator::setTemplateDefVar(const PString & templateDef){
137
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
3 if(templateDef == ""){
138
1/1
✓ Branch 1 taken 2 times.
2 p_templateDefVar = "";
139
1/1
✓ Branch 1 taken 2 times.
2 p_templateListVar = "";
140 2 p_useTemplate = false;
141 2 return;
142 }
143
1/1
✓ Branch 1 taken 1 times.
1 p_templateDefVar = templateDef;
144
1/1
✓ Branch 1 taken 1 times.
1 PVecString listDef(templateDef.split(','));
145
1/1
✓ Branch 1 taken 1 times.
1 p_templateListVar = "";
146 1 bool firstDef(true);
147
2/2
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
2 for(PVecString::iterator it(listDef.begin()); it != listDef.end(); ++it){
148
1/1
✓ Branch 2 taken 1 times.
1 PVecString partDef(it->split(' '));
149
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if(partDef.size() == 2lu){
150
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if(firstDef){
151
1/1
✓ Branch 2 taken 1 times.
1 p_templateListVar += partDef.back();
152 1 firstDef = false;
153 }else{
154 p_templateListVar += ", " + partDef.back();
155 }
156 }
157 1 }
158 1 p_useTemplate = true;
159
4/4
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 10 taken 1 times.
1 p_classTypeName = p_className + "<" + p_templateListVar + ">";
160
4/4
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 10 taken 1 times.
1 p_classNameSpace = p_className + "<" + p_templateListVar + ">";
161 1 }
162
163 ///Initialisation function of PClassGenerator
164 3 void PClassGenerator::initialisationPClassGenerator(){
165 3 p_templateDefVar = "";
166 3 p_templateListVar = "";
167 3 p_useCopyFunction = true;
168 3 p_useTemplate = false;
169 3 p_classTypeName = "";
170 3 }
171
172 ///Saves the template defition
173 /** @param fs : file in witch to write
174 */
175 14 void PClassGenerator::saveTemplateDefinition(std::ofstream & fs){
176
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
14 if(!p_useTemplate) return;
177 7 fs << "template<" << p_templateDefVar << ">" << std::endl;
178 }
179
180 ///Saves the template defition
181 /** @param fs : file in witch to write
182 * @param fileNameSource : source file
183 */
184 2 void PClassGenerator::saveTemplateEndDefinition(std::ofstream & fs, const PPath & fileNameSource){
185
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if(!p_useTemplate) return;
186 1 fs << "#include \"" << fileNameSource << "\"" << std::endl;
187 }
188
189 ///Saves class implementation
190 /** @param fs : file to write
191 * @param fileNameInclude : file to write
192 */
193 2 void PClassGenerator::saveTemplateBeginImpl(std::ofstream & fs, const PPath & fileNameInclude){
194
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if(!p_useTemplate) return;
195
7/7
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
✓ Branch 19 taken 1 times.
2 PString macroDef("__"+fileNameInclude.replace(".", "_").toUpper()+"_IMPL__");
196
3/3
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
1 fs << "#ifndef " << macroDef << std::endl;
197
5/5
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
1 fs << "#define " << macroDef << std::endl << std::endl << std::endl;
198 1 }
199
200 ///Saves class implementation template end
201 /** @param fs : file to write
202 */
203 2 void PClassGenerator::saveTemplateEndImpl(std::ofstream & fs){
204
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if(!p_useTemplate) return;
205 1 fs << std::endl << std::endl << "#endif" << std::endl << std::endl;
206
207 }
208
209 ///Saves the defition of the constructor
210 /** @param fs : file in witch to write
211 */
212 2 void PClassGenerator::saveConstructorDef(std::ofstream & fs){
213 2 fs << "\t\t" << p_className << "();" << std::endl;
214 2 }
215
216 ///Saves the implementation of the constructor
217 /** @param fs : file in witch to write
218 */
219 2 void PClassGenerator::saveConstructorImpl(std::ofstream & fs){
220 2 fs << "///Default constructor of " << p_className << std::endl;
221 2 saveTemplateDefinition(fs);
222 2 fs << p_classNameSpace << "::" << p_className << "(){" << std::endl;
223
3/3
✓ Branch 3 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
2 fs << "\tinitialisation" << p_className.firstToUpper() << "();" << std::endl;
224 2 fs << "}" << std::endl;
225 2 fs << std::endl;
226 2 }
227
228 ///Saves the defition of the destructor
229 /** @param fs : file in witch to write
230 */
231 2 void PClassGenerator::saveDestructorDef(std::ofstream & fs){
232 2 fs << "\t\tvirtual ~" << p_className << "();" << std::endl;
233 2 }
234
235 ///Saves the implementation of the destructor
236 /** @param fs : file in witch to write
237 */
238 2 void PClassGenerator::saveDestructorImpl(std::ofstream & fs){
239 2 fs << "///Destructor of " << p_className << std::endl;
240 2 saveTemplateDefinition(fs);
241 2 fs << p_classNameSpace << "::~" << p_className << "(){" << std::endl;
242 2 fs << "\t" << std::endl;
243 2 fs << "}" << std::endl;
244 2 fs << std::endl;
245 2 }
246
247 ///Saves the definition of the initialisation function
248 /** @param fs : file in witch to write
249 */
250 2 void PClassGenerator::saveInitialisationFunctionDef(std::ofstream & fs){
251
3/3
✓ Branch 3 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
2 fs << "\t\tvoid initialisation" << p_className.firstToUpper() << "();" << std::endl;
252 2 }
253
254 ///Saves the implementation of the initialisation function
255 /** @param fs : file in witch to write
256 */
257 2 void PClassGenerator::saveInitialisationFunctionImpl(std::ofstream & fs){
258 2 fs << "///Initialisation function of the class " << p_className << std::endl;
259 2 saveTemplateDefinition(fs);
260
3/3
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
✓ Branch 11 taken 2 times.
2 fs << "void " << p_classNameSpace << "::initialisation" << p_className.firstToUpper() << "(){" << std::endl;
261 2 fs << "\t" << std::endl;
262 2 fs << "}" << std::endl;
263 2 fs << std::endl;
264 2 }
265
266 ///Saves the defition of the copy constructor
267 /** @param fs : file in witch to write
268 */
269 2 void PClassGenerator::saveCopyContructorDef(std::ofstream & fs){
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if(!p_useCopyFunction) return;
271 2 fs << "\t\t" << p_className << "(const " << p_classTypeName << " & other);" << std::endl;
272 }
273
274 ///Saves the implementation of the copy constructor
275 /** @param fs : file in witch to write
276 */
277 2 void PClassGenerator::saveCopyContructorImpl(std::ofstream & fs){
278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if(!p_useCopyFunction) return;
279 2 fs << "///Copy constructor of " << p_className << std::endl;
280 2 fs << "/**\t@param other : class to copy" << std::endl;
281 2 fs << "*/" << std::endl;
282 2 saveTemplateDefinition(fs);
283 2 fs << p_classNameSpace << "::" << p_className << "(const " << p_classTypeName << " & other){" << std::endl;
284
3/3
✓ Branch 3 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
2 fs << "\tcopy" << p_className.firstToUpper() << "(other);" << std::endl;
285 2 fs << "}" << std::endl;
286 2 fs << std::endl;
287 }
288
289 ///Saves the defition of the equal operator constructor
290 /** @param fs : file in witch to write
291 */
292 2 void PClassGenerator::saveCopyEqualOperatorDef(std::ofstream & fs){
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if(!p_useCopyFunction) return;
294 2 fs << "\t\t" << p_className << " & operator = (const " << p_classTypeName << " & other);" << std::endl;
295 }
296
297 ///Saves the implementation of the equal operator constructor
298 /** @param fs : file in witch to write
299 */
300 2 void PClassGenerator::saveCopyEqualOperatorImpl(std::ofstream & fs){
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if(!p_useCopyFunction) return;
302 2 fs << "///Definition of equal operator of " << p_className << std::endl;
303 2 fs << "/**\t@param other : class to copy" << std::endl;
304 2 fs << " * \t@return copied class" << std::endl;
305 2 fs << "*/" << std::endl;
306 2 saveTemplateDefinition(fs);
307 // if(p_useTemplate) fs << "typename " << p_classNameSpace << "::";
308 2 fs << p_classTypeName << " & " << p_classNameSpace << "::operator = (const " << p_classTypeName << " & other){" << std::endl;
309
3/3
✓ Branch 3 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
2 fs << "\tcopy" << p_className.firstToUpper() << "(other);" << std::endl;
310 2 fs << "\treturn *this;" << std::endl;
311 2 fs << "}" << std::endl;
312 2 fs << std::endl;
313 }
314
315 ///Saves the defition of the copy function
316 /** @param fs : file in witch to write
317 */
318 2 void PClassGenerator::saveCopyFunctionDef(std::ofstream & fs){
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if(!p_useCopyFunction) return;
320
5/5
✓ Branch 3 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 9 taken 2 times.
✓ Branch 12 taken 2 times.
✓ Branch 15 taken 2 times.
2 fs << "\t\tvoid copy" << p_className.firstToUpper() << "(const " << p_classTypeName << " & other);" << std::endl;
321 }
322
323 ///Saves the implementation of the copy function
324 /** @param fs : file in witch to write
325 */
326 2 void PClassGenerator::saveCopyFunctionImpl(std::ofstream & fs){
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if(!p_useCopyFunction) return;
328 2 fs << "///Copy function of " << p_className << std::endl;
329 2 fs << "/**\t@param other : class to copy" << std::endl;
330 2 fs << "*/" << std::endl;
331 2 saveTemplateDefinition(fs);
332
5/5
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
✓ Branch 11 taken 2 times.
✓ Branch 14 taken 2 times.
✓ Branch 17 taken 2 times.
2 fs << "void " << p_classNameSpace << "::copy" << p_className.firstToUpper() << "(const " << p_classTypeName << " & other){" << std::endl;
333 2 fs << "\t" << std::endl;
334 2 fs << "}" << std::endl;
335 2 fs << std::endl;
336 }
337
338
339
340