7#include "phoenix_color.h"
8#include "openFileStream.h"
18 if(varType.size() == 0lu){
return false;}
19 return varType.find(
'*') && varType.back() ==
'*';
27 if(listTemplate.size() == 0lu){
return "";}
28 PString defTemplate(
"<"), comma(
"");
29 for(PVecString::const_iterator it(listTemplate.begin()); it != listTemplate.end(); ++it){
30 PVecString vecPart = it->split(
' ');
31 if(vecPart.size() != 0lu){
32 defTemplate += comma + vecPart.back();
45 if(listTemplate.size() == 0lu){
return "";}
46 PString templateDef(
"template<");
48 for(PVecString::const_iterator it(listTemplate.begin()); it != listTemplate.end(); ++it){
64 PString name(classConfig.
getName());
65 fs <<
"///Constructor of class " << name << std::endl;
66 fs << templateDeclaration;
67 fs << name << defTemplate <<
"::" << name <<
"()";
70 fs << std::endl <<
"\t: " << *it <<
"()";
73 fs <<
", " << *it <<
"()";
79 fs <<
"{" << std::endl;
80 fs <<
"\tinitialisation" << name <<
"();" << std::endl;
81 fs <<
"}" << std::endl << std::endl;
92 PString name(classConfig.
getName());
93 fs <<
"///Destructor of class " << name << std::endl;
94 fs << templateDeclaration;
95 fs << name << defTemplate <<
"::~" << name <<
"(){" << std::endl;
96 fs <<
"\t" << std::endl;
97 fs <<
"}" << std::endl << std::endl;
107 PString name(classConfig.
getName());
108 fs <<
"///Initialisation Function of class " << name << std::endl;
109 fs << templateDeclaration;
110 fs <<
"void " << name << defTemplate <<
"::initialisation" << name <<
"(){" << std::endl;
111 const std::vector<PClassAttribute> & listAttr(classConfig.
getListAttribute());
112 for(std::vector<PClassAttribute>::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
113 if(it->getDefaultValue() !=
""){
114 fs <<
"\tp_" << it->getName() <<
" = "<<it->getDefaultValue()<<
";" << std::endl;
116 PString varType(it->getType());
120 fs <<
"\tp_" << it->getName() <<
" = NULL;" << std::endl;
121 }
else if(isSimpleType){
123 if(defaultValue !=
""){
124 fs <<
"\tp_" << it->getName() <<
" = "<<defaultValue<<
";" << std::endl;
126 }
else if(varType ==
"PString"){
127 fs <<
"\tp_" << it->getName() <<
" = \"\";" << std::endl;
131 fs <<
"}" << std::endl << std::endl;
139 bool hasDefaultValue =
false;
140 for(std::vector<PClassAttribute>::const_iterator it(vecAttr.begin()); it != vecAttr.end() && !hasDefaultValue; ++it){
141 hasDefaultValue |= it->getDefaultValue() !=
"";
143 return hasDefaultValue;
154 PString name(classConfig.
getName());
155 fs <<
"namespace " << name <<
" {\n";
157 fs <<
"\tenum " << name <<
" {\n\t\t";
158 const std::vector<PClassAttribute> & vecAttr(classConfig.
getListAttribute());
162 for(std::vector<PClassAttribute>::const_iterator it(vecAttr.begin()); it != vecAttr.end(); ++it){
163 if(it->getDocumentation() !=
"") fs <<
"\t\t" << it->getDocumentation() << std::endl;
164 fs << comma << it->getName();
165 if(isDefaultCounterActivated){
167 }
else if(it->getDefaultValue() !=
""){
168 fs <<
" = " << it->getDefaultValue();
173 fs <<
"\n\t};" << std::endl;
174 fs <<
"}" << std::endl << std::endl;
176 fs << std::endl << std::endl;
188 PString name(classConfig.
getName());
197 fs <<
"class " << name;
200 fs <<
" : public " << *it;
208 fs <<
"{" << std::endl;
209 fs <<
"\tpublic:" << std::endl;
210 fs <<
"\t\t" << name <<
"();" << std::endl;
211 fs <<
"\t\tvirtual ~" << name <<
"();" << std::endl;
214 fs <<
"\tprotected:" << std::endl;
217 fs <<
"\tprivate:" << std::endl;
218 fs <<
"\t\tvoid initialisation" << name <<
"();" << std::endl;
220 const std::vector<PClassAttribute> & listAttr(classConfig.
getListAttribute());
221 for(std::vector<PClassAttribute>::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
222 if(it->getDocumentation() !=
"") fs <<
"\t\t" << it->getDocumentation() << std::endl;
224 fs <<
"\t\t" << it->getType() <<
" p_" << it->getName() <<
";" << std::endl;
226 fs <<
"};" << std::endl << std::endl;
228 fs << std::endl << std::endl;
241 const PVecPath & vecInclude,
const PString & includeTemplate)
243 if(headerFile ==
"")
return false;
245 if(!openFileStream(fs, headerFile)){
return false;}
248 fs <<
"#ifndef " << macroDef << std::endl;
249 fs <<
"#define " << macroDef << std::endl << std::endl;
250 fs <<
"#include <string>" << std::endl;
251 if(vecInclude.size() != 0lu){
252 for(PVecPath::const_iterator it(vecInclude.begin()); it != vecInclude.end(); ++it){
253 fs <<
"#include " << *it << std::endl;
259 for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
268 if(includeTemplate !=
""){
269 fs <<
"#include \""<<includeTemplate<<
"\"" << std::endl << std::endl;
272 fs << std::endl << std::endl <<
"#endif" << std::endl << std::endl;
313 const std::vector<PClassConfig> & vecClassConfig,
const GeneratorMode & mode,
bool isTemplateImpl)
315 if(sourceFile ==
"" || headerFile ==
"")
return false;
317 if(!openFileStream(fs, sourceFile)){
return false;}
321 fs <<
"#ifndef " << macroDef << std::endl;
322 fs <<
"#define " << macroDef << std::endl << std::endl;
324 fs << std::endl <<
"#include \"" << headerFile.getFileName() <<
"\"" << std::endl << std::endl;
325 for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
329 fs <<
"#endif" << std::endl << std::endl;
346 for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
347 bool isTemplate = it->getListTemplate().size() != 0lu;
348 hasSource |= !isTemplate;
349 hasTemplate |= isTemplate;
362 if(baseFileName ==
""){
363 std::cerr << termRed() <<
"generator_class_cpp : baseFileName is empty" << termDefault() << std::endl;
366 bool hasSource(
false), hasTemplate(
false);
368 PString includeTemplate(
"");
370 includeTemplate = PString(baseFileName.getFileName() +
"_impl.h");
372 PPath headerFile = outputSourceDir / PPath(baseFileName +
".h");
374 std::cerr << termRed() <<
"generator_class_cpp : cannot generate header file '"<<headerFile<<
"'" << termDefault() << std::endl;
378 PPath sourceFile = outputSourceDir / PPath(baseFileName + PString(
".cpp"));
380 std::cerr << termRed() <<
"generator_class_cpp : cannot generate source file '"<<sourceFile<<
"'" << termDefault() << std::endl;
385 PPath implFile = outputSourceDir / PPath(baseFileName + PString(
"_impl.h"));
387 std::cerr << termRed() <<
"generator_class_cpp : cannot generate impl file '"<<implFile<<
"'" << termDefault() << std::endl;
405 if(!openFileStream(fs, PPath(outputCurrentTestDir + PString(
"/main.cpp")))){
return false;}
411 fs <<
"#include \"" + baseFileName +
".h\"\n\n";
413 fs <<
"int main(int argc, char ** argv){\n";
416 fs <<
"\treturn 0;\n";
430 PString body(
""), testName(
"test_class_" + classConfig.
getName().toLowerUnderscore());
432 body +=
"add_executable("+testName+
" main.cpp)\n";
433 body +=
"target_link_libraries("+testName+
" ${"+projectName.toUpper()+
"_TEST_DEPENDENCIES})\n";
435 body +=
"add_test(NAME "+testName.firstToUpper()+
"\n";
436 body +=
"\tCOMMAND ${CMAKE_CURRENT_BINARY_DIR}/"+testName+
"\n";
437 body +=
"\tWORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}\n";
439 return PPath(outputCurrentTestDir + PString(
"/CMakeLists.txt")).saveFileContent(body);
452 PString name(classConfig.
getName());
453 PPath outputCurrentTestDir(outputTestDir + PString(
"/TEST_") + name.toUpper());
454 if(!outputCurrentTestDir.createDirectory()){std::cerr <<
"generator_class_test : cannot create TEST directory '"<<outputCurrentTestDir<<
"'" << std::endl;
return false;}
472 PString cmakeListBody;
475 cmakeListBody +=
"#Generating tests for " + PString::toString(vecClassConfig.size()) +
"\n";
476 for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
477 cmakeListBody +=
"add_subdirectory(TEST_" + it->getName().toUpper() +
")\n";
480 cmakeListBody +=
"\n\n";
481 PPath cmakelist(outputTestDir / PPath(
"CMakeLists.txt"));
482 b &= cmakelist.saveFileContent(cmakeListBody);
495 PString cmakeListBody;
499 PPath baseFileName = config.getFileName().getFileName().eraseExtension();
501 std::cerr << termRed() <<
"generator_class_full : cannot generate C++ sources at '"<<projectParam.
outputSourceDir<<
"'" << termDefault() << std::endl;
504 PPath outputCurrentTestDir = projectParam.
outputTestDir / PPath(
"TEST_" + config.getFileName().getFileName().eraseExtension().toUpper());
505 cmakeListBody +=
"add_subdirectory(" + outputCurrentTestDir.getFileName() +
")\n";
507 std::cerr << termRed() <<
"generator_class_full : cannot generate C++ tests at '"<<projectParam.
outputTestDir<<
"'" << termDefault() << std::endl;
511 cmakeListBody +=
"\n\n";
512 PPath cmakelist(projectParam.
outputTestDir / PPath(
"CMakeLists.txt"));
513 b &= cmakelist.saveFileContent(cmakeListBody);
std::vector< PDataConfig > PVecDataConfig
Class to describe a basic class.
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.
bool getIsEnum() const
Say if the current PClassConfig is an enum.
const PVecString & getListParentClass() const
Returns the list of the parents of the class.
Class to describe a basic class.
Manager of the Trait backends.
void protectedMethodDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Declaration of protected methods.
void classExtraFunctionDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Declaration of extra functions.
void testCallFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Call of the test function.
void classExtraFunctionImplementation(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of extra functions.
void headerExtraInclude(std::ofstream &fs, const GeneratorMode &mode) const
Add extra include on the header.
void privateMethodDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Declaration of private methods.
void protectedMethodImplementation(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of protected methods.
void privateMethodImplementation(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of private methods.
void publicMethodImplementation(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of public methods.
void publicMethodDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Declaration of public methods.
void testFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of test function.
PString getCMakeListsHeader()
Get the CMakeLists.txt header.
bool generator_checkDefaultValueExist(const std::vector< PClassAttribute > &vecAttr)
Check if the vector of PClassAttribute has default value.
PString class_getClassDefTemplate(const PVecString &listTemplate)
Get the template call in the class declaration.
void generator_class_cpp_source(const PTraitBackendManager &manager, std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode)
Create the implementation of the given class.
void class_saveClassDestructorImpl(std::ofstream &fs, const PClassConfig &classConfig, const PString &defTemplate, const PString &templateDeclaration)
Saves destructor of the class.
void generator_class_cpp_header(const PTraitBackendManager &manager, std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode)
Create the declaration of the given class.
bool generator_class_cpp(const PTraitBackendManager &manager, const std::vector< PClassConfig > &vecClassConfig, const PPath &outputSourceDir, const PPath &baseFileName, const GeneratorMode &mode, const PVecPath &vecInclude)
Creates header and source files.
PString class_getClassDeclTempalteDef(const PVecString &listTemplate)
Get the template definition in the class declaration.
bool generator_class_test(const PTraitBackendManager &manager, const PPath &outputTestDir, const PClassConfig &classConfig, const PString &projectName, const PPath &baseFileName, const GeneratorMode &mode)
Save the unit test of the generated PClassConfig.
bool generator_class_cpp_sourceFile(const PTraitBackendManager &manager, const PPath &sourceFile, const PPath &headerFile, const std::vector< PClassConfig > &vecClassConfig, const GeneratorMode &mode, bool isTemplateImpl)
Create the implementation of the given file.
void class_saveClassConstructorImpl(std::ofstream &fs, const PClassConfig &classConfig, const PString &defTemplate, const PString &templateDeclaration)
Saves constructor of the class.
void class_saveClassInitialisationFunctionImpl(std::ofstream &fs, const PClassConfig &classConfig, const PString &defTemplate, const PString &templateDeclaration)
Saves the copy function of a class.
bool generator_class_cpp_test(const PTraitBackendManager &manager, const PPath &outputTestDir, const std::vector< PClassConfig > &vecClassConfig, const PString &projectName, const PPath &baseFileName, const GeneratorMode &mode)
Save the unit test of the generated PClassConfig.
void class_checkClassConfig(bool &hasSource, bool &hasTemplate, const std::vector< PClassConfig > &vecClassConfig)
Check if the configuration has source or template to determine if the .cpp or _impl....
bool class_getIsPointer(const PString &varType)
Check if the given type is a pointer or not.
bool generator_class_full(const PTraitBackendManager &manager, const ProjectParam &projectParam)
Generate the full sources and related unit tests from configuration.
bool generator_class_cpp_headerFile(const PTraitBackendManager &manager, const PPath &headerFile, const std::vector< PClassConfig > &vecClassConfig, const GeneratorMode &mode, const PVecPath &vecInclude, const PString &includeTemplate)
Create the declaration of the given class.
bool generator_class_testMain(const PTraitBackendManager &manager, const PPath &outputCurrentTestDir, const PClassConfig &classConfig, const PPath &baseFileName, const GeneratorMode &mode)
Save the CMakeLists to be used to compile the unit test.
void generator_enum_cpp_header(const PTraitBackendManager &manager, std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode)
Create the declaration of the given class.
bool generator_class_testCMakeLists(const PPath &outputCurrentTestDir, const PClassConfig &classConfig, const PString &projectName, const GeneratorMode &mode)
Save the CMakeLists to be used to compile the unit test.
PString defTemplate
Template definition for the class header.
PString assertInclude
Include of the assert to be used.
PString templateDeclaration
Template declaration of the method implementation.
Set of parameters to generate a project.
GeneratorMode mode
Mode to be used to generate the project.
PPath outputTestDir
Output path of the unit tests.
PVecDataConfig vecDataConfig
Configuration of classes to be generated.
PString name
Name of the project.
PPath outputSourceDir
Output path of the sources.
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.