PhoenixGenerator  2.2.0
Set of tools to generate code
Loading...
Searching...
No Matches
NanobindTraitCheckStream Class Reference

Class generator for Nanobind property trait. More...

#include <NanobindTraitCheckStream.h>

+ Inheritance diagram for NanobindTraitCheckStream:
+ Collaboration diagram for NanobindTraitCheckStream:

Public Member Functions

virtual void headerExtraInclude (std::set< std::string > &includes, const PClassConfig &classConfig, const GeneratorMode &mode) const
 
virtual void headerTestInclude (std::ofstream &fs, const GeneratorMode &mode, const PString &baseFileName) const
 
 NanobindTraitCheckStream ()
 Constructor of NanobindTraitCheckStream.
 
virtual void registerConstructor (std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
 
virtual void registerMethod (std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
 
virtual void registerProperty (std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
 
virtual void registerStaticMethod (std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
 
virtual void testCallFunction (std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
 
virtual void testFunction (std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode, const PString &baseFileName) const
 
virtual ~NanobindTraitCheckStream ()
 Destructor of NanobindTraitCheckStream.
 

Detailed Description

Class generator for Nanobind property trait.

Definition at line 15 of file NanobindTraitCheckStream.h.

Constructor & Destructor Documentation

◆ NanobindTraitCheckStream()

NanobindTraitCheckStream::NanobindTraitCheckStream ( )

Constructor of NanobindTraitCheckStream.

Definition at line 11 of file NanobindTraitCheckStream.cpp.

References PAbstractNanobindTraitBackend::PAbstractNanobindTraitBackend().

+ Here is the call graph for this function:

◆ ~NanobindTraitCheckStream()

NanobindTraitCheckStream::~NanobindTraitCheckStream ( )
virtual

Destructor of NanobindTraitCheckStream.

Definition at line 16 of file NanobindTraitCheckStream.cpp.

16{}

Member Function Documentation

◆ headerExtraInclude()

virtual void PAbstractNanobindTraitBackend::headerExtraInclude ( std::set< std::string > & includes,
const PClassConfig & classConfig,
const GeneratorMode & mode ) const
inlinevirtualinherited

Reimplemented in NanobindTraitInclude.

Definition at line 70 of file PAbstractTraitBackend.h.

70{}

◆ headerTestInclude()

virtual void PAbstractNanobindTraitBackend::headerTestInclude ( std::ofstream & fs,
const GeneratorMode & mode,
const PString & baseFileName ) const
inlinevirtualinherited

Reimplemented in NanobindTraitTestSetup.

Definition at line 71 of file PAbstractTraitBackend.h.

71{}

◆ registerConstructor()

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

Reimplemented in NanobindTraitMethod.

Definition at line 76 of file PAbstractTraitBackend.h.

76{}

◆ registerMethod()

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

Reimplemented in NanobindTraitDataStream.

Definition at line 75 of file PAbstractTraitBackend.h.

75{}

◆ registerProperty()

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

Reimplemented in NanobindTraitProperty.

Definition at line 73 of file PAbstractTraitBackend.h.

73{}

◆ registerStaticMethod()

void NanobindTraitCheckStream::registerStaticMethod ( std::ofstream & fs,
const PClassConfig & classConfig,
const GeneratorMode & mode ) const
virtual

Registration of static method

Parameters
fsstream to write to
classConfigconfig of the class
modegenerator mode

Reimplemented from PAbstractNanobindTraitBackend.

Definition at line 22 of file NanobindTraitCheckStream.cpp.

22 {
23 if(!mode.enableCheckStream){return;}
24 PString name(classConfig.getName());
25 fs << std::endl << std::endl << "\t\t///Check stream method to compare two " << name << " objects";
26 fs << std::endl << "\t\t.def_static(\"check_stream\", [](const std::string & fieldDescription, const " << name << " & data, const " << name << " & reference) -> bool {";
27 fs << std::endl << "\t\t\tstd::stringstream out;";
28 fs << std::endl << "\t\t\tif(!CheckStream<" << name << ">::check_stream(fieldDescription, data, reference, out)){";
29 fs << std::endl << "\t\t\t\tthrow nb::value_error(out.str().c_str());";
30 fs << std::endl << "\t\t\t}";
31 fs << std::endl << "\t\t\treturn true;";
32 fs << std::endl << "\t\t}, ";
33 fs << std::endl << "\t\tnb::arg(\"fieldDescription\"), nb::arg(\"data\"), nb::arg(\"reference\"),";
34 fs << std::endl << "\t\t\"Check stream for " << name << " (equivalent to CheckStream<" << name << ">::check_stream)\")";
35}
const PString & getName() const
Returns the class name.
bool enableCheckStream
True to enable check stream interface generator.

References GeneratorMode::enableCheckStream, and PClassConfig::getName().

+ Here is the call graph for this function:

◆ testCallFunction()

void NanobindTraitCheckStream::testCallFunction ( std::ofstream & fs,
const PClassConfig & classConfig,
const GeneratorMode & mode ) const
virtual

Generate test call to check stream function

Parameters
fsstream to write to
classConfigconfig of the class
modegenerator mode

Reimplemented from PAbstractNanobindTraitBackend.

Definition at line 104 of file NanobindTraitCheckStream.cpp.

104 {
105 if(!mode.enableUnitTest || !mode.enableCheckStream){return;}
106 PString name(classConfig.getName());
107 fs << "test_check_stream()" << std::endl;
108}
bool enableUnitTest
True to enable the unit tests.

References GeneratorMode::enableCheckStream, GeneratorMode::enableUnitTest, and PClassConfig::getName().

+ Here is the call graph for this function:

◆ testFunction()

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

Generate test function for check stream

Parameters
fsstream to write to
classConfigconfig of the class
modegenerator mode
baseFileName: base name of the configuration

Reimplemented from PAbstractNanobindTraitBackend.

Definition at line 42 of file NanobindTraitCheckStream.cpp.

42 {
43 if(!mode.enableUnitTest || !mode.enableCheckStream){return;}
44 PString name(classConfig.getName());
45 PString moduleName = baseFileName.toLower() + "_module";
46
47 fs << "def test_check_stream():" << std::endl;
48 fs << "\t\"\"\"Test CheckStream functionality\"\"\"" << std::endl;
49 fs << std::endl;
50 fs << "\t# Create two identical " << name << std::endl;
51 fs << "\ttest_value1 = " << moduleName << "." << name << "()" << std::endl;
52 const PVecClassAttribute & listAttr(classConfig.getListAttribute());
53 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
54 PString defaultValue = getTestDefaultValueTypeInPython(it->getType());
55 fs << "\ttest_value1." << it->getName() << " = " << defaultValue << std::endl;
56 }
57 fs << std::endl;
58 fs << "\ttest_value2 = " << moduleName << "." << name << "()" << std::endl;
59 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
60 PString defaultValue = getTestDefaultValueTypeInPython(it->getType());
61 fs << "\ttest_value2." << it->getName() << " = " << defaultValue << std::endl;
62 }
63 fs << std::endl;
64 fs << "\t# Test identical " << name << " - should return True" << std::endl;
65 fs << "\tresult_identical = " << moduleName << "." << name << ".check_stream(\"Test identical\", test_value1, test_value2)" << std::endl;
66 fs << "\tassert result_identical == True, \"CheckStream should return True for identical " << name << "\"" << std::endl;
67 fs << std::endl;
68 fs << "\t# Create a different " << name << std::endl;
69 fs << "\ttest_value3 = " << moduleName << "." << name << "()" << std::endl;
70 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
71 PString defaultValue = getTestDefaultValueTypeInPython(it->getType());
72 PString differentValue = defaultValue;
73
74 if(it == listAttr.begin()){
75 if(it->getType() == "int" || it->getType() == "short" || it->getType() == "long" || it->getType() == "size_t"){
76 differentValue = defaultValue + " + 1"; // 42 + 1 = 43
77 }else if(it->getType() == "float" || it->getType() == "double"){
78 differentValue = defaultValue + " + 0.5"; // 1.0 + 0.5 = 1.5
79 }else if(it->getType() == "bool"){
80 differentValue = "not " + defaultValue; // not True = False
81 }else if(it->getType() == "std::string" || it->getType() == "PString"){
82 differentValue = defaultValue + " + \"_modified\""; // "Some string" + "_modified"
83 }else{
84 differentValue = "\"different_value\"";
85 }
86 }
87
88 fs << "\ttest_value3." << it->getName() << " = " << differentValue << std::endl;
89 }
90 fs << std::endl;
91 fs << "\t#test different " << name << " - should return False" << std::endl;
92 fs << "\ttry:" << std::endl;
93 fs << "\t\tresult_different = " << moduleName << "." << name << ".check_stream(\"Test different\", test_value1, test_value3)" << std::endl;
94 fs << "\t\tassert False, \"CheckStream should raise an exception for different " << name << "\"" << std::endl << std::endl;
95 fs << "\texcept:" << std::endl;
96 fs << "\t\tassert True" << std::endl;
97 // fs << "\tassert result_different == False, \"CheckStream should return False for different " << name << "\"" << std::endl << std::endl;
98}
std::vector< PClassAttribute > PVecClassAttribute
Definition PDataConfig.h:13
const std::vector< PClassAttribute > & getListAttribute() const
Returns the list of attributes of the class.
PString getTestDefaultValueTypeInPython(const PString &type)
Get default test value for a given type in Python.

References GeneratorMode::enableCheckStream, GeneratorMode::enableUnitTest, PClassConfig::getListAttribute(), PClassConfig::getName(), and getTestDefaultValueTypeInPython().

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: