JNA:如何定义具有自定义位大小字段的结构?

时间:2016-07-15 03:58:19

标签: java struct structure jna bits

例如,我有以下C ++结构......

struct dleaf_t
{
    int                 contents;           // OR of all brushes (not needed?)
    short               cluster;            // cluster this leaf is in
    short               area : 9;           // area this leaf is in
    short               flags : 7;          // flags
    short               mins[ 3 ];          // for frustum culling
    short               maxs[ 3 ];
    unsigned short      firstleafface;      // index into leaffaces
    unsigned short      numleaffaces;
    unsigned short      firstleafbrush;     // index into leafbrushes
    unsigned short      numleafbrushes;
    short               leafWaterDataID;    // -1 for not in water

    //!!! NOTE: for maps of version 19 or lower uncomment this block
    /*
    CompressedLightCube ambientLighting;    // Precaculated light info for entities.
    short           padding;        // padding to 4-byte boundary
    */
};

通常我可以使用JNA在Java中轻松地表示结构,但是这种结构使用自定义位数量的数据类型(如果我没有弄错的话)。

例如,area 9 位的short,而不是像往常一样 16 位。 flags缺少 7 位......等等。

如何使用自定义位大小的数据类型定义JNA结构?

2 个答案:

答案 0 :(得分:2)

将两个位字段合并为一个int字段,然后编写成员方法以提取已合并的各个字段的值,例如

public int area_flags;
public int getArea() { return area_flags & 0x1FF; }
public int getFlags() { return (area_flags >> 9) & 0x3F; }

您可能需要稍微改变一下,具体取决于编译器如何打包这些位。

答案 1 :(得分:1)

我不知道JNA是否可行。请查看文档,看看是否能找到有关您问题的信息。