[C++-sig] Boost Python and ctypes

Tristam MacDonald swiftcoder at gmail.com
Tue Feb 26 23:05:05 CET 2008


Now we are into the dizzying world of template error messages :)
So I have a class Matrix, which exposes a pointer to float, and a conversion
operator to POINTER(c_float):

struct Matrix {
int test();
float *ptr();
};

struct ctypes_float_ptr
{
static PyObject *convert(float *ptr)
{
   object ctypes = import("ctypes");
  long_ addr((unsigned)ptr);
  object pointerType(ctypes["POINTER"](ctypes["c_float"]));
  return ctypes["cast"](addr, pointerType).ptr();
}
};

BOOST_PYTHON_MODULE(math)
{
to_python_converter<float *, ctypes_float_ptr>();

class_<Matrix>("Matrix")
.def("test", &Matrix::test) // this compiles fine
.def("ptr", &Matrix::ptr) // error here
;
}

And the error is as follows:

src/math.cpp:146: error: no matching function for call to
'boost::python::class_<math::Matrix, boost::python::detail::not_specified,
boost::python::detail::not_specified,
boost::python::detail::not_specified>::def(const char [4], <unknown type>)'

Any pointers on where to go from here?

On 26/02/2008, Joseph Lisee <jlisee at gmail.com> wrote:
>
> > Thank you for the prompt answer - that accomplishes exactly what I was
> trying
> to do!
> > I haven't had a lot of luck tracking down the hooks to add a manual
> conversion
> for 'float *' into the types registry, do you have any reference for that
> handy?
> >
> > - Tristam
>
>
> No problem, I think this is what you are looking for:
> http://www.boost.org/libs/python/doc/v2/to_python_converter.html
>
>
> -Joseph Lisee
>
>
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20080226/e0fb79c5/attachment.htm>


More information about the Cplusplus-sig mailing list