문제

Is there a good, automated way to allow me to pass shared_ptr's into functions that expect references in a SWIG interface?

I have a library that provides functions like:

// Module message
typedef boost::shared_ptr< Item > ItemPtr;
class Message{
  public:
     ItemPtr getItem( int index) ...
     ...
};

And then another library that uses them:

// Module client
class Client{
  public:
    void processItem( const Item& item );
}

These are in separate modules.

Obviously, I want to do something like:

>>> item=message.getItem(4)
>>> client.processItem( item )

in python, but the types do not line up, the item is a shared_ptr while the function expects a reference.

So far my solution is to override the functions in the interface file

%extend client {
    void processItem( ItemPtr ptr){ 
        $self->processItem( *ptr);
    }
}

Is there an approach where where I can avoid having to duplicate all of the interface functions that expect a reference, but where I'll have shared_ptr's?

도움이 되었습니까?

해결책

Just return an Item instance from your getItem(int index) function. Ohterwise you'll have to overload the functions in order to receive both arguments types (shared_ptr and Item references)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top