开发者

Java C++ Wrapper for Android: how to wrap c++ templates

I'm working on an android project (a 3d realtime application) and would like to use a c++ library I've written. Since it's relying on templates I'm looking for a good solution to write a Java wrapper around it.

One idea I had, was to include the java class name in the JNI call when I create an object. For example I instantiate a Java class like this:

//java
A a = new A(Integer.class());


//jni call
if(strcmp("java.lang.integer", className) == 0) return (jlong) new A<int>(); 
else if(strcmp("java.lang.float", className) == 0) return (jlong) new A<float>(); 
else if( .... )

The problem with this solution is, that whenev开发者_运维问答er I want to use a new data type I have to add another elseif code block and compile the c++ code again.


The problem with this solution is, that whenever I want to use a new data type I have to add another elseif code block.

Remember that there are only 8 primitive types in Java. If you add one if-else for each of those, you will be able to handle any primitive type argument.

If you also add a case for jobject you can also use that to handle any object type. Just be careful to handle your JNI object references correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜