@Retention(value=RUNTIME) @Target(value=TYPE) public @interface CStruct
PointerBase
,
i.e., it is a word type. There is never a Java class that implements the interface.
Field accesses are done via interface methods that are annotated with CField
,
CFieldAddress
, or CFieldOffset
. All calls of the interface methods are replaced
with the appropriate memory or address arithmetic operations. Here is an example to define a
complex number structure:
@The annotated interface, or an outer class that contains the interface, must be annotated withCStruct
interface ComplexValue extendsPointerBase
{ @CField
("re") double realPart(); @CField
("re") void realPart(double re); @CField
("im") double imagineryPart(); @CField
("im") void imagineryPart(double im); }
CContext
. Allocate an instances of the struct
either by
StackValue.get(java.lang.Class)
or by
UnmanagedMemory.malloc(org.graalvm.word.UnsignedWord)
.
To access an array of structs one can define a special addressOf
method:
@Implementation of such method then allows one to do array arithmetics - e.g. obtain pointer to the first element of the array and then access the others:CStruct
("int_double") interface IntOrDouble extendsPointerBase
{ // allows access to individual structs in an array IntOrDouble addressOf(int index); @CField
int i(); @CField
void i(int i); @CField
double d(); @CField
void d(double d); }
private static double acceptIntIntDouble(IntOrDouble arr) { IntOrDouble firstInt = arr.addressOf(0); IntOrDouble secondInt = arr.addressOf(1); IntOrDouble thirdDouble = arr.addressOf(2); return firstInt.i() + secondInt.i() + thirdDouble.d(); }
StackValue
,
UnmanagedMemory
Modifier and Type | Optional Element and Description |
---|---|
boolean |
addStructKeyword
Add the C "struct" keyword to the name specified in
CStruct.value() . |
boolean |
isIncomplete
If marked as incomplete, we will not try to determine the size of the struct.
|
String |
value
Specifies the name of the imported C struct type.
|
public abstract String value
public abstract boolean isIncomplete
public abstract boolean addStructKeyword
CStruct.value()
.