3
0
mirror of https://github.com/triqs/dft_tools synced 2024-10-31 11:13:46 +01:00
Correction converter specialization
(avoid redeclaration).
This commit is contained in:
Olivier Parcollet 2014-05-19 22:18:17 +02:00
parent 914901864a
commit dd91c51647

View File

@ -22,33 +22,26 @@ namespace triqs { namespace py_tools {
%for n,t in enumerate(module.wrapped_types_by_me) : %for n,t in enumerate(module.wrapped_types_by_me) :
template<> struct py_converter<${t}> { template<> void ** py_converter<${t}>::init() {
static void ** init();
static PyObject * c2py(${t} const & x);
static ${t}& py2c(PyObject * ob);
static bool is_convertible(PyObject *ob, bool raise_exception);
};
void ** py_converter<${t}>::init() {
PyObject * mod = PyImport_ImportModule("${module.full_name}"); PyObject * mod = PyImport_ImportModule("${module.full_name}");
if (mod ==NULL) return NULL; if (mod ==NULL) return NULL;
void ** table = (void **)PyCapsule_Import("${module.full_name}._exported_wrapper_convert_fnt", 0); void ** table = (void **)PyCapsule_Import("${module.full_name}._exported_wrapper_convert_fnt", 0);
return table; return table;
} }
PyObject * py_converter<${t}>::c2py(${t} const & x){ template<> PyObject * py_converter<${t}>::c2py(${t} const & x){
static void **wrapped_convert_fnt = init(); static void **wrapped_convert_fnt = init();
if (wrapped_convert_fnt == NULL) return NULL; if (wrapped_convert_fnt == NULL) return NULL;
return ((PyObject * (*)(${t} const &)) wrapped_convert_fnt[3*${n}])(x); return ((PyObject * (*)(${t} const &)) wrapped_convert_fnt[3*${n}])(x);
} }
${t}& py_converter<${t}>::py2c(PyObject * ob){ template<> ${t}& py_converter<${t}>::py2c(PyObject * ob){
static void **wrapped_convert_fnt = init(); static void **wrapped_convert_fnt = init();
if (wrapped_convert_fnt == NULL) std::terminate(); // It should never happen since py2c is called only is is_convertible is true (py_converter specs) if (wrapped_convert_fnt == NULL) std::terminate(); // It should never happen since py2c is called only is is_convertible is true (py_converter specs)
return ((${t}& (*)(PyObject *)) wrapped_convert_fnt[3*${n}+1])(ob); return ((${t}& (*)(PyObject *)) wrapped_convert_fnt[3*${n}+1])(ob);
} }
bool py_converter<${t}>::is_convertible(PyObject *ob, bool raise_exception) { template<> bool py_converter<${t}>::is_convertible(PyObject *ob, bool raise_exception) {
static void **wrapped_convert_fnt = init(); static void **wrapped_convert_fnt = init();
if (wrapped_convert_fnt == NULL) { if (wrapped_convert_fnt == NULL) {
if (!raise_exception && PyErr_Occurred()) {PyErr_Print();PyErr_Clear();} if (!raise_exception && PyErr_Occurred()) {PyErr_Print();PyErr_Clear();}