Skip to content

Commit c8081ae

Browse files
authored
Merge pull request #286 from jcarpent/devel
Allow template specialization of getitem
2 parents f53d37e + a4fa076 commit c8081ae

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

include/eigenpy/user-type.hpp

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,27 @@ static void cast(void* from_, void* to_, npy_intp n, void* /*fromarr*/,
3838
}
3939
}
4040

41+
template <typename T>
42+
struct getitem {
43+
///
44+
/// \brief Get a python object from an array
45+
/// It returns a standard Python object from
46+
/// a single element of the array object arr pointed to by data.
47+
/// \param[in] data Pointer to the first element of the C++ data stream
48+
/// \param[in] arr Pointer to the first element of the Python object data
49+
/// stream
50+
///
51+
/// \returns PyObject corresponding to the python datastream.
52+
///
53+
static PyObject* run(void* data, void* /* arr */) {
54+
// std::cout << "getitem" << std::endl;
55+
T* elt_ptr = static_cast<T*>(data);
56+
bp::object m(boost::ref(*elt_ptr));
57+
Py_INCREF(m.ptr());
58+
return m.ptr();
59+
}
60+
};
61+
4162
template <typename T, int type_code = NumpyEquivalentType<T>::type_code>
4263
struct SpecialMethods {
4364
inline static void copyswap(void* /*dst*/, void* /*src*/, int /*swap*/,
@@ -71,8 +92,8 @@ struct OffsetOf {
7192

7293
template <typename T>
7394
struct SpecialMethods<T, NPY_USERDEF> {
74-
inline static void copyswap(void* dst, void* src, int swap, void* /*arr*/) {
75-
// std::cout << "copyswap" << std::endl;
95+
static void copyswap(void* dst, void* src, int swap, void* /*arr*/) {
96+
// std::cout << "copyswap" << std::endl;
7697
if (src != NULL) {
7798
T& t1 = *static_cast<T*>(dst);
7899
T& t2 = *static_cast<T*>(src);
@@ -86,22 +107,8 @@ struct SpecialMethods<T, NPY_USERDEF> {
86107
}
87108
}
88109

89-
///
90-
/// \brief Get a python object from an array
91-
/// It returns a standard Python object from
92-
/// a single element of the array object arr pointed to by data.
93-
/// \param[in] data Pointer to the first element of the C++ data stream
94-
/// \param[in] arr Pointer to the first element of the Python object data
95-
/// stream
96-
///
97-
/// \returns PyObject corresponding to the python datastream.
98-
///
99-
inline static PyObject* getitem(void* ip, void* /*ap*/) {
100-
// std::cout << "getitem" << std::endl;
101-
T* elt_ptr = static_cast<T*>(ip);
102-
bp::object m(*elt_ptr);
103-
Py_INCREF(m.ptr());
104-
return m.ptr();
110+
static PyObject* getitem(void* ip, void* ap) {
111+
return eigenpy::internal::getitem<T>::run(ip, ap);
105112
}
106113

107114
///
@@ -120,7 +127,7 @@ struct SpecialMethods<T, NPY_USERDEF> {
120127
///
121128

122129
inline static int setitem(PyObject* src_obj, void* dest_ptr, void* array) {
123-
// std::cout << "setitem" << std::endl;
130+
// std::cout << "setitem" << std::endl;
124131
if (array == NULL) {
125132
eigenpy::Exception("Cannot retrieve the type stored in the array.");
126133
return -1;
@@ -158,7 +165,7 @@ struct SpecialMethods<T, NPY_USERDEF> {
158165

159166
inline static void copyswapn(void* dst, long dstride, void* src, long sstride,
160167
long n, int swap, void* array) {
161-
// std::cout << "copyswapn" << std::endl;
168+
// std::cout << "copyswapn" << std::endl;
162169

163170
char* dstptr = static_cast<char*>(dst);
164171
char* srcptr = static_cast<char*>(src);
@@ -174,7 +181,7 @@ struct SpecialMethods<T, NPY_USERDEF> {
174181
}
175182

176183
inline static npy_bool nonzero(void* ip, void* array) {
177-
// std::cout << "nonzero" << std::endl;
184+
// std::cout << "nonzero" << std::endl;
178185
static const T ZeroValue = T(0);
179186
PyArrayObject* py_array = static_cast<PyArrayObject*>(array);
180187
if (py_array == NULL || PyArray_ISBEHAVED_RO(py_array)) {
@@ -190,7 +197,7 @@ struct SpecialMethods<T, NPY_USERDEF> {
190197

191198
inline static void dotfunc(void* ip0_, npy_intp is0, void* ip1_, npy_intp is1,
192199
void* op, npy_intp n, void* /*arr*/) {
193-
// std::cout << "dotfunc" << std::endl;
200+
// std::cout << "dotfunc" << std::endl;
194201
typedef Eigen::Matrix<T, Eigen::Dynamic, 1> VectorT;
195202
typedef Eigen::InnerStride<Eigen::Dynamic> InputStride;
196203
typedef const Eigen::Map<const VectorT, 0, InputStride> ConstMapType;
@@ -205,7 +212,7 @@ struct SpecialMethods<T, NPY_USERDEF> {
205212

206213
inline static int fillwithscalar(void* buffer_, npy_intp length, void* value,
207214
void* /*arr*/) {
208-
// std::cout << "fillwithscalar" << std::endl;
215+
// std::cout << "fillwithscalar" << std::endl;
209216
T r = *static_cast<T*>(value);
210217
T* buffer = static_cast<T*>(buffer_);
211218
npy_intp i;
@@ -216,7 +223,7 @@ struct SpecialMethods<T, NPY_USERDEF> {
216223
}
217224

218225
static int fill(void* data_, npy_intp length, void* /*arr*/) {
219-
// std::cout << "fillwithscalar" << std::endl;
226+
// std::cout << "fill" << std::endl;
220227
T* data = static_cast<T*>(data_);
221228
const T delta = data[1] - data[0];
222229
T r = data[1];

0 commit comments

Comments
 (0)