Class Structure

java.lang.Object
de.linusdev.lutils.struct.abstracts.Structure
All Implemented Interfaces:
MemorySizeable, NativeParsable
Direct Known Subclasses:
BBFloatMxN, BBVector, ModTrackingStructure, PrimitiveTypeArray

public abstract class Structure extends Object implements NativeParsable
  • Field Details

    • mostParentStructure

      protected Structure mostParentStructure
    • byteBuf

      protected ByteBuffer byteBuf
    • offset

      protected int offset
    • modified

      protected volatile boolean modified
  • Constructor Details

    • Structure

      public Structure()
  • Method Details

    • generateStructCode

      @NotNull public static @NotNull String generateStructCode(@NotNull @NotNull Language language, @NotNull @NotNull Class<? extends Structure> structClass)
      Generate struct code for given structClass
      Parameters:
      language - Language to generate the struct code in
      structClass - Class of the structure to generate code for
      Returns:
      struct code
      Throws:
      IllegalArgumentException - if given class cannot generate struct code.
    • useBuffer

      protected void useBuffer(@NotNull @NotNull Structure mostParentStructure, int offset)
      Set this Structure to be a child of mostParentStructure.
      Parameters:
      mostParentStructure - most parental structure
      offset - start of this structure
    • callUseBufferOf

      protected void callUseBufferOf(@NotNull @NotNull Structure structure, @NotNull @NotNull Structure mostParentStructure, int offset)
      Call useBuffer(Structure, int) of structure
    • claimBuffer

      protected void claimBuffer(@NotNull @NotNull ByteBuffer buffer)
      Will set the ByteOrder to native order. The most parental structure will be this.
      Parameters:
      buffer - ByteBuffer to claim
    • allocate

      public void allocate()
      Creates an 8 byte aligned direct byte buffer and calls useBuffer(Structure, int).
    • getInfo

      @NotNull public abstract @NotNull StructureInfo getInfo()
    • getAlignment

      public int getAlignment()
      Description copied from interface: MemorySizeable
      The recommended alignment of this object in memory. Either 1, 2, 4, 8 or 16.
      Specified by:
      getAlignment in interface MemorySizeable
      Returns:
      recommended alignment
    • getRequiredSize

      public int getRequiredSize()
      Description copied from interface: MemorySizeable
      Required size including padding.
      Specified by:
      getRequiredSize in interface MemorySizeable
      Returns:
      the size required by this object in memory
    • getOffset

      public int getOffset()
      Offset in the byte buffer of the most parental structure.
      Returns:
      offset in bytes
    • getByteBuffer

      public ByteBuffer getByteBuffer()
      Description copied from interface: NativeParsable
      The ByteBuffer containing the native data. May not be null if NativeParsable.isInitialised() is true.
      Specified by:
      getByteBuffer in interface NativeParsable
      Returns:
      ByteBuffer
    • isInitialised

      public boolean isInitialised()
      Description copied from interface: NativeParsable
      Whether this NativeParsable is already backed by a buffer.
      Specified by:
      isInitialised in interface NativeParsable
      Returns:
      true if initialised
    • toString

      protected String toString(@NotNull @NotNull String name, @Nullable @Nullable String content)
      Parameters:
      name - name of this struct
      content - content to be inside the {content}
      Returns:
      A nice string describing this structure
    • modified

      public void modified(int offset, int size)
      Mark size bytes at offset as modified.
      Parameters:
      offset - region start
      size - region size
    • modified

      public void modified()
      Mark the whole structure as modified.
    • unmodified

      public void unmodified()
      Marks this structure has not modified. May only be called on the most parental structure.
    • isModified

      public boolean isModified()
      Will be true after a call to modified(int, int). Will be reset by unmodified(). Note that modifications are only tracked on the most parental structure. The return value of this method is undefined, if it is not the most parental structure.
      Returns:
      whether this Structure has been modified.
    • onModification

      protected void onModification(int offset, int size)
      Called on the most parental structure if on it or any of its children modified(int, int) is called.
      Parameters:
      offset - modified region start
      size - modified region size
    • getMostParentStructure

      public Structure getMostParentStructure()
      Will never be null after either claimBuffer(ByteBuffer) or useBuffer(Structure, int) has been called.
      Returns:
      the most parental structure.