Class: LibBin::Structure::Bitfield Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/libbin/data_types.rb

Overview

This class is abstract.

A parent class that represent a bitfield that is

loading integers as an instance of itself. User should inherit this base class.

Scalars collapse

Scalars collapse

Class Attribute Details

.map ⇒ Object

Bitfield's field names and number of bits



505
506
507
# File 'lib/libbin/data_types.rb', line 505

def map
  @map
end

.signed ⇒ Object (readonly)

Signedness of the underlying type



507
508
509
# File 'lib/libbin/data_types.rb', line 507

def signed
  @signed
end

.type ⇒ Object

Underlying type



509
510
511
# File 'lib/libbin/data_types.rb', line 509

def type
  @type
end

Instance Attribute Details

#__remainder ⇒ Object

Unused bits of underlying value



548
549
550
# File 'lib/libbin/data_types.rb', line 548

def __remainder
  @__remainder
end

Class Method Details

.align ⇒ Object

Returns the underlying type align property



601
602
603
# File 'lib/libbin/data_types.rb', line 601

def self.align
  @type.align
end

.always_align ⇒ Object

Returns the underlying type always_align property



596
597
598
# File 'lib/libbin/data_types.rb', line 596

def self.always_align
  @type.always_align
end

.convert(input, output, input_big = LibBin::default_big?, output_big = !input_big,, parent = nil, index = nil, length = nil) ⇒ Bitfield+

Convert a LibBin::Structure::Bitfield by loading it from input and dumping it to output. Returns the loaded field.

Parameters:

  • input (IO) —

    the stream to load the field from.

  • output (IO) —

    the stream to dump the field to.

  • input_big (Boolean) (defaults to: LibBin::default_big?) —

    the endianness of input

  • output_big (Boolean) (defaults to: !input_big,) —

    the endianness of output.

  • parent (Structure) (defaults to: nil) —

    ignored.

  • index (Integer) (defaults to: nil) —

    ignored.

  • length (Integer) (defaults to: nil) —

    if given the length of the vector. Else the length is considered to be 1.

Returns:

  • (Bitfield, Array<Bitfield>) —

    an instance of self, or an Array if length was specified



651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'lib/libbin/data_types.rb', line 651

def self.convert(input, output, input_big = LibBin::default_big?, output_big = !input_big, parent = nil, index = nil, length = nil)
  v = @type.convert(input, output, input_big, output_big, parent, index, length)
  if length
    v.collect { |val|
       bf = self.new
       bf.__set_value(val)
       bf
    }
  else
    bf = self.new
    bf.__set_value(v)
    bf
  end
end

.dump(value, output, output_big = LibBin::default_big?, parent = nil, index = nil, length = nil) ⇒ nil

Dump a LibBin::Structure::Bitfield to output.

Parameters:

  • value (Bitfield, Array<Bitfield>) —

    the Ruby representation of the type, or the Array representation of the vector if length is specified.

  • output (IO) —

    the stream to dump the field to.

  • output_big (Boolean) (defaults to: LibBin::default_big?) —

    the endianness of output.

  • parent (Structure) (defaults to: nil) —

    ignored.

  • index (Integer) (defaults to: nil) —

    ignored.

  • length (Integer) (defaults to: nil) —

    if given the length of the vector. Else the length is considered to be 1.

Returns:

  • (nil)


677
678
679
680
681
682
683
684
685
686
687
# File 'lib/libbin/data_types.rb', line 677

def self.dump(value, output, output_big = LibBin::default_big?, parent = nil, index = nil, length = nil)
  v =
    if length
      length.times.collect { |i|
        value[i].__get_value
      }
    else
      value.__get_value
    end
  @type.dump(v, output, output_big, parent, index, length)
end

.load(input, input_big = LibBin::default_big?, parent = nil, index = nil, length = nil) ⇒ Bitfield+

Load a LibBin::Structure::Bitfield from input, and return it.

Parameters:

  • input (IO) —

    the stream to load the field from.

  • input_big (Boolean) (defaults to: LibBin::default_big?) —

    the endianness of input

  • parent (Structure) (defaults to: nil) —

    ignored.

  • index (Integer) (defaults to: nil) —

    ignored.

  • length (Integer) (defaults to: nil) —

    if given the length of the vector. Else the length is considered to be 1.

Returns:

  • (Bitfield, Array<Bitfield>) —

    an instance of self, or an Array if length was specified



624
625
626
627
628
629
630
631
632
633
634
635
636
637
# File 'lib/libbin/data_types.rb', line 624

def self.load(input, input_big = LibBin::default_big?, parent = nil, index = nil, length = nil)
  v = @type.load(input, input_big, parent, index, length)
  if length
    v.collect { |val|
       bf = self.new
       bf.__set_value(val)
       bf
    }
  else
    bf = self.new
    bf.__set_value(v)
    bf
  end
end

.set_type_size(sz, signed = false) ⇒ Object

Set the size and signedness of the underlying type



512
513
514
515
516
517
518
519
# File 'lib/libbin/data_types.rb', line 512

def set_type_size(sz, signed = false)
  tname = "#{signed ? "" : "U"}Int#{sz}"
  t = eval tname
  raise "unsupported bitfield type #{tname}" unless t
  @type = t
  @signed = signed
  return sz
end

.shape(value = nil, previous_offset = 0, parent = nil, index = nil, kind = DataShape, length = nil) ⇒ Object

Forwards the call to the underlying type



611
612
613
# File 'lib/libbin/data_types.rb', line 611

def self.shape(value = nil, previous_offset = 0, parent = nil, index = nil, kind = DataShape, length = nil)
  @type.shape(value, previous_offset, parent, index, kind, length)
end

.size(value = nil, previous_offset = 0, parent = nil, index = nil, length = nil) ⇒ Object

Forwards the call to the underlying type



606
607
608
# File 'lib/libbin/data_types.rb', line 606

def self.size(value = nil, previous_offset = 0, parent = nil, index = nil, length = nil)
  @type.size(value, previous_offset, parent, index, length)
end