class Ronin::Support::Binary::CTypes::EnumType

Base class for all enum types.

Attributes

int_type[R]

The underlying integer type.

@return [IntType]

mapping[R]

The enum’s mapping of symbols to their integer values.

@return [Hash{Symbol => Integer}]

reverse_mapping[R]

The reverse mapping of the enum’s integers to their symbol values.

@return [Hash{Integer => Symbol}]

Public Class Methods

new(int_type,mapping) click to toggle source

Initializes the enum type.

@param [IntType] int_type

The underlying int type for the enum.

@param [Hash{Symbol => Integer}] mapping

The mapping of Symbols to Integer values for the enum.
Calls superclass method
# File lib/ronin/support/binary/ctypes/enum_type.rb, line 54
def initialize(int_type,mapping)
  super(pack_string: nil)

  @int_type        = int_type
  @mapping         = mapping
  @reverse_mapping = @mapping.invert
end

Public Instance Methods

align(new_alignment) click to toggle source

Creates a copy of the enum type with different alignment.

@param [Integer] new_alignment

The new alignment for the enum type.

@return [EnumType]

The new enum type.
# File lib/ronin/support/binary/ctypes/enum_type.rb, line 103
def align(new_alignment)
  self.class.new(@int_type.align(new_alignment),@mapping)
end
alignment() click to toggle source

The alignment of the enum type.

@return [Integer]

The alignment of the underlying {#int_type} in bytes.
# File lib/ronin/support/binary/ctypes/enum_type.rb, line 90
def alignment
  @int_type.alignment
end
dequeue_value(values) click to toggle source

Dequeues a value from the flat list of values.

@param [Array] values

The flat array of values.

@return [Symbol, Integer]

The dequeued value.
# File lib/ronin/support/binary/ctypes/enum_type.rb, line 173
def dequeue_value(values)
  @int_type.dequeue_value(values)
end
enqueue_value(values,value) click to toggle source

Enqueues a value onto the flat list of values.

@param [Array] values

The flat array of values.

@param [Symbol, Integer] value

The value to enqueue.
# File lib/ronin/support/binary/ctypes/enum_type.rb, line 160
def enqueue_value(values,value)
  @int_type.enqueue_value(values,value)
end
pack(value) click to toggle source

Packs an enum value.

@param [Symbol, Integer] value

The enum value. Can be either a Symbol from the enum or an
Integer.

@return [String]

The packed enum value.

@raise [ArgumentError]

The enum value either was not a Symbol or an Integer, or was a
Symbol value not in {#mapping}.
# File lib/ronin/support/binary/ctypes/enum_type.rb, line 121
def pack(value)
  int = case value
        when Integer then value
        when Symbol
          @mapping.fetch(value) do
            raise(ArgumentError,"invalid enum value: #{value.inspect}")
          end
        else
          raise(ArgumentError,"enum value must be a Symbol or an Integer: #{value.inspect}")
        end

  @int_type.pack(int)
end
size() click to toggle source

The size of the enum type.

@return [Integer]

The size of the underlying {#int_type} in bytes.
# File lib/ronin/support/binary/ctypes/enum_type.rb, line 80
def size
  @int_type.size
end
uninitialized_value() click to toggle source

The uninitinalized value for the enum.

@return [Symbol, Integer]

The enum Symbol which maps to `0` or `0`.
# File lib/ronin/support/binary/ctypes/enum_type.rb, line 68
def uninitialized_value
  default_value = @int_type.uninitialized_value

  @reverse_mapping.fetch(default_value,default_value)
end
unpack(data) click to toggle source

Unpacks a previously packed enum value.

@param [String] data

The packed enum value.

@return [Symbol, Integer]

The enum Symbol value or an Integer if the Integer was not in
{#reverse_mapping}.
# File lib/ronin/support/binary/ctypes/enum_type.rb, line 145
def unpack(data)
  int = @int_type.unpack(data)

  return @reverse_mapping.fetch(int,int)
end