PhoenixGenerator  2.0.4
Set of tools to generate code
Loading...
Searching...
No Matches
wrapper_generator_module.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#include "openFileStream.h"
8#include "header_generator.h"
9
11
12
14
17void project_wrapper_modulePyTypeReady(std::ofstream & fs, const PVecClassConfig & vecClassConfig){
18 for(PVecClassConfig::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
19 if(it->getIsEnum()){
20 std::string varName(it->getName()+"EnumType");
21 fs << "\t//PyObject* "+varName+" = (PyObject*)Py_TYPE(WP"+it->getName()+"_newC(NULL, NULL, NULL));\t//1: A nice segmentation fault of python\n";
22 fs << "\tPyObject* "+varName+" = WP"+it->getName()+"_newC(NULL, NULL, NULL);\t//2: alone => SystemError: initialization of pystereodata raised unreported exception, while importing generated module\n";
23 fs << "\tif("+varName+" == NULL){" << std::endl;
24 fs << "\t\tstd::cerr << \"Cannot create enum '"<<it->getName()<<"'\" << std::endl;" << std::endl;
25 fs << "\t\treturn NULL;" << std::endl;
26 fs << "\t}" << std::endl;
27// fs << "\tif(PyType_Ready("+varName+") < 0){std::cerr << \"Type for enum '"<<it->getName()<<"' not ready\" << std::endl;return NULL;}\n";
28 }else{
29 fs << "\tif(PyType_Ready(&WP"+it->getName()+"Type) < 0){return NULL;}\n";
30 }
31 }
32}
33
35
38void project_wrapper_modulePyIncref(std::ofstream & fs, const PVecClassConfig & vecClassConfig){
39 for(PVecClassConfig::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
40 if(it->getIsEnum()){
41 fs << "\t//Py_INCREF("+it->getName()+"EnumType);\t//2: An other segmentation fault of python, when pytest is collecting tests\n";
42 }else{
43 fs << "\tPy_INCREF(&WP"+it->getName()+"Type);\n";
44 }
45 }
46}
47
49
52void project_wrapper_moduleAddObject(std::ofstream & fs, const PVecClassConfig & vecClassConfig){
53 for(PVecClassConfig::const_iterator it(vecClassConfig.begin()); it != vecClassConfig.end(); ++it){
54 if(it->getIsEnum()){
55 fs << "\tPyModule_AddObject(m, \""+it->getName()+"\", "+it->getName()+"EnumType);\n";
56 }else{
57 fs << "\tPyModule_AddObject(m, \""+it->getName()+"\", (PyObject *)&WP"+it->getName()+"Type);\n";
58 }
59 }
60}
61
63
67bool project_wrapper_moduleGeneratorMain(const PPath & fileName, const ProjectParam & projectParam){
68 PString moduleName(projectParam.mode.moduleName);
69 std::ofstream fs;
70 if(!openFileStream(fs, fileName)){return false;}
71 licenceSave(fs);
72 fs << "\n";
73// fs << "#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION\n";
74// fs << "#ifndef DISABLE_COOL_ARRAY\n";
75// fs << "#\tdefine PY_ARRAY_UNIQUE_SYMBOL "+moduleName+"_ARRAY_API\n";
76// fs << "#endif\n";
77 fs << "\n";
78 fs << "#include <Python.h>\n";
79 fs << "#include \"structmember.h\"\n";
80// fs << "#include <numpy/arrayobject.h>\n";
81 fs << "\n";
82 fs << "#include <string>\n";
83 fs << "#include <iostream>\n";
84 //TODO : put the includes of all class definitions
85 for(const PDataConfig & config : projectParam.vecDataConfig){
86 fs << "#include \"" << config.getFileName().getFileName().eraseExtension() <<"_wrapper.h\"\n";
87 }
88 fs << "\n";
89// fs << "#include \""+ baseImplInclude + "_wrapper.h\"\n\n";
90
91 fs << "static PyMethodDef _"+moduleName+"_methods[] = {\n";
92 fs << "\t{NULL, NULL}\n";
93 fs << "};\n\n";
94
95 fs << "static PyModuleDef _"+moduleName+"_module = {\n";
96 fs << "\tPyModuleDef_HEAD_INIT,\n";
97 fs << "\t\""+moduleName+"\",\n";
98 fs << "\t\"\",\n";
99 fs << "\t-1,\n";
100 fs << "\t_"+moduleName+"_methods,\n";
101 fs << "\tNULL,\n";
102 fs << "\tNULL,\n";
103 fs << "\tNULL,\n";
104 fs << "\tNULL\n";
105 fs << "};\n\n";
106
107 fs << "///Create the python module "+moduleName+"\n";
108 fs << "/**\t@return python module "+moduleName+"\n";
109 fs << "*/\n";
110 fs << "PyMODINIT_FUNC PyInit_"+moduleName+"(void){\n";
111// fs << "\timport_array();\n"; //For numpy stuff
112 fs << "\n";
113 for(const PDataConfig & config : projectParam.vecDataConfig){
115 }
116 fs << "\t\n";
117 fs << "\tPyObject * m = PyModule_Create(&_"+moduleName+"_module);\n";
118 fs << "\tif(m == NULL){\n";
119 fs << "\t\treturn NULL;\n";
120 fs << "\t}\n";
121 fs << "\t\n";
122 for(const PDataConfig & config : projectParam.vecDataConfig){
124 }
125 fs << "\t\n";
126 for(const PDataConfig & config : projectParam.vecDataConfig){
128 }
129 fs << "\t\n";
130 fs << "\treturn m;\n";
131 fs << "}\n\n";
132 fs.close();
133 return true;
134}
135
136
137
138
std::vector< PClassConfig > PVecClassConfig
Definition PDataConfig.h:14
Class to describe a basic class.
Definition PDataConfig.h:17
const PVecClassConfig & getVecClassConfig() const
Get the vector of all config class of the current pdata file.
const PPath & getFileName() const
Get the file name of the current PDataConfig.
void licenceSave(std::ofstream &fs)
Saves the policy.
PString moduleName
Name of the wrapper python module.
Set of parameters to generate a project.
GeneratorMode mode
Mode to be used to generate the project.
PVecDataConfig vecDataConfig
Configuration of classes to be generated.
void project_wrapper_modulePyTypeReady(std::ofstream &fs, const PVecClassConfig &vecClassConfig)
Create the PyType_Ready for all classes of the module.
bool project_wrapper_moduleGeneratorMain(const PPath &fileName, const ProjectParam &projectParam)
Create the main wrapper module.
void project_wrapper_moduleAddObject(std::ofstream &fs, const PVecClassConfig &vecClassConfig)
Create the Py_INCREF for all classes of the module.
void project_wrapper_modulePyIncref(std::ofstream &fs, const PVecClassConfig &vecClassConfig)
Create the Py_INCREF for all classes of the module.