Class: Cplus2Ruby::Typing

Inherits:
Object
  • Object
show all
Defined in:
lib/cplus2ruby/typing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTyping

Returns a new instance of Typing.



6
7
8
9
10
# File 'lib/cplus2ruby/typing.rb', line 6

def initialize
  @map = default_map() 
  @aliases = OrderedHash.new
  clone_entry Object, 'VALUE'
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



4
5
6
# File 'lib/cplus2ruby/typing.rb', line 4

def aliases
  @aliases
end

Instance Method Details

#add_entry(type, entry) ⇒ Object



12
13
14
15
16
# File 'lib/cplus2ruby/typing.rb', line 12

def add_entry(type, entry)
  type = to_key(type)
  raise if @map.include?(type)
  @map[type] = entry
end

#add_object_type(klass) ⇒ Object



81
82
83
# File 'lib/cplus2ruby/typing.rb', line 81

def add_object_type(klass)
  add_entry(klass, object_type(klass.name))
end

#alias_entry(from, to) ⇒ Object

Add a type alias. Also modifies type map.



28
29
30
31
32
33
# File 'lib/cplus2ruby/typing.rb', line 28

def alias_entry(from, to)
  from = to_key(from)
  to = to_key(to)
  @aliases[from] = to
  clone_entry(from, to)
end

#can_convert?(type) ⇒ Boolean

Returns true if Ruby <-> C conversion for this type is possible

Returns:

  • (Boolean)


73
74
75
# File 'lib/cplus2ruby/typing.rb', line 73

def can_convert?(type)
  get_entry(type) ? true : false
end

#clone_entry(from, to) ⇒ Object



18
19
20
21
22
23
# File 'lib/cplus2ruby/typing.rb', line 18

def clone_entry(from, to)
  from = to_key(from)
  to = to_key(to)
  raise if @map.include?(from)
  @map[from] = @map[to] # FIXME: .dup?
end

#convert(type, var, kind) ⇒ Object



77
78
79
# File 'lib/cplus2ruby/typing.rb', line 77

def convert(type, var, kind)
  (get_entry(type)[kind] || "").gsub('%s', var.to_s)
end

#get_entry(type) ⇒ Object



35
36
37
# File 'lib/cplus2ruby/typing.rb', line 35

def get_entry(type)
  @map[to_key(type)]
end

#lookup_entry(attribute, options, type) ⇒ Object

Looks up first in the annotation options, then in the type options.



43
44
45
# File 'lib/cplus2ruby/typing.rb', line 43

def lookup_entry(attribute, options, type)
  (get_entry(type) || {}).dup.update(options)[attribute]
end

#var_assgn(name, value) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
# File 'lib/cplus2ruby/typing.rb', line 61

def var_assgn(name, value)
  raise ArgumentError if value.nil?
  if value.is_a?(String) and value.include?('%s')
    value.gsub('%s', name)
  else
    "#{name} = #{value}"
  end
end

#var_decl(type, name) ⇒ Object

Returns a C++ declaration



50
51
52
53
54
55
56
57
58
59
# File 'lib/cplus2ruby/typing.rb', line 50

def var_decl(type, name)
  if entry = get_entry(type)
    entry[:ctype].gsub("%s", name.to_s)
  # FIXME type.to_s
  elsif type.to_s.include?("%s")
    type.gsub("%s", name.to_s)
  else
    "#{type} #{name}"
  end
end