C ++ / JNI - How to access an enum declared in .h in JAVA (Android NDK)

I have a C ++ header file that declares some enum.

// enum.h
enum event {
    event_1,
    event_2,
    event_3,
    event_4
}

      

C ++ files and header are compiled (with GNU) in the .so library.

Now I want to do in my Java class:

// Test.java
public class Test {
    private event currentEvent;

    public test() {
        if (currentEvent == event.event_1)
            ; // I will do my stuff
    }
}

      

Anyone have any suggestions?

thank

+3


source to share


1 answer


I doubt there is a direct way to do this.

For C or C ++, you can use SWIG . It automatically creates Java wrappers for C ++ classes and elements.



21.3.5.5 Simple enumerations

This approach is similar to the unsafe type approach. Each element of the enumeration is also wrapped as a static final integer. However, these integers are not generated in a class named after the C / C ++ enumeration. Instead, global enums are generated into a constant interface. Also enumerated enums in a C ++ class have enumeration members generated directly in the Java proxy class, not an inner class in the Java proxy class. In fact, this approach effectively wraps enums as if they were anonymous enums and the resulting code is consistent with anonymous enums. the implementation is in the file "enumsimple.swg".

Compatibility note: SWIG-1.3.21 and earlier wrapped all enums using this approach. An unsafe approach like this is preferred over this one and this simple approach is only included in reverse compatibility with these earlier versions of SWIG.

+1


source







All Articles