class IcAgent::Candid::FixedIntClass

Represents an IDL fixed-width Int(n)

Public Class Methods

new(bits) click to toggle source
Calls superclass method IcAgent::Candid::PrimitiveType::new
# File lib/ic_agent/candid.rb, line 474
def initialize(bits)
  super()
  @bits = bits
  unless [8, 16, 32, 64].include?(@bits)
    raise ArgumentError, 'bits only support 8, 16, 32, 64'
  end
end

Public Instance Methods

covariant(x) click to toggle source
# File lib/ic_agent/candid.rb, line 482
def covariant(x)
  min_val = -1 * 2**(@bits - 1)
  max_val = -1 + 2**(@bits - 1)
  if x >= min_val && x <= max_val
    true
  else
    false
  end
end
decode_value(b, t) click to toggle source
# File lib/ic_agent/candid.rb, line 513
def decode_value(b, t)
  check_type(t)
  by = IcAgent::Candid.safe_read(b, @bits / 8)
  case @bits
  when 8
    by.hex2str.unpack('c')[0] # signed char -> Int8
  when 16
    by.hex2str.unpack('s')[0] # short -> Int16
  when 32
    by.hex2str.unpack('l')[0] # int -> Int32
  when 64
    by.hex2str.unpack('q')[0] # long long -> Int64
  else
    raise ArgumentError, 'bits only support 8, 16, 32, 64'
  end
end
encode_type(type_table = nil) click to toggle source
# File lib/ic_agent/candid.rb, line 508
def encode_type(type_table = nil)
  offset = (Math.log2(@bits) - 3).to_i
  LEB128.encode_signed(-9 - offset).string
end
encode_value(val) click to toggle source
# File lib/ic_agent/candid.rb, line 492
def encode_value(val)
  case @bits
  when 8
    buf = [val].pack('c') # signed char -> Int8
  when 16
    buf = [val].pack('s') # short -> Int16
  when 32
    buf = [val].pack('l') # int -> Int32
  when 64
    buf = [val].pack('q') # long long -> Int64
  else
    raise ArgumentError, 'bits only support 8, 16, 32, 64'
  end
  buf
end
id() click to toggle source
# File lib/ic_agent/candid.rb, line 534
def id
  case @bits
  when 8
    TypeIds::Int8
  when 16
    TypeIds::Int16
  when 32
    TypeIds::Int32
  when 64
    TypeIds::Int64
  end
end
name() click to toggle source
# File lib/ic_agent/candid.rb, line 530
def name
  "int#{@bits.to_s}"
end