Class: CAStruct::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/carray/struct.rb

Overview

Reflection record for a single struct member. Frozen by default so callers can treat it as a value object. Lifted from MEMBER_TABLE's [offset, type, opts] tuple to give first-class access to member metadata without exposing the opaque hash shape.

Two flavours:

  • Regular byte-typed member: name / offset / type / bytes describe the placement. For typed primitives, type is the Symbol (:int32 etc.); for nested CAStruct subclass members, type is the class; for CArray-template members, type is the template CArray.
  • Bit member: bitfield? returns true; bits and bit_offset hold the bit-level placement. offset is the byte containing the LSB of the field (for raw-record indexing), bytes is nil.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, offset:, type:, bytes: nil, bits: nil, bit_offset: nil, endian: nil) ⇒ Field

Returns a new instance of Field.



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/carray/struct.rb', line 103

def initialize (name:, offset:, type:, bytes: nil,
                bits: nil, bit_offset: nil, endian: nil)
  @name       = name
  @offset     = offset
  @type       = type
  @bytes      = bytes
  @bits       = bits
  @bit_offset = bit_offset
  @endian     = endian
  freeze
end

Instance Attribute Details

#bit_offsetObject (readonly)

Returns the value of attribute bit_offset.



101
102
103
# File 'lib/carray/struct.rb', line 101

def bit_offset
  @bit_offset
end

#bitsObject (readonly)

Returns the value of attribute bits.



101
102
103
# File 'lib/carray/struct.rb', line 101

def bits
  @bits
end

#bytesObject (readonly)

Returns the value of attribute bytes.



101
102
103
# File 'lib/carray/struct.rb', line 101

def bytes
  @bytes
end

#endianObject (readonly)

Returns the value of attribute endian.



101
102
103
# File 'lib/carray/struct.rb', line 101

def endian
  @endian
end

#nameObject (readonly)

Returns the value of attribute name.



101
102
103
# File 'lib/carray/struct.rb', line 101

def name
  @name
end

#offsetObject (readonly)

Returns the value of attribute offset.



101
102
103
# File 'lib/carray/struct.rb', line 101

def offset
  @offset
end

#typeObject (readonly)

Returns the value of attribute type.



101
102
103
# File 'lib/carray/struct.rb', line 101

def type
  @type
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Field-wise comparison of the member's declaration (name, offset, type, width, bit placement, endianness). Aliased as eql?.

Returns:

  • (Boolean)


137
138
139
140
141
142
143
144
145
146
# File 'lib/carray/struct.rb', line 137

def == (other)
  other.is_a?(Field)        &&
    @name       == other.name &&
    @offset     == other.offset &&
    @type       == other.type &&
    @bytes      == other.bytes &&
    @bits       == other.bits &&
    @bit_offset == other.bit_offset &&
    @endian     == other.endian
end

#bitfield?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/carray/struct.rb', line 115

def bitfield?
  @type == :bitfield
end

#hashInteger

Returns a hash consistent with #==.

Returns:

  • (Integer)

    a hash consistent with #==.



151
152
153
# File 'lib/carray/struct.rb', line 151

def hash
  [@name, @offset, @type, @bytes, @bits, @bit_offset, @endian].hash
end

#inspectString Also known as: to_s

Returns:

  • (String)


120
121
122
123
124
125
126
127
128
129
130
# File 'lib/carray/struct.rb', line 120

def inspect
  if bitfield?
    format("#<%s %p bitfield bits=%d bit_offset=%d>",
           self.class, @name, @bits, @bit_offset)
  else
    endian_part = @endian ? format(" endian=%p", @endian) : ""
    format("#<%s %p type=%p offset=%d bytes=%s%s>",
           self.class, @name, @type, @offset, @bytes.inspect,
           endian_part)
  end
end