简体   繁体   中英

Mapping primitive type in JNA while keeping the native typedef class

I have this type defined in my DLL:

typedef int FMOD_MEMORY_TYPE;

Can I make a class for this in Java so I will know FMOD_MEMORY_TYPE is just an int ?

public final class FMOD_MEMORY_TYPE {
    public int value;
}

As I presume it takes the same amount of memory?

This type is passed into functions a lot and it would make it a lot easier if in Java it had its own typedef almost too. If not, how can I make some of mapping to a type in Java?

Essentially it's just an int but having to replace all occurrences with int would take a lot of time and this would be easier.

Essentially I Want to be able to map the code like this: (C):

void some_function(FMOD_MEMORY_TYPE argument);

<=> Java:

public void some_function(FMOD_MEMORY_TYPE arg);

instead of:

public void some_function(int arg);

Yes, you can create a wrapper class for primitives, but not quite as simply as you describe.

A wrapper class gives you some ease in porting code as you've requested, as well as type safety. The class will take a little bit more memory on the Java side as you're allocating space for an entire object instead of simply the memory for a primitive. And there will be a tiny performance hit for storing and retrieving the internal value. However, if these trade-offs are worth it to you and aren't used in a performance-sensitive application, it's not a problem.

However, you can't just use a class as you've described in your question without adding additional functionality for JNA to determine the byte size of the object and its corresponding native equivalent. Specifically, you would have to implement the NativeMapped interface. This is most easily done by extending an existing JNA class which already does this work for you.

For an integer typedef, I would recommend that you create a type that extends IntegerType and provides you access to those methods. So in your case, I'd do:

public final class FMOD_MEMORY_TYPE extends IntegerType {

    public static final int SIZE = 4; // bytes

    public FMOD_MEMORY_TYPE() {
        this(0);
    }

    public FMOD_MEMORY_TYPE(int value) {
        super(SIZE, value);
    }
}

Doing this will will inherit setValue() from IntegerType and inherit all of the core JavaNumber class methods like intValue() .

You could also choose to implement Comparable or add other methods as needed.

There are plenty of examples in JNA's WinDef class , among others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM