class Ronin::Support::Binary::CTypes::EnumType
Base class for all enum types.
Attributes
The underlying integer type.
@return [IntType]
The enum’s mapping of symbols to their integer values.
@return [Hash{Symbol => Integer}]
The reverse mapping of the enum’s integers to their symbol values.
@return [Hash{Integer => Symbol}]
Public Class Methods
Source
# 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
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.
Public Instance Methods
Source
# 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
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.
Source
# File lib/ronin/support/binary/ctypes/enum_type.rb, line 90 def alignment @int_type.alignment end
The alignment of the enum type.
@return [Integer]
The alignment of the underlying {#int_type} in bytes.
Source
# File lib/ronin/support/binary/ctypes/enum_type.rb, line 173 def dequeue_value(values) @int_type.dequeue_value(values) end
Dequeues a value from the flat list of values.
@param [Array] values
The flat array of values.
@return [Symbol, Integer]
The dequeued value.
Source
# File lib/ronin/support/binary/ctypes/enum_type.rb, line 160 def enqueue_value(values,value) @int_type.enqueue_value(values,value) end
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.
Source
# 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
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}.
Source
# File lib/ronin/support/binary/ctypes/enum_type.rb, line 80 def size @int_type.size end
The size of the enum type.
@return [Integer]
The size of the underlying {#int_type} in bytes.
Source
# 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
The uninitinalized value for the enum.
@return [Symbol, Integer]
The enum Symbol which maps to `0` or `0`.
Source
# 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
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}.