[C++-sig] Smart Pointers in Boost.Python with AutomaticDereferencing

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Tue May 20 01:19:50 CEST 2008


No, sorry.

Very wild idea: can your smart pointer be converted both ways to/from a boost::shared_ptr? And are all your smart pointer objects passed by value
or const&? Then maybe a custom rvalue converter could do the trick.

I hope someone else with actual experience in this area will be able
to help.

Ralf

----- Original Message ----
From: "Bilokon, Paul" <paul.bilokon at lehman.com>
To: Development of Python/C++ integration <c++-sig at python.org>
Sent: Monday, May 19, 2008 10:18:38 AM
Subject: Re: [C++-sig] Smart Pointers in Boost.Python with AutomaticDereferencing


Hi Ralf,

Many thanks for your reply.

Unfortunately, I do have a custom "smart" pointer and it would be very
difficult to convert it.

Do you know if there are any code examples for custom smart pointers?

Many thanks,
Paul

-----Original Message-----
From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org] On
Behalf Of Ralf W. Grosse-Kunstleve
Sent: Monday, May 19, 2008 5:52 PM
To: Development of Python/C++ integration
Subject: Re: [C++-sig] Smart Pointers in Boost.Python with
AutomaticDereferencing

Boost.Python has special support for boost::shared_ptr<>. E.g. this
should do what you want (untested):

  class_<A, shared_ptr<A> >("A")

It is more difficult only if you have your own smart pointers.

Ralf

----- Original Message ----
From: "Bilokon, Paul" <paul.bilokon at lehman.com>
To: Development of Python/C++ integration <c++-sig at python.org>
Sent: Monday, May 19, 2008 5:30:59 AM
Subject: [C++-sig] Smart Pointers in Boost.Python with Automatic
Dereferencing


Hi all,

I'm trying to find an authoritative resource on exposing functions that
return smart pointers using Boost.Python.

So far my best example is this:

http://wiki.python.org/moin/boost.python/PointersAndSmartPointers

However, I find the need to explicitly dereference the pointer in Python
deeply unsatisfactory:

print (+an_A).hello()

This is essentially C++ programming in Python. The whole purpose of the
exercise is to avoid the need for explicit memory management!

Is there another method that will simply allow me to call

print an_A.hello()

Many thanks for your help!

Best wishes,
Paul


P.S. Just for your information I'll paste the C++ code:

#include <boost/shared_ptr.h>
using namespace boost;

struct A {
    shared_ptr<A> create () { return shared_ptr<A>(new A); }
    std::string   hello  () { return "Just nod if you can hear me!"; }
};

BOOST_PYTHON_MODULE(shared_ptr)
{
    class_<A>("A",init<>())
        .def("create",&A::create,return_value_policy<return_by_value>())
        .staticmethod("create")
        .def("hello",&A::hello)
        ;

    class_< shared_ptr<A> >("A_ptr", init<const shared_ptr<A>& >())

.def("__pos__",&boost::shared_ptr<A>::get,return_internal_reference<>())
        ;
}

And the sample Python program:

from shared_ptr import *
an_A = A_ptr.create()
print (+an_A).hello()
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - -

This message is intended only for the personal and confidential use of
the designated recipient(s) named above.  If you are not the intended
recipient of this message you are hereby notified that any review,
dissemination, distribution or copying of this message is strictly
prohibited.  This communication is for information purposes only and
should not be regarded as an offer to sell or as a solicitation of an
offer to buy any financial product, an official confirmation of any
transaction, or as an official statement of Lehman Brothers.  Email
transmission cannot be guaranteed to be secure or error-free.
Therefore, we do not represent that this information is complete or
accurate and it should not be relied upon as such.  All information is
subject to change without notice.



_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig

_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This message is intended only for the personal and confidential use of the designated recipient(s) named above.  If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited.  This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers.  Email transmission cannot be guaranteed to be secure or error-free.  Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such.  All information is subject to change without notice.



_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig




More information about the Cplusplus-sig mailing list