Class: CTypes::Bitmap
- Inherits:
-
Object
- Object
- CTypes::Bitmap
- Extended by:
- Forwardable
- Includes:
- Type
- Defined in:
- lib/ctypes/bitmap.rb
Overview
map bits in an integer to specific flags
This class enables the mapping of individual bits in an integer to specific flags that each bit represents. This class only supports single bit fields. See ‘Bitfield` if you need to unpack multibit values.
Instance Attribute Summary
Attributes included from Type
Instance Method Summary collapse
- #fixed_size? ⇒ Boolean
-
#initialize(bits:, type: Helpers.uint32) ⇒ Bitmap
constructor
create a new Bitmap.
-
#pack(value, endian: default_endian, validate: true) ⇒ ::String
pack a ruby Array containing symbol names for each bit into a binary string.
-
#permissive ⇒ Bitmap
get a permissive version of this bitmap.
- #size ⇒ Object
-
#unpack_one(buf, endian: default_endian) ⇒ ::Array
convert a String containing the binary represention of a c type into the equivalent ruby type.
Methods included from Type
#default_endian, #default_value, #greedy?, #pread, #read, #unpack, #unpack_all, #with_endian, #without_endian
Constructor Details
#initialize(bits:, type: Helpers.uint32) ⇒ Bitmap
create a new Bitmap
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ctypes/bitmap.rb', line 46 def initialize(bits:, type: Helpers.uint32) raise Error, "bits must be an Enum instance: %p" % bits unless bits.is_a?(Enum) @type = type @bits = bits @bits_max = type.size * 8 @bits_constraint = Dry::Types["integer"] .constrained(gteq: 0, lt: @bits_max) @dry_type = Dry::Types["array"].of(@bits.dry_type).default([].freeze) end |
Instance Method Details
#fixed_size? ⇒ Boolean
63 64 65 |
# File 'lib/ctypes/bitmap.rb', line 63 def fixed_size? @type.fixed_size? end |
#pack(value, endian: default_endian, validate: true) ⇒ ::String
pack a ruby Array containing symbol names for each bit into a binary string
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/ctypes/bitmap.rb', line 85 def pack(value, endian: default_endian, validate: true) value = @dry_type[value] unless validate == false mapping = @bits.mapping bits = value.inject(0) do |out, v| bit = case v when Integer v when /\Abit_(\d+)\z/ $1.to_i when Symbol mapping[v] else raise Error, "unknown bitmap value: %p" % v end @bits_constraint[bit] out |= 1 << bit end @type.pack(bits, endian: @type.endian || endian, validate: validate) end |
#permissive ⇒ Bitmap
get a permissive version of this bitmap
150 151 152 |
# File 'lib/ctypes/bitmap.rb', line 150 def permissive Bitmap.new(type: @type, bits: @bits.permissive) end |
#size ⇒ Object
59 60 61 |
# File 'lib/ctypes/bitmap.rb', line 59 def size @type.size end |
#unpack_one(buf, endian: default_endian) ⇒ ::Array
convert a String containing the binary represention of a c type into the equivalent ruby type
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ctypes/bitmap.rb', line 129 def unpack_one(buf, endian: default_endian) value, rest = @type.unpack_one(buf, endian: @type.endian || endian) bits = [] @bits_max.times do |bit| next if value & (1 << bit) == 0 v = @bits.dry_type[bit] v = :"bit_#{v}" if v.is_a?(Integer) bits << v end [bits, rest] end |