register.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 INRIA
3  */
4 
5 #include "eigenpy/register.hpp"
6 
7 namespace eigenpy
8 {
9 
10  PyArray_Descr * Register::getPyArrayDescr(PyTypeObject * py_type_ptr)
11  {
12  MapDescr::iterator it = instance().py_array_descr_bindings.find(py_type_ptr);
13  if(it != instance().py_array_descr_bindings.end())
14  return it->second;
15  else
16  return NULL;
17  }
18 
19  bool Register::isRegistered(PyTypeObject * py_type_ptr)
20  {
21  if(getPyArrayDescr(py_type_ptr) != NULL)
22  return true;
23  else
24  return false;
25  }
26 
27  int Register::getTypeCode(PyTypeObject * py_type_ptr)
28  {
29  MapCode::iterator it = instance().py_array_code_bindings.find(py_type_ptr);
30  if(it != instance().py_array_code_bindings.end())
31  return it->second;
32  else
33  return PyArray_TypeNum(py_type_ptr);
34  }
35 
36  int Register::registerNewType(PyTypeObject * py_type_ptr,
37  const std::type_info * type_info_ptr,
38  const int type_size,
39  PyArray_GetItemFunc * getitem,
40  PyArray_SetItemFunc * setitem,
41  PyArray_NonzeroFunc * nonzero,
42  PyArray_CopySwapFunc * copyswap,
43  PyArray_CopySwapNFunc * copyswapn,
44  PyArray_DotFunc * dotfunc)
45  {
46  PyArray_Descr * descr_ptr = new PyArray_Descr(*call_PyArray_DescrFromType(NPY_OBJECT));
47  PyArray_Descr & descr = *descr_ptr;
48  descr.typeobj = py_type_ptr;
49  descr.kind = 'V';
50  descr.byteorder = '=';
51  descr.elsize = type_size;
52  descr.flags = NPY_LIST_PICKLE | NPY_USE_GETITEM | NPY_USE_SETITEM | NPY_NEEDS_INIT | NPY_NEEDS_PYAPI;
53  // descr->names = PyTuple_New(0);
54  // descr->fields = PyDict_New();
55 
56  PyArray_ArrFuncs * funcs_ptr = new PyArray_ArrFuncs;
57  PyArray_ArrFuncs & funcs = *funcs_ptr;
58  descr.f = funcs_ptr;
59  call_PyArray_InitArrFuncs(funcs_ptr);
60  funcs.getitem = getitem;
61  funcs.setitem = setitem;
62  funcs.nonzero = nonzero;
63  funcs.copyswap = copyswap;
64  funcs.copyswapn = copyswapn;
65  funcs.dotfunc = dotfunc;
66  // f->cast = cast;
67 
68  const int code = call_PyArray_RegisterDataType(descr_ptr);
69  assert(code >= 0 && "The return code should be positive");
70  PyArray_Descr * new_descr = call_PyArray_DescrFromType(code);
71 
72  instance().type_to_py_type_bindings.insert(std::make_pair(type_info_ptr,py_type_ptr));
73  instance().py_array_descr_bindings[py_type_ptr] = new_descr;
74  instance().py_array_code_bindings[py_type_ptr] = code;
75 
76  // PyArray_RegisterCanCast(descr,NPY_OBJECT,NPY_NOSCALAR);
77  return code;
78  }
79 
81  {
82  static Register self;
83  return self;
84  }
85 
86 } // namespace eigenpy
#define call_PyArray_RegisterDataType(dtype)
Definition: numpy.hpp:68
#define call_PyArray_DescrFromType(typenum)
Definition: numpy.hpp:65
MapCode py_array_code_bindings
Definition: register.hpp:117
static PyArray_Descr * getPyArrayDescr(PyTypeObject *py_type_ptr)
Definition: register.cpp:10
static int getTypeCode()
Definition: register.hpp:59
Structure collecting all the types registers in Numpy via EigenPy.
Definition: register.hpp:21
MapDescr py_array_descr_bindings
Definition: register.hpp:114
static int registerNewType(PyTypeObject *py_type_ptr, const std::type_info *type_info_ptr, const int type_size, PyArray_GetItemFunc *getitem, PyArray_SetItemFunc *setitem, PyArray_NonzeroFunc *nonzero, PyArray_CopySwapFunc *copyswap, PyArray_CopySwapNFunc *copyswapn, PyArray_DotFunc *dotfunc)
Definition: register.cpp:36
#define call_PyArray_InitArrFuncs(funcs)
Definition: numpy.hpp:67
static Register & instance()
Definition: register.cpp:80
static bool isRegistered()
Definition: register.hpp:27
MapInfo type_to_py_type_bindings
Definition: register.hpp:111
int EIGENPY_DLLAPI PyArray_TypeNum(PyTypeObject *type)
Definition: numpy.cpp:18


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Apr 17 2021 02:37:59