GeneralBrokenLines V03-01-03
using EIGEN
|
how to use wrapper functions within java
More information about JNA can be found at its GitHub repository.
In general, the most difficult part of implementing the JNA-GBL interaction is getting the transfer of data between the java objects and the C++/GBL objects done well and safely. The JNA documentation apart of its repository contains more information about how to do this well - here I will simply walk through an example accessing a single class from GBL within java. In reality, a working solution would require a similar setup for almost all of the different GBL classes one would be interested in using.
It is suggested to enable the JNA_DEBUG build option when first developing a JNA-based java usage of GBL. This will help you make sure that you aren't leaking memory and avoid a program crash.
It is easiest to simply look at the java source code for this class so one can see how to interface with it. The basic idea is to define a singleton that represents the GBL library as loaded by JNA and then have classes whose job is to wrap these functions in simpler, object-oriented forms.
For function signatures to "match" between the functions defined in the JNA library extensions (GBLInterface
above) and the ones defined here, the return value type, the name, and the argument types need to match. The name is easy, but the types are slightly more complicated since the typename between java and C++ are different.
int
and double
) are the same.String
is converted to a C-style string char *
.IntByReference
(or DoubleByReference
) on the java side and a pointer on the C side.java
and then simply pass the pointer to the array to the C side.double position[3]
syntax and one just has to make sure to allocate the correct size on the java side and the C function will simply write to those addresses.PointerByReference
.When using JNA to call functions from a native library, the memory allocated by those functions is not monitored and cleaned up by java's garbage collector. Effectively, this means you need to have a delete
call for every Ctor
call you make.
Since this can get complicated very quickly, it is recommended to develop your java program with JNA_MEMORY_MONITOR enabled in the GBL C++ library. This will print out a summary of the GBL structures still allocated at the end of running allowing you to make sure that the GBL structures you created are also deleted.