PhoenixGenerator
2.8.0
Set of tools to generate code
Toggle main menu visibility
Loading...
Searching...
No Matches
NanobindTraitCheckStream.cpp
Go to the documentation of this file.
1
/***************************************
2
Auteur : Thibaut Oprinsen
3
Mail : thibaut.oprinsen@lapp.in2p3.fr
4
Licence : CeCILL-C
5
****************************************/
6
7
#include "
NanobindTraitCheckStream.h
"
8
#include "
type_utils.h
"
9
11
NanobindTraitCheckStream::NanobindTraitCheckStream
()
12
:
PAbstractNanobindTraitBackend
()
13
{}
14
16
NanobindTraitCheckStream::~NanobindTraitCheckStream
(){}
17
22
void
NanobindTraitCheckStream::registerStaticMethod
(std::ofstream & fs,
const
PClassConfig
& classConfig,
const
GeneratorMode
& mode)
const
{
23
if
(!mode.
enableCheckStream
){
return
;}
24
PString name(classConfig.
getName
());
25
if
(classConfig.
getIsEnum
()){
26
name = name +
"::"
+ name;
27
}
28
fs << std::endl << std::endl <<
"\t\t///Check stream method to compare two "
<< name <<
" objects"
;
29
fs << std::endl <<
"\t\t.def_static(\"check_stream\", [](const std::string & fieldDescription, const "
<< name <<
" & data, const "
<< name <<
" & reference) -> bool {"
;
30
fs << std::endl <<
"\t\t\tstd::stringstream out;"
;
31
fs << std::endl <<
"\t\t\tif(!CheckStream<"
<< name <<
">::check_stream(fieldDescription, data, reference, out)){"
;
32
fs << std::endl <<
"\t\t\t\tthrow nb::value_error(out.str().c_str());"
;
33
fs << std::endl <<
"\t\t\t}"
;
34
fs << std::endl <<
"\t\t\treturn true;"
;
35
fs << std::endl <<
"\t\t}, "
;
36
fs << std::endl <<
"\t\tnb::arg(\"fieldDescription\"), nb::arg(\"data\"), nb::arg(\"reference\"),"
;
37
fs << std::endl <<
"\t\t\"Check stream for "
<< name <<
" (equivalent to CheckStream<"
<< name <<
">::check_stream)\")"
;
38
}
39
45
void
NanobindTraitCheckStream::testFunction
(std::ofstream & fs,
const
PClassConfig
& classConfig,
const
GeneratorMode
& mode,
const
PString & baseFileName)
const
{
46
if
(!mode.
enableUnitTest
|| !mode.
enableCheckStream
){
return
;}
47
PString name(classConfig.
getName
());
48
PString moduleName = baseFileName.toLower();
49
50
fs <<
"def test_check_stream():"
<< std::endl;
51
fs <<
"\t\"\"\"Test CheckStream functionality\"\"\""
<< std::endl;
52
fs << std::endl;
53
fs <<
"\t# Create two identical "
<< name << std::endl;
54
const
PVecClassAttribute
& listAttr(classConfig.
getListAttribute
());
55
if
(classConfig.
getIsEnum
()){
56
if
(listAttr.size() != 0lu){
57
fs <<
"\ttest_value1 = "
<< moduleName <<
"."
<< name <<
"."
<< listAttr.front().getName() << std::endl;
58
fs <<
"\ttest_value2 = "
<< moduleName <<
"."
<< name <<
"."
<< listAttr.front().getName() << std::endl;
59
}
60
fs <<
"\t# Test identical "
<< name <<
" - should return True"
<< std::endl;
61
fs <<
"\tresult_identical = "
<< moduleName <<
"."
<< name <<
".check_stream(\"Test identical\", test_value1, test_value2)"
<< std::endl;
62
fs <<
"\tassert result_identical == True, \"CheckStream should return True for identical "
<< name <<
"\""
<< std::endl;
63
}
else
{
64
fs <<
"\ttest_value1 = "
<< moduleName <<
"."
<< name <<
"()"
<< std::endl;
65
66
for
(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
67
PString defaultValue =
getTestDefaultValueTypeInPython
(it->getType());
68
if
(defaultValue !=
""
){
69
fs <<
"\ttest_value1."
<< it->getName() <<
" = "
<< defaultValue << std::endl;
70
}
71
}
72
fs << std::endl;
73
fs <<
"\ttest_value2 = "
<< moduleName <<
"."
<< name <<
"()"
<< std::endl;
74
for
(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
75
PString defaultValue =
getTestDefaultValueTypeInPython
(it->getType());
76
if
(defaultValue !=
""
){
77
fs <<
"\ttest_value2."
<< it->getName() <<
" = "
<< defaultValue << std::endl;
78
}
79
}
80
fs << std::endl;
81
fs <<
"\t# Test identical "
<< name <<
" - should return True"
<< std::endl;
82
fs <<
"\tresult_identical = "
<< moduleName <<
"."
<< name <<
".check_stream(\"Test identical\", test_value1, test_value2)"
<< std::endl;
83
fs <<
"\tassert result_identical == True, \"CheckStream should return True for identical "
<< name <<
"\""
<< std::endl;
84
fs << std::endl;
85
}
86
}
87
92
void
NanobindTraitCheckStream::testCallFunction
(std::ofstream & fs,
const
PClassConfig
& classConfig,
const
GeneratorMode
& mode)
const
{
93
if
(!mode.
enableUnitTest
|| !mode.
enableCheckStream
){
return
;}
94
PString name(classConfig.
getName
());
95
fs <<
"test_check_stream()"
<< std::endl;
96
}
NanobindTraitCheckStream.h
PVecClassAttribute
std::vector< PClassAttribute > PVecClassAttribute
Definition
PDataConfig.h:13
NanobindTraitCheckStream::~NanobindTraitCheckStream
virtual ~NanobindTraitCheckStream()
Destructor of NanobindTraitCheckStream.
Definition
NanobindTraitCheckStream.cpp:16
NanobindTraitCheckStream::testCallFunction
virtual void testCallFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Definition
NanobindTraitCheckStream.cpp:92
NanobindTraitCheckStream::registerStaticMethod
virtual void registerStaticMethod(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
Definition
NanobindTraitCheckStream.cpp:22
NanobindTraitCheckStream::NanobindTraitCheckStream
NanobindTraitCheckStream()
Constructor of NanobindTraitCheckStream.
Definition
NanobindTraitCheckStream.cpp:11
NanobindTraitCheckStream::testFunction
virtual void testFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode, const PString &baseFileName) const
Definition
NanobindTraitCheckStream.cpp:45
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::enableCheckStream
bool enableCheckStream
True to enable check stream interface generator.
Definition
GeneratorMode.h:37
GeneratorMode::enableUnitTest
bool enableUnitTest
True to enable the unit tests.
Definition
GeneratorMode.h:35
getTestDefaultValueTypeInPython
PString getTestDefaultValueTypeInPython(const PString &type)
Get default test value for a given type in Python.
Definition
type_utils.cpp:127
type_utils.h
src
TraitBackEnd
Nanobind
NanobindTraitCheckStream
NanobindTraitCheckStream.cpp
Generated by
1.17.0