Class: PacketGen::Types::Enum Abstract

Inherits:
Int
  • Object
show all
Defined in:
lib/packetgen/types/enum.rb

Overview

This class is abstract.

Base enum class to handle binary integers with limited authorized values

An Enum type is used to handle an Int field with limited and named values.

Simple example

enum = Int8Enum.new(0, 'low' => 0, 'medium' => 1, 'high' => 2})

In this example, enum is a 8-bit field which may take one among three values: low, medium or high:

enum.value = 'high'
enum.value              # => 2
enum.value = 1
enum.value              # => 1
enum.to_human           # => "medium"

Setting an unknown value will raise an exception:

enum.value = 4          # => raise!
enum.value = 'unknown'  # => raise!

But Int#read will not raise when reading an outbound value. This to enable decoding (or forging) of bad packets.

Author:

  • Sylvain Daubert

Since:

  • 2.1.3

Direct Known Subclasses

Int16Enum, Int32Enum, Int8Enum

Instance Attribute Summary collapse

Attributes inherited from Int

#default, #endian, #value, #width

Instance Method Summary collapse

Methods inherited from Int

#nbits, #read, #sz, #to_f, #to_i, #to_s

Methods included from Fieldable

#read, #sz, #to_s, #type_name

Constructor Details

#initialize(enum, endian = nil, width = nil, default = nil) ⇒ Enum

Returns a new instance of Enum.

Parameters:

  • enum (Hash)

    enumerated values. Default value is taken from first element unless given.

  • endian (:little, :big, nil) (defaults to: nil)
  • width (Integer, nil) (defaults to: nil)
  • default (Integer, nil) (defaults to: nil)

    default value

Since:

  • 2.1.3



41
42
43
44
45
# File 'lib/packetgen/types/enum.rb', line 41

def initialize(enum, endian=nil, width=nil, default=nil)
  default ||= enum[enum.keys.first]
  super(nil, endian, width, default)
  @enum = enum
end

Instance Attribute Details

#enumHash (readonly)

Returns:

  • (Hash)

Since:

  • 2.1.3



34
35
36
# File 'lib/packetgen/types/enum.rb', line 34

def enum
  @enum
end

Instance Method Details

#format_inspectObject

Since:

  • 2.1.3



75
76
77
# File 'lib/packetgen/types/enum.rb', line 75

def format_inspect
  format_str % [to_human, to_i]
end

#to_humanString

Get human readable value (enum name)

Returns:

Since:

  • 2.1.3



71
72
73
# File 'lib/packetgen/types/enum.rb', line 71

def to_human
  @enum.key(to_i) || "<unknown:#{@value}>"
end

#value=(value) ⇒ Integer Also known as: from_human

Setter for value attribute

Parameters:

  • value (#to_i, String, nil)

    value as an Integer or as a String from enumration

Returns:

  • (Integer)

Raises:

  • (ArgumentError)

    String value is unknown

Since:

  • 2.1.3



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/packetgen/types/enum.rb', line 52

def value=(value)
  ival = case value
         when NilClass
           nil
         when ::String
           raise ArgumentError, "#{value.inspect} not in enumeration" unless @enum.key? value

           @enum[value]
         else
           value.to_i
         end
  @value = ival
end