11//
2- // Copyright (c) 2020-2021 INRIA
2+ // Copyright (c) 2020-2022 INRIA
33//
44
55#ifndef __eigenpy_user_type_hpp__
@@ -19,14 +19,18 @@ namespace eigenpy
1919 {
2020 static To run (const From & from)
2121 {
22- return (To)from;
22+ #pragma GCC diagnostic push
23+ #pragma GCC diagnostic ignored "-Wconversion"
24+ #pragma GCC diagnostic ignored "-Wfloat-conversion"
25+ return static_cast <To>(from);
26+ #pragma GCC diagnostic pop
2327 }
24-
28+
2529 };
2630
2731 namespace internal
2832 {
29-
33+
3034 template <typename From, typename To>
3135 static void cast (void * from_, void * to_, npy_intp n, void * /* fromarr*/ , void * /* toarr*/ )
3236 {
@@ -38,7 +42,7 @@ namespace eigenpy
3842 to[i] = eigenpy::cast<From,To>::run (from[i]);
3943 }
4044 }
41-
45+
4246 template <typename T, int type_code = NumpyEquivalentType<T>::type_code>
4347 struct SpecialMethods
4448 {
@@ -54,7 +58,7 @@ namespace eigenpy
5458 inline static int fillwithscalar (void * buffer_, npy_intp length,
5559 void * value, void * arr);
5660 };
57-
61+
5862 template <typename T>
5963 struct OffsetOf
6064 {
@@ -63,10 +67,10 @@ namespace eigenpy
6367 char c;
6468 T v;
6569 };
66-
70+
6771 enum { value = offsetof (Data, v) };
6872 };
69-
73+
7074 template <typename T>
7175 struct SpecialMethods <T,NPY_USERDEF>
7276 {
@@ -79,7 +83,7 @@ namespace eigenpy
7983 T & t2 = *static_cast <T*>(src);
8084 t1 = t2;
8185 }
82-
86+
8387 if (swap)
8488 {
8589 T & t1 = *static_cast <T*>(dst);
@@ -112,14 +116,14 @@ namespace eigenpy
112116 // / pointed to by data. This function deals with “misbehaved” arrays.
113117 // / If successful, a zero is returned, otherwise, a negative one is returned
114118 // / (and a Python error set).
115-
119+
116120 // / \param[in] src_obj Pointer to the location of the python object
117121 // / \param[in] dest_ptr Pointer to the location in the array where the source object should be saved.
118122 // / \param[in] array Pointer to the location of the array
119123 // /
120124 // / \returns int Success(0) or Failure(-1)
121125 // /
122-
126+
123127 inline static int setitem (PyObject * src_obj, void * dest_ptr, void * array)
124128 {
125129// std::cout << "setitem" << std::endl;
@@ -132,7 +136,7 @@ namespace eigenpy
132136 PyArray_Descr * descr = PyArray_DTYPE (py_array);
133137 PyTypeObject * array_scalar_type = descr->typeobj ;
134138 PyTypeObject * src_obj_type = Py_TYPE (src_obj);
135-
139+
136140 if (array_scalar_type != src_obj_type)
137141 {
138142 std::stringstream ss;
@@ -141,7 +145,7 @@ namespace eigenpy
141145 eigenpy::Exception (ss.str ());
142146 return -1 ;
143147 }
144-
148+
145149 bp::extract<T&> extract_src_obj (src_obj);
146150 if (!extract_src_obj.check ())
147151 {
@@ -151,33 +155,33 @@ namespace eigenpy
151155 eigenpy::Exception (ss.str ());
152156 return -1 ;
153157 }
154-
158+
155159 const T & src = extract_src_obj ();
156160 T & dest = *static_cast <T*>(dest_ptr);
157161 dest = src;
158162
159163 return 0 ;
160164 }
161-
165+
162166 inline static void copyswapn (void * dst, long dstride, void * src, long sstride,
163167 long n, int swap, void * array)
164168 {
165169// std::cout << "copyswapn" << std::endl;
166-
170+
167171 char *dstptr = static_cast <char *>(dst);
168172 char *srcptr = static_cast <char *>(src);
169-
173+
170174 PyArrayObject * py_array = static_cast <PyArrayObject *>(array);
171175 PyArray_CopySwapFunc * copyswap = PyArray_DESCR (py_array)->f ->copyswap ;
172-
176+
173177 for (npy_intp i = 0 ; i < n; i++)
174178 {
175179 copyswap (dstptr, srcptr, swap, array);
176180 dstptr += dstride;
177181 srcptr += sstride;
178182 }
179183 }
180-
184+
181185 inline static npy_bool nonzero (void * ip, void * array)
182186 {
183187// std::cout << "nonzero" << std::endl;
@@ -196,7 +200,7 @@ namespace eigenpy
196200 return (npy_bool)(tmp_value != ZeroValue);
197201 }
198202 }
199-
203+
200204 inline static void dotfunc (void * ip0_, npy_intp is0, void * ip1_, npy_intp is1,
201205 void * op, npy_intp n, void * /* arr*/ )
202206 {
@@ -206,13 +210,13 @@ namespace eigenpy
206210 typedef const Eigen::Map<const VectorT,0 ,InputStride> ConstMapType;
207211
208212 ConstMapType
209- v0 (static_cast <T*>(ip0_),n,InputStride (is0/sizeof (T))),
210- v1 (static_cast <T*>(ip1_),n,InputStride (is1/sizeof (T)));
211-
213+ v0 (static_cast <T*>(ip0_),n,InputStride (is0/(Eigen::DenseIndex) sizeof (T))),
214+ v1 (static_cast <T*>(ip1_),n,InputStride (is1/(Eigen::DenseIndex) sizeof (T)));
215+
212216 *static_cast <T*>(op) = v0.dot (v1);
213217 }
214-
215-
218+
219+
216220 inline static int fillwithscalar (void * buffer_, npy_intp length,
217221 void * value, void * /* arr*/ )
218222 {
@@ -225,8 +229,8 @@ namespace eigenpy
225229 }
226230 return 0 ;
227231 }
228-
229-
232+
233+
230234 static int fill (void * data_, npy_intp length, void * /* arr*/ )
231235 {
232236// std::cout << "fillwithscalar" << std::endl;
@@ -240,10 +244,10 @@ namespace eigenpy
240244 }
241245 return 0 ;
242246 }
243-
247+
244248
245249 }; // struct SpecialMethods<T,NPY_USERDEF>
246-
250+
247251 } // namespace internal
248252
249253
@@ -252,17 +256,17 @@ namespace eigenpy
252256 {
253257 PyArray_Descr* from_array_descr = Register::getPyArrayDescr<From>();
254258// int from_typenum = Register::getTypeCode<From>();
255-
259+
256260// PyTypeObject * to_py_type = Register::getPyType<To>();
257261 int to_typenum = Register::getTypeCode<To>();
258262 assert (to_typenum >= 0 && " to_typenum is not valid" );
259263 assert (from_array_descr != NULL && " from_array_descr is not valid" );
260-
264+
261265 std::cout << " From: " << bp::type_info (typeid (From)).name () << " " << Register::getTypeCode<From>()
262266 << " to: " << bp::type_info (typeid (To)).name () << " " << Register::getTypeCode<To>()
263267 << " \n to_typenum: " << to_typenum
264268 << std::endl;
265-
269+
266270 if (call_PyArray_RegisterCastFunc (from_array_descr,
267271 to_typenum,
268272 static_cast <PyArray_VectorUnaryFunc *>(&eigenpy::internal::cast<From,To>)) < 0 )
@@ -277,7 +281,7 @@ namespace eigenpy
277281 eigenpy::Exception (ss.str ());
278282 return false ;
279283 }
280-
284+
281285 if (safe && call_PyArray_RegisterCanCast (from_array_descr,
282286 to_typenum,
283287 NPY_NOSCALAR) < 0 )
@@ -292,7 +296,7 @@ namespace eigenpy
292296 eigenpy::Exception (ss.str ());
293297 return false ;
294298 }
295-
299+
296300 return true ;
297301 }
298302
@@ -307,34 +311,34 @@ namespace eigenpy
307311 bp::type_info type = bp::type_id<T>();
308312 const bp::converter::registration* registration =
309313 bp::converter::registry::query (type);
310-
314+
311315 // If the class is not registered, return None.
312316 if (!registration) {
313317 // std::cerr<<"Class Not Registered. Returning Empty."<<std::endl;
314318 return bp::object ();
315319 }
316-
320+
317321 bp::handle<PyTypeObject> handle (bp::borrowed (registration->get_class_object ()));
318322 return bp::object (handle);
319323 }
320-
324+
321325 template <typename Scalar>
322326 int registerNewType (PyTypeObject * py_type_ptr = NULL )
323327 {
324328 // Check whether the type is a Numpy native type.
325329 // In this case, the registration is not required.
326330 if (isNumpyNativeType<Scalar>())
327331 return NumpyEquivalentType<Scalar>::type_code;
328-
332+
329333 // Retrieve the registered type for the current Scalar
330334 if (py_type_ptr == NULL )
331335 { // retrive the type from Boost.Python
332336 py_type_ptr = Register::getPyType<Scalar>();
333337 }
334-
338+
335339 if (Register::isRegistered (py_type_ptr))
336340 return Register::getTypeCode (py_type_ptr); // the type is already registered
337-
341+
338342 PyArray_GetItemFunc * getitem = &internal::SpecialMethods<Scalar>::getitem;
339343 PyArray_SetItemFunc * setitem = &internal::SpecialMethods<Scalar>::setitem;
340344 PyArray_NonzeroFunc * nonzero = &internal::SpecialMethods<Scalar>::nonzero;
@@ -343,7 +347,7 @@ namespace eigenpy
343347 PyArray_DotFunc * dotfunc = &internal::SpecialMethods<Scalar>::dotfunc;
344348 PyArray_FillFunc * fill = &internal::SpecialMethods<Scalar>::fill;
345349 PyArray_FillWithScalarFunc * fillwithscalar = &internal::SpecialMethods<Scalar>::fillwithscalar;
346-
350+
347351 int code = Register::registerNewType (py_type_ptr,
348352 &typeid (Scalar),
349353 sizeof (Scalar),
@@ -353,13 +357,13 @@ namespace eigenpy
353357 dotfunc,
354358 fill,
355359 fillwithscalar);
356-
360+
357361 call_PyArray_RegisterCanCast (call_PyArray_DescrFromType (NPY_OBJECT),
358362 code, NPY_NOSCALAR);
359-
363+
360364 return code;
361365 }
362-
366+
363367} // namespace eigenpy
364368
365369#endif // __eigenpy_user_type_hpp__
0 commit comments