PhoenixGenerator  2.6.4
Set of tools to generate code
Loading...
Searching...
No Matches
generator_class_cpp.cpp File Reference
#include "phoenix_color.h"
#include "openFileStream.h"
#include "header_generator.h"
#include "cmakelist_generator.h"
#include "generator_class_cpp.h"
+ Include dependency graph for generator_class_cpp.cpp:

Go to the source code of this file.

Functions

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.h files have to be generated.
 
PString class_getClassDeclTempalteDef (const PVecString &listTemplate)
 Get the template definition in the class declaration.
 
PString class_getClassDefTemplate (const PVecString &listTemplate)
 Get the template call in the class declaration.
 
bool class_getIsPointer (const PString &varType)
 Check if the given type is a pointer or not.
 
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_checkDefaultValueExist (const std::vector< PClassAttribute > &vecAttr)
 Check if the vector of PClassAttribute has default value.
 
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.
 
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.
 
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_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.
 
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.
 
bool generator_class_full (const PTraitBackendManager &manager, const ProjectParam &projectParam)
 Generate the full sources and related unit tests from configuration.
 
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_testCMakeLists (const PPath &outputCurrentTestDir, const PClassConfig &classConfig, const PString &projectName, const GeneratorMode &mode)
 Save the CMakeLists to be used to compile the unit test.
 
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.
 

Function Documentation

◆ class_checkClassConfig()

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.h files have to be generated.

Parameters
[out]hasSource: true if the configuration needs .cpp file
[out]hasTemplate: true if the configuration needs _impl.h file
vecClassConfig: class config we want to save

Definition at line 339 of file generator_class_cpp.cpp.

339 {
340 hasSource = false;
341 hasTemplate = false;
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;
346 }
347}

Referenced by generator_class_cpp().

+ Here is the caller graph for this function:

◆ class_getClassDeclTempalteDef()

PString class_getClassDeclTempalteDef ( const PVecString & listTemplate)

Get the template definition in the class declaration.

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

Definition at line 44 of file generator_class_cpp.cpp.

44 {
45 if(listTemplate.size() == 0lu){return "";}
46 PString templateDef("template<");
47 PString comma("");
48 for(PVecString::const_iterator it(listTemplate.begin()); it != listTemplate.end(); ++it){
49 templateDef += comma;
50 templateDef += *it;
51 comma = ", ";
52 }
53 templateDef += ">\n";
54 return templateDef;
55}

Referenced by generator_class_cpp_header(), and generator_class_cpp_source().

+ Here is the caller graph for this function:

◆ 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:

◆ class_getIsPointer()

bool class_getIsPointer ( const PString & varType)

Check if the given type is a pointer or not.

Parameters
varType: type to be checked
Returns
true if the given type is a pointer, false otherwise

Definition at line 17 of file generator_class_cpp.cpp.

17 {
18 if(varType.size() == 0lu){return false;}
19 return varType.find('*') && varType.back() == '*';
20}

Referenced by class_saveClassInitialisationFunctionImpl().

+ Here is the caller graph for this function:

◆ class_saveClassConstructorImpl()

void class_saveClassConstructorImpl ( std::ofstream & fs,
const PClassConfig & classConfig,
const PString & defTemplate,
const PString & templateDeclaration )

Saves constructor of the class.

Parameters
[out]fs: header file name
classConfig: PClassConfig we vant to save
defTemplate: extra template definition for the class name space
templateDeclaration: basic template declaration before each method (template< ... >)

Definition at line 63 of file generator_class_cpp.cpp.

63 {
64 PString name(classConfig.getName());
65 fs << "///Constructor of class " << name << std::endl;
66 fs << templateDeclaration;
67 fs << name << defTemplate << "::" << name << "()";
68 if(classConfig.getListParentClass().size() != 0lu){
69 PVecString::const_iterator it(classConfig.getListParentClass().begin());
70 fs << std::endl << "\t: " << *it << "()";
71 if(classConfig.getListParentClass().size() > 1lu){
72 while(it != classConfig.getListParentClass().end()){
73 fs << ", " << *it << "()";
74 ++it;
75 }
76 }
77 fs << std::endl;
78 }
79 fs << "{" << std::endl;
80 fs << "\tinitialisation" << name << "();" << std::endl;
81 fs << "}" << std::endl << std::endl;
82}
const PString & getName() const
Returns the class name.
const PVecString & getListParentClass() const
Returns the list of the parents of the class.

References PClassConfig::getListParentClass(), and PClassConfig::getName().

Referenced by generator_class_cpp_source().

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

◆ class_saveClassInitialisationFunctionImpl()

void class_saveClassInitialisationFunctionImpl ( std::ofstream & fs,
const PClassConfig & classConfig,
const PString & defTemplate,
const PString & templateDeclaration )

Saves the copy function of a class.

Parameters
[out]fs: header file name
classConfig: PClassConfig we vant to save
defTemplate: extra template definition for the class name space
templateDeclaration: basic template declaration before each method (template< ... >)

Definition at line 90 of file generator_class_cpp.cpp.

90 {
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;
100 }else{
101 PString varType(it->getType());
102 bool isSimpleType = getIsSimpleType(varType);
103 bool isPtr(class_getIsPointer(varType));
104 if(isPtr){
105 fs << "\tp_" << it->getName() << " = NULL;" << std::endl;
106 }else if(isSimpleType){
107 PString defaultValue(getDefaultValueTypeInCpp(varType));
108 if(defaultValue != ""){
109 fs << "\tp_" << it->getName() << " = "<<defaultValue<<";" << std::endl;
110 }
111 }else if(varType == "PString"){
112 fs << "\tp_" << it->getName() << " = \"\";" << std::endl;
113 }
114 }
115 }
116 fs << "}" << std::endl << std::endl;
117}
const std::vector< PClassAttribute > & getListAttribute() const
Returns the list of attributes of the class.
bool class_getIsPointer(const PString &varType)
Check if the given type is a pointer or not.
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.

References class_getIsPointer(), getDefaultValueTypeInCpp(), getIsSimpleType(), PClassConfig::getListAttribute(), and PClassConfig::getName().

Referenced by generator_class_cpp_source().

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

◆ generator_checkDefaultValueExist()

bool generator_checkDefaultValueExist ( const std::vector< PClassAttribute > & vecAttr)

Check if the vector of PClassAttribute has default value.

Parameters
vecAttr: vector of attribute to be checked
Returns
true if there is at least one default value, false if there is none

Definition at line 123 of file generator_class_cpp.cpp.

123 {
124 bool hasDefaultValue = false;
125 for(std::vector<PClassAttribute>::const_iterator it(vecAttr.begin()); it != vecAttr.end() && !hasDefaultValue; ++it){
126 hasDefaultValue |= it->getDefaultValue() != "";
127 }
128 return hasDefaultValue;
129}

Referenced by generator_enum_cpp_header().

+ 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 357 of file generator_class_cpp.cpp.

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

171 {
172 if(classConfig.getClassDocumentation() != "") fs << classConfig.getClassDocumentation() << std::endl;
173 PString name(classConfig.getName());
174 PString namespaceName(classConfig.getNamespace());
175 GeneratorMode classMode(mode);
176 const PVecString & listTemplate = classConfig.getListTemplate();
177 PString defTemplate(class_getClassDefTemplate(listTemplate));
178 PString templateDeclaration(class_getClassDeclTempalteDef(listTemplate));
179 classMode.defTemplate = defTemplate;
180 classMode.templateDeclaration = templateDeclaration;
181
182 fs << classMode.templateDeclaration;
183 fs << "class ";
184 if(namespaceName != ""){
185 fs << namespaceName << "::";
186 }
187 fs << name;
188 if(classConfig.getListParentClass().size() != 0lu){
189 PVecString::const_iterator it(classConfig.getListParentClass().begin());
190 fs << " : public " << *it;
191 if(classConfig.getListParentClass().size() > 1lu){
192 while(it != classConfig.getListParentClass().end()){
193 fs << ", " << *it;
194 ++it;
195 }
196 }
197 }
198 fs << "{" << std::endl;
199 fs << "\tpublic:" << std::endl;
200 fs << "\t\t" << name << "();" << std::endl;
201 fs << "\t\tvirtual ~" << name << "() = default;" << std::endl;
202
203 manager.publicMethodDeclaration(fs, classConfig, classMode);
204 fs << "\tprotected:" << std::endl;
205 manager.protectedMethodDeclaration(fs, classConfig, classMode);
206
207 fs << "\tprivate:" << std::endl;
208 fs << "\t\tvoid initialisation" << name << "();" << std::endl;
209 manager.privateMethodDeclaration(fs, classConfig, classMode);
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;
213
214 fs << "\t\t" << it->getType() << " p_" << it->getName() << ";" << std::endl;
215 }
216 fs << "};" << std::endl << std::endl;
217 manager.classExtraFunctionDeclaration(fs, classConfig, classMode);
218 fs << std::endl << std::endl;
219}
const PString & getClassDocumentation() const
Returns the class documentation.
const PVecString & getListTemplate() const
Returns the list of the template of the class.
const PString & getNamespace() const
Returns the namespace 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(), PClassConfig::getNamespace(), 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_headerFile()

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.

Parameters
manager: PTraitBackendManager which handles all trait backend
headerFile: file to be saved
vecClassConfig: configuration of the class to be used
mode: mode of the generator
vecInclude: vector of include files to be added into the header
includeTemplate: extra include for the template implementation
Returns
true on success, false otherwise

Definition at line 230 of file generator_class_cpp.cpp.

232{
233 if(headerFile == "") return false;
234 std::ofstream fs;
235 if(!openFileStream(fs, headerFile)){return false;}
236 licenceSave(fs);
237 PString macroDef(makeMultiIncludeDefineMacro(headerFile.getFileName()));
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;
244 }
245 fs << std::endl;
246 }
247 manager.headerExtraInclude(fs, mode);
248 fs << std::endl;
249 for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
250 if(it->getIsEnum()){
251 generator_enum_cpp_header(manager, fs, *it, mode);
252// saveEnumDecl(fs, *it, mode);
253 }else{
254 generator_class_cpp_header(manager, fs, *it, mode);
255 }
256 }
257
258 if(includeTemplate != ""){
259 fs << "#include \""<<includeTemplate<<"\"" << std::endl << std::endl;
260 }
261
262 fs << std::endl << std::endl << "#endif" << std::endl << std::endl;
263 fs.close();
264 return true;
265}
void headerExtraInclude(std::ofstream &fs, const GeneratorMode &mode) const
Add extra include on the 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.
void generator_enum_cpp_header(const PTraitBackendManager &manager, std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode)
Create the declaration of the given class.
PString makeMultiIncludeDefineMacro(const PString &fileName)
Create the macro of multi inclusion file name.
void licenceSave(std::ofstream &fs)
Saves the policy.

References generator_class_cpp_header(), generator_enum_cpp_header(), PTraitBackendManager::headerExtraInclude(), licenceSave(), and makeMultiIncludeDefineMacro().

Referenced by generator_class_cpp().

+ 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 273 of file generator_class_cpp.cpp.

273 {
274 manager.classExtraFunctionImplementation(fs, classConfig, mode);
275 GeneratorMode classMode(mode);
276 const PVecString & listTemplate = classConfig.getListTemplate();
277 PString defTemplate(class_getClassDefTemplate(listTemplate));
278 PString templateDeclaration(class_getClassDeclTempalteDef(listTemplate));
279 classMode.defTemplate = defTemplate;
280 classMode.templateDeclaration = templateDeclaration;
281 if(!classConfig.getIsEnum()){
282 class_saveClassConstructorImpl(fs, classConfig, defTemplate, templateDeclaration);
283 }
284 manager.publicMethodImplementation(fs, classConfig, classMode);
285 manager.protectedMethodImplementation(fs, classConfig, classMode);
286 manager.privateMethodImplementation(fs, classConfig, classMode);
287 if(!classConfig.getIsEnum()){
288 class_saveClassInitialisationFunctionImpl(fs, classConfig, defTemplate, templateDeclaration);
289 }
290}
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_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_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_cpp_sourceFile()

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.

Parameters
manager: PTraitBackendManager which handles all trait backend
sourceFile: source file to be saved
headerFile: header to be used with this source file
vecClassConfig: vector of configuration of the classes to be used
mode: mode of the generator
isTemplateImpl: true to activate
Returns
true on success, false otherwise

Definition at line 301 of file generator_class_cpp.cpp.

303{
304 if(sourceFile == "" || headerFile == "") return false;
305 std::ofstream fs;
306 if(!openFileStream(fs, sourceFile)){return false;}
307 licenceSave(fs);
308 if(isTemplateImpl){
309 PString macroDef(makeMultiIncludeDefineMacro(headerFile.getFileName() + "_IMPL"));
310 fs << "#ifndef " << macroDef << std::endl;
311 fs << "#define " << macroDef << std::endl << std::endl;
312 }
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;
318 hasNamespace = true;
319 }
320 }
321 for(std::vector<PClassConfig>::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
322 generator_class_cpp_source(manager, fs, *it, mode);
323 }
324 if(isTemplateImpl){
325 fs << "#endif" << std::endl << std::endl;
326 }
327 fs.close();
328
329 return true;
330}
void generator_class_cpp_source(const PTraitBackendManager &manager, std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode)
Create the implementation of the given class.

References generator_class_cpp_source(), licenceSave(), and makeMultiIncludeDefineMacro().

Referenced by generator_class_cpp().

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

◆ generator_class_cpp_test()

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.

Parameters
manager: PTraitBackendManager which handles all trait backend
outputTestDir: output directory where to put the generated test (typically ../TESTS if the program is called from ./src)
vecClassConfig: vector of class configuration to be used
projectName: configuration of the project
baseFileName: base of output file name of the generated sources
mode: all modes of the generator (data/check/type/config stream)
Returns
true on success, false oterhwise

Definition at line 468 of file generator_class_cpp.cpp.

468 {
469 PString cmakeListBody;
470 cmakeListBody += getCMakeListsHeader();
471 bool b(true);
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";
475 b &= generator_class_test(manager, outputTestDir, *it, projectName, baseFileName, mode);
476 }
477 cmakeListBody += "\n\n";
478 PPath cmakelist(outputTestDir / PPath("CMakeLists.txt"));
479 b &= cmakelist.saveFileContent(cmakeListBody);
480 return b;
481}
PString getCMakeListsHeader()
Get the CMakeLists.txt header.
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.

References generator_class_test(), and getCMakeListsHeader().

Referenced by generator_class_full().

+ 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 488 of file generator_class_cpp.cpp.

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

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

◆ generator_class_test()

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.

Parameters
manager: PTraitBackendManager which handles all trait backend
outputTestDir: output directory where to put the genreated test (typically ../TESTS if the program is called from ./src)
classConfig: class configuration to be used
projectName: configuration of the project
baseFileName: base of output file name of the generated sources
mode: all modes of the generator (data/check/type/config stream)
Returns
true on success, false oterhwise

Definition at line 448 of file generator_class_cpp.cpp.

448 {
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;}
452
453 bool b(true);
454 b &= generator_class_testCMakeLists(outputCurrentTestDir, classConfig, projectName, mode);
455 b &= generator_class_testMain(manager, outputCurrentTestDir, classConfig, baseFileName, mode);
456 return b;
457}
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.
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.

References generator_class_testCMakeLists(), generator_class_testMain(), and PClassConfig::getName().

Referenced by generator_class_cpp_test().

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

◆ generator_class_testCMakeLists()

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.

Parameters
outputCurrentTestDir: current test directory in which to create the CMakeLists.txt
classConfig: class configuration to be used
projectName: configuration of the project
mode: all modes of the generator (data/check/type/config stream)
Returns
true on success, false oterhwise

Definition at line 426 of file generator_class_cpp.cpp.

426 {
427 PString body(""), testName("test_class_" + classConfig.getName().toLowerUnderscore());
428 body += getCMakeListsHeader();
429 body += "add_executable("+testName+" main.cpp)\n";
430 body += "target_link_libraries("+testName+" ${"+projectName.toUpper()+"_TEST_DEPENDENCIES})\n";
431 body += "\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";
435 body += ")\n\n\n";
436 return PPath(outputCurrentTestDir + PString("/CMakeLists.txt")).saveFileContent(body);
437}

References getCMakeListsHeader(), and PClassConfig::getName().

Referenced by generator_class_test().

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

◆ generator_class_testMain()

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.

Parameters
manager: PTraitBackendManager which handles all trait backend
outputCurrentTestDir: current test directoru in which to create the CMakeLists.txt
classConfig: class configuration to be used
baseFileName: base of output file name of the generated sources
mode: all modes of the generator (data/check/type/config stream)
Returns
true on success, false oterhwise

Definition at line 399 of file generator_class_cpp.cpp.

399 {
400 std::ofstream fs;
401 if(!openFileStream(fs, PPath(outputCurrentTestDir + PString("/main.cpp")))){return false;}
402 licenceSave(fs);
403// body += licenceSaveStr() + "\n";
404 if(mode.assertInclude != ""){
405 fs << "#include \"" + mode.assertInclude + "\"\n\n";
406 }
407 fs << "#include \"" + baseFileName + ".h\"\n\n";
408 if(classConfig.getNamespace() != ""){fs << "using namespace " << classConfig.getNamespace() << ";\n\n";}
409 manager.testFunction(fs, classConfig, mode);
410 fs << "int main(int argc, char ** argv){\n";
411 //Do other test if the enableDataStream is true
412 manager.testCallFunction(fs, classConfig, mode);
413 fs << "\treturn 0;\n";
414 fs << "}\n";
415 fs.close();
416 return true;
417}
void testCallFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Call of the test function.
void testFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of test function.
PString assertInclude
Include of the assert to be used.

References GeneratorMode::assertInclude, PClassConfig::getNamespace(), licenceSave(), PTraitBackendManager::testCallFunction(), and PTraitBackendManager::testFunction().

Referenced by generator_class_test().

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

◆ generator_enum_cpp_header()

void generator_enum_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 137 of file generator_class_cpp.cpp.

137 {
138 if(classConfig.getClassDocumentation() != "") fs << classConfig.getClassDocumentation() << std::endl;
139 PString name(classConfig.getName());
140 fs << "namespace " << name << " {\n";
141 if(classConfig.getClassDocumentation() != ""){fs << "\t"<< classConfig.getClassDocumentation() << std::endl;}
142 fs << "\tenum " << name << " {\n\t\t";
143 const std::vector<PClassAttribute> & vecAttr(classConfig.getListAttribute());
144 bool isDefaultCounterActivated = !generator_checkDefaultValueExist(vecAttr);
145 PString comma("");
146 size_t i(0lu);
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){
151 fs << " = " << i;
152 }else if(it->getDefaultValue() != ""){
153 fs << " = " << it->getDefaultValue();
154 }
155 comma = ",\n\t\t";
156 ++i;
157 }
158 fs << "\n\t};" << std::endl;
159 fs << "}" << std::endl << std::endl;
160 manager.classExtraFunctionDeclaration(fs, classConfig, mode);
161 fs << std::endl << std::endl;
162}
bool generator_checkDefaultValueExist(const std::vector< PClassAttribute > &vecAttr)
Check if the vector of PClassAttribute has default value.

References PTraitBackendManager::classExtraFunctionDeclaration(), generator_checkDefaultValueExist(), PClassConfig::getClassDocumentation(), PClassConfig::getListAttribute(), and PClassConfig::getName().

Referenced by generator_class_cpp_headerFile().

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