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;
91 PString name(classConfig.
getName());
92 fs <<
"///Initialisation Function of class " << name << std::endl;
93 fs << templateDeclaration;
94 fs <<
"void " << name << defTemplate <<
"::initialisation" << name <<
"(){" << std::endl;
95 fs <<
"\t//Initialisation of the attributes of the class" << std::endl;
96 const std::vector<PClassAttribute> & listAttr(classConfig.
getListAttribute());
97 for(std::vector<PClassAttribute>::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
98 if(it->getDefaultValue() !=
""){
99 fs <<
"\tp_" << it->getName() <<
" = "<<it->getDefaultValue()<<
";" << std::endl;
101 PString varType(it->getType());
105 fs <<
"\tp_" << it->getName() <<
" = NULL;" << std::endl;
106 }
else if(isSimpleType){
108 if(defaultValue !=
""){
109 fs <<
"\tp_" << it->getName() <<
" = "<<defaultValue<<
";" << std::endl;
111 }
else if(varType ==
"PString"){
112 fs <<
"\tp_" << it->getName() <<
" = \"\";" << std::endl;
116 fs <<
"}" << std::endl << std::endl;
124 bool hasDefaultValue =
false;
125 for(std::vector<PClassAttribute>::const_iterator it(vecAttr.begin()); it != vecAttr.end() && !hasDefaultValue; ++it){
126 hasDefaultValue |= it->getDefaultValue() !=
"";
128 return hasDefaultValue;
139 PString name(classConfig.
getName());
140 fs <<
"namespace " << name <<
" {\n";
142 fs <<
"\tenum " << name <<
" {\n\t\t";
143 const std::vector<PClassAttribute> & vecAttr(classConfig.
getListAttribute());
147 for(std::vector<PClassAttribute>::const_iterator it(vecAttr.begin()); it != vecAttr.end(); ++it){
148 if(it->getDocumentation() !=
"") fs <<
"\t\t" << it->getDocumentation() << std::endl;
149 fs << comma << it->getName();
150 if(isDefaultCounterActivated){
152 }
else if(it->getDefaultValue() !=
""){
153 fs <<
" = " << it->getDefaultValue();
158 fs <<
"\n\t};" << std::endl;
159 fs <<
"}" << std::endl << std::endl;
161 fs << std::endl << std::endl;
173 PString name(classConfig.
getName());
184 if(namespaceName !=
""){
185 fs << namespaceName <<
"::";
190 fs <<
" : public " << *it;
198 fs <<
"{" << std::endl;
199 fs <<
"\tpublic:" << std::endl;
200 fs <<
"\t\t" << name <<
"();" << std::endl;
201 fs <<
"\t\tvirtual ~" << name <<
"() = default;" << std::endl;
204 fs <<
"\tprotected:" << std::endl;
207 fs <<
"\tprivate:" << std::endl;
208 fs <<
"\t\tvoid initialisation" << name <<
"();" << std::endl;
210 const std::vector<PClassAttribute> & listAttr(classConfig.
getListAttribute());
211 for(std::vector<PClassAttribute>::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
212 if(it->getDocumentation() !=
"") fs <<
"\t\t" << it->getDocumentation() << std::endl;
214 fs <<
"\t\t" << it->getType() <<
" p_" << it->getName() <<
";" << std::endl;
216 fs <<
"};" << std::endl << std::endl;
218 fs << std::endl << std::endl;
231 const PVecPath & vecInclude,
const PString & includeTemplate)
233 if(headerFile ==
"")
return false;
235 if(!openFileStream(fs, headerFile)){
return false;}
238 fs <<
"#ifndef " << macroDef << std::endl;
239 fs <<
"#define " << macroDef << std::endl << std::endl;
240 fs <<
"#include <string>" << std::endl;
241 if(vecInclude.size() != 0lu){
242 for(PVecPath::const_iterator it(vecInclude.begin()); it != vecInclude.end(); ++it){
243 fs <<
"#include " << *it << std::endl;
249 for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
258 if(includeTemplate !=
""){
259 fs <<
"#include \""<<includeTemplate<<
"\"" << std::endl << std::endl;
262 fs << std::endl << std::endl <<
"#endif" << std::endl << std::endl;
302 const std::vector<PClassConfig> & vecClassConfig,
const GeneratorMode & mode,
bool isTemplateImpl)
304 if(sourceFile ==
"" || headerFile ==
"")
return false;
306 if(!openFileStream(fs, sourceFile)){
return false;}
310 fs <<
"#ifndef " << macroDef << std::endl;
311 fs <<
"#define " << macroDef << std::endl << std::endl;
313 fs << std::endl <<
"#include \"" << headerFile.getFileName() <<
"\"" << std::endl << std::endl;
314 bool hasNamespace(
false);
315 for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
316 if(it->getNamespace() !=
"" && !hasNamespace){
317 fs <<
"using namespace " << it->getNamespace() <<
";" << std::endl << std::endl;
321 for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
325 fs <<
"#endif" << std::endl << std::endl;
342 for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
343 bool isTemplate = it->getListTemplate().size() != 0lu;
344 hasSource |= !isTemplate;
345 hasTemplate |= isTemplate;
358 if(baseFileName ==
""){
359 std::cerr << termRed() <<
"generator_class_cpp : baseFileName is empty" << termDefault() << std::endl;
362 bool hasSource(
false), hasTemplate(
false);
364 PString includeTemplate(
"");
366 includeTemplate = PString(baseFileName.getFileName() +
"_impl.h");
368 PPath headerFile = outputSourceDir / PPath(baseFileName +
".h");
370 std::cerr << termRed() <<
"generator_class_cpp : cannot generate header file '"<<headerFile<<
"'" << termDefault() << std::endl;
374 PPath sourceFile = outputSourceDir / PPath(baseFileName + PString(
".cpp"));
376 std::cerr << termRed() <<
"generator_class_cpp : cannot generate source file '"<<sourceFile<<
"'" << termDefault() << std::endl;
381 PPath implFile = outputSourceDir / PPath(baseFileName + PString(
"_impl.h"));
383 std::cerr << termRed() <<
"generator_class_cpp : cannot generate impl file '"<<implFile<<
"'" << termDefault() << std::endl;
401 if(!openFileStream(fs, PPath(outputCurrentTestDir + PString(
"/main.cpp")))){
return false;}
407 fs <<
"#include \"" + baseFileName +
".h\"\n\n";
410 fs <<
"int main(int argc, char ** argv){\n";
413 fs <<
"\treturn 0;\n";
427 PString body(
""), testName(
"test_class_" + classConfig.
getName().toLowerUnderscore());
429 body +=
"add_executable("+testName+
" main.cpp)\n";
430 body +=
"target_link_libraries("+testName+
" ${"+projectName.toUpper()+
"_TEST_DEPENDENCIES})\n";
432 body +=
"add_test(NAME "+testName.firstToUpper()+
"\n";
433 body +=
"\tCOMMAND ${CMAKE_CURRENT_BINARY_DIR}/"+testName+
"\n";
434 body +=
"\tWORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}\n";
436 return PPath(outputCurrentTestDir + PString(
"/CMakeLists.txt")).saveFileContent(body);
449 PString name(classConfig.
getName());
450 PPath outputCurrentTestDir(outputTestDir + PString(
"/TEST_") + name.toUpper());
451 if(!outputCurrentTestDir.createDirectory()){std::cerr <<
"generator_class_test : cannot create TEST directory '"<<outputCurrentTestDir<<
"'" << std::endl;
return false;}
469 PString cmakeListBody;
472 cmakeListBody +=
"#Generating tests for " + PString::toString(vecClassConfig.size()) +
"\n";
473 for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
474 cmakeListBody +=
"add_subdirectory(TEST_" + it->getName().toUpper() +
")\n";
477 cmakeListBody +=
"\n\n";
478 PPath cmakelist(outputTestDir / PPath(
"CMakeLists.txt"));
479 b &= cmakelist.saveFileContent(cmakeListBody);
492 PString cmakeListBody;
496 PPath baseFileName = config.getFileName().getFileName().eraseExtension();
498 std::cerr << termRed() <<
"generator_class_full : cannot generate C++ sources at '"<<projectParam.
outputSourceDir<<
"'" << termDefault() << std::endl;
501 PPath outputCurrentTestDir = projectParam.
outputTestDir / PPath(
"TEST_" + config.getFileName().getFileName().eraseExtension().toUpper());
502 cmakeListBody +=
"add_subdirectory(" + outputCurrentTestDir.getFileName() +
")\n";
504 std::cerr << termRed() <<
"generator_class_full : cannot generate C++ tests at '"<<projectParam.
outputTestDir<<
"'" << termDefault() << std::endl;
508 cmakeListBody +=
"\n\n";
509 PPath cmakelist(projectParam.
outputTestDir / PPath(
"CMakeLists.txt"));
510 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.
const PString & getNamespace() const
Returns the namespace 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 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.