首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将boost::tuples::tuple公开到Java绑定?

如何将boost::tuples::tuple公开到Java绑定?
EN

Stack Overflow用户
提问于 2012-03-12 19:33:30
回答 1查看 705关注 0票数 0

我有一份boost::tuple的名单。我想通过SWIG将这个元组列表公开给Java绑定。但是当我尝试编译由SWIG生成的mt wrap.cxx时,我得到了以下错误:

代码语言:javascript
复制
d:\xyz\...\vector.h(115) : error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const boost::tuples::tuple<T0,T1>' (or there is no acceptable conversion)
        with
        [
            T0=std::string,
            T1=std::string
        ]
        c:\program files\microsoft visual studio 8\vc\platformsdk\include\guiddef.h(192): or 'int operator ==(const GUID &,const GUID &)'
        while trying to match the argument list '(const boost::tuples::tuple<T0,T1>, const MyTuple)'
        with
        [
            T0=std::string,
            T1=std::string
        ]
        d:\xyz\...\vector.h(111) : while compiling class template member function 'int Vector<T>::index(const T &) const'
        with
        [
            T=MyTuple
        ]
        d:\xyz\...\MyTuple_wrap.cxx(17731) : see reference to class template instantiation 'Vector<T>' being compiled
        with
        [
            T=MyTuple
        ]

有人能告诉我应该怎么做才能解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-07 19:14:47

目前还不清楚您是如何得出您所显示的错误的。默认情况下,boost::tuple很难包装,而且似乎没有任何标准接口包含在SWIG中。在我的测试中,如果不手动编写接口文件,我就无法接近您所看到的错误。

但是,我使用下面的接口文件成功地包装了boost的元组:

代码语言:javascript
复制
%{
#include <boost/tuple/tuple.hpp>
%}

namespace boost {
  template <typename T1=void, typename T2=void, typename T3=void> 
  struct tuple;

  template <>
  struct tuple<void,void,void> {
  };

  template <typename T1>
  struct tuple<T1, void, void> {
    tuple(T1);
    %extend {
      T1 first() const {
        return boost::get<0>(*$self);
      }
    }
  };

  template <typename T1, typename T2>
  struct tuple <T1, T2, void> {
    tuple(T1,T2);
    %extend {
      T1 first() const {
        return boost::get<0>(*$self);
      }
      T2 second() const { 
        return boost::get<1>(*$self);
      }
    }
  };

  template <typename T1, typename T2, typename T3> 
  struct tuple <T1,T2,T3> {
    tuple(T1,T2,T3);
    %extend {
      T1 first() const {
        return boost::get<0>(*$self);
      }
      T2 second() const {
        return boost::get<1>(*$self);
      }
      T3 third() const {
        return boost::get<2>(*$self);
      }
    }
  };
}

基本上,它所做的就是将访问器函数添加到您可能关心的每个元组的特殊化中。它足以使其在Java或其他语言中最小程度上有用。您可能希望在此基础上进行扩展,以涵盖更大的元组。如果您的元组不是不可变的,那么您可能想让成员函数get/set。

我可以用一个SWIG模块来测试:

代码语言:javascript
复制
%module test

%include "boost_tuple.i"

%template(TestTuple) boost::tuple<int, double, char>;

%template(SingleTuple) boost::tuple<char>;

%inline %{
boost::tuple<int, double, char> func1() {
  return boost::make_tuple(3, 2.0, '1');
}

void test1(boost::tuple<int, double, char>) {
}

%}

它在以下Java中的工作效果与预期一致:

代码语言:javascript
复制
public class run {
  public static void main(String[] argv) {
    System.loadLibrary("test");
    TestTuple t = test.func1();
    System.out.println("1: " + t.first() + " 2: " + t.second() + " 3: " + t.third());
    test.test1(test.func1());
    test.test1(new TestTuple(0, 0.0, '0'));
  }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9666260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档