简体   繁体   中英

SWIG C++ List to Java

I am trying to use SWIG to wrap C++ where it defined a LIST of objects like this

    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:

%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 . Looking at the one provided for std::vector , this can be adapted to 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

%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. 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 . You can just download and use ( %include ) it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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