[C++-sig] Pointers to simple types, arrays and arrays of pointers to types
Nicodemus
nicodemus at globalite.com.br
Sun Oct 5 04:50:19 CEST 2003
Niall Douglas wrote:
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>My apologies if this is really simple. I did search ASPN and didn't
>get anything relevent after 20 mins, so ...
>
>I have everything compiling except for eight of over 100 source files
>(thanks again to everyone here!). Most of these won't compile because
>some method returns a pointer to a simple type eg;
>
>typedef unsigned long FXColor;
>
>class FXImage
>{
>public:
> FXColor* getData() const;
>};
>
>using namespace boost::python;
>
>void Export_FXImage()
>{
> class_< FXImage >("FXImage")
> .def("getData", &FXImage::getData, return_internal_reference< 1
>
>
>>())
>>
>>
> ;
>};
>
>This won't compile because FXColor is not a class. Ideally I'd like
>the python side to be able to access the array of FXColor's being
>returned - no limits are needed because other methods return the
>length.
>
I don't think that's possible, since ints are immutable... it would at
least be very troublesome. 8/
Do you plan to change the values in Python? If not, create a wrapper
that returns a list of ints. If you want to change it in Python, you
will have to create a function setData, that receives a list of ints and
changes the original array. Notice that this function can be present
just in the binding, you don't have to code it for the C++ class. This
interface seems more Pythonic to me. 8)
>In other places still I have a find() function returning a pointer to
>a single float for example. In this situation I'd want the float to
>dereference to a float if the pointer is not null, otherwise return
>none. But if I use a thin wrapper, it can only return float and not
>none! :(
>
The wonders of Boost.Python (untested):
PyObject* find_wrapper(MyClass* cl)
{
float *res = cl.find();
if (res == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
else {
return PyFloat_FromDouble((float)v);
}
}
(I couldn't create a None using Boost Python's api, I tried object(NULL)
but it didn't work... how can one do that?)
>Lastly, some other methods return a type ** ie; an array of pointers
>to object instances. How on earth can this get wrapped? Is that
>indexing suite the right answer for these?
>
I think creating wrappers is the solution here.
Regards,
Nicodemus.
More information about the Cplusplus-sig
mailing list