PhoenixGenerator  2.0.0
Set of tools to generate code
PClassGenerator.cpp
Go to the documentation of this file.
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 
14 }
15 
18 
19 }
20 
22 
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 
39 
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 
62 
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;
79  fs << std::endl << std::endl;
80  fs.close();
81  return b;
82 }
83 
85 
88 bool PClassGenerator::saveClassDef(std::ofstream & fs){
89  if(p_classBrief != "") fs << "///@brief " << p_classBrief << std::endl;
91  fs << "class " << p_className << "{" << std::endl;
92  fs << "\tpublic:" << std::endl;
96 
98  fs << "\tprotected:" << std::endl;
100  fs << "\tprivate:" << std::endl;
102  fs << "};" << std::endl << std::endl;;
103  return true;
104 }
105 
107 
110 bool PClassGenerator::saveClassImpl(std::ofstream & fs){
113  saveDestructorImpl(fs);
114 
116 
118 
120  return true;
121 
122 }
123 
125 
127 void PClassGenerator::setClassName(const PString & className){
128  p_className = className;
129  p_classNameSpace = className;
130  p_classTypeName = className;
131 }
132 
134 
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;
161 }
162 
165  p_templateDefVar = "";
166  p_templateListVar = "";
167  p_useCopyFunction = true;
168  p_useTemplate = false;
169  p_classTypeName = "";
170 }
171 
173 
176  if(!p_useTemplate) return;
177  fs << "template<" << p_templateDefVar << ">" << std::endl;
178 }
179 
181 
184 void PClassGenerator::saveTemplateEndDefinition(std::ofstream & fs, const PPath & fileNameSource){
185  if(!p_useTemplate) return;
186  fs << "#include \"" << fileNameSource << "\"" << std::endl;
187 }
188 
190 
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 
201 
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 
210 
212 void PClassGenerator::saveConstructorDef(std::ofstream & fs){
213  fs << "\t\t" << p_className << "();" << std::endl;
214 }
215 
217 
219 void PClassGenerator::saveConstructorImpl(std::ofstream & fs){
220  fs << "///Default constructor of " << p_className << std::endl;
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 
229 
231 void PClassGenerator::saveDestructorDef(std::ofstream & fs){
232  fs << "\t\tvirtual ~" << p_className << "();" << std::endl;
233 }
234 
236 
238 void PClassGenerator::saveDestructorImpl(std::ofstream & fs){
239  fs << "///Destructor of " << p_className << std::endl;
241  fs << p_classNameSpace << "::~" << p_className << "(){" << std::endl;
242  fs << "\t" << std::endl;
243  fs << "}" << std::endl;
244  fs << std::endl;
245 }
246 
248 
251  fs << "\t\tvoid initialisation" << p_className.firstToUpper() << "();" << std::endl;
252 }
253 
255 
258  fs << "///Initialisation function of the class " << p_className << std::endl;
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 
267 
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 
275 
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;
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 
290 
293  if(!p_useCopyFunction) return;
294  fs << "\t\t" << p_className << " & operator = (const " << p_classTypeName << " & other);" << std::endl;
295 }
296 
298 
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;
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 
316 
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 
324 
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;
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 
std::vector< PString > PVecString
Definition: PString.h:96
virtual ~PClassGenerator()
Destructor of PClassGenerator.
bool saveFileImplDef(const PPath &baseFileName)
Saves both files include and sources of the class.
bool saveClassImpl(std::ofstream &fs)
Saves class implementation.
PString p_className
Name of the class.
bool p_useTemplate
Says if we need to use template.
void setClassName(const PString &className)
Set the class name.
void saveCopyFunctionImpl(std::ofstream &fs)
Saves the implementation of the copy function.
void saveDestructorDef(std::ofstream &fs)
Saves the defition of the destructor.
PString p_classBrief
Brief description of the class.
void saveConstructorImpl(std::ofstream &fs)
Saves the implementation of the constructor.
void saveCopyContructorDef(std::ofstream &fs)
Saves the defition of the copy constructor.
PString p_templateListVar
Template list var.
void saveInitialisationFunctionDef(std::ofstream &fs)
Saves the definition of the initialisation function.
bool saveFileImpl(const PPath &fileNameSource, const PPath &fileNameInclude)
Saves class implementation.
bool p_useCopyFunction
True if we need to use a copy function.
void saveCopyFunctionDef(std::ofstream &fs)
Saves the defition of the copy function.
PString p_classNameSpace
Namespace of the class.
void saveTemplateBeginImpl(std::ofstream &fs, const PPath &fileNameInclude)
Saves class implementation.
bool saveFileDef(const PPath &fileNameInclude, const PPath &fileNameSource)
Saves class definition.
void saveConstructorDef(std::ofstream &fs)
Saves the defition of the constructor.
PClassGenerator()
Constructor of PClassGenerator.
void saveTemplateDefinition(std::ofstream &fs)
Saves the template defition.
void setTemplateDefVar(const PString &templateDef)
Set the template definition.
void saveCopyEqualOperatorImpl(std::ofstream &fs)
Saves the implementation of the equal operator constructor.
void saveCopyEqualOperatorDef(std::ofstream &fs)
Saves the defition of the equal operator constructor.
void saveInitialisationFunctionImpl(std::ofstream &fs)
Saves the implementation of the initialisation function.
void saveTemplateEndImpl(std::ofstream &fs)
Saves class implementation template end.
bool saveClassDef(std::ofstream &fs)
Saves class definition.
void saveTemplateEndDefinition(std::ofstream &fs, const PPath &fileNameSource)
Saves the template defition.
PString p_templateDefVar
Template def var.
void saveDestructorImpl(std::ofstream &fs)
Saves the implementation of the destructor.
void initialisationPClassGenerator()
Initialisation function of PClassGenerator.
PString p_classTypeName
Name of the class type (with template if there is one)
void saveCopyContructorImpl(std::ofstream &fs)
Saves the implementation of the copy constructor.
Path of a directory or a file.
Definition: PPath.h:17
Extends the std::string.
Definition: PString.h:16
PString replace(const PString &pattern, const PString &replaceStr) const
Replace a PString into an other PString.
Definition: PString.cpp:204
std::vector< PString > split(char separator) const
Cut a PString on the given separator char.
Definition: PString.cpp:420
PString toUpper() const
Convert std::string in upper case.
Definition: PString.cpp:639
PString firstToUpper() const
Convert first letter of the PString in upper case.
Definition: PString.cpp:673
PString makeMultiIncludeDefineMacro(const PString &fileName)
Create the macro of multi inclusion file name.
void licenceSave(std::ofstream &fs)
Saves the policy.