Boost logo

Boost Users :

From: David Abrahams (yg-boost-users_at_[hidden])
Date: 2002-07-25 16:13:04


From: "Srivatsan Raghavan" <vraghavan_at_[hidden]>

> hello all.. i've got some questions about using bpl, and i've searched
> the docs & the examples, and
> i can find no answer
>
> this example illustrates the issues i'm having
>
> <cpp>
> class B ;
> class C ;
>
> class A
> {
> public:
> A() ;
> virtual bool func1 ( ) ;
> virtual bool func2 ( ) = 0 ;
> virtual ~A() ;
> virtual int func3 ( std::list<C> ) ;
> protected :
> B m_Bobj ;
> std::string m_Aname ;
> } ;
> </cpp>
>
> now , i know how to expose the virtual and pure virtual functions to
pyhon,
> i just write a derived class A_callback, and write some static default
> functions that forward
> the impl to the virtual function in A .
> my problem is in the func3() and the protected members
> do i need to write my own wrapper functions to python for mapping a
> list<C> ?

You can just use class_builder<std::list<C> > to expose it, the same as you
would any other class.

> or does bpl do this itself in some way?

Unfortunately not. In BPL v2 I hope to incorporate some facilities from
Ralf Grosse-Kunstleve which make it much easier to translate between Python
sequences (lists, tuples, etc) and C++ containers.

> and how do i expose the protected members to python?
> if i do something like this :
> <cpp>
> boost::python::class_builder<A , A_callback> a_class ( this_module ,
> "A" ) ;
> a_class.def ( &A::m_Bobj , "m_Bobj" ) ;
> </cpp>
> that line of code gets me this error (in msvc 6 : sp 5 ) :
> error C2248: 'm_Bobj': cannot access protected member declared in class
> 'Logging::Endpoint'
> <file-name> : see declaration of 'm_Bobj'

Hence the name "protected"

> how can i get this to work?

Well, members are probably protected for a reason...

However, if you really want to break encapsulation, since you're writing a
callback class anyway you could add static get/set member functions to your
callback class, then expose them as __getattr__<NAME>__ and
__setattr__<NAME>__.

HTH,
Dave

           ---------------------------------
           David Abrahams * Boost Consulting
dave_at_[hidden] * http://www.boost-consulting.com


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net