Class: LibBin::Structure::Enum Abstract

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

Overview

This class is abstract.

A parent class representing an enumeration that is loading integers as symbols.

Useer should inherit this base class.

Scalars collapse

Scalars collapse

Class Attribute Details

.mapObject

get the enum map



339
340
341
# File 'lib/libbin/data_types.rb', line 339

def map
  @map
end

.typeObject

Get the underlying type



341
342
343
# File 'lib/libbin/data_types.rb', line 341

def type
  @type
end

Class Method Details

.alignObject

Returns the underlying type align property



377
378
379
# File 'lib/libbin/data_types.rb', line 377

def self.align
  @type.align
end

.always_alignObject

Returns the underlying type always_align property



372
373
374
# File 'lib/libbin/data_types.rb', line 372

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) ⇒ Symbol, ...

Convert a field of class LibBin::Structure::Enum 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:

  • (Symbol, Integer, Array<Symbol,Integer>)

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



429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/libbin/data_types.rb', line 429

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|
       n = @map_to[val]
       n = val unless n
       n
    }
  else
    n = @map_to[v]
    n = v unless n
    n
  end
end

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

Dump a field of class LibBin::Structure::Enum to output.

Parameters:

  • value (Numeric, Array<Numeric>)

    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)


455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/libbin/data_types.rb', line 455

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

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

Load a field of type LibBin::Structure::Enum 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:

  • (Symbol, Integer, Array<Symbol,Integer>)

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



401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/libbin/data_types.rb', line 401

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|
       n = @map_to[val]
       n = val unless n
       n
    }
  else
    n = @map_to[v]
    n = v unless n
    n
  end
end

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

Forwards the call to the underlying type



387
388
389
# File 'lib/libbin/data_types.rb', line 387

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



382
383
384
# File 'lib/libbin/data_types.rb', line 382

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

.type_sizeObject

Get the underlying type size in bits



353
354
355
# File 'lib/libbin/data_types.rb', line 353

def type_size
  @type.size
end

.type_size=(sz) ⇒ Object Also known as: set_type_size

Specify the size of the underlying type



344
345
346
347
348
349
# File 'lib/libbin/data_types.rb', line 344

def type_size=(sz)
  t = eval "Int#{sz}"
  raise "unsupported enum size #{sz}" unless t
  @type = t
  return sz
end