auto_ptr with swig [英] auto_ptr with swig

查看:193
本文介绍了auto_ptr with swig的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图包装一个使用auto_ptr的C ++库。我使用swig
并想生成python绑定。我看到的swig文件的部分如何使用智能指针在这里[0]。但我不能让它工作。



swig生成的代码要使用const
引用初始化auto_ptr,但auto_ptr定义了一个非const
引用的复制构造函数。 auto_ptr(auto_ptr&)。生成的
代码不使用discards const qualifiers编译。当我手动
删除const限定符代码编译正常。



我看到很多邮件列表条目,但没有帮助。可以有人
为我提供一个工作榜样。我的非工作示例在这里:

 %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> ;;

我使用cmake与以下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})

感谢



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

解决方案

我在libRETS中找到了如何做的提示,你需要在每个方法的基础上做:



http://code.crt。 realtors.org/projects/librets/browser/librets/trunk/project/swig/auto_ptr_release.i?rev=HEAD



基本上,您想要解开auto_ptr你从C ++接收并在传递给C ++之前将其包装。要放入.i文件的代码示例是:

  //原始原型:
// virtual void SetSomething (std :: auto_ptr< ValueClass> value)= 0;
//由SWIG生成的替换:
%extend {
void SetSomething(ValueClass * value){
std :: auto_ptr< ValueClass> tmp(value);
$ self-> SetSomething(tmp);
}
}


//使用swig宏检索包含在auto_ptr中的对象:
%define SWIG_RELEASE_AUTO_PTR(RETURN_TYPE,METHOD_NAME,PROTO,ARGS)
%extend {
RETURN_TYPE * METHOD_NAME PROTO {
std :: auto_ptr< RETURN_TYPE> auto_result = self-> METHOD_NAME ARGS;
return auto_result.release();
}
}
%enddef
//然后在类中:
// virtual auto_ptr< ValueClass> SomeMethod(const string& foo)= 0;
//替换为:
SWIG_RELEASE_AUTO_PTR(ValueClass,SomeMethod,(const string& foo),(foo));


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

解决方案

I found hint how to do it in libRETS, and you need to do it on per-method basis:

http://code.crt.realtors.org/projects/librets/browser/librets/trunk/project/swig/auto_ptr_release.i?rev=HEAD

Basically you want to unwrap auto_ptr you receive from C++ and wrap it before passing to C++. Example of code to be put into .i file is:

    //original prototype:
    //virtual void SetSomething(std::auto_ptr<ValueClass> value) = 0;
    //replacement to be generated by SWIG:
    %extend{
        void SetSomething(ValueClass *value){
            std::auto_ptr<ValueClass> tmp(value);
            $self->SetSomething(tmp);
        }
    }


  //retrieving object wrapped in auto_ptr using swig macro:
  %define SWIG_RELEASE_AUTO_PTR(RETURN_TYPE, METHOD_NAME, PROTO, ARGS)
    %extend {
    RETURN_TYPE * METHOD_NAME PROTO {
        std::auto_ptr<RETURN_TYPE> auto_result = self->METHOD_NAME ARGS;
        return auto_result.release();
    }
  }
  %enddef
  // and then inside class:
  // virtual auto_ptr<ValueClass> SomeMethod(const string& foo) = 0;
  // replaced with:
  SWIG_RELEASE_AUTO_PTR(ValueClass,SomeMethod,(const string& foo),(foo));

这篇关于auto_ptr with swig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆