简体   繁体   English

SWIG C ++ List to Java

[英]SWIG C++ List to Java

I am trying to use SWIG to wrap C++ where it defined a LIST of objects like this 我正在尝试使用SWIG来包装C ++,它定义了像这样的对象的LIST

    typedef std::list<Country> CountryList;

What exactly do I have to have in the interface file to accomodate for this? 我在接口文件中究竟需要具备什么才能适应这种情况?

Thanks, 谢谢,

Jack 插口

You simply need to add the following to your swig interface file: 只需将以下内容添加到swig界面文件中:

%include "std_list.i"
%include "Country.h" /* or declaration of Country */
%template(Country_List) std::list<Country>;

EDIT: I did not know that swig does not provide a std_list.i for Java to wrap std::list . 编辑:我不知道swig没有为Java提供std_list.i来包装std::list Looking at the one provided for std::vector , this can be adapted to std::list . 看一下为std::vector提供的那个,这可以适应std::list Note that most functionality is not available, since you really want to use iterators to access list elements. 请注意,大多数功能都不可用,因为您确实希望使用迭代器来访问列表元素。 Anyway, here it goes: 无论如何,这里是:

std_list.i std_list.i

%include <std_common.i>

%{
#include <list>
#include <stdexcept>
%}

namespace std {
    template<class T> class list {
      public:
        typedef size_t size_type;
        typedef T value_type;
        typedef const value_type& const_reference;
        list();
        list(size_type n);
        size_type size() const;
        %rename(isEmpty) empty;
        bool empty() const;
        void clear();
        const_reference back();
        %rename(add) push_back;
        void push_back(const value_type& x);
        void pop_back();
    };
}

Instead of wrapping std::list<Country> , you may instead want to convert this to a corresponding list in Java. 您可能希望将其转换为Java中的相应列表,而不是包装std::list<Country> For this, however, you need to write appropriate typemaps . 但是,为此,您需要编写适当的类型映射

std_list is not yet in the "Latest Release" (swig-3.0.12) available from the Downloads , but already in the master branch on github . std_list尚未在“最新版本”(痛饮-3.0.12)可以从下载 ,但已经在GitHub上的主分支 You can just download and use ( %include ) it. 您可以下载并使用( %include )它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM