2015-09-28 48 views
0

在我的java代码中,我想调用std::set的默认构造函数,并插入一个例如在C++以下代码:std :: set在java swig中的支持

struct foo; 
foo bar(); 
std::set<foo> toto; 
toto.insert(b); 

provides support for various STL containers for java痛饮诸如std::vectorstd::stringstd::map ...但是存在用于std::set没有支持。

所以我发现这个solution它涉及一个C++包装和一个java包装。我还没有尝试过,我不确定它会起作用,我觉得不方便。

我们可以通过提出一个处理基本set构造函数和插入的最小swig接口来做得更好吗?

例如接口std_set.i喜欢:

%{ 
#include <set> 
#include <pair> 
#include <stdexcept> 
%} 
namespace std { 
template<class T> class set { 
public: 
    typedef T value_type; 
    set(); 
    pair<iterator,bool> insert(const value_type& val); // iterator might be the difficulty here 
} 

或特定的模板实例创建包装:

%rename(SetFoo) std::set<foo>; 
class std::set<foo> { 
public: 
    set(); 
    std::pair<std::set<foo>::iterator,bool> insert(const &foo val); // std::set<foo>::iterator not known here... 
}; 

在这两种情况下,我坚持这个迭代器的问题,不能来了采用“简单”解决方案。

我试过玩swig/Lib/std也没有成功的时刻。

回答

0

我终于决定放弃了迭代器,并用一个简单的包装头来到了我的需求:

#include <set> 

template<typename T> 
struct SetWrapper { 
    SetWrapper() : data() {}; 
    void insert(const T& val) { data.insert(val); } 
    std::set<T> data; 
}; 

然后在界面上,我们可以实例化不同版本的模板:

%template(SetFoo) SetWrapper<Foo>; 

我不得不关心的方法有一个参数std::set我不得不扩展一个类如下:

struct A { 
    A(const std::set& s); // this cannot be used directly unfortunately 
} 

%extend A { 
    A(const SetWrapper& s) { // need a constructor with SetWrapper 
    A *p = new A(s.data); // reuse the constructor above 
    return p; 
    } 
} 
+0

你还对“更好”的解决方案感兴趣吗? – Flexo

+0

如果您有任何感谢,谢谢! – coincoin