[Qt-jambi-interest] QNativePointer to void*

Gunnar Sletta gunnar at trolltech.com
Thu Aug 14 10:41:19 CEST 2008


Karim Andras Pinter wrote:
> Hello!
> 
> I would like to create a utility class using jambi generation.
> One of the class functions should be like this:
> 
> Java side :
> void converter( byte[], QNativePointer )
> 
> C++ side :
> void converter( char*, int, void* )
> 
> So basicly byte[] -> to void* and lenght of byte[] and QNativePointer -> void*.
> Questions are : Is it possible to use QNativePointer as a void* if the length is known?
> How will the typesetting will look like? How the QNativePointer can be converted to void*?
> 

The best place to start, before doing any removal etc, is to see how the 
C++ function maps to Java. For the above case you should get (I'm just 
making up variable names):

C++:  void converter(char *str, int strLength, void *data)

Java: void converter(QNativePointer str, int strLength, QNativePointer data)

In the above, "str" will be typed to char and be 8-bit on the native 
side while data will be typeless and size of pointers, 32-bit or 64-bit 
depending on target system.

In your case I would make the java converter function private and 
introduce another converter function which has the API you prefer which 
translates between the two.

so in typesystem:
<modify-function signature="..." access="private" rename="convert_private"/>

And inject code next to the private convert function that looks 
something like this (this code is unchecked, not compiled and comes with 
no guarantee ;-)

void converter(byte str[], QNativePointer data) {
     QNativePointer charPtr = new 
QNativePointer(QNativePointer.Type.Byte, str.length);
     for (int i=0; i<str.length; ++i)
         charPtr.setByteAt(i, byte[i]);
     convert_private(charPtr, str.length, data);
}

That should give you the API you need, but as you can see, there will be 
copying from str to charPtr via a number of JNI calls which is not the 
fastest thing.

An alternative is to simply write the functions by hand, by declaring 
your converter() function native and use the JNI functions to access the 
char* of the byte[] directly, but the JVM may still decide on giving you 
a copy of the data rather than a pointer to internal memory so it may 
not be that much faster.

-
Best regards,
Gunnar



More information about the Qt-jambi-interest mailing list