如何在JNA中映射枚举 [英] How to map enum in JNA

查看:3054
本文介绍了如何在JNA中映射枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下枚举我如何映射到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

Abdul Khaliq

推荐答案

如果您使用JNA,您可能希望显式指定Java中枚举的值。默认情况下,Java的基本枚举类型并不真正给你这个功能,你必须添加EnumSet的构造函数(参见这个这个)。

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).

编码C枚举的一种简单方法是使用与枚举名称相同的类中包含的public static final const int。您可以从Java枚举中获得大部分功能,但稍微减少分配值的开销。

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.

一些很好的JNA示例,包括下面的代码片段)可在此处获得。

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

假设你的C代码如下:

enum Values {
     First,
     Second,
     Last
};

然后Java看起来像:

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;
}

这篇关于如何在JNA中映射枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆