PhoenixGenerator  2.0.0
Set of tools to generate code
saveClassConfig.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 "header_generator.h"
9 #include "saveClassConfig.h"
10 
12 
15 bool getIsSimpleType(const PString & varType){
16  PString restVarName(varType.replace("unsigned", "").eraseChar(" \n\t*&"));
17  return (restVarName == "char" || restVarName == "short" || restVarName == "int" || restVarName == "float" || restVarName == "double" || restVarName == "bool" || restVarName == "long" || restVarName == "longint" || restVarName == "size_t" || restVarName == "ssize_t" || restVarName == "void");
18 }
19 
21 
24 bool getIsPointer(const PString & varType){
25  if(varType.size() == 0lu){return false;}
26  return varType.find('*') && varType.back() == '*';
27 }
28 
30 
34  if(type == "PString"){
35  return "\"\"";
36  }else if(type == "char"){
37  return "0";
38  }else if(type == "unsigned char"){
39  return "0";
40  }else if(type == "int" || type == "short" || type == "unsigned short"){
41  return "0";
42  }else if(type == "unsigned int"){
43  return "0u";
44  }else if(type == "long int" || type == "long" || type == "ssize_t"){
45  return "0l";
46  }else if(type == "size_t" || type == "long unsigned int"){
47  return "0lu";
48  }else if(type == "bool"){
49  return "false";
50  }else{
51  return "";
52  }
53 }
54 
56 
63 PString makeVarType(const PString & varType, bool isSetter, bool isConst, bool isRef, bool isPtr){
64  PString varTypeName("");
65 // std::cout << "makeVarType : varType = '" << varType << "'" << std::endl;
66  bool isSimpleType = getIsSimpleType(varType);
67  if(isSimpleType || isPtr){
68  if(isConst && !isSetter && (!isSimpleType || isPtr)) varTypeName += "const ";
69  varTypeName += varType;
70  if(isRef && !isPtr) varTypeName += " &";
71  }else{
72  if(isConst && !isPtr) varTypeName += "const ";
73  varTypeName += varType + " &";
74  }
75  return varTypeName;
76 }
77 
79 
85 PString createSetterDecl(const PString & varType, const PString & varName, const PString & className, bool isPtr){
86  PString functionDecl("void ");
87  if(className != "") functionDecl += className + "::";
88  functionDecl += "set" + varName.firstToUpper() + "(";
89  functionDecl += makeVarType(varType, true, true, false, isPtr);
90  functionDecl += " " + varName + ")";
91  return functionDecl;
92 }
93 
95 
102 PString createGetterDecl(const PString & varType, const PString & varName, const PString & className, bool isConst, bool isPtr){
103  PString functionDecl("");
104  functionDecl += makeVarType(varType, false, isConst, !isConst, isPtr) + " ";
105  if(className != "") functionDecl += className + "::";
106  functionDecl += "get" + varName.firstToUpper() + "()";
107  return functionDecl;
108 }
109 
111 
114 void saveDeclSetters(std::ofstream & fs, const PClassConfig & classConfig){
115  const std::vector<PClassAttribute> & listAttr(classConfig.getListAttribute());
116  for(std::vector<PClassAttribute>::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
117  fs << "\t\t" << createSetterDecl(it->getType(), it->getName(), "", it->getIsPointer()) << ";" << std::endl;
118  }
119 }
120 
122 
125 void saveDeclGetters(std::ofstream & fs, const PClassConfig & classConfig){
126  const std::vector<PClassAttribute> & listAttr(classConfig.getListAttribute());
127  for(std::vector<PClassAttribute>::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
128  fs << "\t\t" << createGetterDecl(it->getType(), it->getName(), "", true, it->getIsPointer()) << " const;" << std::endl;
129  fs << "\t\t" << createGetterDecl(it->getType(), it->getName(), "", false, it->getIsPointer()) << ";" << std::endl;
130  }
131 }
132 
134 
137 void saveClassDataStreamMethod(std::ofstream & fs, const PClassConfig & classConfig){
138  PString name(classConfig.getName());
139  fs << std::endl;
140  fs << "\t\t///Load the current "<<name<<" with a stream" << std::endl;
141  fs << "\t\t/** @param[out] ds : stream to be used" << std::endl;
142  fs << "\t\t * @return true on success, false otherwise" << std::endl;
143  fs << "\t\t*/" << std::endl;
144  fs << "\t\ttemplate<typename Stream, DataStreamMode::DataStreamMode Mode>" << std::endl;
145  fs << "\t\tbool readWriteStream(Stream & ds){" << std::endl;
146  fs << "\t\t\tbool b(true);" << std::endl;
147 
148  const std::vector<PClassAttribute> & listAttr(classConfig.getListAttribute());
149  for(std::vector<PClassAttribute>::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
150  fs << "\t\t\tb &= DataStream<Stream, Mode, "<<it->getType()<<" >::data_stream(ds, p_" << it->getName() << ");" << std::endl;
151  }
152 
153  fs << "\t\t\treturn b;" << std::endl;
154  fs << "\t\t}" << std::endl << std::endl;
155 }
156 
158 
161 void saveClassDataStreamGenericFunction(std::ofstream & fs, const PClassConfig & classConfig){
162  PString name(classConfig.getName());
163  fs << "///@brief Generic "<<name<<" serialisation/deserialisation, load/save and size function" << std::endl;
164  fs << "template<typename Stream, DataStreamMode::DataStreamMode Mode>" << std::endl;
165  fs << "struct DataStream<Stream, Mode, "<<name<<">{" << std::endl;
166  fs << "\t///Generic function to load/save/serialise/deserialise "<<name << std::endl;
167  fs << "\t/**\t@param data : "<<name<<" to be used" << std::endl;
168  fs << "\t * \t@return true on success, false otherwise" << std::endl;
169  fs << "\t*/" << std::endl;
170  fs << "\tstatic bool data_stream(Stream & ds, "<<name<<" & data){" << std::endl;
171  fs << "\t\treturn data.readWriteStream<Stream, Mode>(ds);" << std::endl;
172  fs << "\t}" << std::endl;
173  fs << "};" << std::endl << std::endl;
174 }
175 
177 
181  if(listTemplate.size() == 0lu){return "";}
182  PString templateDef("template<");
183  PString comma("");
184  for(PVecString::const_iterator it(listTemplate.begin()); it != listTemplate.end(); ++it){
185  templateDef += comma;
186  templateDef += *it;
187  comma = ", ";
188  }
189  templateDef += ">\n";
190  return templateDef;
191 }
192 
194 
197 PString getClassDefTemplate(const PVecString & listTemplate){
198  PString defTemplate("<"), comma("");
199  for(PVecString::const_iterator it(listTemplate.begin()); it != listTemplate.end(); ++it){
200  defTemplate += comma + it->split(' ').back();
201  comma = ", ";
202  }
203  defTemplate += ">";
204  return defTemplate;
205 }
206 
208 
213 void saveEnumDecl(std::ofstream & fs, const PClassConfig & classConfig, bool enableDataStream, bool enableTypeStream){
214  PString name(classConfig.getName());
215  fs << "namespace " << name << " {\n";
216  if(classConfig.getClassDocumentation() != "") fs << classConfig.getClassDocumentation() << std::endl;
217  fs << "\tenum " << name << " {\n\t\t";
218  PString comma("");
219  const std::vector<PClassAttribute> & listAttr(classConfig.getListAttribute());
220  for(std::vector<PClassAttribute>::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
221  if(it->getDocumentation() != "") fs << "\t\t" << it->getDocumentation() << std::endl;
222  fs << comma << it->getName();
223  if(it->getDefaultValue() != ""){
224  fs << " = " << it->getDefaultValue();
225  }
226  comma = ",\n\t\t";
227  }
228  fs << "\n\t};" << std::endl;
229  fs << "}" << std::endl << std::endl;
230 
231  if(enableDataStream){
232  fs << "///@brief Generic "<<name << " deserialisation, load and size function for a "<<name << "::"<<name << "" << std::endl;
233  fs << "template<typename Stream>" << std::endl;
234  fs << "struct DataStream<Stream, DataStreamMode::READ, "<<name << "::"<<name << ">{" << std::endl;
235  fs << "\t///Generic function to load/deserialise "<<name << "" << std::endl;
236  fs << "\t/**\t@param[out] ds : Stream to be used" << std::endl;
237  fs << "\t * \t@param data : "<<name << " to be loaded" << std::endl;
238  fs << "\t * \t@return true on success, false otherwise" << std::endl;
239  fs << "\t*/" << std::endl;
240  fs << "\tstatic bool data_stream(Stream & ds, "<<name << "::"<<name << " & data){" << std::endl;
241  fs << "\t\tint value(0);" << std::endl;
242  fs << "\t\tbool b = DataStream<Stream, DataStreamMode::READ, int>::data_stream(ds, value);" << std::endl;
243  fs << "\t\tdata = ("<<name << "::"<<name << ")value;" << std::endl;
244  fs << "\t\treturn b;" << std::endl;
245  fs << "\t}" << std::endl;
246  fs << "};" << std::endl << std::endl;
247 
248  fs << "///@brief Generic "<<name << " serialisation, save and size function for a "<<name << "::"<<name << "" << std::endl;
249  fs << "template<typename Stream>" << std::endl;
250  fs << "struct DataStream<Stream, DataStreamMode::WRITE, "<<name << "::"<<name << ">{" << std::endl;
251  fs << "\t///Generic function to save/serialise "<<name << "" << std::endl;
252  fs << "\t/**\t@param[out] ds : Stream to be used" << std::endl;
253  fs << "\t * \t@param data : "<<name << " to be saved" << std::endl;
254  fs << "\t * \t@return true on success, false otherwise" << std::endl;
255  fs << "\t*/" << std::endl;
256  fs << "\tstatic bool data_stream(Stream & ds, "<<name << "::"<<name << " & data){" << std::endl;
257  fs << "\t\tint value = (int)data;" << std::endl;
258  fs << "\t\treturn DataStream<Stream, DataStreamMode::WRITE, int>::data_stream(ds, value);" << std::endl;
259  fs << "\t}" << std::endl;
260  fs << "};" << std::endl << std::endl;
261  }
262  if(enableTypeStream){
263  fs << "///Get the type name of "<<name << std::endl;
264  fs << "/**\t@return type name of "<<name << " as string" << std::endl;
265  fs << "*/" << std::endl;
266  fs << "template<>" << std::endl;
267  fs << "std::string phoenix_getTypeName<"<<name<<"::"<<name<<">(){" << std::endl;
268  fs << "\treturn \""<<name<<"\";" << std::endl;
269  fs << "}" << std::endl << std::endl;
270  }
271 }
272 
274 
279 void saveClassDecl(std::ofstream & fs, const PClassConfig & classConfig, bool enableDataStream, bool enableTypeStream){
280  if(classConfig.getClassDocumentation() != "") fs << classConfig.getClassDocumentation() << std::endl;
281  PString name(classConfig.getName());
282  const PVecString & listTemplate = classConfig.getListTemplate();
283  if(listTemplate.size() != 0lu){
284  fs << getClassDeclTempalteDef(listTemplate);
285  }
286  fs << "class " << name;
287  if(classConfig.getListParentClass().size() != 0lu){
288  PVecString::const_iterator it(classConfig.getListParentClass().begin());
289  fs << " : public " << *it;
290  if(classConfig.getListParentClass().size() > 1lu){
291  while(it != classConfig.getListParentClass().end()){
292  fs << ", " << *it;
293  ++it;
294  }
295  }
296  }
297  fs << "{" << std::endl;
298  fs << "\tpublic:" << std::endl;
299  fs << "\t\t" << name << "();" << std::endl;
300  fs << "\t\t" << name << "(const " << name << " & other);" << std::endl;
301  fs << "\t\tvirtual ~" << name << "();" << std::endl;
302  fs << "\t\t" << name << " & operator = (const " << name << " & other);" << std::endl;
303  saveDeclSetters(fs, classConfig);
304  saveDeclGetters(fs, classConfig);
305 
306  if(enableDataStream){
307  saveClassDataStreamMethod(fs, classConfig);
308  }
309 
310  fs << "\tprotected:" << std::endl;
311  fs << "\t\tvoid copy" << name << "(const " << name << " & other);" << std::endl;
312 
313  fs << "\tprivate:" << std::endl;
314  fs << "\t\tvoid initialisation" << name << "();" << std::endl;
315  const std::vector<PClassAttribute> & listAttr(classConfig.getListAttribute());
316  for(std::vector<PClassAttribute>::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
317  if(it->getDocumentation() != "") fs << "\t\t" << it->getDocumentation() << std::endl;
318 
319  fs << "\t\t" << it->getType() << " p_" << it->getName() << ";" << std::endl;
320  }
321  fs << "};" << std::endl << std::endl;
322 
323  if(enableDataStream){
324  saveClassDataStreamGenericFunction(fs, classConfig);
325  fs << std::endl << std::endl;
326  }
327  if(enableTypeStream){
328  if(listTemplate.size() != 0lu){
329  fs << "///Get the name of the class " << name << std::endl;
330  fs << "/**\t@return name of the class " << name << std::endl;
331  fs << "*/" << std::endl;
332  fs << getClassDeclTempalteDef(listTemplate);
333  fs << "std::string phoenix_getTypeName<"<<name << getClassDefTemplate(listTemplate) <<" >(){" << std::endl;
334  fs << "\treturn \""<<name<<"\"";
335  fs << " + \"<\"";
336  for(PVecString::const_iterator it(listTemplate.begin()); it != listTemplate.end(); ++it){
337  fs << " + phoenix_getTypeToStr<"<<it->split(' ').back()<<">()";
338  }
339  fs << " + \">\"";
340  fs << ";" << std::endl;
341  fs << "}" << std::endl << std::endl;
342 
343  }else{
344  fs << "template<>" << std::endl;
345  fs << "std::string phoenix_getTypeName<"<<name<<">();" << std::endl << std::endl;
346  }
347  }
348 }
349 
351 
357 void saveClassGetTypeNameImpl(std::ofstream & fs, const PClassConfig & classConfig, const PString & defTemplate, const PString & templateDeclaration, bool enableTypeStream){
358  if(!enableTypeStream){return;}
359  PString name(classConfig.getName());
360  fs << "///Get the name of the class " << name << std::endl;
361  fs << "/**\t@return name of the class " << name << std::endl;
362  fs << "*/" << std::endl;
363  const PVecString & listTemplate = classConfig.getListTemplate();
364  if(listTemplate.size() == 0lu){
365  fs << "template<>" << std::endl;
366  }else{
367  fs << templateDeclaration;
368  }
369  fs << "std::string phoenix_getTypeName<"<<name << defTemplate <<" >(){" << std::endl;
370  fs << "\treturn \""<<name<<"\"";
371  if(listTemplate.size() != 0lu){
372  fs << " + \"<\"";
373  for(PVecString::const_iterator it(listTemplate.begin()); it != listTemplate.end(); ++it){
374  fs << " + phoenix_getTypeToStr<"<<it->split(' ').back()<<">()";
375  }
376  fs << " + \">\"";
377  }
378  fs << ";" << std::endl;
379  fs << "}" << std::endl << std::endl;
380 }
381 
383 
388 void saveClassConstructorImpl(std::ofstream & fs, const PClassConfig & classConfig, const PString & defTemplate, const PString & templateDeclaration){
389  PString name(classConfig.getName());
390  fs << "///Constructor of class " << name << std::endl;
391  fs << templateDeclaration;
392  fs << name << defTemplate << "::" << name << "()";
393  if(classConfig.getListParentClass().size() != 0lu){
394  PVecString::const_iterator it(classConfig.getListParentClass().begin());
395  fs << std::endl << "\t: " << *it << "()";
396  if(classConfig.getListParentClass().size() > 1lu){
397  while(it != classConfig.getListParentClass().end()){
398  fs << ", " << *it << "()";
399  ++it;
400  }
401  }
402  fs << std::endl;
403  }
404  fs << "{" << std::endl;
405  fs << "\tinitialisation" << name << "();" << std::endl;
406  fs << "}" << std::endl << std::endl;
407 }
408 
410 
415 void saveClassCopyConstructorImpl(std::ofstream & fs, const PClassConfig & classConfig, const PString & defTemplate, const PString & templateDeclaration){
416  PString name(classConfig.getName());
417  fs << "///Copy Constructor of class " << name << std::endl;
418  fs << "/**\t@param other : " << name << " we want ot copy" << std::endl;
419  fs << "*/" << std::endl;
420  fs << templateDeclaration;
421  fs << name << defTemplate << "::" << name << "(const " << name << defTemplate << " & other){" << std::endl;
422  fs << "\tcopy" << name << "(other);" << std::endl;
423  fs << "}" << std::endl << std::endl;
424 }
425 
427 
432 void saveClassDestructorImpl(std::ofstream & fs, const PClassConfig & classConfig, const PString & defTemplate, const PString & templateDeclaration){
433  PString name(classConfig.getName());
434  fs << "///Destructor of class " << name << std::endl;
435  fs << templateDeclaration;
436  fs << name << defTemplate << "::~" << name << "(){" << std::endl;
437  fs << "\t" << std::endl;
438  fs << "}" << std::endl << std::endl;
439 }
440 
442 
447 void saveClassEqualOperatorImpl(std::ofstream & fs, const PClassConfig & classConfig, const PString & defTemplate, const PString & templateDeclaration){
448  PString name(classConfig.getName());
449  fs << "///Operator = of class " << name << std::endl;
450  fs << "/**\t@param other : " << name << " we want ot copy" << std::endl;
451  fs << " * \t@return copied class " << name << std::endl;
452  fs << "*/" << std::endl;
453  fs << templateDeclaration;
454  fs << name << defTemplate << " & " << name << defTemplate << "::operator =" << " (const " << name << defTemplate << " & other){" << std::endl;
455  fs << "\tcopy" << name << "(other);" << std::endl;
456  fs << "\treturn *this;" << std::endl;
457  fs << "}" << std::endl << std::endl;
458 }
459 
461 
466 void saveClassSettersImpl(std::ofstream & fs, const PClassConfig & classConfig, const PString & defTemplate, const PString & templateDeclaration){
467  const std::vector<PClassAttribute> & listAttr(classConfig.getListAttribute());
468  for(std::vector<PClassAttribute>::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
469  fs << "///Sets the " << it->getName() << " of the " << classConfig.getName() << std::endl;
470  fs << "/**\t@param " << it->getName() << " : " << it->getName() << " of the " << classConfig.getName() << std::endl;
471  fs << "*/" << std::endl;
472  fs << templateDeclaration;
473  fs << createSetterDecl(it->getType(), it->getName(), classConfig.getName() + defTemplate, it->getIsPointer()) << "{" << std::endl;
474  fs << "\tp_" << it->getName() << " = " << it->getName() << ";" << std::endl;
475  fs << "}" << std::endl << std::endl;
476  }
477 }
478 
480 
485 void saveClassGettersImpl(std::ofstream & fs, const PClassConfig & classConfig, const PString & defTemplate, const PString & templateDeclaration){
486  const std::vector<PClassAttribute> & listAttr(classConfig.getListAttribute());
487  for(std::vector<PClassAttribute>::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
488  fs << "///Gets the " << it->getName() << " of the " << classConfig.getName() << std::endl;
489  fs << "/**\t@return " << it->getName() << " of the " << classConfig.getName() << std::endl;
490  fs << "*/" << std::endl;
491  fs << templateDeclaration;
492  fs << createGetterDecl(it->getType(), it->getName(), classConfig.getName() + defTemplate, true, it->getIsPointer()) << " const{" << std::endl;
493  fs << "\treturn p_" << it->getName() << ";" << std::endl;
494  fs << "}" << std::endl << std::endl;
495  fs << "///Gets the " << it->getName() << " of the " << classConfig.getName() << std::endl;
496  fs << "/**\t@return " << it->getName() << " of the " << classConfig.getName() << std::endl;
497  fs << "*/" << std::endl;
498  fs << templateDeclaration;
499  fs << createGetterDecl(it->getType(), it->getName(), classConfig.getName() + defTemplate, false, it->getIsPointer()) << "{" << std::endl;
500  fs << "\treturn p_" << it->getName() << ";" << std::endl;
501  fs << "}" << std::endl << std::endl;
502  }
503 }
504 
506 
511 void saveClassCopyFunctionImpl(std::ofstream & fs, const PClassConfig & classConfig, const PString & defTemplate, const PString & templateDeclaration){
512  PString name(classConfig.getName());
513  fs << "///Copy Function of class " << name << std::endl;
514  fs << "/**\t@param other : " << name << " we want ot copy" << std::endl;
515  fs << "*/" << std::endl;
516  fs << templateDeclaration;
517  fs << "void " << name << defTemplate << "::copy" << name << "(const " << name << defTemplate << " & other){" << std::endl;
518  const std::vector<PClassAttribute> & listAttr(classConfig.getListAttribute());
519  for(std::vector<PClassAttribute>::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
520  fs << "\tp_" << it->getName() << " = other.p_" << it->getName() << ";" << std::endl;
521  }
522  fs << "}" << std::endl << std::endl;
523 }
524 
526 
531 void saveClassInitialisationFunctionImpl(std::ofstream & fs, const PClassConfig & classConfig, const PString & defTemplate,
532  const PString & templateDeclaration)
533 {
534  PString name(classConfig.getName());
535  fs << "///Initialisation Function of class " << name << std::endl;
536  fs << templateDeclaration;
537  fs << "void " << name << defTemplate << "::initialisation" << name << "(){" << std::endl;
538  const std::vector<PClassAttribute> & listAttr(classConfig.getListAttribute());
539  for(std::vector<PClassAttribute>::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
540  if(it->getDefaultValue() != ""){
541  fs << "\tp_" << it->getName() << " = "<<it->getDefaultValue()<<";" << std::endl;
542  }else{
543  PString varType(it->getType());
544  bool isSimpleType = getIsSimpleType(varType);
545  bool isPtr(getIsPointer(varType));
546  if(isPtr){
547  fs << "\tp_" << it->getName() << " = NULL;" << std::endl;
548  }else if(isSimpleType){
549  PString defaultValue(getDefaultValueTypeInCpp(varType));
550  if(defaultValue != ""){
551  fs << "\tp_" << it->getName() << " = "<<defaultValue<<";" << std::endl;
552  }
553  }else if(varType == "PString"){
554  fs << "\tp_" << it->getName() << " = \"\";" << std::endl;
555  }
556  }
557  }
558  fs << "}" << std::endl << std::endl;
559 }
560 
562 
566 void saveClassImpl(std::ofstream & fs, const PClassConfig & classConfig, bool enableTypeStream){
567  saveClassGetTypeNameImpl(fs, classConfig, "", "", enableTypeStream);
568  saveClassConstructorImpl(fs, classConfig, "", "");
569  saveClassCopyConstructorImpl(fs, classConfig, "", "");
570  saveClassDestructorImpl(fs, classConfig, "", "");
571  saveClassEqualOperatorImpl(fs, classConfig, "", "");
572  saveClassSettersImpl(fs, classConfig, "", "");
573  saveClassGettersImpl(fs, classConfig, "", "");
574  saveClassCopyFunctionImpl(fs, classConfig, "", "");
575  saveClassInitialisationFunctionImpl(fs, classConfig, "", "");
576 }
577 
579 
587 bool saveClassDecl(const std::vector<PClassConfig> & classConfig, const PPath & headerFile, const PVecPath & listInclude, bool enableDataStream, bool enableTypeStream,
588  const PString & includeTemplate)
589 {
590  if(headerFile == "") return false;
591  std::ofstream fs;
592  fs.open(headerFile.c_str());
593  if(!fs.is_open()){
594  std::cerr << "saveClassDecl : can't open file '" << headerFile << "'" << std::endl;
595  return false;
596  }
597  licenceSave(fs);
598  PString macroDef(makeMultiIncludeDefineMacro(headerFile.getFileName()));
599  fs << "#ifndef " << macroDef << std::endl;
600  fs << "#define " << macroDef << std::endl << std::endl;
601  if(listInclude.size() != 0lu){
602  for(PVecPath::const_iterator it(listInclude.begin()); it != listInclude.end(); ++it){
603  fs << "#include " << *it << std::endl;
604  }
605  fs << std::endl;
606  }
607  if(enableDataStream){
608  fs << "#include \"data_all.h\"" << std::endl << std::endl;
609  }
610  if(enableTypeStream){
611  fs << "#include \"phoenix_type_stream.h\"" << std::endl << std::endl;
612  }
613 
614  for(std::vector<PClassConfig>::const_iterator it(classConfig.begin()); it != classConfig.end(); ++it){
615  if(it->getIsEnum()){
616  saveEnumDecl(fs, *it, enableDataStream, enableTypeStream);
617  }else{
618  saveClassDecl(fs, *it, enableDataStream, enableTypeStream);
619  }
620  }
621 
622  if(includeTemplate != ""){
623  fs << "#include \""<<includeTemplate<<"\"" << std::endl << std::endl;
624  }
625 
626  fs << std::endl << std::endl << "#endif" << std::endl << std::endl;
627  fs.close();
628  return true;
629 }
630 
632 
638 bool saveClassImpl(const std::vector<PClassConfig> & classConfig, const PPath & sourceFile, const PPath & headerFile, bool enableTypeStream){
639  if(sourceFile == "" || headerFile == "") return false;
640  std::ofstream fs;
641  fs.open(sourceFile.c_str());
642  if(!fs.is_open()){
643  std::cerr << "saveClassImpl : can't open file '" << sourceFile << "'" << std::endl;
644  return true;
645  }
646  licenceSave(fs);
647  fs << std::endl << "#include \"" << headerFile.getFileName() << "\"" << std::endl << std::endl;
648  for(std::vector<PClassConfig>::const_iterator it(classConfig.begin()); it != classConfig.end(); ++it){
649  if(it->getIsEnum()){continue;}
650  saveClassImpl(fs, *it, enableTypeStream);
651  }
652  fs.close();
653  return true;
654 }
655 
656 
658 
661 void saveClassTemplate(std::ofstream & fs, const PClassConfig & classConfig){
662  const PVecString & listTemplate = classConfig.getListTemplate();
663  if(listTemplate.size() == 0lu){return;}
664 
665  PString defTemplate(getClassDefTemplate(listTemplate));
666  PString templateDeclaration(getClassDeclTempalteDef(listTemplate));
667 
668  saveClassConstructorImpl(fs, classConfig, defTemplate, templateDeclaration);
669  saveClassCopyConstructorImpl(fs, classConfig, defTemplate, templateDeclaration);
670  saveClassDestructorImpl(fs, classConfig, defTemplate, templateDeclaration);
671  saveClassEqualOperatorImpl(fs, classConfig, defTemplate, templateDeclaration);
672  saveClassSettersImpl(fs, classConfig, defTemplate, templateDeclaration);
673  saveClassGettersImpl(fs, classConfig, defTemplate, templateDeclaration);
674  saveClassCopyFunctionImpl(fs, classConfig, defTemplate, templateDeclaration);
675  saveClassInitialisationFunctionImpl(fs, classConfig, defTemplate, templateDeclaration);
676 }
677 
679 
684 bool saveClassTemplate(const std::vector<PClassConfig> & classConfig, const PPath & sourceFile, const PPath & headerFile){
685  if(sourceFile == "" || headerFile == "") return false;
686  std::ofstream fs;
687  fs.open(sourceFile.c_str());
688  if(!fs.is_open()){
689  std::cerr << "saveClassTemplate : can't open file '" << sourceFile << "'" << std::endl;
690  return true;
691  }
692  licenceSave(fs);
693  PString macroDef(makeMultiIncludeDefineMacro(headerFile.getFileName() + "_IMPL"));
694  fs << "#ifndef " << macroDef << std::endl;
695  fs << "#define " << macroDef << std::endl << std::endl;
696  fs << std::endl << "#include \"" << headerFile.getFileName() << "\"" << std::endl << std::endl;
697  for(std::vector<PClassConfig>::const_iterator it(classConfig.begin()); it != classConfig.end(); ++it){
698  saveClassTemplate(fs, *it);
699  }
700  fs << "\n\n#endif\n\n" << std::endl;
701  fs.close();
702  return true;
703 }
704 
706 
710 void checkClassConfig(bool & hasSource, bool & hasTemplate, const std::vector<PClassConfig> & classConfig){
711  hasSource = false;
712  hasTemplate = false;
713  for(std::vector<PClassConfig>::const_iterator it(classConfig.begin()); it != classConfig.end(); ++it){
714  bool isTemplate = it->getListTemplate().size() != 0lu;
715  hasSource |= !isTemplate;
716  hasTemplate |= isTemplate;
717  }
718 }
719 
721 
728 bool saveClassImplDecl(const std::vector<PClassConfig> & classConfig, const PPath & baseFileName, const PVecPath & listInclude, bool enableDataStream, bool enableTypeStream){
729  if(baseFileName == "") return false;
730  bool hasSource(false), hasTemplate(false);
731  checkClassConfig(hasSource, hasTemplate, classConfig);
732  PPath includeTemplate("");
733  if(hasTemplate){
734  includeTemplate = PString(baseFileName.getFileName() + "_impl.h");
735  }
736 
737  if(!saveClassDecl(classConfig, PString(baseFileName + ".h"), listInclude, enableDataStream, enableTypeStream, includeTemplate)) return false;
738 
739  if(hasSource){
740  if(!saveClassImpl(classConfig, PPath(baseFileName + PString(".cpp")), PPath(baseFileName + PString(".h")), enableTypeStream)) return false;
741  }
742  if(hasTemplate){
743  if(!saveClassTemplate(classConfig, PPath(baseFileName + PString("_impl.h")), PPath(baseFileName + PString(".h")))) return false;
744  }
745  return true;
746 }
747 
748 
std::vector< PPath > PVecPath
Definition: PPath.h:75
std::vector< PString > PVecString
Definition: PString.h:96
Class to describe a basic class.
Definition: PClassConfig.h:14
const PString & getClassDocumentation() const
Returns the class documentation.
const std::vector< PClassAttribute > & getListAttribute() const
Returns the list of attributes of the class.
const PVecString & getListTemplate() const
Returns the list of the template of the class.
const PString & getName() const
Returns the class name.
const PVecString & getListParentClass() const
Returns the list of the parents of the class.
Path of a directory or a file.
Definition: PPath.h:17
PPath getFileName() const
Get the name of the file, from last char to /.
Definition: PPath.cpp:172
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
PString eraseChar(char ch) const
Erase char ch of current string.
Definition: PString.cpp:478
std::vector< PString > split(char separator) const
Cut a PString on the given separator char.
Definition: PString.cpp:420
bool find(char ch) const
Find a char in a string.
Definition: PString.cpp:371
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.
void saveClassGetTypeNameImpl(std::ofstream &fs, const PClassConfig &classConfig, const PString &defTemplate, const PString &templateDeclaration, bool enableTypeStream)
Saves constructor of the class.
void saveClassTemplate(std::ofstream &fs, const PClassConfig &classConfig)
Creates template implementation file.
PString getClassDefTemplate(const PVecString &listTemplate)
Get the template call in the class declaration.
PString createSetterDecl(const PString &varType, const PString &varName, const PString &className, bool isPtr)
Creates a function decl for setters.
void saveClassDataStreamGenericFunction(std::ofstream &fs, const PClassConfig &classConfig)
Creates the method which enable to save/load generated class with any kind of stream/message/file.
void saveClassCopyConstructorImpl(std::ofstream &fs, const PClassConfig &classConfig, const PString &defTemplate, const PString &templateDeclaration)
Saves copy constructor of the class.
void saveDeclGetters(std::ofstream &fs, const PClassConfig &classConfig)
Creates getters header file.
void saveClassDataStreamMethod(std::ofstream &fs, const PClassConfig &classConfig)
Creates the method which enable to save/load generated class with any kind of stream/message/file.
bool saveClassImplDecl(const std::vector< PClassConfig > &classConfig, const PPath &baseFileName, const PVecPath &listInclude, bool enableDataStream, bool enableTypeStream)
Creates header file.
PString getClassDeclTempalteDef(const PVecString &listTemplate)
Get the template definition in the class declaration.
void saveClassDecl(std::ofstream &fs, const PClassConfig &classConfig, bool enableDataStream, bool enableTypeStream)
Creates header file.
void saveClassGettersImpl(std::ofstream &fs, const PClassConfig &classConfig, const PString &defTemplate, const PString &templateDeclaration)
Saves the class getters implementation.
void saveClassImpl(std::ofstream &fs, const PClassConfig &classConfig, bool enableTypeStream)
Creates source file.
PString makeVarType(const PString &varType, bool isSetter, bool isConst, bool isRef, bool isPtr)
Makes the var type by taking account of the type.
void saveClassSettersImpl(std::ofstream &fs, const PClassConfig &classConfig, const PString &defTemplate, const PString &templateDeclaration)
Saves the class setters implementation.
void saveClassCopyFunctionImpl(std::ofstream &fs, const PClassConfig &classConfig, const PString &defTemplate, const PString &templateDeclaration)
Saves the copy function of a class.
PString createGetterDecl(const PString &varType, const PString &varName, const PString &className, bool isConst, bool isPtr)
Creates a function decl for setters.
void saveClassConstructorImpl(std::ofstream &fs, const PClassConfig &classConfig, const PString &defTemplate, const PString &templateDeclaration)
Saves constructor of the class.
bool getIsPointer(const PString &varType)
Check if the given type is a pointer or not.
void saveClassEqualOperatorImpl(std::ofstream &fs, const PClassConfig &classConfig, const PString &defTemplate, const PString &templateDeclaration)
Saves = operator of the class.
void checkClassConfig(bool &hasSource, bool &hasTemplate, const std::vector< PClassConfig > &classConfig)
Check if the configuration has source or template to determine if the .cpp or _impl....
void saveEnumDecl(std::ofstream &fs, const PClassConfig &classConfig, bool enableDataStream, bool enableTypeStream)
Creates header file.
void saveClassDestructorImpl(std::ofstream &fs, const PClassConfig &classConfig, const PString &defTemplate, const PString &templateDeclaration)
Saves destructor of the class.
void saveClassInitialisationFunctionImpl(std::ofstream &fs, const PClassConfig &classConfig, const PString &defTemplate, const PString &templateDeclaration)
Saves the copy function of a class.
PString getDefaultValueTypeInCpp(const PString &type)
Get the default value of a type in C++.
bool getIsSimpleType(const PString &varType)
Check if the given type is a simple type.
void saveDeclSetters(std::ofstream &fs, const PClassConfig &classConfig)
Creates setters header file.