Class: IcAgent::Candid::FixedIntClass
Overview
Represents an IDL fixed-width Int(n)
Instance Method Summary
collapse
#_build_type_table_impl, #check_type
Methods inherited from BaseType
_build_type_table_impl, #build_type_table, check_type, covariant, decode_value, #display, encode_type, encode_value
Constructor Details
Returns a new instance of FixedIntClass.
474
475
476
477
478
479
480
|
# 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
|
Instance Method Details
#covariant(x) ⇒ Object
482
483
484
485
486
487
488
489
490
|
# 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) ⇒ Object
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
# 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] when 16
by.hex2str.unpack('s')[0] when 32
by.hex2str.unpack('l')[0] when 64
by.hex2str.unpack('q')[0] else
raise ArgumentError, 'bits only support 8, 16, 32, 64'
end
end
|
#encode_type(type_table = nil) ⇒ Object
508
509
510
511
|
# 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) ⇒ Object
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
# File 'lib/ic_agent/candid.rb', line 492
def encode_value(val)
case @bits
when 8
buf = [val].pack('c') when 16
buf = [val].pack('s') when 32
buf = [val].pack('l') when 64
buf = [val].pack('q') else
raise ArgumentError, 'bits only support 8, 16, 32, 64'
end
buf
end
|
#id ⇒ Object
534
535
536
537
538
539
540
541
542
543
544
545
|
# 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 ⇒ Object
530
531
532
|
# File 'lib/ic_agent/candid.rb', line 530
def name
"int#{@bits.to_s}"
end
|