Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 444
Jack Zhang Avatar asked Jul 01 '26 00:07

Jack Zhang


1 Answers

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.

like image 92
m7thon Avatar answered Jul 02 '26 13:07

m7thon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!