PhoenixGenerator  2.8.0
Set of tools to generate code
Loading...
Searching...
No Matches
NanobindTraitDataStream.cpp File Reference
Include dependency graph for NanobindTraitDataStream.cpp:

Go to the source code of this file.

Functions

void failed_enum_deserialization (std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode, const PString &baseFileName)
void registerDeserializationMethods (std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode)
 registration of serialization methods
void registerSerializationMethods (std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode)
 registration of serialization methods
void registerSizeMethod (std::ofstream &fs, const PClassConfig &classConfig, const GeneratorMode &mode)
 registration of size method

Function Documentation

◆ failed_enum_deserialization()

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

Test the deserialization of the enum with an invalid value

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

Definition at line 101 of file NanobindTraitDataStream.cpp.

101 {
102 PString name(classConfig.getName());
103 PString moduleName = baseFileName.toLower();
104 fs << "def test_data_stream_deserialization_failed():" << std::endl;
105 fs << "\t\"\"\"Test data stream conversion\"\"\"" << std::endl;
106 fs << "\t#Test with the value 8 even if there are only two values in the enum" << std::endl;
107 fs << "\tdata_stream = bytearray(b'\\x08\\x00\\x00\\x00')" << std::endl;
108 fs << "\twith pytest.raises(ValueError, match=\".* is not a valid "<<name<<"\"):" << std::endl;
109 fs << "\t\ttest_value = " << moduleName << "." << name << ".from_bytes(data_stream)" << std::endl;
110 fs << std::endl;
111}
const PString & getName() const
Returns the class name.

References PClassConfig::getName().

Referenced by NanobindTraitDataStream::testFunction().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ registerDeserializationMethods()

void registerDeserializationMethods ( std::ofstream & fs,
const PClassConfig & classConfig,
const GeneratorMode & mode )

registration of serialization methods

Parameters
fsstream to write to
classConfigconfig of the class
modegenerator mode

Definition at line 21 of file NanobindTraitDataStream.cpp.

21 {
22 PString name(classConfig.getName());
23 if(classConfig.getIsEnum()){
24 name = name + "::" + name;
25 }
26 fs << std::endl << std::endl << "\t\t///Deserialization method for " << name;
27 fs << std::endl << "\t\t.def_static(\"from_bytes\", [](const nb::bytearray &b) -> " << name << " {";
28 fs << std::endl << "\t\t\t\t" << name << " result;";
29 fs << std::endl << "\t\t\t\tDataStreamIter iter((DataStreamIter)b.data());";
30 fs << std::endl << "\t\t\t\tbool res(data_message_load(iter, result));";
31 fs << std::endl << "\t\t\t\tif(!res){";
32 fs << std::endl << "\t\t\t\t\tthrow nb::buffer_error(\"Failed to deserialize " << name << "\");";
33 fs << std::endl << "\t\t\t\t}";
34 fs << std::endl << "\t\t\t\treturn result;";
35 fs << std::endl << "\t\t\t}, ";
36 fs << std::endl << "\t\t\t\"Load a " << name << " from a bytearray\"";
37 fs << std::endl << "\t\t)";
38}
bool getIsEnum() const
Say if the current PClassConfig is an enum.

References PClassConfig::getIsEnum(), and PClassConfig::getName().

Referenced by NanobindTraitDataStream::registerMethod().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ registerSerializationMethods()

void registerSerializationMethods ( std::ofstream & fs,
const PClassConfig & classConfig,
const GeneratorMode & mode )

registration of serialization methods

Parameters
fsstream to write to
classConfigconfig of the class
modegenerator mode

Definition at line 44 of file NanobindTraitDataStream.cpp.

44 {
45 PString name(classConfig.getName());
46 if(classConfig.getIsEnum()){
47 name = name + "::" + name;
48 }
49 fs << std::endl << std::endl << "\t\t///Serialization method for " << name;
50 fs << std::endl << "\t\t.def(\"to_bytes\", [](const " << name << " & self) -> nb::bytearray {";
51 fs << std::endl << "\t\t\t\tsize_t size = data_size(self);";
52 fs << std::endl << "\t\t\t\tnb::bytearray msg(NULL, size);";
53 fs << std::endl << "\t\t\t\tDataStreamIter iter((DataStreamIter)msg.data());";
54 fs << std::endl << "\t\t\t\tbool success = data_message_save(iter, self);";
55 fs << std::endl << "\t\t\t\tif(success){";
56 fs << std::endl << "\t\t\t\t\treturn msg;";
57 fs << std::endl << "\t\t\t\t}else{";
58 fs << std::endl << "\t\t\t\t\tthrow nb::buffer_error(\"Failed to serialize " << name << "\");";
59 fs << std::endl << "\t\t\t\t}";
60 fs << std::endl << "\t\t\t}, ";
61 fs << std::endl << "\t\t\t\"Serialize the " << name << " to bytearray\"";
62 fs << std::endl << "\t\t)";
63}

References PClassConfig::getIsEnum(), and PClassConfig::getName().

Referenced by NanobindTraitDataStream::registerMethod().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ registerSizeMethod()

void registerSizeMethod ( std::ofstream & fs,
const PClassConfig & classConfig,
const GeneratorMode & mode )

registration of size method

Parameters
fsstream to write to
classConfigconfig of the class
modegenerator mode

Definition at line 69 of file NanobindTraitDataStream.cpp.

69 {
70 PString name(classConfig.getName());
71 if(classConfig.getIsEnum()){
72 name = name + "::" + name;
73 }
74 fs << std::endl << std::endl << "\t\t///Size method for " << name;
75 fs << std::endl << "\t\t.def(\"get_size\", [](const " << name << " & self) -> size_t {";
76 fs << std::endl << "\t\t\t\treturn data_size(self);";
77 fs << std::endl << "\t\t\t}, ";
78 fs << std::endl << "\t\t\t\"Get the size of the " << name << " in bytes\"";
79 fs << std::endl << "\t\t)";
80}

References PClassConfig::getIsEnum(), and PClassConfig::getName().

Referenced by NanobindTraitDataStream::registerMethod().

Here is the call graph for this function:
Here is the caller graph for this function: