[C++-sig] python::object that takes ownership of a C++ instance

Roman Yakovenko roman.yakovenko at gmail.com
Sat May 17 19:30:18 CEST 2008


On Fri, May 16, 2008 at 10:49 PM, Francis Moreau
<francis_moreau at hotmail.com> wrote:
> Hi,
>
> I would like to know if there is the equivalent of manage_new_object
> return_policy but for an argument pass to Python?
> I have a Python class
> derived from a C++ base class:
>
> class PyEventProcessor(CppEventProcessor):
>  def processEvent(in_event):
>    # do something
>
> And then in C++ I want to call processEvent and transferring the ownership
> of the event to Python:
>
> void CppEventProcessor_Wrapped::pyProcessEvent(const Event& in_event)
> {
>  // This following line is working well, but I want to transfert the
> ownership of the event instead of passing a reference on it.
>  return python::call_method<void>(m_pSelf, "processEvent",
> boost::ref(in_event)):
>
>  // I tried something like this but it's not working.
>  Event* pNewEvent = in_event.clone();
>  python::object obj(&pNewEvent); // Event is a type exposed in Python
> }

If I remember right, you can pass here std::auto_ptr<Event>

> I would like to know what is the best way to do something like this, is it
> by using a auto_ptr, or a from_python convertor, or something else? Also, I
> have a few types richer than Event, and exposed to Python too, and I want to
> be able to use them directly in Python, e.g.:
>
> class PyEventProcessor(CppEventProcessor):
>  def processEvent(in_event):
>    if in_event is ConnectionEvent:
>      srcAddr = in_event.getSourceAddr()

The last question is not clear. If you created module, than you can
import it and use.

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list