class FFI::BitMasks::BitMask

The bitmask data converter.

Attributes

bitmasks[R]

The masks of the bitmask.

@return [Hash{Integer => Symbol}]

The mapping of bitmasks to bit-flags.
flags[R]

Flags of the bitmask.

@return [Hash{Symbol => Integer}]

The mapping of bit-flags to bitmasks.
native_type[R]

The underlying native type.

@return [FFI::Type]

The FFI primitive.

Public Class Methods

new(flags,type=:uint) click to toggle source

Initializes a new bitmask.

@param [Hash{Symbol => Integer}] flags

The flags and their masks.

@param [Symbol] type

The underlying type.
# File lib/ffi/bit_masks/bit_mask.rb, line 45
def initialize(flags,type=:uint)
  @flags       = flags
  @bitmasks    = flags.invert
  @native_type = FFI.find_type(type)
end

Public Instance Methods

[](query) click to toggle source

Maps flags to masks and vice versa.

@overload [](query)

@param [Symbol] query
  The flag name.

@return [Integer]
  The mask for the flag.

@overload [](query)

@param [Integer] query
  The mask.

@return [Symbol]
  The flag for the mask.
# File lib/ffi/bit_masks/bit_mask.rb, line 104
def [](query)
  case query
  when Symbol  then @flags[query]
  when Integer then @bitmasks[query]
  end
end
Also aliased as: find
find(query)

@note For compatibility with ‘FFI::Enum`.

Alias for: []
from_native(value,ctx=nil) click to toggle source

Converts a bitmask into multiple flags.

@param [Integer] value

The raw bitmask.

@return [Hash{Symbol => Boolean}]

The flags for the bitmask.
# File lib/ffi/bit_masks/bit_mask.rb, line 175
def from_native(value,ctx=nil)
  flags = {}

  @flags.each do |flag,bitmask|
    flags[flag] ||= ((value & bitmask) == bitmask)
  end

  return flags
end
reference_required?() click to toggle source
# File lib/ffi/bit_masks/bit_mask.rb, line 185
def reference_required?
  false
end
symbol_map() click to toggle source

The mapping of acceptable Symbols to their Integer equivalents.

@return [Hash{Symbol => Integer}]

The mapping of Symbols.

@note For compatibility with ‘FFI::Enum`.

# File lib/ffi/bit_masks/bit_mask.rb, line 71
def symbol_map
  @flags
end
Also aliased as: to_h, to_hash
symbols() click to toggle source

The Symbols that can be passed to the data converter.

@return [Array<Symbol>]

The Array of Symbols.

@note For compatibility with ‘FFI::Enum`.

# File lib/ffi/bit_masks/bit_mask.rb, line 59
def symbols
  @flags.keys
end
to_h()

@note For compatibility with ‘FFI::Enum`.

Alias for: symbol_map
to_hash()

@note For compatibility with ‘FFI::Enum`.

Alias for: symbol_map
to_native(value,ctx=nil) click to toggle source

Converts flags to a bitmask.

@overload to_native(value)

@param [Hash{Symbol => Boolean}] value
  The flags and their values.

@return [Integer]
  The bitmask for the given flags.

@overload to_native(value)

@param [#to_int] value
  The raw bitmask.

@return [Integer]
  The bitmask.

@raise [ArgumentError]

# File lib/ffi/bit_masks/bit_mask.rb, line 137
def to_native(value,ctx=nil)
  uint = 0

  case value
  when Hash
    uint = 0

    value.each do |flag,value|
      if (@flags.has_key?(flag) && value)
        uint |= @flags[flag]
      end
    end

    return uint
  else
    if value.respond_to?(:to_int)
      int = value.to_int

      @bitmasks.each_key do |mask|
        uint |= (int & mask)
      end

      return uint
    else
      raise(ArgumentError,"invalid bitmask value #{value.inspect}")
    end
  end
end