PhoenixGenerator  2.0.4
Set of tools to generate code
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 : CeCILL-C
6****************************************/
7
8#include "CppTraitCopy.h"
9
10
12
16void class_createCheckClassEquality(std::ofstream & fs, const PClassConfig & classConfig, bool isConst){
17 PString name(classConfig.getName()), strConst("");
18 if(isConst){
19 strConst = "const ";
20 }
21 fs << "///Check the equality between two "+name+" classes\n";
22 fs << "/**\t@param var : variable to be checked\n";
23 fs << " * \t@param reference : reference\n";
24 fs << " * \t@return true on success, false otherwise\n";
25 fs << "*/\n";
26 fs << "bool check"+name+"Equality";
27 if(isConst){
28 fs << "Const";
29 }
30 fs << "(" + strConst + name + " & var, " + strConst + name + " & reference){\n";
31 fs << "\tbool b(true);\n";
32 const PVecClassAttribute & listAttr = classConfig.getListAttribute();
33 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
34 PString attrName(it->getName().firstToUpper());
35 bool isSimpleType = getIsSimpleType(it->getType());
36// std::cout << "createCheckClassEquality : attrName = '"<<attrName<<"', isSimpleType = " << isSimpleType << ", it->getIsPointer() = " << it->getIsPointer() << ", it->getIsReference() = " << it->getIsReference() << std::endl;
37 if(isSimpleType || it->getIsPointer() || it->getType() == "PString"){
38 fs << "\tb &= var.get"+attrName+"() == reference.get"+attrName+"();\n";
39 }
40 }
41 fs << "\treturn b;\n";
42 fs << "}\n\n";
43}
44
46
50void class_createTestClassCopy(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode){
51 PString name(classConfig.getName());
52 fs << "///Check copy of class "+name+"\n";
53 fs << "void check"+name+"Copy(){\n";
54 fs << "\t"+name+" reference;\n";
55 fs << "\t//Let's use the setters\n";
56 const PVecClassAttribute & listAttr = classConfig.getListAttribute();
57 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
58 PString defaultValue(getTestDefaultValueTypeInCpp(it->getType())), attrName(it->getName().firstToUpper());;
59 if(defaultValue != ""){
60 fs << "\treference.set"+attrName+"("+defaultValue+");\n";
61 }
62 }
63 fs << "\t"+name+" varCopy(reference);\n";
64 fs << "\t"+name+" varEqual;\n";
65 fs << "\tvarEqual = reference;\n";
66 fs << "\t"<<mode.assertCall<<"(check"+name+"Equality(varCopy, reference));\n";
67 fs << "\t"<<mode.assertCall<<"(check"+name+"EqualityConst(varCopy, reference));\n";
68 fs << "\t"<<mode.assertCall<<"(check"+name+"Equality(varEqual, reference));\n";
69 fs << "\t"<<mode.assertCall<<"(check"+name+"EqualityConst(varEqual, reference));\n";
70 fs << "}\n\n";
71}
72
77
80
82
86void CppTraitCopy::publicMethodDeclaration(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
87 if(!mode.enableCopy || classConfig.getIsEnum()){return;}
88 PString name(classConfig.getName());
89 fs << "\t\t" << name << "(const " << name << mode.defTemplate << " & other);" << std::endl;
90 fs << "\t\t" << name << " & operator = (const " << name << mode.defTemplate << " & other);" << std::endl;
91}
92
94
98void CppTraitCopy::publicMethodImplementation(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
99 if(!mode.enableCopy || classConfig.getIsEnum()){return;}
100 PString name(classConfig.getName());
101 fs << "///Copy Constructor of class " << name << std::endl;
102 fs << "/**\t@param other : " << name << " we want ot copy" << std::endl;
103 fs << "*/" << std::endl;
104 fs << mode.templateDeclaration;
105 fs << name << mode.defTemplate << "::" << name << "(const " << name << mode.defTemplate << " & other){" << std::endl;
106 fs << "\tcopy" << name << "(other);" << std::endl;
107 fs << "}" << std::endl << std::endl;
108 fs << "///Operator = of class " << name << std::endl;
109 fs << "/**\t@param other : " << name << " we want ot copy" << std::endl;
110 fs << " * \t@return copied class " << name << std::endl;
111 fs << "*/" << std::endl;
112 fs << mode.templateDeclaration;
113 fs << name << mode.defTemplate << " & " << name << mode.defTemplate << "::operator =" << " (const " << name << mode.defTemplate << " & other){" << std::endl;
114 fs << "\tcopy" << name << "(other);" << std::endl;
115 fs << "\treturn *this;" << std::endl;
116 fs << "}" << std::endl << std::endl;
117}
118
120
124void CppTraitCopy::protectedMethodDeclaration(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
125 if(!mode.enableCopy || classConfig.getIsEnum()){return;}
126 PString name(classConfig.getName());
127 fs << "\t\tvoid copy" << name << "(const " << name << mode.defTemplate << " & other);" << std::endl;
128}
129
131
135void CppTraitCopy::protectedMethodImplementation(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
136 if(!mode.enableCopy || classConfig.getIsEnum()){return;}
137 PString name(classConfig.getName());
138 fs << "///Copy Function of class " << name << std::endl;
139 fs << "/**\t@param other : " << name << " we want ot copy" << std::endl;
140 fs << "*/" << std::endl;
141 fs << mode.templateDeclaration;
142 fs << "void " << name << mode.defTemplate << "::copy" << name << "(const " << name << mode.defTemplate << " & other){" << std::endl;
143 const PVecClassAttribute & vecAttr(classConfig.getListAttribute());
144 for(PVecClassAttribute::const_iterator it(vecAttr.begin()); it != vecAttr.end(); ++it){
145 fs << "\tp_" << it->getName() << " = other.p_" << it->getName() << ";" << std::endl;
146 }
147 fs << "}" << std::endl << std::endl;
148}
149
150
152
156void CppTraitCopy::testFunction(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
157 if(!mode.enableCopy || !mode.enableGetter || classConfig.getIsEnum()){return;}
158 class_createCheckClassEquality(fs, classConfig, false);
159 class_createCheckClassEquality(fs, classConfig, true);
160 class_createTestClassCopy(fs, classConfig, mode);
161}
162
164
168void CppTraitCopy::testCallFunction(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
169 if(!mode.enableCopy || classConfig.getIsEnum()){return;}
170 PString name(classConfig.getName());
171 if(!mode.enableGetter){
172 fs << "\t//Cannot create check"+name+"Copy() because there is no getter enabled" << std::endl;
173 return;
174 }
175 fs << "\tcheck"+name+"Copy();\n";
176}
177
178
179
180
void class_createCheckClassEquality(std::ofstream &fs, const PClassConfig &classConfig, bool isConst)
Create the class check.
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 defTemplate
Template definition for the class header.
PString templateDeclaration
Template declaration of the method implementation.
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 getIsSimpleType(const PString &varType)
Check if the given type is a simple type.