GCC Code Coverage Report


Directory: ./
File: src/PClassGenerator.cpp
Date: 2025-04-25 19:10:50
Exec Total Coverage
Lines: 0 179 0.0%
Branches: 0 302 0.0%

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