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 Class Reference

Rust trait generating getters and setters for every attribute. More...

#include <RustTraitGetterSetter.h>

Inheritance diagram for RustTraitGetterSetter:
Collaboration diagram for RustTraitGetterSetter:

Public Member Functions

virtual void classExtraDeclaration (std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
virtual void headerExtraInclude (std::ofstream &fs, const GeneratorMode &mode) const
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.
virtual ~RustTraitGetterSetter ()
 Desctructor of RustTraitGetterSetter.

Detailed Description

Rust trait generating getters and setters for every attribute.

Definition at line 14 of file RustTraitGetterSetter.h.

Constructor & Destructor Documentation

◆ RustTraitGetterSetter()

RustTraitGetterSetter::RustTraitGetterSetter ( )

Consctructor of RustTraitGetterSetter.

Definition at line 11 of file RustTraitGetterSetter.cpp.

References PAbstractRustTraitBackend::PAbstractRustTraitBackend().

Here is the call graph for this function:

◆ ~RustTraitGetterSetter()

RustTraitGetterSetter::~RustTraitGetterSetter ( )
virtual

Desctructor of RustTraitGetterSetter.

Definition at line 16 of file RustTraitGetterSetter.cpp.

16{}

Member Function Documentation

◆ classExtraDeclaration()

virtual void PAbstractRustTraitBackend::classExtraDeclaration ( std::ofstream & fs,
const PClassConfig & classConfig,
const GeneratorMode & mode ) const
inlinevirtualinherited

Definition at line 94 of file PAbstractTraitBackend.h.

94{}

◆ headerExtraInclude()

virtual void PAbstractRustTraitBackend::headerExtraInclude ( std::ofstream & fs,
const GeneratorMode & mode ) const
inlinevirtualinherited

Reimplemented in RustTraitCheckStream, RustTraitDataStream, and RustTraitTypeStream.

Definition at line 90 of file PAbstractTraitBackend.h.

90{}

◆ implMethodDeclaration()

void RustTraitGetterSetter::implMethodDeclaration ( std::ofstream & fs,
const PClassConfig & classConfig,
const GeneratorMode & mode ) const
virtual

Implementation of the impl block methods.

Parameters
[out]fs: file to be completed
classConfig: PClassConfig to be used
mode: mode of the generator

Reimplemented from PAbstractRustTraitBackend.

Definition at line 23 of file RustTraitGetterSetter.cpp.

23 {
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}
std::vector< PClassAttribute > PVecClassAttribute
Definition PDataConfig.h:13
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.
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.
bool enableGetter
True to enable getters trait generator.
bool enableSetter
True to enable setters trait generator.
bool getIsPointerType(const PString &varType)
Say if a given type is a pointer type (i.e. it ends with a '*').

References GeneratorMode::enableGetter, GeneratorMode::enableSetter, PClassConfig::getIsEnum(), getIsPointerType(), PClassConfig::getListAttribute(), PClassConfig::getName(), getRustType(), and getRustTypeIsCopy().

Here is the call graph for this function:

◆ testFunction()

void RustTraitGetterSetter::testFunction ( std::ofstream & fs,
const PClassConfig & classConfig,
const GeneratorMode & mode ) const
virtual

Implementation of test function.

Parameters
[out]fs: file to be completed
classConfig: PClassConfig to be used
mode: mode of the generator

Reimplemented from PAbstractRustTraitBackend.

Definition at line 81 of file RustTraitGetterSetter.cpp.

81 {
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}
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...
bool enableUnitTest
True to enable the unit tests.

References GeneratorMode::enableGetter, GeneratorMode::enableSetter, GeneratorMode::enableUnitTest, PClassConfig::getIsEnum(), getIsPointerType(), PClassConfig::getListAttribute(), PClassConfig::getName(), getRustTestValue(), getRustType(), and getRustTypeIsCopy().

Here is the call graph for this function:

The documentation for this class was generated from the following files:
  • /builds/CTA-LAPP/PHOENIX_LIBS2/code-generator/PhoenixGenerator/src/TraitBackEnd/Rust/RustTraitGetterSetter/RustTraitGetterSetter.h
  • /builds/CTA-LAPP/PHOENIX_LIBS2/code-generator/PhoenixGenerator/src/TraitBackEnd/Rust/RustTraitGetterSetter/RustTraitGetterSetter.cpp