PhoenixGenerator  2.2.0
Set of tools to generate code
Loading...
Searching...
No Matches
NanobindTraitDataStream.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
8
13
16
21void registerDeserializationMethods(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode){
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)";
30}
31
36void registerSerializationMethods(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode){
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)";
52}
53
58void registerSizeMethod(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode){
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)";
66}
67
72void NanobindTraitDataStream::registerMethod(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
73 if(!mode.enableDataStream){return;}
74 PString name(classConfig.getName());
75
76 registerSerializationMethods(fs, classConfig, mode);
77 registerDeserializationMethods(fs, classConfig, mode);
78 registerSizeMethod(fs, classConfig, mode);
79}
80
86void NanobindTraitDataStream::testFunction(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode, const PString & baseFileName) const{
87 if(!mode.enableDataStream){return;}
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;
95 const PVecClassAttribute & listAttr(classConfig.getListAttribute());
96 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
97 PString defaultValue = getTestDefaultValueTypeInPython(it->getType());
98 fs << "\t" << name.toLower() << "." << it->getName() << " = " << defaultValue << std::endl;
99 }
100 fs << std::endl;
101 fs << "\tdata_stream = " << name.toLower() << ".to_bytes()" << std::endl;
102 fs << std::endl;
103 fs << "\tnew_" << name.toLower() << " = " << moduleName << "." << name << "()" << std::endl;
104 fs << "\tsuccess = new_" << name.toLower() << ".from_bytes(data_stream)" << std::endl;
105 fs << std::endl;
106 fs << "\tassert success" << std::endl;
107 for(PVecClassAttribute::const_iterator it(listAttr.begin()); it != listAttr.end(); ++it){
108 PString defaultValue = getTestDefaultValueTypeInPython(it->getType());
109 fs << "\tassert new_" << name.toLower() << "." << it->getName() << " == " << defaultValue << std::endl;
110 }
112 fs << std::endl;
113 fs << "def test_serialization_type_errors():" << std::endl;
114 fs << "\t\"\"\"Test erro types for from_bytes\"\"\"" << std::endl;
115 fs << 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;
122 fs << 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;
128 fs << 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;
134}
135
140void NanobindTraitDataStream::testCallFunction(std::ofstream & fs, const PClassConfig & classConfig, const GeneratorMode & mode) const{
141 if(!mode.enableDataStream){return;}
142 PString name(classConfig.getName());
143 fs << "\ttest_data_stream_conversion()" << std::endl;
144 fs << "\ttest_serialization_type_errors()" << std::endl;
145}
146
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
Definition PDataConfig.h:13
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
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.
All the genertor modes.
bool enableDataStream
True to enable data stream interface generator.
PString getTestDefaultValueTypeInPython(const PString &type)
Get default test value for a given type in Python.