Mapping C++ std::vector in Structs

1,181 views
Skip to first unread message

mustafa

unread,
Jun 27, 2012, 8:50:05 AM6/27/12
to jna-...@googlegroups.com
I need to map some C++ structs including std::vector<T> types. For example:

struct MyStruct {
    double mynum;
    std::vector<double> numlist;
    std::vector<MessageStruct> messagelist;
}

How to map this struct with Java?

My second question is: I have a C++ class, namely Matrix, deriving from vector:

    template<class T>
    class Matrix : public vector<T>

and this Matrix type is used within some structs. For example:

struct AnalysisOutputStruct {
    double mynum;
    Matrix<double> mymatrix;
}

Again how to map such a struct?
Thanks.

Timothy Wall

unread,
Jun 27, 2012, 6:05:10 PM6/27/12
to jna-...@googlegroups.com
short answer: you don't
long answer: C++ classes aren't automatically mapped onto something more or less equivalent in Java, and can't be without a significant amount of work. You'd need to provide an alternate struct where the data is directly accessible as a contigous block of double or MessageStruct (with a field type of double or Structure.ByReference).

You *might* be able to extract such a contiguous block directly from the std::vector, but such data is likely private and for robustness would likely require copying the contents out into a more appropriate format.

Mustafa ÖZÇETiN

unread,
Jun 27, 2012, 11:59:34 PM6/27/12
to jna-...@googlegroups.com
So I think I should write an adapter dll with C++ and map the vector<T> types with one dimensional array of structs.
Thanks for the response.

Mustafa



From: Timothy Wall <twal...@java.net>
To: jna-...@googlegroups.com
Sent: Thursday, June 28, 2012 1:05 AM
Subject: Re: Mapping C++ std::vector in Structs

Kustaa Nyholm

unread,
Jun 28, 2012, 1:47:33 AM6/28/12
to jna-...@googlegroups.com
From: Timothy Wall <twal...@java.net>
>short answer: you don't
>long answer: C++ classes aren't automatically mapped onto something more or less equivalent in Java,
>and can't be without a significant amount of work. 

This type of question (mapping C++) often pops up on this list.

I accept both the short and long answers but I've not seen it explained why?

So I'm sort of looking for an 'even longer answer'.

Is it *only* the name mangling (which I presume varies from C++ compiler to compiler)
or is there something else? I mean at the end of the day every C++ class member function will
be a standard C function with a mangled name with the first argument being a pointer
to an instance of the class, right?

Trickier part is properly calling virtual functions as one would need to figure out how the
vtable works. But again I presume there is some sort of index to a vtable that the
C++ compiler creates and that is passed to some vtable lookup system and it should be
possible to call them using that mechanism?

The info for above could likely be harvested from the the C++ code with disassembly
tools or compiler linker/listings...

So above is what Timothy meant when he wrote "can't be without a significant amount of work"?!

I can see virtual base classes and pointer casting could get pretty interesting ...
and runtime type information too...begins to feel like a challenge!

Templates seem to add yet another complication as the functions are generated
only if the template is instantiated ... that would be pretty much impossible with JNA?

Why am I rambling about this? Cause it would seem to me that for some simple
cases it would/should be feasible to call C++ stuff from Java using JNA but it can
get awfully complicated pretty easily.

I don't see writing a C wrapper as an option as then one loses the very thing that
makes JNA attractive ie getting rid of the C tool chain. On might as well use JNI
then. Of course it is totally doable.

So the 'short answer' is mostly spot on I guess.

One more thing: an entry (lost the reference) in StackOverflow suggested that
jnaerator can generate JNA mappings for C++ classes, is that correct?

If so what are the limitations?

I have no immediate need for this, hence too lazy to try to find out, but if
someone wants to educate the followers of this list feel free to educate us...

cheers Kusti

And thanks for Timothy for JNA and the support!




Timothy Wall

unread,
Jun 28, 2012, 8:31:18 AM6/28/12
to jna-...@googlegroups.com

On Jun 28, 2012, at 1:47 AM, Kustaa Nyholm wrote:

> From: Timothy Wall <twal...@java.net>
>> short answer: you don't
>> long answer: C++ classes aren't automatically mapped onto something more or less equivalent in Java,
>> and can't be without a significant amount of work.
>
> This type of question (mapping C++) often pops up on this list.
>
> I accept both the short and long answers but I've not seen it explained why?
>
> So I'm sort of looking for an 'even longer answer'.
>
> Is it *only* the name mangling (which I presume varies from C++ compiler to compiler)
> or is there something else? I mean at the end of the day every C++ class member function will
> be a standard C function with a mangled name with the first argument being a pointer
> to an instance of the class, right?
>
> Trickier part is properly calling virtual functions as one would need to figure out how the
> vtable works. But again I presume there is some sort of index to a vtable that the
> C++ compiler creates and that is passed to some vtable lookup system and it should be
> possible to call them using that mechanism?
>
> The info for above could likely be harvested from the the C++ code with disassembly
> tools or compiler linker/listings...
>
> So above is what Timothy meant when he wrote "can't be without a significant amount of work"?!

The issues are these (to start with). Some of which have been addressed by JNAerator; in the end it's someone who's got the time and motivation to implement this stuff, probably at a layer on top of JNA with a few things pushed down to the native layer as needed (most, if not all, would *not* need to be implemented at a native level). Samuel Audet has done a fantastic job with all the bits that JNAerator does.

* Name mangling (straightforward to replicate for gcc, less so (but doable) for MSVC, after that progressively more difficult, depending on whether the compiler maker documents their stuff and access to said compilers)
* base level of memory allocation/deallocation support (new/delete w/array variants)
* vtable invocation - required for calling object methods; again, specific to the compiler

And on to the other things you mentioned...
* templates
* multiple inheritance
* many other more esoteric and complicated things that make people either love or hate C++

Some of the above require detailed specification of the classes in question, which either has to be done manually (yuck) or you have to run a parsing step (as JNAerator does) to ensure you get the correct information. Before long you're re-implementing gcc AND MSVC AND any other compiler in Java.

Given the proportion of StackOverflow questions on JNI versus JNA, it seems a large number of people are still unaware of JNA (although a large number of JNI questions are android-related).
Reply all
Reply to author
Forward
0 new messages