Class CompositeDataTypeImpl

All Implemented Interfaces:
Composite, CompositeInternal, DataType
Direct Known Subclasses:
StructureDataType, UnionDataType

public abstract class CompositeDataTypeImpl extends GenericDataType implements CompositeInternal
Common implementation methods for structure and union
  • Field Details

    • minimumAlignment

      protected int minimumAlignment
    • packing

      protected int packing
  • Method Details

    • getAlignedLength

      public final int getAlignedLength()
      Description copied from interface: DataType
      Get the aligned-length of this datatype as a number of 8-bit bytes.

      For primitive datatypes this is equivalent to the C/C++ "sizeof" operation within source code and should be used when determining Array element length or component sizing for a Composite. For Pointer, Composite and Array types this will return the same value as DataType.getLength().

      Example: For x86 32-bit gcc an 80-bit long double raw data length of 10-bytes will fit within a floating point register while its aligned-length of 12-bytes is used by the gcc compiler for data/array/component allocations to maintain alignment (i.e., sizeof(long double) ).

      NOTE: Other than the VoidDataType, no datatype should ever return 0, even if DataType.isZeroLength(), and only Dynamic / FactoryDataType / FunctionDefinition datatypes should return -1. If DataType.isZeroLength() is true a length of 1 should be returned.

      Specified by:
      getAlignedLength in interface DataType
      Overrides:
      getAlignedLength in class DataTypeImpl
      Returns:
      byte length of binary encoding.
    • getStoredPackingValue

      public int getStoredPackingValue()
      Description copied from interface: CompositeInternal
      Gets the current packing value (typically a power of 2). Other special values which may be returned include 0 and -1.
      Specified by:
      getStoredPackingValue in interface CompositeInternal
      Returns:
      the current positive packing value, 0 or -1.
    • getStoredMinimumAlignment

      public int getStoredMinimumAlignment()
      Description copied from interface: CompositeInternal
      Get the minimum alignment setting for this Composite which contributes to the actual computed alignment value (see Composite.getAlignment().
      Specified by:
      getStoredMinimumAlignment in interface CompositeInternal
      Returns:
      the minimum alignment setting for this Composite or a reserved value to indicate either CompositeInternal.DEFAULT_ALIGNMENT or CompositeInternal.MACHINE_ALIGNMENT.
    • dataTypeNameChanged

      public void dataTypeNameChanged(DataType dt, String oldName)
      Description copied from interface: DataType
      Informs this datatype that its name has changed from the indicated old name.

      TODO: This method is reserved for internal DB use.

      Specified by:
      dataTypeNameChanged in interface DataType
      Overrides:
      dataTypeNameChanged in class AbstractDataType
      Parameters:
      dt - the datatype whose name changed
      oldName - the datatype's old name
    • getPreferredComponentLength

      protected int getPreferredComponentLength(DataType dataType, int length, int maxLength)
      Get the preferred length for a new component. If type is dynamic length must be specified (assuming Dynamic.canSpecifyLength() is true). Otherwise, when packing is enabled the DataType.getAlignedLength() is returned; when packing disabled for Union use of fixed-length type size is forced. Otherwise the decision is deferred to DataTypeComponentImpl.getPreferredComponentLength(DataType, int). During packing the actual component length may be changed.
      Parameters:
      dataType - new component datatype
      length - constrained length or -1 to force use of dataType size for non-packing. Dynamic types such as string must have a positive length specified. This value is ignored for fixed-length types if maxLength has been specified.
      maxLength - applies to non-packed structures only to indicate available space for fixed-length types using DataType.getLength(). Specify -1 to ignore this value.
      Returns:
      preferred component length
      Throws:
      IllegalArgumentException - if length not specified for a Dynamic dataType.
    • getPreferredComponentLength

      protected int getPreferredComponentLength(DataType dataType, int length)
      Get the preferred length for a new component. If type is dynamic length must be specified (assuming Dynamic.canSpecifyLength() is true). Otherwise, when packing is enabled the DataType.getAlignedLength() is returned; when packing disabled for Union use of fixed-length type size is forced. Otherwise the decision is deferred to DataTypeComponentImpl.getPreferredComponentLength(DataType, int). During packing the actual component length may be changed.
      Parameters:
      dataType - new component datatype
      length - constrained length or -1 to force use of dataType size for non-packing. Dynamic types such as string must have a positive length specified.
      Returns:
      preferred component length
      Throws:
      IllegalArgumentException - if length not specified for a Dynamic dataType.
    • hasLanguageDependantLength

      public abstract boolean hasLanguageDependantLength()
      Description copied from interface: DataType
      Indicates if the length of this data-type is determined based upon the DataOrganization obtained from the associated DataTypeManager.
      Specified by:
      hasLanguageDependantLength in interface DataType
      Overrides:
      hasLanguageDependantLength in class AbstractDataType
      Returns:
      true length is language/compiler-specification dependent, else false
    • isNotYetDefined

      public final boolean isNotYetDefined()
      Determine if this composite should be treated as undefined.

      A composite is considered undefined with a zero-length when it has no components and packing is disabled. A DataTypeComponent defined by an an datatype which is not-yet-defined (i.e., DataType.isNotYetDefined() is true) will always have a size of 1. If an empty composite should be treated as fully specified, packing on the composite should be enabled to ensure that a zero-length component is used should the occassion arise (e.g., empty structure placed within union as a component).

      Specified by:
      isNotYetDefined in interface DataType
      Overrides:
      isNotYetDefined in class AbstractDataType
      Returns:
      true if this type is not yet defined.
    • isPartOf

      public boolean isPartOf(DataType dataTypeOfInterest)
      Description copied from interface: Composite
      Check if a data type is part of this data type. A data type could be part of another by:
      Being the same data type.
      containing the data type directly
      containing another data type that has the data type as a part of it.
      Specified by:
      isPartOf in interface Composite
      Parameters:
      dataTypeOfInterest - the data type to look for.
      Returns:
      true if the indicated data type is part of a sub-component of this data type.
    • checkAncestry

      protected void checkAncestry(DataType dataType) throws IllegalArgumentException
      This method throws an exception if the indicated data type is an ancestor of this data type (i.e., the specified data type has a component or sub-component containing this data type).
      Parameters:
      dataType - the data type
      Throws:
      IllegalArgumentException - if the data type is an ancestor of this data type.
    • validateDataType

      protected DataType validateDataType(DataType dataType)
      This method throws an exception if the indicated data type is not a valid data type for a component of this composite data type. If the DEFAULT datatype is specified when unsupported an Undefined1 will be returned in its place (e.g., packing enabled, Union).
      Parameters:
      dataType - the data type to be checked.
      Returns:
      datatype to be used for insert/add
      Throws:
      IllegalArgumentException - if the data type is invalid.
    • updateBitFieldDataType

      protected boolean updateBitFieldDataType(DataTypeComponentImpl bitfieldComponent, DataType oldDt, DataType newDt) throws InvalidDataTypeException
      Handle replacement of datatype which may impact bitfield datatype.
      Parameters:
      bitfieldComponent - bitfield component
      oldDt - affected datatype which has been removed or replaced
      newDt - replacement datatype
      Returns:
      true if bitfield component was modified
      Throws:
      InvalidDataTypeException - if new datatype is not
    • setDescription

      public void setDescription(String desc)
      Description copied from class: DataTypeImpl
      Sets a String briefly describing this DataType.
      If a data type that extends this class wants to allow the description to be changed, then it must override this method.
      Specified by:
      setDescription in interface Composite
      Specified by:
      setDescription in interface DataType
      Overrides:
      setDescription in class DataTypeImpl
      Parameters:
      desc - a one-liner describing this DataType.
    • getDescription

      public String getDescription()
      Description copied from interface: DataType
      Get a String briefly describing this DataType.
      Specified by:
      getDescription in interface DataType
      Returns:
      a one-liner describing this DataType.
    • getValue

      public Object getValue(MemBuffer buf, Settings settings, int length)
      Description copied from interface: DataType
      Returns the interpreted data value as an instance of the advertised value class.

      For instance, Pointer data types should return an Address object (or null), or integer data types should return a Scalar object.

      Specified by:
      getValue in interface DataType
      Parameters:
      buf - the data buffer
      settings - the settings to use.
      length - indicates the maximum number of bytes that may be consumed by a Dynamic datatype, otherwise this value is ignored. A value of -1 may be specified to allow a Dynamic datatype to determine the length based upon the actual data bytes
      Returns:
      the data object, or null if data is invalid
    • setValue

      public void setValue(MemBuffer buf, Settings settings, int length, Object value)
    • add

      public final DataTypeComponent add(DataType dataType)
      Description copied from interface: Composite
      Adds a new datatype to the end of this composite. This is the preferred method to use for adding components to an aligned structure for fixed-length dataTypes.
      Specified by:
      add in interface Composite
      Parameters:
      dataType - the datatype to add.
      Returns:
      the DataTypeComponent created.
    • add

      public final DataTypeComponent add(DataType dataType, int length)
      Description copied from interface: Composite
      Adds a new datatype to the end of this composite. This is the preferred method to use for adding components to an aligned structure for dynamic dataTypes such as strings whose length must be specified.
      Specified by:
      add in interface Composite
      Parameters:
      dataType - the datatype to add.
      length - the length to associate with the datatype. For fixed length types a length <= 0 will use the length of the resolved dataType.
      Returns:
      the componentDataType created.
    • add

      public final DataTypeComponent add(DataType dataType, String fieldName, String comment)
      Description copied from interface: Composite
      Adds a new datatype to the end of this composite. This is the preferred method to use for adding components to an aligned structure for fixed-length dataTypes.
      Specified by:
      add in interface Composite
      Parameters:
      dataType - the datatype to add.
      fieldName - the field name to associate with this component.
      comment - the comment to associate with this component.
      Returns:
      the componentDataType created.
    • insert

      public final DataTypeComponent insert(int ordinal, DataType dataType, int length)
      Description copied from interface: Composite
      Inserts a new datatype at the specified ordinal position in this composite.
      Note: For an aligned structure the ordinal position will get adjusted automatically to provide the proper alignment.
      Specified by:
      insert in interface Composite
      Parameters:
      ordinal - the ordinal where the new datatype is to be inserted (numbering starts at 0).
      dataType - the datatype to insert.
      length - the length to associate with the datatype. For fixed length types a length <= 0 will use the length of the resolved dataType.
      Returns:
      the componentDataType created.
    • insert

      public final DataTypeComponent insert(int ordinal, DataType dataType)
      Description copied from interface: Composite
      Inserts a new datatype at the specified ordinal position in this composite.
      Note: For an aligned structure the ordinal position will get adjusted automatically to provide the proper alignment.
      Specified by:
      insert in interface Composite
      Parameters:
      ordinal - the ordinal where the new datatype is to be inserted (numbering starts at 0).
      dataType - the datatype to insert.
      Returns:
      the componentDataType created.
    • getMnemonic

      public String getMnemonic(Settings settings)
      Description copied from interface: DataType
      Get the mnemonic for this DataType.
      Specified by:
      getMnemonic in interface DataType
      Overrides:
      getMnemonic in class AbstractDataType
      Parameters:
      settings - settings which may influence the result or null
      Returns:
      the mnemonic for this DataType.
    • setName

      public void setName(String name) throws InvalidNameException
      Description copied from interface: DataType
      Sets the name of the datatype
      Specified by:
      setName in interface DataType
      Overrides:
      setName in class GenericDataType
      Parameters:
      name - the new name for this datatype.
      Throws:
      InvalidNameException - if the given name does not form a valid name.
    • repack

      public final void repack()
      Description copied from interface: Composite
      Updates packed composite to any changes in the data organization. If the composite does not have packing enabled this method does nothing.
      NOTE: Changes to data organization is discouraged. Attempts to use this method in such cases should be performed on all composites in dependency order (ignoring pointer components).
      Specified by:
      repack in interface Composite
    • repack

      public abstract boolean repack(boolean notify)
      Repack components within this composite based on the current packing, alignment and DataOrganization settings. Non-packed Structures: change detection is limited to component count and length is assumed to already be correct.

      NOTE: If modifications to stored length are made prior to invoking this method, detection of a size change may not be possible.

      NOTE: Currently a change in calculated alignment can not be provided since this value is not stored.

      Parameters:
      notify - if true notification will be sent to parents if a size change or component placement change is detected.
      Returns:
      true if a layout change was detected.
    • setPackingEnabled

      public void setPackingEnabled(boolean enabled)
      Description copied from interface: Composite
      Sets whether this data type's internal components are currently packed. The affect of disabled packing differs between Structure and Union. When packing disabled:
      • Structures utilize explicit component offsets and produce undefined filler components where defined components do not consume space.
      • Unions always place components at offset 0 and do not pad for alignment.
      In addition, when packing is disabled the default alignment is always 1 unless a different minimum alignment has been set. When packing is enabled the overall composite length influenced by the composite's minimum alignment setting. If a change in enablement occurs, the default alignment and packing behavior will be used.
      Specified by:
      setPackingEnabled in interface Composite
      Parameters:
      enabled - true enables packing of components respecting component alignment and pack setting, whereas false disables packing.
    • getPackingType

      public PackingType getPackingType()
      Specified by:
      getPackingType in interface Composite
      Returns:
      the packing type set for this composite
    • setToDefaultPacking

      public void setToDefaultPacking()
      Description copied from interface: Composite
      Enables default packing behavior. If packing was previously disabled, packing will be enabled. Composite will automatically pack based upon the alignment requirements of its components with overall composite length possibly influenced by the composite's minimum alignment setting.
      Specified by:
      setToDefaultPacking in interface Composite
    • getExplicitPackingValue

      public int getExplicitPackingValue()
      Description copied from interface: Composite
      Gets the current packing value (typically a power of 2). If this isn't a packed composite with an explicit packing value (see Composite.hasExplicitPackingValue()) then the return value is undefined.
      Specified by:
      getExplicitPackingValue in interface Composite
      Returns:
      the current packing value or an undefined non-positive value
    • setExplicitPackingValue

      public void setExplicitPackingValue(int packingValue)
      Description copied from interface: Composite
      Sets the pack value for this composite (positive value, usually a power of 2). If packing was previously disabled, packing will be enabled. This value will establish the maximum effective alignment for this composite and each of the components during the alignment computation (e.g., a value of 1 will eliminate any padding). The overall composite length may be influenced by the composite's minimum alignment setting.
      Specified by:
      setExplicitPackingValue in interface Composite
      Parameters:
      packingValue - the new positive packing value.
    • getAlignmentType

      public AlignmentType getAlignmentType()
      Specified by:
      getAlignmentType in interface Composite
      Returns:
      the alignment type set for this composite
    • setToDefaultAligned

      public void setToDefaultAligned()
      Description copied from interface: Composite
      Sets this data type's alignment to its default alignment. For packed composites, this data type's alignment will be based upon the components it contains and its current pack settings. This is the default state and only needs to be used when changing from a non-default alignment type.
      Specified by:
      setToDefaultAligned in interface Composite
    • setToMachineAligned

      public void setToMachineAligned()
      Description copied from interface: Composite
      Sets this data type's minimum alignment to the machine alignment which is specified by DataOrganization.getMachineAlignment(). The machine alignment is defined as the maximum useful alignment for the target machine.
      Specified by:
      setToMachineAligned in interface Composite
    • getExplicitMinimumAlignment

      public int getExplicitMinimumAlignment()
      Description copied from interface: Composite
      Get the explicit minimum alignment setting for this Composite which contributes to the actual computed alignment value (see Composite.getAlignment().
      Specified by:
      getExplicitMinimumAlignment in interface Composite
      Returns:
      the minimum alignment setting for this Composite or an undefined non-positive value if an explicit minimum alignment has not been set.
    • setExplicitMinimumAlignment

      public void setExplicitMinimumAlignment(int minimumAlignment)
      Description copied from interface: Composite
      Sets this data type's explicit minimum alignment (positive value). Together with the pack setting and component alignments will affect the actual computed alignment of this composite. When packing is enabled, the alignment setting may also affect padding at the end of the composite and its length. When packing is disabled, this setting will not affect the length of this composite.
      Specified by:
      setExplicitMinimumAlignment in interface Composite
      Parameters:
      minimumAlignment - the minimum alignment for this Composite.
    • getNonPackedAlignment

      protected final int getNonPackedAlignment()
    • getAlignment

      public abstract int getAlignment()
      Description copied from interface: DataType
      Gets the alignment to be used when aligning this datatype within another datatype.
      Specified by:
      getAlignment in interface Composite
      Specified by:
      getAlignment in interface DataType
      Overrides:
      getAlignment in class DataTypeImpl
      Returns:
      this datatype's alignment.
    • toString

      public String toString()
      Overrides:
      toString in class AbstractDataType