Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map enum in JNA

I have the following enum how do i map in jna ??

This enum is further referenced in structure.

typedef enum
{
 eFtUsbDeviceNotShared,
 eFtUsbDeviceSharedActive,
 eFtUsbDeviceSharedNotActive,
 eFtUsbDeviceSharedNotPlugged,
 eFtUsbDeviceSharedProblem
} eFtUsbDeviceStatus;

Abdul Khaliq

like image 906
Abdul Khaliq Avatar asked Jul 21 '09 07:07

Abdul Khaliq


People also ask

What is an enum map in Java?

The EnumMap class of the Java collections framework provides a map implementation for elements of an enum. In EnumMap , enum elements are used as keys. It implements the Map interface.

Can enum have NULL values Java?

Java allows any reference to be null, and references to enums aren't so special that a special case needs to be made to prevent it, or to provide another version of behaviour that is already well-specified and well-understood.


1 Answers

If you're using JNA you probably want to explicitly specify the values of the enumeration in Java. By default, Java's basic enum type doesn't really give you that functionality, you have to add a constructor for an EnumSet (see this and this).

A simple way to encode C enumerations is to use public static final const ints wrapped in a class with the same name as the enum. You get most of the functionality you'd get from a Java enum but slightly less overhead to assign values.

Some good JNA examples, including the snippets below (which were copied) are available here.

Suppose your C code looks like:

enum Values {
     First,
     Second,
     Last
};

Then the Java looks like:

public static interface Values {
    public static final int First = 0;
    public static final int Second = 1;
    public static final int Last = 2;
}
like image 137
Mark Elliot Avatar answered Oct 28 '22 15:10

Mark Elliot