views:

50

answers:

0

Hi,

I'm trying to wrap a C++ library which uses auto_ptr. I'm using swig and want to generate python bindings. I'v seen the section of the swig docu on how to use swig with smart pointers here[0]. But I can't get it to work.

swig generates code that wants to initialize the auto_ptr using a const reference, but auto_ptr defines the copy constructor with a non-const reference e.g. auto_ptr(auto_ptr &). The generated code does not compile with "discards const qualifiers". When I manually delete the const qualifier the code compiles fine.

I'v seen lots of mailing list entries but nothing helped. Can someone provide me with a working example. My non working sample is here:

%module auto_ptr_test
%{
#include <memory>
#include <iostream>
using namespace std;
%}
namespace std {
template <class T>
class auto_ptr {
    auto_ptr();
    auto_ptr(auto_ptr &);
    T *operator->() const;
};
}

%inline %{
class Test {
Test() {
    cout << "Test()" << endl;
}
public:
static std::auto_ptr<Test> create() const {
    return auto_ptr<Test>(new Test());
}
void greet() {
    cout << "hello" << endl;
}
};
%}

%template () std::auto_ptr<Test>;

I compiled it using cmake with the following CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})

FIND_PACKAGE(PythonLibs)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

SET(CMAKE_SWIG_FLAGS "")

SET_SOURCE_FILES_PROPERTIES(auto_ptr_test.i PROPERTIES CPLUSPLUS ON)
SWIG_ADD_MODULE(auto_ptr_test python auto_ptr_test.i)
SWIG_LINK_LIBRARIES(auto_ptr_test ${PYTHON_LIBRARIES})

Thanks

[0] http://www.swig.org/Doc2.0/SWIGDocumentation.html#SWIGPlus_smart_pointers