PhoenixGenerator  3.4.0
Generate code from template files and projects in C++, Rust, Python and wrapped C++ to Python
Loading...
Searching...
No Matches
CppTraitCopy.cpp
Go to the documentation of this file.
1
2/***************************************
3 Auteur : Pierre Aubert
4 Mail : pierre.aubert@lapp.in2p3.fr
5 Licence : LGPL-3.0-only
6****************************************/
7
8#include "type_utils.h"
9#include "CppTraitCopy.h"
10
11
13
17void class_createCheckClassEquality(std::ofstream & fs, const PClassConfig & classConfig, bool isConst){
18 PString name(classConfig.getName()), strConst("");
19 if(isConst){
20 strConst = "const ";
21 }
22 fs << "///Check the equality between two "+name+" classes\n";
23 fs << "/**\t@param var : variable to be checked\n";
24 fs << " * \t@param reference : reference\n";
25 fs << " * \t@return true on success, false otherwise\n";
26 fs << "*/\n";
27 fs << "bool check"+name+"Equality";
28 if(isConst){
29 fs << "Const";
30 }
31 fs << "(" + strConst + name + " & var, " + strConst + name + " & reference){\n";
32 fs << "\tbool b(true);\n";
33 const PVecClassAttribute & listAttr = classConfig.getListAttribute();
34 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
35 PString attrName(it->getName().firstToUpper());
36 bool isSimpleType = getIsSimpleType(it->getType());
37// std::cout << "createCheckClassEquality : attrName = '"<<attrName<<"', isSimpleType = " << isSimpleType << ", it->getIsPointer() = " << it->getIsPointer() << ", it->getIsReference() = " << it->getIsReference() << std::endl;
38 if(isSimpleType || it->getIsPointer() || it->getType() == "PString"){
39 fs << "\tb &= var.get"+attrName+"() == reference.get"+attrName+"();\n";
40 }
41 }
42 fs << "\treturn b;\n";
43 fs << "}\n\n";
44}
45
47
50bool copy_hasPointerAttribute(const PClassConfig & classConfig){
51 const PVecClassAttribute & listAttr(classConfig.getListAttribute());
52 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
53 if(getIsPointerType(it->getType())){return true;}
54 }
55 return false;
56}
57
59
63void class_createTestClassCopy(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode){
64 PString name(classConfig.getName());
65 fs << "///Check copy of class "+name+"\n";
66 fs << "void check"+name+"Copy(){\n";
67 fs << "\t"+name+" reference;\n";
68 fs << "\t//Let's use the setters\n";
69 const PVecClassAttribute & listAttr = classConfig.getListAttribute();
70 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
71 PString defaultValue(getTestDefaultValueTypeInCpp(it->getType())), attrName(it->getName().firstToUpper());;
72 if(defaultValue != ""){
73 fs << "\treference.set"+attrName+"("+defaultValue+");\n";
74 }
75 }
76 fs << "\t"+name+" varCopy(reference);\n";
77 fs << "\t"+name+" varEqual;\n";
78 fs << "\tvarEqual = reference;\n";
79 fs << "\t"<<mode.assertCall<<"(check"+name+"Equality(varCopy, reference));\n";
80 fs << "\t"<<mode.assertCall<<"(check"+name+"EqualityConst(varCopy, reference));\n";
81 fs << "\t"<<mode.assertCall<<"(check"+name+"Equality(varEqual, reference));\n";
82 fs << "\t"<<mode.assertCall<<"(check"+name+"EqualityConst(varEqual, reference));\n";
83 fs << "}\n\n";
84}
85
90
93
95
99void CppTraitCopy::publicMethodDeclaration(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
100 //Rule of zero : no hand-written copy constructor / operator= anymore, let the compiler generate them (or delete them, for classes owning a unique_ptr attribute)
101}
102
104
108void CppTraitCopy::publicMethodImplementation(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
109 //Rule of zero : no hand-written copy constructor / operator= anymore, let the compiler generate them (or delete them, for classes owning a unique_ptr attribute)
110}
111
113
117void CppTraitCopy::protectedMethodDeclaration(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
118 //Rule of zero : no hand-written copy<Name>() helper anymore
119}
120
122
126void CppTraitCopy::protectedMethodImplementation(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
127 //Rule of zero : no hand-written copy<Name>() helper anymore
128}
129
130
132
136void CppTraitCopy::testFunction(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
137 if(!mode.enableCopy || !mode.enableGetter || classConfig.getIsEnum()){return;}
138 class_createCheckClassEquality(fs, classConfig, false);
139 class_createCheckClassEquality(fs, classConfig, true);
140 //A class owning a pointer attribute (std::unique_ptr storage) is no longer copyable under rule of zero, so no copy test can be generated for it
141 if(!copy_hasPointerAttribute(classConfig)){
142 class_createTestClassCopy(fs, classConfig, mode);
143 }
144}
145
147
151void CppTraitCopy::testCallFunction(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
152 if(!mode.enableCopy || classConfig.getIsEnum()){return;}
153 PString name(classConfig.getName());
154 if(!mode.enableGetter){
155 fs << "\t//Cannot create check"+name+"Copy() because there is no getter enabled" << std::endl;
156 return;
157 }
158 if(copy_hasPointerAttribute(classConfig)){
159 fs << "\t//Cannot create check"+name+"Copy() because "+name+" owns a pointer attribute and is not copyable anymore" << std::endl;
160 return;
161 }
162 fs << "\tcheck"+name+"Copy();\n";
163}
164
165
166
167
void class_createCheckClassEquality(std::ofstream &fs, const PClassConfig &classConfig, bool isConst)
Create the class check.
bool copy_hasPointerAttribute(const PClassConfig &classConfig)
Check if the given class owns at least one pointer attribute.
void class_createTestClassCopy(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode)
Create the class check.
std::vector< PClassAttribute > PVecClassAttribute
Definition PDataConfig.h:13
virtual void publicMethodDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Declaration of public methods.
virtual void publicMethodImplementation(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of public methods.
CppTraitCopy()
Consctructor of CppTraitCopy.
virtual void testFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of test function.
virtual ~CppTraitCopy()
Desctructor of CppTraitCopy.
virtual void protectedMethodImplementation(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of protected methods.
virtual void protectedMethodDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Declaration of protected methods.
virtual void testCallFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Call of the test function.
Class to describe a basic class.
const std::vector< PClassAttribute > & getListAttribute() const
Returns the list of attributes of the class.
const PString & getName() const
Returns the class name.
bool getIsEnum() const
Say if the current PClassConfig is an enum.
All the genertor modes.
bool enableGetter
True to enable getters trait generator.
PString assertCall
Assert to be used.
bool enableCopy
True to enable copy trait generator.
PString getTestDefaultValueTypeInCpp(const PString &type)
Get the default value of a type in C++.
bool getIsPointerType(const PString &varType)
Say if a given type is a pointer type (i.e. it ends with a '*').
bool getIsSimpleType(const PString &varType)
Check if the given type is a simple type.