PhoenixGenerator  2.0.4
Set of tools to generate code
Loading...
Searching...
No Matches
wrapper_utils.h File Reference
#include "PClassConfig.h"
#include "type_utils.h"
#include "wrapper_convertType.h"
+ Include dependency graph for wrapper_utils.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

PString project_wrapper_attributeDef (const PClassAttribute &attr)
 Get the C++ definition of an attribute.
 
PString project_wrapper_classImplPythonType (const PClassConfig &classConfig, const PString &moduleName, const PString &fromOtherType)
 Create the Python type of the given class.
 
PString wrapper_getBuildValueStr (const PString &type)
 Get the string description of the Py_BuildValue call for the given type.
 
PString wrapper_getClassName (const PClassConfig &classConfig)
 Get the corresponding wrapper class name.
 
PString wrapper_getExpectedType (const PString &type)
 Get the expected type of a given python type.
 
PString wrapper_getObjectParseTuple (const PString &varName)
 Get the proper code for PyArg_ParseTuple with one object.
 
PString wrapper_getObjectToValue (const PString &type)
 Get the function to convert the given object to a value.
 
PString wrapper_getValueToObject (const PString &type)
 Get the function to convert the given value to a PyObject.
 

Function Documentation

◆ project_wrapper_attributeDef()

PString project_wrapper_attributeDef ( const PClassAttribute & attr)

Get the C++ definition of an attribute.

Parameters
attr: attribute of a class
Returns
corresponding C++

Definition at line 91 of file wrapper_utils.cpp.

91 {
92 PString body;
93 body += "\t" + attr.getDocumentation() + "\n";
94 if(getIsSimpleType(attr.getType())){
95 body += "\t" + attr.getType();
96 }else{
97 body += "\tPyObject *";
98 }
99 body += " "+attr.getName()+";\n";
100 return body;
101}
const PString & getDocumentation() const
Gets the documentation of the PClassAttribute.
const PString & getType() const
Gets the type of the PClassAttribute.
const PString & getName() const
Gets the name of the PClassAttribute.
bool getIsSimpleType(const PString &varType)
Check if the given type is a simple type.

References PClassAttribute::getDocumentation(), getIsSimpleType(), PClassAttribute::getName(), and PClassAttribute::getType().

Referenced by wrapper_generator_class_header().

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

◆ project_wrapper_classImplPythonType()

PString project_wrapper_classImplPythonType ( const PClassConfig & classConfig,
const PString & moduleName,
const PString & fromOtherType )

Create the Python type of the given class.

Parameters
classConfig: class to be used
moduleName: name of the module where the class should be defined
fromOtherType: name of an other PyTypeObject to inheritate
Returns
corresponding C++

Definition at line 109 of file wrapper_utils.cpp.

109 {
110 PString body, className(wrapper_getClassName(classConfig));
111
112 body += "///Define the type of the "+className+"\n";
113 body += "PyTypeObject "+className+"Type = {\n";
114 body += "\tPyVarObject_HEAD_INIT("+fromOtherType+", 0)\n";
115 body += "\t\""+moduleName+"."+classConfig.getName()+"\", /* tp_name */\n";
116 body += "\tsizeof("+className+"), /* tp_basicsize */\n";
117 body += "\t0, /* tp_itemsize */\n";
118 body += "\t(destructor)"+className+"_dealloc, /* tp_dealloc */\n";
119 body += "\t0, /* tp_print */\n";
120 body += "\t0, /* tp_getattr */\n";
121 body += "\t0, /* tp_setattr */\n";
122 body += "\t0, /* tp_reserved */\n";
123 body += "\t0, /* tp_repr */\n";
124 body += "\t0, /* tp_as_number */\n";
125 body += "\t0, /* tp_as_sequence */\n";
126 body += "\t0, /* tp_as_mapping */\n";
127 body += "\t0, /* tp_hash */\n";
128 body += "\t0, /* tp_call */\n";
129 body += "\t0, /* tp_str */\n";
130 body += "\t0, /* tp_getattro */\n";
131 body += "\t0, /* tp_setattro */\n";
132 body += "\t0, /* tp_as_buffer */\n";
133 body += "\tPy_TPFLAGS_DEFAULT |\n";
134 body += "\tPy_TPFLAGS_BASETYPE, /* tp_flags */\n";
135 body += "\t\""+className+" doc : "+classConfig.getClassDocumentation().replace("///", "")+"\", /* tp_doc */\n";
136 body += "\t0, /* tp_traverse */\n";
137 body += "\t0, /* tp_clear */\n";
138 body += "\t0, /* tp_richcompare */\n";
139 body += "\t0, /* tp_weaklistoffset */\n";
140 body += "\t0, /* tp_iter */\n";
141 body += "\t0, /* tp_iternext */\n";
142 body += "\t"+className+"_methods, /* tp_methods */\n";
143 body += "\t"+className+"_members, /* tp_members */\n";
144 body += "\t"+className+"_getseters,/* tp_getset */\n";
145 body += "\t0, /* tp_base */\n";
146 body += "\t0, /* tp_dict */\n";
147 body += "\t0, /* tp_descr_get */\n";
148 body += "\t0, /* tp_descr_set */\n";
149 body += "\t0, /* tp_dictoffset */\n";
150 body += "\t0, /* tp_init */\n";
151 body += "\t0, /* tp_alloc */\n";
152 body += "\t"+className+"_newC, /* tp_new */\n";
153 body += "};\n\n";
154 return body;
155}
const PString & getClassDocumentation() const
Returns the class documentation.
const PString & getName() const
Returns the class name.
PString wrapper_getClassName(const PClassConfig &classConfig)
Get the corresponding wrapper class name.

References PClassConfig::getClassDocumentation(), PClassConfig::getName(), and wrapper_getClassName().

Referenced by wrapper_generator_class_source().

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

◆ wrapper_getBuildValueStr()

PString wrapper_getBuildValueStr ( const PString & type)

Get the string description of the Py_BuildValue call for the given type.

Parameters
type: type of the used
Returns
corresponding string description or UnknownType if the type is not yet supported by this function

Definition at line 14 of file wrapper_utils.cpp.

14 {
15 if(type == "int"){return "i";}
16 else if(type == "float"){return "f";}
17 else if(type == "double"){return "d";}
18 else if(type == "char"){return "c";}
19 else if(type == "std::string"){return "s";}
20 else{
21 return "UnknownType";
22 }
23}

◆ wrapper_getClassName()

◆ wrapper_getExpectedType()

PString wrapper_getExpectedType ( const PString & type)

Get the expected type of a given python type.

Parameters
type: type ot be used
Returns
corresponding Python type or NoPythonTypeYet if the type is not yet supported by this function

Definition at line 57 of file wrapper_utils.cpp.

57 {
58 if(type == "long" || type == "int" || type == "short" || type == "char"){return "int";}
59 else if(type == "float" || type == "double"){return "float";}
60 else if(type == "std::string"){return "std";}
61 else{
62 return "NoPythonTypeYet";
63 }
64}

Referenced by data_stream_trait_wrapper_classImplCheck().

+ Here is the caller graph for this function:

◆ wrapper_getObjectParseTuple()

PString wrapper_getObjectParseTuple ( const PString & varName)

Get the proper code for PyArg_ParseTuple with one object.

Parameters
varName: name of the variable to be checked
Returns
corresponding C++

Definition at line 78 of file wrapper_utils.cpp.

78 {
79 PString body;
80 body += "\tif(!PyArg_ParseTuple(args, \"O\", &"+varName+")){\n";
81 body += "\t\tPyErr_SetString(PyExc_RuntimeError, \"missing filename or keyword not supported\\n\");\n";
82 body += "\t\treturn NULL;\n";
83 body += "\t}\n";
84 return body;
85}

Referenced by data_stream_trait_wrapper_classPythonFromBytes().

+ Here is the caller graph for this function:

◆ wrapper_getObjectToValue()

PString wrapper_getObjectToValue ( const PString & type)

Get the function to convert the given object to a value.

Parameters
type: tpye to be used
Returns
corresponding function or UnknownConvertFunctionToValue if the type is not yet supported by this function

Definition at line 29 of file wrapper_utils.cpp.

29 {
30 if(type == "float" || type == "double"){return "PyFloat_AsDouble";}
31 else if(type == "long" || type == "short" || type == "char"){return "PyLong_AsLong";}
32 else if(type == "int"){return "PyLong_AsInt";}
33 else if(type == "size_t"){return "PyLong_ToSize_t";}
34 else{
35 return "UnknownConvertFunctionToValue";
36 }
37}

Referenced by data_stream_trait_wrapper_classAttributeToMessage().

+ Here is the caller graph for this function:

◆ wrapper_getValueToObject()

PString wrapper_getValueToObject ( const PString & type)

Get the function to convert the given value to a PyObject.

Parameters
type: tpye to be used
Returns
corresponding function or UnknownConvertFunctionFromValue if the type is not yet supported by this function

Definition at line 43 of file wrapper_utils.cpp.

43 {
44 if(type == "float" || type == "double"){return "PyFloat_FromDouble";}
45 else if(type == "long" || type == "short" || type == "char" || type == "int"){return "PyLong_FromLong";}
46 else if(type == "unsigned long" || type == "unsigned short" || type == "unsigned char" || type == "unsigned int"){return "PyLong_FromLong";}
47 else if(type == "size_t"){return "PyLong_FromSize_t";}
48 else{
49 return "UnknownConvertFunctionFromValue";
50 }
51}

Referenced by data_stream_trait_wrapper_classAttributeFromMessage().

+ Here is the caller graph for this function: