logo       

Re: How to return a std::vector of pointers to Java?: msg#00148

programming.swig

Subject: Re: How to return a std::vector of pointers to Java?

Henrietta Slack wrote:

Hi, I'm just getting started with SWIG as a Java to C++ bridge. I need to return a std::vector of pointers from C++ to Java and I'm stuck; the code below works beautifully, I just need to convert the values to pointers.

namespace std {
%template(VectorOfClassA) vector<ClassA>;
};

class ClassA{
public:
int amember;
};

class ClassB{
public:
// this is handled fine; why not in vectors?
ClassA* createClassA();

// this works too but I want to avoid creating new objects
// via the copy c'tor when adding to the vector.
vector<ClassA> getClassAVector();
};

Can anybody help me change all of "vector<ClassA>" to "vector<ClassA*>", which I guess may need a new or improved typemap. Using the current std_vector.i typemap with the pointers generates a "VectorOfClassA.java" with a get() method that returns a "SWIGTYPE_p_p_ClassA" - I want it to return a "ClassA".


Firstly, the %template should really be placed after the definition of A, at the end of the file should work. Vectors of pointers are not very well supported, however, you can get them to work with a bit of manual intervention by supplying some new typemaps with the help of the specialize_std_vector macro:

%include "std_vector.i"
namespace std {
specialize_std_vector(ClassA*);
}

William
_______________________________________________
Swig maillist - Swig@xxxxxxxxxxxxxxx
http://mailman.cs.uchicago.edu/mailman/listinfo/swig



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise