|
8 | 8 | #include "eigenpy/fwd.hpp" |
9 | 9 | #include "eigenpy/scalar-conversion.hpp" |
10 | 10 |
|
11 | | -#include <patchlevel.h> // For PY_MAJOR_VERSION |
12 | 11 | #include <stdexcept> |
13 | 12 | #include <typeinfo> |
14 | 13 | #include <sstream> |
@@ -73,121 +72,42 @@ namespace eigenpy |
73 | 72 | ARRAY_TYPE |
74 | 73 | }; |
75 | 74 |
|
76 | | - struct NumpyType |
| 75 | + struct EIGENPY_DLLEXPORT NumpyType |
77 | 76 | { |
78 | 77 |
|
79 | | - static NumpyType & getInstance() |
80 | | - { |
81 | | - static NumpyType instance; |
82 | | - return instance; |
83 | | - } |
| 78 | + static NumpyType & getInstance(); |
84 | 79 |
|
85 | 80 | operator bp::object () { return getInstance().CurrentNumpyType; } |
86 | 81 |
|
87 | | - static bp::object make(PyArrayObject* pyArray, bool copy = false) |
88 | | - { return make((PyObject*)pyArray,copy); } |
| 82 | + static bp::object make(PyArrayObject* pyArray, bool copy = false); |
89 | 83 |
|
90 | | - static bp::object make(PyObject* pyObj, bool copy = false) |
91 | | - { |
92 | | - bp::object m; |
93 | | - if(isMatrix()) |
94 | | - m = getInstance().NumpyMatrixObject(bp::object(bp::handle<>(pyObj)), bp::object(), copy); |
95 | | -// m = NumpyAsMatrixObject(bp::object(bp::handle<>(pyObj))); |
96 | | - else if(isArray()) |
97 | | - m = bp::object(bp::handle<>(pyObj)); // nothing to do here |
98 | | - |
99 | | - Py_INCREF(m.ptr()); |
100 | | - return m; |
101 | | - } |
| 84 | + static bp::object make(PyObject* pyObj, bool copy = false); |
102 | 85 |
|
103 | | - static void setNumpyType(bp::object & obj) |
104 | | - { |
105 | | - PyTypeObject * obj_type = PyType_Check(obj.ptr()) ? reinterpret_cast<PyTypeObject*>(obj.ptr()) : obj.ptr()->ob_type; |
106 | | - if(PyType_IsSubtype(obj_type,getInstance().NumpyMatrixType)) |
107 | | - switchToNumpyMatrix(); |
108 | | - else if(PyType_IsSubtype(obj_type,getInstance().NumpyArrayType)) |
109 | | - switchToNumpyArray(); |
110 | | - } |
| 86 | + static void setNumpyType(bp::object & obj); |
111 | 87 |
|
112 | | - static void sharedMemory(const bool value) |
113 | | - { |
114 | | - getInstance().shared_memory = value; |
115 | | - } |
| 88 | + static void sharedMemory(const bool value); |
116 | 89 |
|
117 | | - static bool sharedMemory() |
118 | | - { |
119 | | - return getInstance().shared_memory; |
120 | | - } |
| 90 | + static bool sharedMemory(); |
121 | 91 |
|
122 | | - static void switchToNumpyArray() |
123 | | - { |
124 | | - getInstance().CurrentNumpyType = getInstance().NumpyArrayObject; |
125 | | - getInstance().getType() = ARRAY_TYPE; |
126 | | - } |
| 92 | + static void switchToNumpyArray(); |
127 | 93 |
|
128 | | - static void switchToNumpyMatrix() |
129 | | - { |
130 | | - getInstance().CurrentNumpyType = getInstance().NumpyMatrixObject; |
131 | | - getInstance().getType() = MATRIX_TYPE; |
132 | | - } |
| 94 | + static void switchToNumpyMatrix(); |
133 | 95 |
|
134 | | - static NP_TYPE & getType() |
135 | | - { |
136 | | - return getInstance().np_type; |
137 | | - } |
| 96 | + static NP_TYPE & getType(); |
138 | 97 |
|
139 | | - static bp::object getNumpyType() |
140 | | - { |
141 | | - return getInstance().CurrentNumpyType; |
142 | | - } |
| 98 | + static bp::object getNumpyType(); |
143 | 99 |
|
144 | | - static const PyTypeObject * getNumpyMatrixType() |
145 | | - { |
146 | | - return getInstance().NumpyMatrixType; |
147 | | - } |
| 100 | + static const PyTypeObject * getNumpyMatrixType(); |
148 | 101 |
|
149 | | - static const PyTypeObject * getNumpyArrayType() |
150 | | - { |
151 | | - return getInstance().NumpyArrayType; |
152 | | - } |
| 102 | + static const PyTypeObject * getNumpyArrayType(); |
153 | 103 |
|
154 | | - static bool isMatrix() |
155 | | - { |
156 | | - return PyType_IsSubtype(reinterpret_cast<PyTypeObject*>(getInstance().CurrentNumpyType.ptr()), |
157 | | - getInstance().NumpyMatrixType); |
158 | | - } |
| 104 | + static bool isMatrix(); |
159 | 105 |
|
160 | | - static bool isArray() |
161 | | - { |
162 | | - if(getInstance().isMatrix()) return false; |
163 | | - return PyType_IsSubtype(reinterpret_cast<PyTypeObject*>(getInstance().CurrentNumpyType.ptr()), |
164 | | - getInstance().NumpyArrayType); |
165 | | - } |
| 106 | + static bool isArray(); |
166 | 107 |
|
167 | 108 | protected: |
168 | 109 |
|
169 | | - NumpyType() |
170 | | - { |
171 | | - pyModule = bp::import("numpy"); |
172 | | - |
173 | | -#if PY_MAJOR_VERSION >= 3 |
174 | | - // TODO I don't know why this Py_INCREF is necessary. |
175 | | - // Without it, the destructor of NumpyType SEGV sometimes. |
176 | | - Py_INCREF(pyModule.ptr()); |
177 | | -#endif |
178 | | - |
179 | | - NumpyMatrixObject = pyModule.attr("matrix"); |
180 | | - NumpyMatrixType = reinterpret_cast<PyTypeObject*>(NumpyMatrixObject.ptr()); |
181 | | - NumpyArrayObject = pyModule.attr("ndarray"); |
182 | | - NumpyArrayType = reinterpret_cast<PyTypeObject*>(NumpyArrayObject.ptr()); |
183 | | - //NumpyAsMatrixObject = pyModule.attr("asmatrix"); |
184 | | - //NumpyAsMatrixType = reinterpret_cast<PyTypeObject*>(NumpyAsMatrixObject.ptr()); |
185 | | - |
186 | | - CurrentNumpyType = NumpyArrayObject; // default conversion |
187 | | - np_type = ARRAY_TYPE; |
188 | | - |
189 | | - shared_memory = true; |
190 | | - } |
| 110 | + NumpyType(); |
191 | 111 |
|
192 | 112 | bp::object CurrentNumpyType; |
193 | 113 | bp::object pyModule; |
|
0 commit comments