PhoenixGenerator  3.4.0
Generate code from template files and projects in C++, Rust, Python and wrapped C++ to Python
Loading...
Searching...
No Matches
RustTraitGetterSetter.cpp
Go to the documentation of this file.
1/***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : LGPL-3.0-only
5****************************************/
6
7#include "rust_type_utils.h"
9
14
17
19
23void RustTraitGetterSetter::implMethodDeclaration(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
24 if(classConfig.getIsEnum()){return;}
25 const PVecClassAttribute & vecAttr(classConfig.getListAttribute());
26 for(PVecClassAttribute::const_iterator it(vecAttr.begin()); it != vecAttr.end(); ++it){
27 PString varType(it->getType()), fieldName(it->getName().toSnakeCase());
28 if(getIsPointerType(varType)){
29 PString pointeeType(getRustType(varType.eraseFirstLastChar(" \n\t*")));
30 if(mode.enableGetter){
31 fs << "\t///Gets the " << it->getName() << " of the " << classConfig.getName() << "\n";
32 fs << "\tpub fn get_" << fieldName << "(&self) -> Option<&" << pointeeType << ">{\n";
33 fs << "\t\tself." << fieldName << ".as_deref()\n";
34 fs << "\t}\n\n";
35 fs << "\t///Gets a mutable reference to the " << it->getName() << " of the " << classConfig.getName() << "\n";
36 fs << "\tpub fn get_" << fieldName << "_mut(&mut self) -> Option<&mut " << pointeeType << ">{\n";
37 fs << "\t\tself." << fieldName << ".as_deref_mut()\n";
38 fs << "\t}\n\n";
39 }
40 if(mode.enableSetter){
41 fs << "\t///Sets the " << it->getName() << " of the " << classConfig.getName() << "\n";
42 fs << "\tpub fn set_" << fieldName << "(&mut self, " << fieldName << ": " << pointeeType << "){\n";
43 fs << "\t\tself." << fieldName << " = Some(Box::new(" << fieldName << "));\n";
44 fs << "\t}\n\n";
45 }
46 continue;
47 }
48 PString rustType(getRustType(varType));
49 bool isCopy(getRustTypeIsCopy(varType));
50 if(mode.enableGetter){
51 fs << "\t///Gets the " << it->getName() << " of the " << classConfig.getName() << "\n";
52 if(isCopy){
53 fs << "\tpub fn get_" << fieldName << "(&self) -> " << rustType << "{\n";
54 fs << "\t\tself." << fieldName << "\n";
55 }else{
56 fs << "\tpub fn get_" << fieldName << "(&self) -> &" << rustType << "{\n";
57 fs << "\t\t&self." << fieldName << "\n";
58 }
59 fs << "\t}\n\n";
60 if(!isCopy){
61 fs << "\t///Gets a mutable reference to the " << it->getName() << " of the " << classConfig.getName() << "\n";
62 fs << "\tpub fn get_" << fieldName << "_mut(&mut self) -> &mut " << rustType << "{\n";
63 fs << "\t\t&mut self." << fieldName << "\n";
64 fs << "\t}\n\n";
65 }
66 }
67 if(mode.enableSetter){
68 fs << "\t///Sets the " << it->getName() << " of the " << classConfig.getName() << "\n";
69 fs << "\tpub fn set_" << fieldName << "(&mut self, " << fieldName << ": " << rustType << "){\n";
70 fs << "\t\tself." << fieldName << " = " << fieldName << ";\n";
71 fs << "\t}\n\n";
72 }
73 }
74}
75
77
81void RustTraitGetterSetter::testFunction(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
82 if(classConfig.getIsEnum() || !mode.enableUnitTest || (!mode.enableGetter && !mode.enableSetter)){return;}
83 PString name(classConfig.getName());
84 fs << "#[test]\n";
85 fs << "fn test_" << name.toSnakeCase() << "_getter_setter(){\n";
86 fs << "\tlet mut obj = " << name << "::default();\n";
87 const PVecClassAttribute & vecAttr(classConfig.getListAttribute());
88 for(PVecClassAttribute::const_iterator it(vecAttr.begin()); it != vecAttr.end(); ++it){
89 PString varType(it->getType()), fieldName(it->getName().toSnakeCase());
90 if(getIsPointerType(varType)){
91 PString pointeeType(getRustType(varType.eraseFirstLastChar(" \n\t*")));
92 fs << "\tlet next_" << fieldName << " = " << pointeeType << "::default();\n";
93 fs << "\tobj.set_" << fieldName << "(next_" << fieldName << ".clone());\n";
94 fs << "\tassert_eq!(*obj.get_" << fieldName << "().unwrap(), next_" << fieldName << ");\n";
95 continue;
96 }
97 PString testValue(getRustTestValue(*it));
98 if(testValue == ""){continue;} //list/nested type : no generic literal test value, skip
99 bool isCopy(getRustTypeIsCopy(varType));
100 fs << "\tobj.set_" << fieldName << "(" << testValue << ");\n";
101 if(isCopy){
102 fs << "\tassert_eq!(obj.get_" << fieldName << "(), " << testValue << ");\n";
103 }else{
104 fs << "\tassert_eq!(*obj.get_" << fieldName << "(), " << testValue << ");\n";
105 }
106 }
107 fs << "}\n\n";
108}
std::vector< PClassAttribute > PVecClassAttribute
Definition PDataConfig.h:13
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.
virtual ~RustTraitGetterSetter()
Desctructor of RustTraitGetterSetter.
virtual void implMethodDeclaration(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of the impl block methods.
RustTraitGetterSetter()
Consctructor of RustTraitGetterSetter.
virtual void testFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Implementation of test function.
bool getRustTypeIsCopy(const PString &varType)
Say if the Rust type matching a pdata type implements Copy (so getters can return it by value instead...
PString getRustType(const PString &varType)
Get the Rust type matching a pdata type.
PString getRustTestValue(const PClassAttribute &attr)
Get a non-default Rust literal value of an attribute, to be used to exercise its setter/getter in gen...
All the genertor modes.
bool enableGetter
True to enable getters trait generator.
bool enableSetter
True to enable setters trait generator.
bool enableUnitTest
True to enable the unit tests.
bool getIsPointerType(const PString &varType)
Say if a given type is a pointer type (i.e. it ends with a '*').