PhoenixGenerator  2.0.4
Set of tools to generate code
Loading...
Searching...
No Matches
generator_class_cpp.h File Reference
#include "ProjectParam.h"
+ Include dependency graph for generator_class_cpp.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

PString class_getClassDefTemplate (const PVecString &listTemplate)
 Get the template call in the class declaration.
 
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.
 
void generator_class_cpp_header (const PTraitBackendManager &manager, std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode)
 Create the declaration of the given class.
 
void generator_class_cpp_source (const PTraitBackendManager &manager, std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode)
 Create the implementation of the given class.
 
bool generator_class_full (const PTraitBackendManager &manager, const ProjectParam &projectParam)
 Generate the full sources and related unit tests from configuration.
 

Function Documentation

◆ class_getClassDefTemplate()

PString class_getClassDefTemplate ( const PVecString & listTemplate)

Get the template call in the class declaration.

Parameters
listTemplate: list of template def
Returns
template definition in the class declaration (< T, U, .... >)

Definition at line 26 of file generator_class_cpp.cpp.

26 {
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();
33 comma = ", ";
34 }
35 }
36 defTemplate += ">";
37 return defTemplate;
38}

Referenced by generator_class_cpp_header(), and generator_class_cpp_source().

+ Here is the caller graph for this function:

◆ generator_class_cpp()

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.

Parameters
manager: PTraitBackendManager which handles all trait backend
vecClassConfig: vector of class config we want to save
baseFileName: base file name for header or source file
outputSourceDir: output directory where to save sources
mode: all modes of the generator (data/check/type/config stream)
Returns
true on success, false otherwise

Definition at line 361 of file generator_class_cpp.cpp.

361 {
362 if(baseFileName == ""){
363 std::cerr << termRed() << "generator_class_cpp : baseFileName is empty" << termDefault() << std::endl;
364 return false;
365 }
366 bool hasSource(false), hasTemplate(false);
367 class_checkClassConfig(hasSource, hasTemplate, vecClassConfig);
368 PString includeTemplate("");
369 if(hasTemplate){
370 includeTemplate = PString(baseFileName.getFileName() + "_impl.h");
371 }
372 PPath headerFile = outputSourceDir / PPath(baseFileName + ".h");
373 if(!generator_class_cpp_headerFile(manager, headerFile, vecClassConfig, mode, vecInclude, includeTemplate)){
374 std::cerr << termRed() << "generator_class_cpp : cannot generate header file '"<<headerFile<<"'" << termDefault() << std::endl;
375 return false;
376 }
377 if(hasSource){
378 PPath sourceFile = outputSourceDir / PPath(baseFileName + PString(".cpp"));
379 if(!generator_class_cpp_sourceFile(manager, sourceFile, PPath(baseFileName + PString(".h")), vecClassConfig, mode, false)){
380 std::cerr << termRed() << "generator_class_cpp : cannot generate source file '"<<sourceFile<<"'" << termDefault() << std::endl;
381 return false;
382 }
383 }
384 if(hasTemplate){
385 PPath implFile = outputSourceDir / PPath(baseFileName + PString("_impl.h"));
386 if(!generator_class_cpp_sourceFile(manager, implFile, PPath(baseFileName + PString(".h")), vecClassConfig, mode, true)){
387 std::cerr << termRed() << "generator_class_cpp : cannot generate impl file '"<<implFile<<"'" << termDefault() << std::endl;
388 return false;
389 }
390 }
391 return true;
392}
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_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 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.

References class_checkClassConfig(), generator_class_cpp_headerFile(), and generator_class_cpp_sourceFile().

Referenced by generator_class_full().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generator_class_cpp_header()

void generator_class_cpp_header ( const PTraitBackendManager & manager,
std::ofstream & fs,
const PClassConfig & classConfig,
const GeneratorMode & mode )

Create the declaration of the given class.

Parameters
manager: PTraitBackendManager which handles all trait backend
[out]fs: file to be completed
classConfig: configuration of the class to be used
mode: mode of the generator

Definition at line 186 of file generator_class_cpp.cpp.

186 {
187 if(classConfig.getClassDocumentation() != "") fs << classConfig.getClassDocumentation() << std::endl;
188 PString name(classConfig.getName());
189 GeneratorMode classMode(mode);
190 const PVecString & listTemplate = classConfig.getListTemplate();
191 PString defTemplate(class_getClassDefTemplate(listTemplate));
192 PString templateDeclaration(class_getClassDeclTempalteDef(listTemplate));
193 classMode.defTemplate = defTemplate;
194 classMode.templateDeclaration = templateDeclaration;
195
196 fs << classMode.templateDeclaration;
197 fs << "class " << name;
198 if(classConfig.getListParentClass().size() != 0lu){
199 PVecString::const_iterator it(classConfig.getListParentClass().begin());
200 fs << " : public " << *it;
201 if(classConfig.getListParentClass().size() > 1lu){
202 while(it != classConfig.getListParentClass().end()){
203 fs << ", " << *it;
204 ++it;
205 }
206 }
207 }
208 fs << "{" << std::endl;
209 fs << "\tpublic:" << std::endl;
210 fs << "\t\t" << name << "();" << std::endl;
211 fs << "\t\tvirtual ~" << name << "();" << std::endl;
212
213 manager.publicMethodDeclaration(fs, classConfig, classMode);
214 fs << "\tprotected:" << std::endl;
215 manager.protectedMethodDeclaration(fs, classConfig, classMode);
216
217 fs << "\tprivate:" << std::endl;
218 fs << "\t\tvoid initialisation" << name << "();" << std::endl;
219 manager.privateMethodDeclaration(fs, classConfig, classMode);
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;
223
224 fs << "\t\t" << it->getType() << " p_" << it->getName() << ";" << std::endl;
225 }
226 fs << "};" << std::endl << std::endl;
227 manager.classExtraFunctionDeclaration(fs, classConfig, classMode);
228 fs << std::endl << std::endl;
229}
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.
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 privateMethodDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Declaration of private methods.
void publicMethodDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Declaration of public methods.
PString class_getClassDefTemplate(const PVecString &listTemplate)
Get the template call in the class declaration.
PString class_getClassDeclTempalteDef(const PVecString &listTemplate)
Get the template definition in the class declaration.
All the genertor modes.

References class_getClassDeclTempalteDef(), class_getClassDefTemplate(), PTraitBackendManager::classExtraFunctionDeclaration(), GeneratorMode::defTemplate, PClassConfig::getClassDocumentation(), PClassConfig::getListAttribute(), PClassConfig::getListParentClass(), PClassConfig::getListTemplate(), PClassConfig::getName(), PTraitBackendManager::privateMethodDeclaration(), PTraitBackendManager::protectedMethodDeclaration(), PTraitBackendManager::publicMethodDeclaration(), and GeneratorMode::templateDeclaration.

Referenced by generator_class_cpp_headerFile().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generator_class_cpp_source()

void generator_class_cpp_source ( const PTraitBackendManager & manager,
std::ofstream & fs,
const PClassConfig & classConfig,
const GeneratorMode & mode )

Create the implementation of the given class.

Parameters
manager: PTraitBackendManager which handles all trait backend
[out]fs: file to be completed
classConfig: configuration of the class to be used
mode: mode of the generator

Definition at line 283 of file generator_class_cpp.cpp.

283 {
284 manager.classExtraFunctionImplementation(fs, classConfig, mode);
285 GeneratorMode classMode(mode);
286 const PVecString & listTemplate = classConfig.getListTemplate();
287 PString defTemplate(class_getClassDefTemplate(listTemplate));
288 PString templateDeclaration(class_getClassDeclTempalteDef(listTemplate));
289 classMode.defTemplate = defTemplate;
290 classMode.templateDeclaration = templateDeclaration;
291 if(!classConfig.getIsEnum()){
292 class_saveClassConstructorImpl(fs, classConfig, defTemplate, templateDeclaration);
293 class_saveClassDestructorImpl(fs, classConfig, defTemplate, templateDeclaration);
294 }
295 manager.publicMethodImplementation(fs, classConfig, classMode);
296 manager.protectedMethodImplementation(fs, classConfig, classMode);
297 manager.privateMethodImplementation(fs, classConfig, classMode);
298 if(!classConfig.getIsEnum()){
299 class_saveClassInitialisationFunctionImpl(fs, classConfig, defTemplate, templateDeclaration);
300 }
301}
bool getIsEnum() const
Say if the current PClassConfig is an enum.
void classExtraFunctionImplementation(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of extra functions.
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 class_saveClassDestructorImpl(std::ofstream &fs, const PClassConfig &classConfig, const PString &defTemplate, const PString &templateDeclaration)
Saves destructor of the class.
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.

References class_getClassDeclTempalteDef(), class_getClassDefTemplate(), class_saveClassConstructorImpl(), class_saveClassDestructorImpl(), class_saveClassInitialisationFunctionImpl(), PTraitBackendManager::classExtraFunctionImplementation(), GeneratorMode::defTemplate, PClassConfig::getIsEnum(), PClassConfig::getListTemplate(), PTraitBackendManager::privateMethodImplementation(), PTraitBackendManager::protectedMethodImplementation(), PTraitBackendManager::publicMethodImplementation(), and GeneratorMode::templateDeclaration.

Referenced by generator_class_cpp_sourceFile().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generator_class_full()

bool generator_class_full ( const PTraitBackendManager & manager,
const ProjectParam & projectParam )

Generate the full sources and related unit tests from configuration.

Parameters
manager: PTraitBackendManager which handles all trait backend
projectParam: description of classes and how and where to generate sources and test
Returns
true on success, false otherwise

Definition at line 491 of file generator_class_cpp.cpp.

491 {
492 bool b(true);
493 b &= projectParam.outputSourceDir.createDirectory();
494 b &= projectParam.outputTestDir.createDirectory();
495 PString cmakeListBody;
496 cmakeListBody += getCMakeListsHeader();
497 const PVecDataConfig & vecDataConfig = projectParam.vecDataConfig;
498 for(const PDataConfig & config : vecDataConfig){
499 PPath baseFileName = config.getFileName().getFileName().eraseExtension();
500 if(!generator_class_cpp(manager, config.getVecClassConfig(), projectParam.outputSourceDir, baseFileName, projectParam.mode, config.getVecInclude())){
501 std::cerr << termRed() << "generator_class_full : cannot generate C++ sources at '"<<projectParam.outputSourceDir<<"'" << termDefault() << std::endl;
502 b = false; //best effort strategy
503 }
504 PPath outputCurrentTestDir = projectParam.outputTestDir / PPath("TEST_" + config.getFileName().getFileName().eraseExtension().toUpper());
505 cmakeListBody += "add_subdirectory(" + outputCurrentTestDir.getFileName() + ")\n";
506 if(!generator_class_cpp_test(manager, outputCurrentTestDir, config.getVecClassConfig(), projectParam.name, baseFileName, projectParam.mode)){
507 std::cerr << termRed() << "generator_class_full : cannot generate C++ tests at '"<<projectParam.outputTestDir<<"'" << termDefault() << std::endl;
508 b = false; //best effort strategy
509 }
510 }
511 cmakeListBody += "\n\n";
512 PPath cmakelist(projectParam.outputTestDir / PPath("CMakeLists.txt"));
513 b &= cmakelist.saveFileContent(cmakeListBody);
514 return b;
515}
std::vector< PDataConfig > PVecDataConfig
Definition PDataConfig.h:48
Class to describe a basic class.
Definition PDataConfig.h:17
PString getCMakeListsHeader()
Get the CMakeLists.txt header.
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.
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.
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.

References generator_class_cpp(), generator_class_cpp_test(), getCMakeListsHeader(), ProjectParam::mode, ProjectParam::name, ProjectParam::outputSourceDir, ProjectParam::outputTestDir, and ProjectParam::vecDataConfig.

Referenced by simple_project_generate_source().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: