register.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 INRIA
3 //
4 
5 #ifndef __eigenpy_register_hpp__
6 #define __eigenpy_register_hpp__
7 
8 #include <algorithm>
9 #include <map>
10 #include <string>
11 #include <typeinfo>
12 
13 #include "eigenpy/exception.hpp"
14 #include "eigenpy/fwd.hpp"
15 #include "eigenpy/numpy.hpp"
16 
17 namespace eigenpy {
18 
20 struct EIGENPY_DLLAPI Register {
21  static PyArray_Descr *getPyArrayDescr(PyTypeObject *py_type_ptr);
22 
23  template <typename Scalar>
24  static bool isRegistered() {
25  return isRegistered(Register::getPyType<Scalar>());
26  }
27 
28  static bool isRegistered(PyTypeObject *py_type_ptr);
29 
30  static int getTypeCode(PyTypeObject *py_type_ptr);
31 
32  template <typename Scalar>
33  static PyTypeObject *getPyType() {
34  if (!isNumpyNativeType<Scalar>()) {
35  const PyTypeObject *const_py_type_ptr =
36  bp::converter::registered_pytype<Scalar>::get_pytype();
37  if (const_py_type_ptr == NULL) {
38  std::stringstream ss;
39  ss << "The type " << typeid(Scalar).name()
40  << " does not have a registered converter inside Boot.Python."
41  << std::endl;
42  throw std::invalid_argument(ss.str());
43  }
44  PyTypeObject *py_type_ptr = const_cast<PyTypeObject *>(const_py_type_ptr);
45  return py_type_ptr;
46  } else {
47  PyArray_Descr *new_descr =
49  return new_descr->typeobj;
50  }
51  }
52 
53  template <typename Scalar>
54  static PyArray_Descr *getPyArrayDescr() {
55  if (!isNumpyNativeType<Scalar>()) {
56  return getPyArrayDescr(getPyType<Scalar>());
57  } else {
59  }
60  }
61 
62  template <typename Scalar>
63  static int getTypeCode() {
64  if (isNumpyNativeType<Scalar>())
66  else {
67  const std::type_info &info = typeid(Scalar);
68  if (instance().type_to_py_type_bindings.find(&info) !=
69  instance().type_to_py_type_bindings.end()) {
70  PyTypeObject *py_type = instance().type_to_py_type_bindings[&info];
71  int code = instance().py_array_code_bindings[py_type];
72 
73  return code;
74  } else
75  return -1; // type not registered
76  }
77  }
78 
79  static int registerNewType(
80  PyTypeObject *py_type_ptr, const std::type_info *type_info_ptr,
81  const int type_size, const int alignment, PyArray_GetItemFunc *getitem,
82  PyArray_SetItemFunc *setitem, PyArray_NonzeroFunc *nonzero,
83  PyArray_CopySwapFunc *copyswap, PyArray_CopySwapNFunc *copyswapn,
84  PyArray_DotFunc *dotfunc, PyArray_FillFunc *fill,
85  PyArray_FillWithScalarFunc *fillwithscalar);
86 
87  static Register &instance();
88 
89  private:
90  Register(){};
91 
93  bool operator()(const PyTypeObject *a, const PyTypeObject *b) const {
94  return std::string(a->tp_name) < std::string(b->tp_name);
95  }
96  };
97 
99  bool operator()(const std::type_info *a, const std::type_info *b) const {
100  return std::string(a->name()) < std::string(b->name());
101  }
102  };
103 
104  typedef std::map<const std::type_info *, PyTypeObject *, Compare_TypeInfo>
107 
108  typedef std::map<PyTypeObject *, PyArray_Descr *, Compare_PyTypeObject>
111 
112  typedef std::map<PyTypeObject *, int, Compare_PyTypeObject> MapCode;
114 };
115 
116 } // namespace eigenpy
117 
118 #endif // __eigenpy_register_hpp__
static PyArray_Descr * getPyArrayDescr()
Definition: register.hpp:54
MapCode py_array_code_bindings
Definition: register.hpp:113
bool operator()(const PyTypeObject *a, const PyTypeObject *b) const
Definition: register.hpp:93
static int getTypeCode()
Definition: register.hpp:63
bool operator()(const std::type_info *a, const std::type_info *b) const
Definition: register.hpp:99
Structure collecting all the types registers in Numpy via EigenPy.
Definition: register.hpp:20
MapDescr py_array_descr_bindings
Definition: register.hpp:110
std::map< const std::type_info *, PyTypeObject *, Compare_TypeInfo > MapInfo
Definition: register.hpp:105
static PyTypeObject * getPyType()
Definition: register.hpp:33
static bool isRegistered()
Definition: register.hpp:24
std::map< PyTypeObject *, int, Compare_PyTypeObject > MapCode
Definition: register.hpp:112
std::map< PyTypeObject *, PyArray_Descr *, Compare_PyTypeObject > MapDescr
Definition: register.hpp:109
MapInfo type_to_py_type_bindings
Definition: register.hpp:106
int registerNewType(PyTypeObject *py_type_ptr=NULL)
Definition: user-type.hpp:305
PyArray_Descr * call_PyArray_DescrFromType(int typenum)
Definition: numpy.hpp:165
void fill(Eigen::Ref< MatType > mat, const typename MatType::Scalar &value)
Definition: eigen_ref.cpp:62


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Fri Jun 2 2023 02:10:26