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.

Constant Summary collapse

@@mutex =

:nodoc: all

Mutex.new

Class Method Summary collapse

Class Method Details

.define_class(name, nbits, endian, signed) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bindata/int.rb', line 12

def define_class(name, nbits, endian, signed)
  @@mutex.synchronize do
    unless BinData.const_defined?(name)
      new_class = Class.new(BinData::BasePrimitive)
      Int.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(int_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
# File 'lib/bindata/int.rb', line 26

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_to_binary_s_code(nbits, endian, signed)}
    end

    def read_and_return_value(io)
      #{create_read_code(nbits, endian, signed)}
    end
  END
end