简体   繁体   English

如何在jna中实现具有联合类型的结构sizeof()

[英]How to implement in jna a structure sizeof() with an union type

I updated the jvm openvr binding to the last openvr version, 1.0.5, but I am unsure about one thing. 我将jvm openvr绑定更新为最新的openvr版本1.0.5,但是我不确定一件事。

In cpp, there is the IVROverlay class with this virtual SetOverlayIntersectionMask function: 在cpp中,存在具有此虚拟SetOverlayIntersectionMask函数的IVROverlay类:

virtual EVROverlayError SetOverlayIntersectionMask(
             VROverlayHandle_t ulOverlayHandle, 
             VROverlayIntersectionMaskPrimitive_t *pMaskPrimitives, 
             uint32_t unNumMaskPrimitives, 
             uint32_t unPrimitiveSize = sizeof( VROverlayIntersectionMaskPrimitive_t ) ) = 0;

My doubts regards the last argument. 我的疑问是关于最后一个论点的。

VROverlayIntersectionMaskPrimitive_t : VROverlayIntersectionMaskPrimitive_t

struct VROverlayIntersectionMaskPrimitive_t
{
    EVROverlayIntersectionMaskPrimitiveType m_nPrimitiveType;
    VROverlayIntersectionMaskPrimitive_Data_t m_Primitive;
};

is a struct with a enum type and an union type variable, called VROverlayIntersectionMaskPrimitive_Data_t : 是具有枚举类型和联合类型变量的结构,称为VROverlayIntersectionMaskPrimitive_Data_t

typedef union
{
    IntersectionMaskRectangle_t m_Rectangle;
    IntersectionMaskCircle_t m_Circle;
} VROverlayIntersectionMaskPrimitive_Data_t;

Which is being implemented by the two classes right above, IntersectionMaskRectangle_t and IntersectionMaskCircle_t 由上面的两个类IntersectionMaskRectangle_tIntersectionMaskCircle_t

Now, the enum translates to an Int but the latter? 现在,枚举可转换为Int但后者为Int Since it should be a pointer, I guess it shall be Pointer.SIZE ? 由于它应该是一个指针,所以我猜它应该是Pointer.SIZE

However this is my implementation, where VROverlayIntersectionMaskPrimitive_Data_t is an abstract class: 但这是我的实现,其中VROverlayIntersectionMaskPrimitive_Data_t是一个抽象类:

abstract class VROverlayIntersectionMaskPrimitive_Data_t : Structure {

    constructor() : super()
    constructor(peer: Pointer) : super(peer)
}

Implemented in turn by the two other classes . 另外两个类依次实现。

My first guess is that sizeof(VROverlayIntersectionMaskPrimitive_Data_t) translates to Int + Pointer.SIZE 我的第一个猜测sizeof(VROverlayIntersectionMaskPrimitive_Data_t)转换为Int + Pointer.SIZE

@JvmOverloads fun setOverlayIntersectionMask(
                ulOverlayHandle: VROverlayHandle_t,
                pMaskPrimitives: VROverlayIntersectionMaskPrimitive_t.ByReference, 
                unNumMaskPrimitives: Int, 
                unPrimitiveSize: Int = Int.BYTES + Pointer.SIZE)

Does my reasoning appear correct? 我的推理是否正确?

You obtain the size of a Structure (including Union ) with the size() method. 您可以通过size()方法获得一个Structure的大小(包括Union )。 In the case of unions, you'll get the size of the largest member. 对于工会,您将获得最大成员的人数。

You set the desired type of the union with Union.setType() . 您可以使用Union.setType()设置所需的联合类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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