Class: Ronin::Support::Binary::CTypes::EnumType
- Defined in:
- lib/ronin/support/binary/ctypes/enum_type.rb
Overview
Base class for all enum types.
Instance Attribute Summary collapse
-
#int_type ⇒ IntType
readonly
The underlying integer type.
-
#mapping ⇒ Hash{Symbol => Integer}
readonly
The enum's mapping of symbols to their integer values.
-
#reverse_mapping ⇒ Hash{Integer => Symbol}
readonly
The reverse mapping of the enum's integers to their symbol values.
Attributes inherited from Type
Instance Method Summary collapse
-
#align(new_alignment) ⇒ EnumType
Creates a copy of the enum type with different alignment.
-
#alignment ⇒ Integer
The alignment of the enum type.
-
#dequeue_value(values) ⇒ Symbol, Integer
Dequeues a value from the flat list of values.
-
#enqueue_value(values, value) ⇒ Object
Enqueues a value onto the flat list of values.
-
#initialize(int_type, mapping) ⇒ EnumType
constructor
Initializes the enum type.
-
#pack(value) ⇒ String
Packs an enum value.
-
#size ⇒ Integer
The size of the enum type.
-
#uninitialized_value ⇒ Symbol, Integer
The uninitinalized value for the enum.
-
#unpack(data) ⇒ Symbol, Integer
Unpacks a previously packed enum value.
Methods inherited from Type
Constructor Details
#initialize(int_type, mapping) ⇒ EnumType
Initializes the enum type.
54 55 56 57 58 59 60 |
# 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 |
Instance Attribute Details
#int_type ⇒ IntType (readonly)
The underlying integer type.
33 34 35 |
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 33 def int_type @int_type end |
#mapping ⇒ Hash{Symbol => Integer} (readonly)
The enum's mapping of symbols to their integer values.
38 39 40 |
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 38 def mapping @mapping end |
#reverse_mapping ⇒ Hash{Integer => Symbol} (readonly)
The reverse mapping of the enum's integers to their symbol values.
43 44 45 |
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 43 def reverse_mapping @reverse_mapping end |
Instance Method Details
#align(new_alignment) ⇒ EnumType
Creates a copy of the enum type with different alignment.
103 104 105 |
# 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 ⇒ Integer
The alignment of the enum type.
90 91 92 |
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 90 def alignment @int_type.alignment end |
#dequeue_value(values) ⇒ Symbol, Integer
Dequeues a value from the flat list of values.
173 174 175 |
# 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) ⇒ Object
Enqueues a value onto the flat list of values.
160 161 162 |
# 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) ⇒ String
Packs an enum value.
121 122 123 124 125 126 127 128 129 130 131 132 133 |
# 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 ⇒ Integer
The size of the enum type.
80 81 82 |
# File 'lib/ronin/support/binary/ctypes/enum_type.rb', line 80 def size @int_type.size end |
#uninitialized_value ⇒ Symbol, Integer
The uninitinalized value for the enum.
68 69 70 71 72 |
# 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) ⇒ Symbol, Integer
Unpacks a previously packed enum value.
145 146 147 148 149 |
# 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 |