22 PString name(classConfig.
getName());
23 fs << std::endl << std::endl <<
"\t\t///Deserialization method for " << name;
24 fs << std::endl <<
"\t\t.def(\"from_bytes\", []( " << name <<
" & self, const nb::bytearray &b) -> bool {";
25 fs << std::endl <<
"\t\t\tDataStreamIter iter((DataStreamIter)b.data());;";
26 fs << std::endl <<
"\t\t\treturn data_message_load(iter, self);";
27 fs << std::endl <<
"\t\t}, ";
28 fs << std::endl <<
"\t\t\"Load a " << name <<
" from a bytearray\"";
29 fs << std::endl <<
"\t\t)";
37 PString name(classConfig.
getName());
38 fs << std::endl << std::endl <<
"\t\t///Serialization method for " << name;
39 fs << std::endl <<
"\t\t.def(\"to_bytes\", [](const " << name <<
" & self) -> nb::bytearray {";
40 fs << std::endl <<
"\t\t\tsize_t size = data_size(self);";
41 fs << std::endl <<
"\t\t\tnb::bytearray msg(NULL, size);";
42 fs << std::endl <<
"\t\t\tDataStreamIter iter((DataStreamIter)msg.data());";
43 fs << std::endl <<
"\t\t\tbool success = data_message_save(iter, self);";
44 fs << std::endl <<
"\t\t\tif(success){";
45 fs << std::endl <<
"\t\t\t\treturn msg;";
46 fs << std::endl <<
"\t\t\t}else{";
47 fs << std::endl <<
"\t\t\t\tthrow nb::buffer_error(\"Failed to serialize " << name <<
"\");";
48 fs << std::endl <<
"\t\t\t}";
49 fs << std::endl <<
"\t\t}, ";
50 fs << std::endl <<
"\t\t\"Serialize the " << name <<
" to bytearray\"";
51 fs << std::endl <<
"\t\t)";
59 PString name(classConfig.
getName());
60 fs << std::endl << std::endl <<
"\t\t///Size method for " << name;
61 fs << std::endl <<
"\t\t.def(\"get_size\", [](const " << name <<
" & self) -> size_t {";
62 fs << std::endl <<
"\t\t\treturn data_size(self);";
63 fs << std::endl <<
"\t\t}, ";
64 fs << std::endl <<
"\t\t\"Get the size of the " << name <<
" in bytes\"";
65 fs << std::endl <<
"\t\t)";
74 PString name(classConfig.
getName());
88 PString name(classConfig.
getName());
89 PString moduleName = baseFileName.toLower() +
"_module";
91 fs <<
"def test_data_stream_conversion():" << std::endl;
92 fs <<
"\t\"\"\"Test data stream conversion\"\"\"" << std::endl;
93 fs <<
"\t" << name.toLower() <<
" = " << moduleName <<
"." << name <<
"()" << std::endl;
94 fs <<
"\t# Set some example values" << std::endl;
96 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
98 fs <<
"\t" << name.toLower() <<
"." << it->getName() <<
" = " << defaultValue << std::endl;
101 fs <<
"\tdata_stream = " << name.toLower() <<
".to_bytes()" << std::endl;
103 fs <<
"\tnew_" << name.toLower() <<
" = " << moduleName <<
"." << name <<
"()" << std::endl;
104 fs <<
"\tsuccess = new_" << name.toLower() <<
".from_bytes(data_stream)" << std::endl;
106 fs <<
"\tassert success" << std::endl;
107 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
109 fs <<
"\tassert new_" << name.toLower() <<
"." << it->getName() <<
" == " << defaultValue << std::endl;
113 fs <<
"def test_serialization_type_errors():" << std::endl;
114 fs <<
"\t\"\"\"Test erro types for from_bytes\"\"\"" << std::endl;
116 fs <<
"\t" << name.toLower() <<
" = " << moduleName <<
"." << name <<
"()" << std::endl;
117 fs <<
"\ttry:" << std::endl;
118 fs <<
"\t\tresult = " << name.toLower() <<
".from_bytes(\"A string is not a bytearray !!!\") # deserialize string instead of bytearray" << std::endl;
119 fs <<
"\t\tassert False" << std::endl;
120 fs <<
"\texcept TypeError:" << std::endl;
121 fs <<
"\t\tpass" << std::endl;
123 fs <<
"\ttry:" << std::endl;
124 fs <<
"\t\tresult = " << name.toLower() <<
".from_bytes(42) # deserialize instead of bytearray" << std::endl;
125 fs <<
"\t\tassert False" << std::endl;
126 fs <<
"\texcept TypeError:" << std::endl;
127 fs <<
"\t\tpass" << std::endl;
129 fs <<
"\ttry:" << std::endl;
130 fs <<
"\t\tresult = " << name.toLower() <<
".from_bytes([1, 2, 3, 4]) # deserialize list instead of bytearray" << std::endl;
131 fs <<
"\t\tassert False" << std::endl;
132 fs <<
"\texcept TypeError:" << std::endl;
133 fs <<
"\t\tpass" << std::endl << std::endl;
142 PString name(classConfig.
getName());
143 fs <<
"\ttest_data_stream_conversion()" << std::endl;
144 fs <<
"\ttest_serialization_type_errors()" << std::endl;
void registerSizeMethod(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode)
registration of size method
void registerSerializationMethods(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode)
registration of serialization methods
void registerDeserializationMethods(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode)
registration of serialization methods
std::vector< PClassAttribute > PVecClassAttribute
virtual ~NanobindTraitDataStream()
Destructor of NanobindTraitDataStream.
virtual void testFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode, const PString &baseFileName) const
NanobindTraitDataStream()
Constructor of NanobindTraitDataStream.
virtual void registerMethod(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
virtual void testCallFunction(std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode) const
PAbstractNanobindTraitBackend()
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 enableDataStream
True to enable data stream interface generator.
PString getTestDefaultValueTypeInPython(const PString &type)
Get default test value for a given type in Python.