PhoenixGenerator
2.8.0
Set of tools to generate code
Toggle main menu visibility
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
10
#include "
wrapper_generator_module.h
"
11
12
14
17
void
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
38
void
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
52
void
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
67
bool
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
){
114
project_wrapper_modulePyTypeReady
(fs, config.
getVecClassConfig
());
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
){
123
project_wrapper_modulePyIncref
(fs, config.
getVecClassConfig
());
124
}
125
fs <<
"\t\n"
;
126
for
(
const
PDataConfig
& config : projectParam.
vecDataConfig
){
127
project_wrapper_moduleAddObject
(fs, config.
getVecClassConfig
());
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
PVecClassConfig
std::vector< PClassConfig > PVecClassConfig
Definition
PDataConfig.h:14
PDataConfig
Class to describe a basic class.
Definition
PDataConfig.h:17
PDataConfig::getVecClassConfig
const PVecClassConfig & getVecClassConfig() const
Get the vector of all config class of the current pdata file.
Definition
PDataConfig.cpp:60
PDataConfig::getFileName
const PPath & getFileName() const
Get the file name of the current PDataConfig.
Definition
PDataConfig.cpp:46
licenceSave
void licenceSave(std::ofstream &fs)
Saves the policy.
Definition
header_generator.cpp:26
header_generator.h
GeneratorMode::moduleName
PString moduleName
Name of the wrapper python module.
Definition
GeneratorMode.h:56
ProjectParam
Set of parameters to generate a project.
Definition
ProjectParam.h:13
ProjectParam::mode
GeneratorMode mode
Mode to be used to generate the project.
Definition
ProjectParam.h:29
ProjectParam::vecDataConfig
PVecDataConfig vecDataConfig
Configuration of classes to be generated.
Definition
ProjectParam.h:27
project_wrapper_modulePyTypeReady
void project_wrapper_modulePyTypeReady(std::ofstream &fs, const PVecClassConfig &vecClassConfig)
Create the PyType_Ready for all classes of the module.
Definition
wrapper_generator_module.cpp:17
project_wrapper_moduleGeneratorMain
bool project_wrapper_moduleGeneratorMain(const PPath &fileName, const ProjectParam &projectParam)
Create the main wrapper module.
Definition
wrapper_generator_module.cpp:67
project_wrapper_moduleAddObject
void project_wrapper_moduleAddObject(std::ofstream &fs, const PVecClassConfig &vecClassConfig)
Create the Py_INCREF for all classes of the module.
Definition
wrapper_generator_module.cpp:52
project_wrapper_modulePyIncref
void project_wrapper_modulePyIncref(std::ofstream &fs, const PVecClassConfig &vecClassConfig)
Create the Py_INCREF for all classes of the module.
Definition
wrapper_generator_module.cpp:38
wrapper_generator_module.h
src
BackEnd
wrapper_generator_module.cpp
Generated by
1.17.0