PhoenixGenerator  2.0.4
Set of tools to generate code
Loading...
Searching...
No Matches
wrapper_utils.cpp
Go to the documentation of this file.
1/***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5****************************************/
6
7
8#include "wrapper_utils.h"
9
11
14PString wrapper_getBuildValueStr(const PString & type){
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}
24
26
29PString wrapper_getObjectToValue(const PString & type){
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}
38
40
43PString wrapper_getValueToObject(const PString & type){
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}
52
54
57PString wrapper_getExpectedType(const PString & type){
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}
65
67
70PString wrapper_getClassName(const PClassConfig & classConfig){
71 return "WP"+classConfig.getName();
72}
73
75
78PString wrapper_getObjectParseTuple(const PString & varName){
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}
86
88
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}
102
104
109PString project_wrapper_classImplPythonType(const PClassConfig & classConfig, const PString & moduleName, const PString & fromOtherType){
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}
156
Describes a class attribute.
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.
Class to describe a basic class.
const PString & getClassDocumentation() const
Returns the class documentation.
const PString & getName() const
Returns the class name.
bool getIsSimpleType(const PString &varType)
Check if the given type is a simple type.
PString project_wrapper_classImplPythonType(const PClassConfig &classConfig, const PString &moduleName, const PString &fromOtherType)
Create the Python type of the given class.
PString wrapper_getExpectedType(const PString &type)
Get the expected type of a given python type.
PString wrapper_getObjectToValue(const PString &type)
Get the function to convert the given object to a value.
PString project_wrapper_attributeDef(const PClassAttribute &attr)
Get the C++ definition of an attribute.
PString wrapper_getObjectParseTuple(const PString &varName)
Get the proper code for PyArg_ParseTuple with one object.
PString wrapper_getBuildValueStr(const PString &type)
Get the string description of the Py_BuildValue call for the given type.
PString wrapper_getValueToObject(const PString &type)
Get the function to convert the given value to a PyObject.
PString wrapper_getClassName(const PClassConfig &classConfig)
Get the corresponding wrapper class name.