Class: Canoser::IntField

Inherits:
Object
  • Object
show all
Defined in:
lib/canoser/field.rb

Constant Summary collapse

@@pack_map =
{8 => "C", 16 => "S", 32 => "L", 64 => "Q"}

Instance Method Summary collapse

Constructor Details

#initialize(int_bits, signed = false) ⇒ IntField

Returns a new instance of IntField.



6
7
8
9
# File 'lib/canoser/field.rb', line 6

def initialize(int_bits, signed=false)
  @int_bits = int_bits
  @signed = signed
end

Instance Method Details

#decode(cursor) ⇒ Object



37
38
39
40
# File 'lib/canoser/field.rb', line 37

def decode(cursor)
  bytes = cursor.read_bytes(@int_bits/8)
  decode_bytes(bytes)
end

#decode_bytes(bytes) ⇒ Object



33
34
35
# File 'lib/canoser/field.rb', line 33

def decode_bytes(bytes)
  bytes.unpack(pack_str)[0]
end

#encode(value) ⇒ Object



29
30
31
# File 'lib/canoser/field.rb', line 29

def encode(value)
  [value].pack(pack_str)
end

#inspectObject



11
12
13
14
15
16
17
# File 'lib/canoser/field.rb', line 11

def inspect
  if @signed
    "Int#{@int_bits}"
  else
    "Uint#{@int_bits}"
  end
end

#max_valueObject



42
43
44
45
46
47
48
# File 'lib/canoser/field.rb', line 42

def max_value
  if @signed
    2**(@int_bits-1) - 1
  else
    2**@int_bits - 1
  end
end

#pack_strObject



23
24
25
26
27
# File 'lib/canoser/field.rb', line 23

def pack_str
  str = @@pack_map[@int_bits]
  str = str.downcase if @signed
  str
end

#to_sObject



19
20
21
# File 'lib/canoser/field.rb', line 19

def to_s
  inspect
end