Class: FFI::Enum

Inherits:
Object
  • Object
show all
Includes:
DataConverter
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).

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Enum) initialize(info, tag = nil)

A new instance of Enum

Parameters:

  • info (nil, Enumerable)
  • tag (defaults to: nil)

    enum tag



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ffi/enum.rb', line 80

def initialize(info, tag=nil)
  @tag = tag
  @kv_map = Hash.new
  unless info.nil?
    last_cst = nil
    value = 0
    info.each do |i|
      case i
      when Symbol
        @kv_map[i] = value
        last_cst = i
        value += 1
      when Integer
        @kv_map[last_cst] = i
        value = i+1
      end
    end
  end
  @vk_map = Hash[@kv_map.map{|k,v| [v,k]}]
end

Instance Attribute Details

- (Object) tag (readonly)

Returns the value of attribute tag



76
77
78
# File 'lib/ffi/enum.rb', line 76

def tag
  @tag
end

Instance Method Details

- (Integer) [](query) - (Symbol) [](query) Also known as: find

Get a symbol or a value from the enum.

Overloads:

  • - (Integer) [](query)

    Get enum value from symbol.

    Parameters:

    • query (Symbol)

    Returns:

    • (Integer)
  • - (Symbol) [](query)

    Get enum symbol from value.

    Parameters:

    • query (Integer)

    Returns:

    • (Symbol)


115
116
117
118
119
120
121
122
# File 'lib/ffi/enum.rb', line 115

def [](query)
  case query
  when Symbol
    @kv_map[query]
  when Integer
    @vk_map[query]
  end
end

- (Object) from_native(val, ctx)

Symbol name if it exists for val.

Parameters:

  • val

Returns:

  • symbol name if it exists for val.



155
156
157
# File 'lib/ffi/enum.rb', line 155

def from_native(val, ctx)
  @vk_map[val] || val
end

- (Type::INT) native_type

Get native type of Enum

Returns:

  • (Type::INT)


136
137
138
# File 'lib/ffi/enum.rb', line 136

def native_type
  Type::INT
end

- (Hash) symbol_map Also known as: to_h, to_hash

Get the symbol map.

Returns:

  • (Hash)


127
128
129
# File 'lib/ffi/enum.rb', line 127

def symbol_map
  @kv_map
end

- (Array) symbols

Enum symbol names

Returns:

  • (Array)

    enum symbol names



102
103
104
# File 'lib/ffi/enum.rb', line 102

def symbols
  @kv_map.keys
end

- (Integer) to_native(val, ctx)

Value of a enum symbol

Parameters:

  • val (Symbol, Integer, #to_int)
  • ctx

    unused

Returns:

  • (Integer)

    value of a enum symbol



143
144
145
146
147
148
149
150
151
# File 'lib/ffi/enum.rb', line 143

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