[C++-sig] Re: Problem: held type, std::auto_ptr, ReferenceErrors.
David Abrahams
dave at boost-consulting.com
Tue Aug 26 04:22:34 CEST 2003
Nicodemus <nicodemus at globalite.com.br> writes:
> David Abrahams wrote:
>
>>Prabhu Ramachandran <prabhu at aero.iitm.ernet.in> writes:
>>
>>> DA> struct Foo_Wrapper : Foo {
>>> DA> Foo_Wrapper(PyObject* self)
>>> DA> : m_self(self) {}
>>>
>>> DA> Foo* f() {
>>> DA> object self(handle<>(borrowed(m_self)));
>>> DA> return extract<Foo*>(self.attr("f")());
>>> DA> } DA> PyObject* m_self;
>>> DA> };
>>>
>>>Yes, that is possible but I'm using Pyste. Maybe we need another hook
>>> to insert code there?
>>
>>I guess that's up to Nicodemus.
>>
>
> You can do that already, you just have to write a wrapper for
> f. Here's an example:
>
> ---------------- foo.h:
>
> struct Foo
> {
> virtual int f() { return 0; }
> };
>
>
> --------------- foo.pyste
>
> Include('iostream')
> f_wrapper = Wrapper('f_wrapper',
> '''
> int f_wrapper(Foo *foo) {
> std::cout << "f!" << std::endl;
> return foo->Foo::f();
> }
> ''')
>
> Foo = Class('Foo', 'foo.h')
> set_wrapper(Foo.f, f_wrapper) Will generate this:
>
> int f_wrapper(Foo *foo) {
> std::cout << "f!" << std::endl;
> return foo->Foo::f();
> }
>
> namespace {
>
> struct Foo_Wrapper: Foo
> {
> ...
>
> int f() {
> return call_method< int >(self, "f");
> }
>
> int default_f() {
> return f_wrapper(this);
> }
>
> ...
> }; But perhaps this is not enough, since the
> method in the original post needed to access the PyObject* of the
> Wrapper class. Is it possible to get the PyObject* given a Foo
> instance?
No, but you can use a back_reference<Foo> or shared_ptr<Foo> argument
and pass it to object() to get the PyObject*.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Cplusplus-sig
mailing list