Class: FFI::BitMasks::BitMask

Inherits:
Object
  • Object
show all
Includes:
DataConverter
Defined in:
lib/ffi/bit_masks/bit_mask.rb

Overview

The bitmask data converter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags, type = :uint) ⇒ BitMask

Initializes a new bitmask.

Parameters:

  • flags (Hash{Symbol => Integer})

    The flags and their masks.

  • type (Symbol) (defaults to: :uint)

    The underlying type.



45
46
47
48
49
# 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

Instance Attribute Details

#bitmasksHash{Integer => Symbol} (readonly)

The masks of the bitmask.

Returns:

  • (Hash{Integer => Symbol})

    The mapping of bitmasks to bit-flags.



26
27
28
# File 'lib/ffi/bit_masks/bit_mask.rb', line 26

def bitmasks
  @bitmasks
end

#flagsHash{Symbol => Integer} (readonly)

Flags of the bitmask.

Returns:

  • (Hash{Symbol => Integer})

    The mapping of bit-flags to bitmasks.



18
19
20
# File 'lib/ffi/bit_masks/bit_mask.rb', line 18

def flags
  @flags
end

#native_typeFFI::Type (readonly)

The underlying native type.

Returns:

  • (FFI::Type)

    The FFI primitive.



34
35
36
# File 'lib/ffi/bit_masks/bit_mask.rb', line 34

def native_type
  @native_type
end

Instance Method Details

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

Maps flags to masks and vice versa.

Overloads:

  • #[](query) ⇒ Integer

    Returns The mask for the flag.

    Parameters:

    • query (Symbol)

      The flag name.

    Returns:

    • (Integer)

      The mask for the flag.

  • #[](query) ⇒ Symbol

    Returns The flag for the mask.

    Parameters:

    • query (Integer)

      The mask.

    Returns:

    • (Symbol)

      The flag for the mask.



104
105
106
107
108
109
# 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

#from_native(value, ctx = nil) ⇒ Hash{Symbol => Boolean}

Converts a bitmask into multiple flags.

Parameters:

  • value (Integer)

    The raw bitmask.

Returns:

  • (Hash{Symbol => Boolean})

    The flags for the bitmask.



175
176
177
178
179
180
181
182
183
# 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?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/ffi/bit_masks/bit_mask.rb', line 185

def reference_required?
  false
end

#symbol_mapHash{Symbol => Integer} Also known as: to_h, to_hash

Note:

For compatibility with FFI::Enum.

The mapping of acceptable Symbols to their Integer equivalents.

Returns:

  • (Hash{Symbol => Integer})

    The mapping of Symbols.



71
72
73
# File 'lib/ffi/bit_masks/bit_mask.rb', line 71

def symbol_map
  @flags
end

#symbolsArray<Symbol>

Note:

For compatibility with FFI::Enum.

The Symbols that can be passed to the data converter.

Returns:

  • (Array<Symbol>)

    The Array of Symbols.



59
60
61
# File 'lib/ffi/bit_masks/bit_mask.rb', line 59

def symbols
  @flags.keys
end

#to_native(value) ⇒ Integer #to_native(value) ⇒ Integer

Converts flags to a bitmask.

Overloads:

  • #to_native(value) ⇒ Integer

    Returns The bitmask for the given flags.

    Parameters:

    • value (Hash{Symbol => Boolean})

      The flags and their values.

    Returns:

    • (Integer)

      The bitmask for the given flags.

  • #to_native(value) ⇒ Integer

    Returns The bitmask.

    Parameters:

    • value (#to_int)

      The raw bitmask.

    Returns:

    • (Integer)

      The bitmask.

Raises:

  • (ArgumentError)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# 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