[C++-sig] Exposing members of members (original) (raw)

Ricardo Abreu gumbeto at gmail.com
Tue Jan 20 11:30:30 CET 2009


Hello,

Thank you very much for your answer, but what I really wanted was to expose to Python just the first element of the pair and not the whole pair, so that the fact that it is a pair in c++ would be transparent to python. For instance, if I had struct A{std::pair<int,bool> a;};, I just wanted to expose a.first, as a simple int... Any ideas for this?

Thanks, Ricardo

Ralf W. Grosse-Kunstleve wrote:

I'd define to/from Python conversions for the std::pair, mapping to Python tuples, then use .addproperty().

Alternatively, wrap std::pair the normal way via class, then you can use .defreadwrite(). You need to handle each std::pair<A,B> combination of A,B separately, like any other template class you want to wrap or convert to/from a Python object. See also: http://www.boost.org/doc/libs/1370/libs/python/doc/v2/faq.html#customstring This could be a starting point for std::pair<A,B> conversions. Probably you want to make it into a converter template if you have more than one combination of A,B. Here is code we use for the topython conversions: http://cci.lbl.gov/cctbxsources/boostadaptbx/ stdpairconversion.h stdpairext.cpp We don't have corresponding frompython conversions. If you add them, let me know! The next item in the FAQ is also relevant.

----- Original Message ---- From: Ricardo Abreu <gumbeto at gmail.com> To: cplusplus-sig at python.org Sent: Monday, January 19, 2009 7:27:46 AM Subject: [C++-sig] Exposing members of members Hello, Suppose that I have a class A with a member of type std::pair, called a. If I want to expose class A to python while allowing read/write access to the object pointed to by a.first, what to I put in defreadwrite? I tried something like class("A").defreadwrite("a", &A::a::first); but it doesn't work. Also been trying with boost::bind but didn't have any luck :s I know I could do getters and setters, but there would be a lot of them because my "A" has a lot of members like "a". Thank you in advance, Ricardo



More information about the Cplusplus-sig mailing list