Class: ABI::Tuple

Inherits:
Type
  • Object
show all
Defined in:
lib/abicoder/type_tuple.rb

Constant Summary

Constants inherited from Type

ABI::Type::BASE_TYPE_RX, ABI::Type::TUPLE_TYPE_RX

Instance Attribute Summary collapse

Attributes inherited from Type

#base, #dims, #sub

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Type

_parse_base_type, #dynamic?, parse, size_type

Constructor Details

#initialize(types, dims = []) ⇒ Tuple

Returns a new instance of Tuple.



58
59
60
61
62
63
64
# File 'lib/abicoder/type_tuple.rb', line 58

def initialize( types, dims=[] )
  super( 'tuple', '', dims )
  @types = types

  ## puts "tuple:"

  ## pp self

end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



56
57
58
# File 'lib/abicoder/type_tuple.rb', line 56

def types
  @types
end

Class Method Details

._parse(tuple, dims = []) ⇒ Object

note: use Type.parse NOT Tuple._parse

to parse Tuple!!!


43
44
45
46
47
48
49
50
51
52
# File 'lib/abicoder/type_tuple.rb', line 43

def self._parse( tuple, dims=[] )
  ## puts "  enter Tuple.parse( types: >#{tuple.inspect}<, dims: >#{dims.inspect}< )"


  # e.g.

  #=>   enter Tuple.parse( types: >"string,string,bool"<, dims: >[]< )

  types        = _parse_tuple_type( tuple )
  parsed_types = types.map{ |t| Type.parse( t ) }

  Tuple.new( parsed_types, dims )
end

._parse_tuple_type(str) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/abicoder/type_tuple.rb', line 6

def self._parse_tuple_type( str )
  ## note: types assumes string WITHOUT enclosing () e.g.

  ##  tuple(string,string,bool)  =>  expected as "string,string,bool"


  depth     = 0
  collected = []
  current   = ''

  ### todo/fix: replace with a simple parser!!!

  ##    allow  () and move verbose tuple() too!!!

  str.each_char do |c|
      case c
      when ',' then
        if depth == 0
          collected << current
          current = ''
        else
          current += c
        end
      when '(' then
        depth += 1
        current += c
      when ')' then
        depth -= 1
        current += c
      else
        current += c
      end
  end
  collected << current unless current.empty?

  collected
end

Instance Method Details

#==(another_type) ⇒ Object



67
68
69
70
71
# File 'lib/abicoder/type_tuple.rb', line 67

def ==(another_type)
  another_type.kind_of?(Tuple) &&
    @types == another_type.types  &&
    @dims  == another_type.dims
end

#calculate_sizeObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/abicoder/type_tuple.rb', line 77

def calculate_size
  if @dims.empty?
    s = 0
    @types.each do |type|
      ts = type.size
      return nil if ts.nil?
      s += ts
    end
    s
  else
    if @dims.last == 0     # note:  0 used for dynamic array []

      nil
    else
      subtype.dynamic? ? nil : @dims.last * subtype.size
    end
  end
end

#formatObject



99
100
101
102
# File 'lib/abicoder/type_tuple.rb', line 99

def format
  ## rebuild minimal string

  buf = "(#{@types.map {|t| t.format }.join(',')})"
end

#sizeObject



73
74
75
# File 'lib/abicoder/type_tuple.rb', line 73

def size
  @size ||= calculate_size
end

#subtypeObject



95
96
97
# File 'lib/abicoder/type_tuple.rb', line 95

def subtype
  @subtype ||= Tuple.new( types, dims[0...-1] )
end