Module: BinData::BitField
- Defined in:
- lib/bindata/bits.rb
Overview
Defines a number of classes that contain a bit based integer. The integer is defined by endian and number of bits.
Constant Summary collapse
- @@mutex =
:nodoc: all
Mutex.new
Class Method Summary collapse
- .create_clamp_code(nbits, signed) ⇒ Object
- .create_do_num_bytes_code(nbits) ⇒ Object
- .create_dynamic_clamp_code(signed) ⇒ Object
- .create_fixed_clamp_code(nbits, signed) ⇒ Object
- .create_int2uint_code(nbits, signed) ⇒ Object
- .create_nbits_code(nbits) ⇒ Object
- .create_params_code(nbits) ⇒ Object
- .create_uint2int_code(nbits, signed) ⇒ Object
- .define_class(name, nbits, endian, signed = :unsigned) ⇒ Object
- .define_methods(bit_class, nbits, endian, signed) ⇒ Object
Class Method Details
.create_clamp_code(nbits, signed) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/bindata/bits.rb', line 92 def create_clamp_code(nbits, signed) if nbits == :nbits create_dynamic_clamp_code(signed) else create_fixed_clamp_code(nbits, signed) end end |
.create_do_num_bytes_code(nbits) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/bindata/bits.rb', line 84 def create_do_num_bytes_code(nbits) if nbits == :nbits "nbits / 8.0" else nbits / 8.0 end end |
.create_dynamic_clamp_code(signed) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/bindata/bits.rb', line 100 def create_dynamic_clamp_code(signed) if signed == :signed max = "max = (1 << (nbits - 1)) - 1" min = "min = -(max + 1)" else max = "max = (1 << nbits) - 1" min = "min = 0" end "#{max}; #{min}; val = (val < min) ? min : (val > max) ? max : val" end |
.create_fixed_clamp_code(nbits, signed) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/bindata/bits.rb', line 112 def create_fixed_clamp_code(nbits, signed) if nbits == 1 && signed == :signed raise "signed bitfield must have more than one bit" end if signed == :signed max = "max = (1 << (#{nbits} - 1)) - 1" min = "min = -(max + 1)" else min = "min = 0" max = "max = (1 << #{nbits}) - 1" end clamp = "(#{max}; #{min}; val = (val < min) ? min : (val > max) ? max : val)" if nbits == 1 # allow single bits to be used as booleans clamp = "(val == true) ? 1 : (not val) ? 0 : #{clamp}" end "val = #{clamp}" end |
.create_int2uint_code(nbits, signed) ⇒ Object
135 136 137 138 139 140 141 142 143 |
# File 'lib/bindata/bits.rb', line 135 def create_int2uint_code(nbits, signed) if signed != :signed "" elsif nbits == :nbits "val &= (1 << nbits) - 1" else "val &= #{(1 << nbits) - 1}" end end |
.create_nbits_code(nbits) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/bindata/bits.rb', line 76 def create_nbits_code(nbits) if nbits == :nbits "nbits = eval_parameter(:nbits)" else "" end end |
.create_params_code(nbits) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/bindata/bits.rb', line 68 def create_params_code(nbits) if nbits == :nbits "mandatory_parameter :nbits" else "" end end |
.create_uint2int_code(nbits, signed) ⇒ Object
145 146 147 148 149 150 151 152 153 |
# File 'lib/bindata/bits.rb', line 145 def create_uint2int_code(nbits, signed) if signed != :signed "" elsif nbits == :nbits "val -= (1 << nbits) if (val >= (1 << (nbits - 1)))" else "val -= #{1 << nbits} if (val >= #{1 << (nbits - 1)})" end end |
.define_class(name, nbits, endian, signed = :unsigned) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/bindata/bits.rb', line 12 def define_class(name, nbits, endian, signed = :unsigned) @@mutex.synchronize do unless BinData.const_defined?(name) new_class = Class.new(BinData::BasePrimitive) BitField.define_methods(new_class, nbits, endian.to_sym, signed.to_sym) RegisteredClasses.register(name, new_class) BinData.const_set(name, new_class) end end BinData.const_get(name) end |
.define_methods(bit_class, nbits, endian, signed) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bindata/bits.rb', line 26 def define_methods(bit_class, nbits, endian, signed) bit_class.module_eval <<-END #{create_params_code(nbits)} def assign(val) #{create_nbits_code(nbits)} #{create_clamp_code(nbits, signed)} super(val) end def do_write(io) #{create_nbits_code(nbits)} val = _value #{create_int2uint_code(nbits, signed)} io.writebits(val, #{nbits}, :#{endian}) end def do_num_bytes #{create_nbits_code(nbits)} #{create_do_num_bytes_code(nbits)} end def bit_aligned? true end #--------------- private def read_and_return_value(io) #{create_nbits_code(nbits)} val = io.readbits(#{nbits}, :#{endian}) #{create_uint2int_code(nbits, signed)} val end def sensible_default 0 end END end |