Class: FFI::Enum
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
DataConverter
|
|
Inherits: | Object |
Defined in: | lib/ffi/enum.rb |
Overview
Represents a C enum.
For a C enum:
enum fruits {
apple,
banana,
orange,
pineapple
};
are defined this vocabulary:
-
a symbol is a word from the enumeration (ie. apple, by example);
-
a value is the value of a symbol in the enumeration (by example, apple has value 0 and banana 1).
Class Method Summary
- .new(info, tag = nil) ⇒ Enum constructor
Instance Attribute Summary
- #native_type readonly
- #tag readonly
Instance Method Summary
-
#[](query) ⇒ Integer
(also: #find)
Get a symbol or a value from the enum.
-
#find(query)
Alias for #[].
- #from_native(val, ctx) ⇒ Object
-
#symbol_map ⇒ Hash
(also: #to_h, #to_hash)
Get the symbol map.
- #symbols ⇒ Array
-
#to_h
Alias for #symbol_map.
-
#to_hash
Alias for #symbol_map.
- #to_native(val, ctx) ⇒ Integer
DataConverter
- Included
#from_native | Convert from a native type. |
#native_type | Get native type. |
#to_native | Convert to a native type. |
Constructor Details
.new(info, tag = nil) ⇒ Enum
.new(native_type, info, tag = nil) ⇒ Enum
Enum
.new(native_type, info, tag = nil) ⇒ Enum
# File 'lib/ffi/enum.rb', line 96
def initialize(*args) @native_type = args.first.kind_of?(FFI::Type) ? args.shift : Type::INT info, @tag = *args @kv_map = Hash.new unless info.nil? last_cst = nil value = 0 info.each do |i| case i when Symbol raise ArgumentError, "duplicate enum key" if @kv_map.has_key?(i) @kv_map[i] = value last_cst = i value += 1 when Integer @kv_map[last_cst] = i value = i+1 end end end @vk_map = @kv_map.invert end
Instance Attribute Details
#native_type (readonly)
[ GitHub ]# File 'lib/ffi/enum.rb', line 87
attr_reader :native_type
#tag (readonly)
[ GitHub ]# File 'lib/ffi/enum.rb', line 86
attr_reader :tag
Instance Method Details
#[](query) ⇒ Integer
#[](query) ⇒ Symbol
Also known as: #find
Integer
#[](query) ⇒ Symbol
Get a symbol or a value from the enum.
# File 'lib/ffi/enum.rb', line 133
def [](query) case query when Symbol @kv_map[query] when Integer @vk_map[query] end end
#find(query)
Alias for #[].
# File 'lib/ffi/enum.rb', line 141
alias find []
#from_native(val, ctx) ⇒ Object
# File 'lib/ffi/enum.rb', line 167
def from_native(val, ctx) @vk_map[val] || val end
#symbol_map ⇒ Hash
Also known as: #to_h, #to_hash
Get the symbol map.
# File 'lib/ffi/enum.rb', line 145
def symbol_map @kv_map end
#symbols ⇒ Array
# File 'lib/ffi/enum.rb', line 120
def symbols @kv_map.keys end
#to_h
Alias for #symbol_map.
# File 'lib/ffi/enum.rb', line 149
alias to_h symbol_map
#to_hash
Alias for #symbol_map.
# File 'lib/ffi/enum.rb', line 150
alias to_hash symbol_map
#to_native(val, ctx) ⇒ Integer
# File 'lib/ffi/enum.rb', line 155
def to_native(val, ctx) @kv_map[val] || if val.is_a?(Integer) val elsif val.respond_to?(:to_int) val.to_int else raise ArgumentError, "invalid enum value, #{val.inspect}" end end