elect
elect

Reputation: 7190

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.

In cpp, there is the IVROverlay class with this virtual SetOverlayIntersectionMask function:

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:

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:

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

Now, the enum translates to an Int but the latter? Since it should be a pointer, I guess it shall be Pointer.SIZE?

However this is my implementation, where VROverlayIntersectionMaskPrimitive_Data_t is an abstract class:

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

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

Does my reasoning appear correct?

Upvotes: 2

Views: 1609

Answers (1)

technomage
technomage

Reputation: 10069

You obtain the size of a Structure (including Union) with the size() method. 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().

Upvotes: 3

Related Questions