Class: BinStruct::BitAttr Abstract
- Inherits:
-
Object
- Object
- BinStruct::BitAttr
- Includes:
- Structable
- Defined in:
- lib/bin_struct/bit_attr.rb
Overview
Subclasses must de derived using BitAttr.create.
Define a bitfield attribute to embed in a Struct. Use it through Struct.define_bit_attr
Defined Under Namespace
Classes: Parameters
Class Attribute Summary collapse
- .bit_methods ⇒ ::Array<Symbol> readonly
- .parameters ⇒ Parameters readonly
Instance Attribute Summary collapse
-
#width ⇒ Integer
readonly
Width in bits of bit attribute.
Class Method Summary collapse
-
.create(width:, endian: :big, **fields) ⇒ Class
Create a new BitAttr subclass with specified parameters.
Instance Method Summary collapse
- #bit_methods ⇒ ::Array<Symbol>
- #format_inspect ⇒ ::String
-
#from_human(value) ⇒ self
Set fields from associated integer.
-
#initialize(values = {}) ⇒ self
constructor
Initialize bit attribute.
-
#initialize_copy ⇒ Object
Dup internal @data hash.
-
#read(str) ⇒ self
Populate bit attribute from
str. -
#to_i ⇒ Integer
(also: #to_human)
Give integer associated to this attribute.
-
#to_s ⇒ ::String
Return binary string.
-
#type_name ⇒ ::String
Get type name.
Methods included from Structable
Constructor Details
#initialize(values = {}) ⇒ self
Initialize bit attribute
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/bin_struct/bit_attr.rb', line 131 def initialize(values = {}) parameters = self.class.parameters raise NotImplementedError, "#initialize may only be called on subclass of #{self.class}" if parameters.nil? @width = parameters.width @fields = parameters.fields @int = parameters.int.dup @data = {} parameters.fields.each_key do |name| @data[name] = values[name] || 0 end @int.value = compute_integer end |
Class Attribute Details
.bit_methods ⇒ ::Array<Symbol> (readonly)
49 50 51 |
# File 'lib/bin_struct/bit_attr.rb', line 49 def bit_methods @bit_methods end |
.parameters ⇒ Parameters (readonly)
45 46 47 |
# File 'lib/bin_struct/bit_attr.rb', line 45 def parameters @parameters end |
Instance Attribute Details
#width ⇒ Integer (readonly)
Returns width in bits of bit attribute.
34 35 36 |
# File 'lib/bin_struct/bit_attr.rb', line 34 def width @width end |
Class Method Details
.create(width:, endian: :big, **fields) ⇒ Class
Create a new BinStruct::BitAttr subclass with specified parameters
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/bin_struct/bit_attr.rb', line 60 def create(width:, endian: :big, **fields) raise ArgumentError, 'with must be 8, 16, 24, 32 or 64' unless [8, 16, 24, 32, 64].include?(width) hsh = compute_hash(width, endian, fields) cached = cache[hsh] return cached if cached total_size = fields.reduce(0) { |acc, ary| acc + ary.last } raise ArgumentError, "sum of bitfield sizes is not equal to #{width}" unless total_size == width cache[hsh] = create_subclass(width, endian, fields.dup.freeze) end |
Instance Method Details
#bit_methods ⇒ ::Array<Symbol>
152 153 154 |
# File 'lib/bin_struct/bit_attr.rb', line 152 def bit_methods self.class.bit_methods end |
#format_inspect ⇒ ::String
202 203 204 205 |
# File 'lib/bin_struct/bit_attr.rb', line 202 def format_inspect str = @int.format_inspect << "\n" str << @data.map { |name, value| "#{name}:#{value}" }.join(' ') end |
#from_human(value) ⇒ self
Set fields from associated integer
195 196 197 198 199 |
# File 'lib/bin_struct/bit_attr.rb', line 195 def from_human(value) ivalue = value.to_i @int.value = ivalue compute_data(ivalue) end |
#initialize_copy ⇒ Object
Dup internal @data hash
147 148 149 |
# File 'lib/bin_struct/bit_attr.rb', line 147 def initialize_copy(*) @data = @data.dup end |
#read(str) ⇒ self
Populate bit attribute from str
172 173 174 175 176 177 |
# File 'lib/bin_struct/bit_attr.rb', line 172 def read(str) return self if str.nil? @int.read(str) compute_data(@int.to_i) end |
#to_i ⇒ Integer Also known as: to_human
Give integer associated to this attribute
181 182 183 |
# File 'lib/bin_struct/bit_attr.rb', line 181 def to_i @int.to_i end |
#to_s ⇒ ::String
Return binary string
188 189 190 |
# File 'lib/bin_struct/bit_attr.rb', line 188 def to_s @int.to_s end |
#type_name ⇒ ::String
Get type name
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/bin_struct/bit_attr.rb', line 158 def type_name return @type_name if defined? @type_name endian_suffix = case @int.endian when :big then '' when :little then 'le' when :native then 'n' end @type_name = "BitAttr#{@width}#{endian_suffix}" end |