[C++-sig] Re: Getting Python Object for C++ object

Peter Dimov pdimov at mmltd.net
Fri Aug 15 15:46:57 CEST 2003


<abeyer at uni-osnabrueck.de> wrote in message
news:1163.192.124.248.20.1060937893.squirrel at webmail.rz.uni-osnabrueck.de
>
> That works. Find below an example. Note, that I am referencing the
> '_internal_weak_this' in order to find out if I actually have a shared
> pointer for this.

Please don't. _internal_things are implementation details.

> (I do not want to wait for the bad_weak_ptr
> exception.) Dave, I am not on the boost developer list. Could you
> suggest the
> following extension to enable_shared_from_this:
>
> bool has_shared_from_this()
> {
>      return ! _internal_weak_this.expired();
> }

[...]

> class A : public enable_shared_from_this<A> {
>   public:
>     typedef shared_ptr<A> A_ptr;
>     virtual A_ptr self() {
>     if( _internal_weak_this.expired() )
> throw RuntimeError("Shared pointer not available.");
>     shared_ptr<A> self = shared_from_this();

Sorry, I can't bring myself to view this as a legitimate motivating example
for the extension you are proposing. The only thing the code does is throw
another exception in place of bad_weak_ptr.

>     assert(self != 0);
>     return self;
>     }
>     void call_from_cpp(PyObject* other) {
>        python::call_method<void>( other, "hello",
>     python::object(self()) ); }
>     virtual ~A() {}
> }







More information about the Cplusplus-sig mailing list