Sha256: 3442451560a32d85bdfdf6d5e4bcb1d0335c7d098f10e03321ed3e4e13946f5a

Contents?: true

Size: 1.33 KB

Versions: 17

Compression:

Stored size: 1.33 KB

Contents

module FFI

  class Enums

    def initialize
      @all_enums = Array.new
      @tagged_enums = Hash.new
      @symbol_map = Hash.new
    end

    def <<(enum)
      @all_enums << enum
      @tagged_enums[enum.tag] = enum unless enum.tag.nil?
      @symbol_map.merge!(enum.symbol_map)
    end

    def find(query)
      if @tagged_enums.has_key?(query)
        @tagged_enums[query]
      else
        @all_enums.detect { |enum| enum.symbols.include?(query) }
      end
    end

    def __map_symbol(symbol)
      @symbol_map[symbol]
    end

  end

  class Enum
    attr_reader :tag

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

    def symbols
      @kv_map.keys
    end

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

    def symbol_map
      @kv_map
    end
    alias to_h symbol_map

  end

end

Version data entries

17 entries across 17 versions & 4 rubygems

Version Path
ffi-ffi-0.5.0 lib/ffi/enum.rb
remogatto-ffi-0.5.0 lib/ffi/enum.rb
ffi-0.5.4-x86-mswin32 lib/ffi/enum.rb
ffi-0.5.4-x86-mingw32 lib/ffi/enum.rb
ffi-0.5.4 lib/ffi/enum.rb
ffi-0.5.3-x86-mswin32 lib/ffi/enum.rb
ffi-0.5.3 lib/ffi/enum.rb
ffi-0.5.3-x86-mingw32 lib/ffi/enum.rb
minilab-1.1.0-x86-mswin32-60 vendor/ffi-0.4.0-x86-mswin32/lib/ffi/enum.rb
ffi-0.5.1-x86-mswin32 lib/ffi/enum.rb
ffi-0.5.1 lib/ffi/enum.rb
ffi-0.5.1-x86-mingw32 lib/ffi/enum.rb
ffi-0.5.0-x86-mswin32 lib/ffi/enum.rb
ffi-0.5.0-x86-mingw32 lib/ffi/enum.rb
ffi-0.5.0 lib/ffi/enum.rb
ffi-0.4.0-x86-mswin32 lib/ffi/enum.rb
ffi-0.4.0 lib/ffi/enum.rb