Class: FFI::BitMasks::BitMask
- Inherits:
-
Object
- Object
- FFI::BitMasks::BitMask
- Includes:
- DataConverter
- Defined in:
- lib/ffi/bit_masks/bit_mask.rb
Overview
The bitmask data converter.
Instance Attribute Summary collapse
-
#bitmasks ⇒ Hash{Integer => Symbol}
readonly
The masks of the bitmask.
-
#flags ⇒ Hash{Symbol => Integer}
readonly
Flags of the bitmask.
-
#native_type ⇒ FFI::Type
readonly
The underlying native type.
Instance Method Summary collapse
-
#[](query) ⇒ Object
(also: #find)
Maps flags to masks and vice versa.
-
#from_native(value, ctx = nil) ⇒ Hash{Symbol => Boolean}
Converts a bitmask into multiple flags.
-
#initialize(flags, type = :uint) ⇒ BitMask
constructor
Initializes a new bitmask.
-
#symbol_map ⇒ Hash{Symbol => Integer}
(also: #to_h, #to_hash)
The mapping of acceptable Symbols to their Integer equivalents.
-
#symbols ⇒ Array<Symbol>
The Symbols that can be passed to the data converter.
-
#to_native(value, ctx = nil) ⇒ Object
Converts flags to a bitmask.
Constructor Details
Instance Attribute Details
#bitmasks ⇒ Hash{Integer => Symbol} (readonly)
The masks of the bitmask.
26 27 28 |
# File 'lib/ffi/bit_masks/bit_mask.rb', line 26 def bitmasks @bitmasks end |
#flags ⇒ Hash{Symbol => Integer} (readonly)
Flags of the bitmask.
18 19 20 |
# File 'lib/ffi/bit_masks/bit_mask.rb', line 18 def flags @flags end |
#native_type ⇒ FFI::Type (readonly)
The underlying native type.
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.
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.
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 |
#symbol_map ⇒ Hash{Symbol => Integer} Also known as: to_h, to_hash
Note:
For compatibility with FFI::Enum
.
The mapping of acceptable Symbols to their Integer equivalents.
71 72 73 |
# File 'lib/ffi/bit_masks/bit_mask.rb', line 71 def symbol_map @flags end |
#symbols ⇒ Array<Symbol>
Note:
For compatibility with FFI::Enum
.
The Symbols that can be passed to the data converter.
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.
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 |