Module: BinData::Int
- Defined in:
- lib/bindata/int.rb
Overview
Defines a number of classes that contain an integer. The integer is defined by endian, signedness and number of bytes.
Class Method Summary collapse
- .class_name(nbits, endian, signed) ⇒ Object
- .define_class(nbits, endian, signed) ⇒ Object
- .define_methods(int_class, nbits, endian, signed) ⇒ Object
Class Method Details
.class_name(nbits, endian, signed) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/bindata/int.rb', line 22 def class_name(nbits, endian, signed) endian_str = (endian == :big) ? "be" : "le" base = (signed == :signed) ? "Int" : "Uint" "#{base}#{nbits}#{endian_str}" end |
.define_class(nbits, endian, signed) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/bindata/int.rb', line 9 def define_class(nbits, endian, signed) name = class_name(nbits, endian, signed) unless BinData.const_defined?(name) BinData.module_eval <<-END class #{name} < BinData::BasePrimitive Int.define_methods(self, #{nbits}, :#{endian}, :#{signed}) end END end BinData.const_get(name) end |
.define_methods(int_class, nbits, endian, signed) ⇒ Object
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 |
# File 'lib/bindata/int.rb', line 29 def define_methods(int_class, nbits, endian, signed) raise "nbits must be divisible by 8" unless (nbits % 8).zero? int_class.module_eval <<-END def assign(val) #{create_clamp_code(nbits, signed)} super(val) end def do_num_bytes #{nbits / 8} end #--------------- private def sensible_default 0 end def value_to_binary_string(val) #{create_clamp_code(nbits, signed)} #{create_int2uint_code(nbits) if signed == :signed} #{create_to_binary_s_code(nbits, endian)} end def read_and_return_value(io) val = #{create_read_code(nbits, endian)} #{create_uint2int_code(nbits) if signed == :signed} val end END end |