PhoenixGenerator
2.8.0
Set of tools to generate code
Toggle main menu visibility
Loading...
Searching...
No Matches
NanobindTraitProperty.cpp
Go to the documentation of this file.
1
/***************************************
2
Auteur : Thibaut Oprinsen, Pierre Aubert
3
Mail : thibaut.oprinsen@lapp.in2p3.fr, pierre.aubert@lapp.in2p3.fr
4
Licence : CeCILL-C
5
****************************************/
6
7
#include "
NanobindTraitProperty.h
"
8
10
NanobindTraitProperty::NanobindTraitProperty
()
11
:
PAbstractNanobindTraitBackend
()
12
{}
13
15
NanobindTraitProperty::~NanobindTraitProperty
(){}
16
18
21
PString
createGetterMethodName
(
const
PString & varName){
22
return
"get"
+ varName.firstToUpper();
23
}
24
26
29
PString
createSetterMethodName
(
const
PString & varName){
30
return
"set"
+ varName.firstToUpper();
31
}
32
37
void
NanobindTraitProperty::registerProperty
(std::ofstream & fs,
const
PClassConfig
& classConfig,
const
GeneratorMode
& mode)
const
{
38
const
PVecClassAttribute
& listAttr(classConfig.
getListAttribute
());
39
if
(classConfig.
getIsEnum
()){
40
PString enumName = classConfig.
getName
();
41
for
(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
42
PString attributeValue(it->getName());
43
fs <<
"\t\t.value(\""
<< it->getName() <<
"\","
<< enumName <<
"::"
<< attributeValue <<
")"
<< std::endl;
44
}
45
return
;
46
}
47
if
(!mode.
enableSetter
|| !mode.
enableGetter
){
return
;}
48
for
(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
49
fs <<
"\t\t.def_prop_rw(\""
<< it->getName() <<
"\","
;
50
fs << std::endl <<
"\t\t\t[]( "
<< classConfig.
getName
() <<
" &c) { return c."
<<
createGetterMethodName
(it->getName()) <<
"(); },"
;
51
fs << std::endl <<
"\t\t\t[]( "
<< classConfig.
getName
() <<
" &c, "
<<
makeVarType
(it->getType(),
true
,
false
,
false
, it->getIsPointer()) <<
" value) { c."
<<
createSetterMethodName
(it->getName()) <<
"(value); })"
;
52
if
(it != listAttr.end() - 1){
53
fs << std::endl;
54
}
55
}
56
}
57
63
void
NanobindTraitProperty::testFunction
(std::ofstream & fs,
const
PClassConfig
& classConfig,
const
GeneratorMode
& mode,
const
PString & baseFileName)
const
{
64
if
(!mode.
enableSetter
|| !mode.
enableSetter
|| classConfig.
getIsEnum
()){
return
;}
65
PString moduleName = baseFileName.toLower();
66
PString className = classConfig.
getName
().toLower();
67
fs <<
"#Test getters and setters of the "
<< classConfig.
getName
() <<
" property trait"
<< std::endl;
68
fs <<
"def test_attributes_setters_getters():"
<< std::endl;
69
fs <<
"\t\"\"\"Test setters and getters of attributes generated by Nanobind\"\"\""
<< std::endl;
70
fs <<
"\t"
<<
"# Test default constructor"
<< std::endl;
71
fs <<
"\t"
<< className <<
" = "
<< moduleName <<
"."
<< classConfig.
getName
() <<
"()"
<< std::endl;
72
fs <<
"\t"
<<
"# Test properties"
<< std::endl;
73
const
PVecClassAttribute
& listAttr(classConfig.
getListAttribute
());
74
for
(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
75
PString defaultValue =
getTestDefaultValueTypeInPython
(it->getType());
76
if
(defaultValue !=
""
){
77
fs <<
"\t"
<< className <<
"."
<< it->getName() <<
" = "
<< defaultValue << std::endl;
78
fs <<
"\t"
<<
"assert "
<< className <<
"."
<< it->getName() <<
" == "
<< defaultValue << std::endl;
79
}
80
}
81
fs << std::endl;
82
}
83
88
void
NanobindTraitProperty::testCallFunction
(std::ofstream & fs,
const
PClassConfig
& classConfig,
const
GeneratorMode
& mode)
const
{
89
if
(!mode.
enableSetter
|| !mode.
enableSetter
|| classConfig.
getIsEnum
()){
return
;}
90
91
fs <<
"\ttest_attributes_setters_getters()"
<< std::endl;
92
}
createSetterMethodName
PString createSetterMethodName(const PString &varName)
Creates a setter method name.
Definition
NanobindTraitProperty.cpp:29
createGetterMethodName
PString createGetterMethodName(const PString &varName)
Creates a getter method name.
Definition
NanobindTraitProperty.cpp:21
NanobindTraitProperty.h
PVecClassAttribute
std::vector< PClassAttribute > PVecClassAttribute
Definition
PDataConfig.h:13
NanobindTraitProperty::~NanobindTraitProperty
virtual ~NanobindTraitProperty()
Destructor of NanobindTraitProperty.
Definition
NanobindTraitProperty.cpp:15
NanobindTraitProperty::NanobindTraitProperty
NanobindTraitProperty()
Constructor of NanobindTraitProperty.
Definition
NanobindTraitProperty.cpp:10
NanobindTraitProperty::testCallFunction
virtual void testCallFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Definition
NanobindTraitProperty.cpp:88
NanobindTraitProperty::registerProperty
virtual void registerProperty(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Registration of property.
Definition
NanobindTraitProperty.cpp:37
NanobindTraitProperty::testFunction
virtual void testFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode, const PString &baseFileName) const
Definition
NanobindTraitProperty.cpp:63
PAbstractNanobindTraitBackend::PAbstractNanobindTraitBackend
PAbstractNanobindTraitBackend()
Definition
PAbstractTraitBackend.h:67
PClassConfig
Class to describe a basic class.
Definition
PClassConfig.h:14
PClassConfig::getListAttribute
const std::vector< PClassAttribute > & getListAttribute() const
Returns the list of attributes of the class.
Definition
PClassConfig.cpp:167
PClassConfig::getName
const PString & getName() const
Returns the class name.
Definition
PClassConfig.cpp:129
PClassConfig::getIsEnum
bool getIsEnum() const
Say if the current PClassConfig is an enum.
Definition
PClassConfig.cpp:197
GeneratorMode
All the genertor modes.
Definition
GeneratorMode.h:27
GeneratorMode::enableGetter
bool enableGetter
True to enable getters trait generator.
Definition
GeneratorMode.h:42
GeneratorMode::enableSetter
bool enableSetter
True to enable setters trait generator.
Definition
GeneratorMode.h:40
getTestDefaultValueTypeInPython
PString getTestDefaultValueTypeInPython(const PString &type)
Get default test value for a given type in Python.
Definition
type_utils.cpp:127
makeVarType
PString makeVarType(const PString &varType, bool isSetter, bool isConst, bool isRef, bool isPtr)
Makes the var type by taking account of the type.
Definition
type_utils.cpp:153
src
TraitBackEnd
Nanobind
NanobindTraitProperty
NanobindTraitProperty.cpp
Generated by
1.17.0