[C++-sig] Custom from-python converter (almost working)

pyplusplus at assumetheposition.nl pyplusplus at assumetheposition.nl
Mon Dec 8 10:34:28 CET 2008


Hi,

>> void
>> p(vec3f *v)
>> {
>>     printf("%f, %f, %f\n", v->x, v->y, v->z);
>> }
>
> This is indeed not supported, since it would require "converters with
> write-back".

Ok, that makes sense.

> vec3f const* v
>
> should work, but
>
> ve3f* v
>
> doesn't because the missing const indicates that you want to modify the
> pointee in place.

Unfortunately I don't control the API. The actual use case I'm working on
(the vec3f was just a simplified example) is a class method that takes a
non-const pointer, but it does not modify the pointee. The pointee is
basically stored in the instance and is used as a data array. As such I
wanted a conversion from Python list of tuples to a C++ instance.

> For a Python tuple this can definitely not work since tuples are
> immutable.
> It could in theory work for a Python list, but Boost.Python doesn't
> support this.
> A few years ago we had extensive discussions about this, but it was too
> hard to
> implement.

I think I can imagine some obstacles :)

In SWIG it is possible to simply define an extra class method that takes a
PyObject* and perform any conversion you want in that method (although
this means having to add that extra method for all cases where the
non-const pointer is used). Would something similar work in Boost.Python,
e.g.

void
extra_method(C& self, PyObject *obj)
{

}

class_<C>("C")


>
> Ralf
>
> P.S.: I'm using this seasoned file for all "list/tuple/iter <-> small C++
> array" conversions:
>
> http://cctbx.svn.sourceforge.net/viewvc/cctbx/trunk/scitbx/boost_python/container_conversions.h?view=markup
>
> Probably, all you need is
>
>   tuple_mapping_fixed_capacity<vec3f>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>




More information about the Cplusplus-sig mailing list