Package jnr.ffi

Class Struct

java.lang.Object
jnr.ffi.Struct
Direct Known Subclasses:
Union

public abstract class Struct extends Object
Representation of C structures in java. Note: This class is not threadsafe.
  • Field Details

  • Constructor Details

    • Struct

      protected Struct(Runtime runtime)
      Creates a new Struct.
      Parameters:
      runtime - The current runtime.
    • Struct

      protected Struct(Runtime runtime, Struct.Alignment alignment)
    • Struct

      protected Struct(Runtime runtime, Struct enclosing)
    • Struct

      protected Struct(Runtime runtime, boolean isUnion)
      Creates a new Struct.
      Parameters:
      runtime - the current jnr-ffi runtime
      isUnion - if this Struct is a Union
  • Method Details

    • getRuntime

      public final Runtime getRuntime()
    • useMemory

      public final void useMemory(Pointer address)
      Uses the specified memory address as the backing store for this structure.
      Parameters:
      address - the native memory area.
    • getMemory

      public static Pointer getMemory(Struct struct)
    • getMemory

      public static Pointer getMemory(Struct struct, int flags)
    • size

      public static int size(Struct struct)
      Gets the size of a Struct instance in bytes
      Parameters:
      struct - the Struct instance
      Returns:
      the size of the Struct in bytes
    • size

      public static <T extends Struct> int size(Class<T> structClass, Runtime runtime)
      Gets the size of a Struct type in bytes
      Type Parameters:
      T - the Struct type
      Parameters:
      structClass - the Class of the Struct
      runtime - the Runtime that will be used to create an instance of the Struct
      Returns:
      the size of the Struct in bytes
    • size

      public static <T extends Struct> int size(Class<T> structClass)
      Gets the size of a Struct type in bytes
      Type Parameters:
      T - the Struct type
      Parameters:
      structClass - the Class of the Struct
      Returns:
      the size of the Struct in bytes
    • alignment

      public static int alignment(Struct struct)
    • isDirect

      public static boolean isDirect(Struct struct)
    • align

      private static int align(int offset, int align)
    • arrayOf

      public static <T extends Struct> T[] arrayOf(Runtime runtime, Class<T> type, int length)
    • toString

      public String toString()
      Returns a human readable String representation of the structure.
      Overrides:
      toString in class Object
      Returns:
      a String representation of this structure.
    • arrayBegin

      protected final void arrayBegin()
      Starts an array construction session
    • arrayEnd

      protected final void arrayEnd()
      Ends an array construction session
    • array

      protected <T extends Struct.Member> T[] array(T[] array)
      Creates an array of Member instances.
      Type Parameters:
      T - The type of the Member subclass to create.
      Parameters:
      array - the array to store the instances in
      Returns:
      the array that was passed in
    • array

      protected <T extends Enum<T>> Struct.Enum8<T>[] array(Struct.Enum8<T>[] array, Class<T> enumClass)
      Creates an array of Enum8 instances.
      Type Parameters:
      T - The type of the java.lang.Enum
      Parameters:
      array - the array to store the instances in
      enumClass - class of java.lang.Enum, these Enum8 instances will represent
      Returns:
      the array that was passed in
    • array

      protected <T extends Enum<T>> Struct.Enum16<T>[] array(Struct.Enum16<T>[] array, Class<T> enumClass)
      Creates an array of Enum16 instances.
      Type Parameters:
      T - The type of the java.lang.Enum
      Parameters:
      array - the array to store the instances in
      enumClass - class of java.lang.Enum, these Enum16 instances will represent
      Returns:
      the array that was passed in
    • array

      protected <T extends Enum<T>> Struct.Enum32<T>[] array(Struct.Enum32<T>[] array, Class<T> enumClass)
      Creates an array of Enum32 instances.
      Type Parameters:
      T - The type of the java.lang.Enum
      Parameters:
      array - the array to store the instances in
      enumClass - class of java.lang.Enum, these Enum32 instances will represent
      Returns:
      the array that was passed in
    • array

      protected <T extends Enum<T>> Struct.Enum64<T>[] array(Struct.Enum64<T>[] array, Class<T> enumClass)
      Creates an array of Enum64 instances.
      Type Parameters:
      T - The type of the java.lang.Enum
      Parameters:
      array - the array to store the instances in
      enumClass - class of java.lang.Enum, these Enum64 instances will represent
      Returns:
      the array that was passed in
    • array

      protected <T extends Enum<T>> Struct.Enum<T>[] array(Struct.Enum<T>[] array, Class<T> enumClass)
      Creates an array of Enum instances.
      Type Parameters:
      T - The type of the java.lang.Enum
      Parameters:
      array - the array to store the instances in
      enumClass - class of java.lang.Enum, these Enum instances will represent
      Returns:
      the array that was passed in
    • array

      protected <T extends Struct> T[] array(T[] array)
      Creates an array of Struct instances.
      Type Parameters:
      T - the type of Struct
      Parameters:
      array - the array to store the instances in
      Returns:
      the array that was passed in
    • array

      protected final Struct.Signed8[] array(Struct.Signed8[] array)
      Creates an array of Signed8 instances.
      Parameters:
      array - the array to store the instances in
      Returns:
      the array that was passed in
    • array

      protected final Struct.Unsigned8[] array(Struct.Unsigned8[] array)
      Creates an array of Unsigned8 instances.
      Parameters:
      array - the array to store the instances in
      Returns:
      the array that was passed in
    • array

      protected final Struct.Signed16[] array(Struct.Signed16[] array)
      Creates an array of Signed16 instances.
      Parameters:
      array - the array to store the instances in
      Returns:
      the array that was passed in
    • array

      protected final Struct.Unsigned16[] array(Struct.Unsigned16[] array)
      Creates an array of Unsigned16 instances.
      Parameters:
      array - the array to store the instances in
      Returns:
      the array that was passed in
    • array

      protected final Struct.Signed32[] array(Struct.Signed32[] array)
      Creates an array of Signed32 instances.
      Parameters:
      array - the array to store the instances in
      Returns:
      the array that was passed in
    • array

      protected final Struct.Unsigned32[] array(Struct.Unsigned32[] array)
      Creates an array of Unsigned32 instances.
      Parameters:
      array - the array to store the instances in
      Returns:
      the array that was passed in
    • array

      protected final Struct.Signed64[] array(Struct.Signed64[] array)
      Creates an array of Signed64 instances.
      Parameters:
      array - the array to store the instances in
      Returns:
      the array that was passed in
    • array

      protected final Struct.Unsigned64[] array(Struct.Unsigned64[] array)
      Creates an array of Unsigned64 instances.
      Parameters:
      array - the array to store the instances in
      Returns:
      the array that was passed in
    • array

      protected final Struct.SignedLong[] array(Struct.SignedLong[] array)
      Creates an array of SignedLong instances.
      Parameters:
      array - the array to store the instances in
      Returns:
      the array that was passed in
    • array

      protected final Struct.UnsignedLong[] array(Struct.UnsignedLong[] array)
      Creates an array of UnsignedLong instances.
      Parameters:
      array - the array to store the instances in
      Returns:
      the array that was passed in
    • array

      protected final Struct.Float[] array(Struct.Float[] array)
      Creates an array of Float instances.
      Parameters:
      array - the array to store the instances in
      Returns:
      the array that was passed in
    • array

      protected final Struct.Double[] array(Struct.Double[] array)
      Creates an array of Double instances.
      Parameters:
      array - the array to store the instances in
      Returns:
      the array that was passed in
    • array

      protected final Struct.Address[] array(Struct.Address[] array)
      Creates an array of Address instances.
      Parameters:
      array - the array to store the instances in
      Returns:
      the array that was passed in
    • array

      protected final Struct.Pointer[] array(Struct.Pointer[] array)
      Creates an array of Pointer instances.
      Parameters:
      array - the array to store the instances in
      Returns:
      the array that was passed in
    • array

      protected Struct.UTF8String[] array(Struct.UTF8String[] array, int stringLength)
      Creates an array of UTF8String instances.
      Parameters:
      array - the array to store the instances in
      stringLength - length of each string in array
      Returns:
      the array that was passed in
    • inner

      protected final <T extends Struct> T inner(T struct)
      Creates inner Structs in Struct definitions. Example:
      
           public class InnerStruct extends Struct {
               public final Signed32 innerInt32 = new Signed32();
               public final Double innerDouble = new Double();
      
               public InnerStruct(Runtime runtime) {super(runtime);}
           }
      
           public class MyStruct extends Struct {
               public final Signed32 myInt32 = new Signed32();
               public final InnerStruct myInnerStruct = inner(new InnerStruct());
      
               public MyStruct(Runtime runtime) {super(runtime);}
           }
      
      Type Parameters:
      T - the type of the inner Struct
      Parameters:
      struct - an instance of the inner Struct
      Returns:
      the instance of the inner Struct of type Struct
    • inner

      protected final <T extends Struct> T inner(Class<T> structClass)
      Creates inner Structs in Struct definitions. Example:
      
           public class InnerStruct extends Struct {
               public final Signed32 innerInt32 = new Signed32();
               public final Double innerDouble = new Double();
      
               public InnerStruct(Runtime runtime) {super(runtime);}
           }
      
           public class MyStruct extends Struct {
               public final Signed32 myInt32 = new Signed32();
               public final InnerStruct myInnerStruct = inner(InnerStruct.class);
      
               public MyStruct(Runtime runtime) {super(runtime);}
           }
      
      Type Parameters:
      T - the type of the inner Struct
      Parameters:
      structClass - the Class of the inner Struct
      Returns:
      the newly created inner Struct of type Struct
    • function

      protected final <T> Struct.Function<T> function(Class<T> closureClass)