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


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