Class: CAStruct::Field
- Inherits:
-
Object
- Object
- CAStruct::Field
- 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/bytesdescribe the placement. For typed primitives,typeis the Symbol (:int32etc.); for nested CAStruct subclass members,typeis the class; for CArray-template members,typeis the template CArray. - Bit member:
bitfield?returns true;bitsandbit_offsethold the bit-level placement.offsetis the byte containing the LSB of the field (for raw-record indexing),bytesis nil.
Instance Attribute Summary collapse
-
#bit_offset ⇒ Object
readonly
Returns the value of attribute bit_offset.
-
#bits ⇒ Object
readonly
Returns the value of attribute bits.
-
#bytes ⇒ Object
readonly
Returns the value of attribute bytes.
-
#endian ⇒ Object
readonly
Returns the value of attribute endian.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Field-wise comparison of the member's declaration (name, offset, type, width, bit placement, endianness).
- #bitfield? ⇒ Boolean
-
#hash ⇒ Integer
A hash consistent with #==.
-
#initialize(name:, offset:, type:, bytes: nil, bits: nil, bit_offset: nil, endian: nil) ⇒ Field
constructor
A new instance of Field.
- #inspect ⇒ String (also: #to_s)
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_offset ⇒ Object (readonly)
Returns the value of attribute bit_offset.
101 102 103 |
# File 'lib/carray/struct.rb', line 101 def bit_offset @bit_offset end |
#bits ⇒ Object (readonly)
Returns the value of attribute bits.
101 102 103 |
# File 'lib/carray/struct.rb', line 101 def bits @bits end |
#bytes ⇒ Object (readonly)
Returns the value of attribute bytes.
101 102 103 |
# File 'lib/carray/struct.rb', line 101 def bytes @bytes end |
#endian ⇒ Object (readonly)
Returns the value of attribute endian.
101 102 103 |
# File 'lib/carray/struct.rb', line 101 def endian @endian end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
101 102 103 |
# File 'lib/carray/struct.rb', line 101 def name @name end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
101 102 103 |
# File 'lib/carray/struct.rb', line 101 def offset @offset end |
#type ⇒ Object (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?.
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
115 116 117 |
# File 'lib/carray/struct.rb', line 115 def bitfield? @type == :bitfield end |
#hash ⇒ Integer
Returns 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 |
#inspect ⇒ String Also known as: to_s
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 |