Class: IcAgent::Candid::TypeTable

Inherits:
Object
  • Object
show all
Defined in:
lib/ic_agent/candid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTypeTable

Returns a new instance of TypeTable.



50
51
52
53
# File 'lib/ic_agent/candid.rb', line 50

def initialize
  @typs = []
  @idx = {}
end

Instance Attribute Details

#idxObject

Returns the value of attribute idx.



48
49
50
# File 'lib/ic_agent/candid.rb', line 48

def idx
  @idx
end

#typsObject

Returns the value of attribute typs.



48
49
50
# File 'lib/ic_agent/candid.rb', line 48

def typs
  @typs
end

Instance Method Details

#add(obj, buf) ⇒ Object



59
60
61
62
63
# File 'lib/ic_agent/candid.rb', line 59

def add(obj, buf)
  idx = @typs.length
  @idx[obj.name] = idx
  @typs.append(buf)
end

#encodeObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ic_agent/candid.rb', line 78

def encode
  l = 0
  @typs.each do |t|
    if t.length != 0
      l += 1
    end
  end

  length = LEB128.encode_signed(l).string
  buf = @typs.join('')
  return "#{length}#{buf}"
end

#has(obj) ⇒ Object



55
56
57
# File 'lib/ic_agent/candid.rb', line 55

def has(obj)
  return @idx.has_key?(obj.name)
end

#index_of(type_name) ⇒ Object

Raises:



91
92
93
94
95
# File 'lib/ic_agent/candid.rb', line 91

def index_of(type_name)
  raise ValueError, "Missing type index for #{type_name}" if !@idx.has_key?(type_name)

  return LEB128.encode_signed(@idx[type_name] | 0).string
end

#merge(obj, knot) ⇒ Object

Raises:



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ic_agent/candid.rb', line 65

def merge(obj, knot)
  idx = self.has(obj) ? @idx[obj.name] : nil
  knot_idx = @idx.has_key?(knot) ? @idx[knot] : nil

  raise ValueError, "Missing type index for #{obj.name}" if idx == nil
  raise ValueError, "Missing type index for #{knot}" if knot_idx == nil

  @typs[idx] = @typs[knot_idx]
  # delete the type
  @typs.delete_at(knot_idx)
  @idx.delete(knot)
end