[C++-sig] Custom from-python converter (almost working)
pyplusplus at assumetheposition.nl
pyplusplus at assumetheposition.nl
Mon Dec 8 10:37:58 CET 2008
Dang, my mailer got in the way and sent this before I finished it ...
> 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.
class C
{
public:
void set_data(data* d);
};
void
extra_method(C& self, PyObject *obj)
{
// if obj is list of tuples, convert to
// data* and
// call self.set_data(d)
}
class_<C>("C")
.def("set_data", &C::set_data)
.def("set_data", &C::extra_method)
;
Or will this completely muck up the way Python objects are handled on the
interface?
>> 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>
Thanks for the hint,
Paul
More information about the Cplusplus-sig
mailing list