Class: Cplus2Ruby::Model

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModel

Returns a new instance of Model.



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

def initialize
  @typing = Cplus2Ruby::Typing.new
  @code = ""
  @includes = []
  @settings = default_settings()
  @order_cnt = 0
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



10
11
12
# File 'lib/cplus2ruby/model.rb', line 10

def code
  @code
end

#includesObject (readonly)

Returns the value of attribute includes.



11
12
13
# File 'lib/cplus2ruby/model.rb', line 11

def includes
  @includes
end

#typingObject (readonly)

Returns the value of attribute typing.



9
10
11
# File 'lib/cplus2ruby/model.rb', line 9

def typing
  @typing
end

Instance Method Details

#cmp_entities(a, b) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cplus2ruby/model.rb', line 48

def cmp_entities(a,b)
  if a.ancestors.include?(b)
    # a 'after' b (a > b)
    1
  elsif b.ancestors.include?(a)
    -1
  else
    ea = entity_usage(a, b)
    eb = entity_usage(b, a)

    if ea > 0 and eb == 0
      -1
    elsif eb > 0 and ea == 0 
      1
    else
      ao = (a.heritage(:__options__) || {})[:order] || 0
      bo = (b.heritage(:__options__) || {})[:order] || 0
      ao <=> bo
    end
  end
end

#entitiesObject



31
32
33
34
35
36
37
# File 'lib/cplus2ruby/model.rb', line 31

def entities
  entities = []
  ObjectSpace.each_object(Class) {|o|
    entities << o if o.kind_of?(Cplus2Ruby::Entity)
  }
  entities
end

#entities_orderedObject



70
71
72
73
74
75
76
77
# File 'lib/cplus2ruby/model.rb', line 70

def entities_ordered
  ents = entities()
  ents.sort_by {|entity|
    ents.inject(0) {|accum, e|
      accum + (cmp_entities(entity, e) == 1 ? 1 : 0)
    }
  }
end

#entity_usage(klass, other) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/cplus2ruby/model.rb', line 39

def entity_usage(klass, other)
  usage_cnt = 0
  klass.local_annotations.each do |name, opts|
    usage_cnt += 1 if opts[:arguments] and opts[:arguments].values.include?(other)
    usage_cnt += 1 if opts[:type] and opts[:type] == other 
  end
  usage_cnt
end

#finish!Object



25
26
27
28
29
# File 'lib/cplus2ruby/model.rb', line 25

def finish!
  entities.each do |klass|
    @typing.add_object_type(klass)
  end
end

#next_order_cntObject



13
14
15
# File 'lib/cplus2ruby/model.rb', line 13

def next_order_cnt
  @order_cnt += 1
end

#settings(h = {}) ⇒ Object

Update or retrieve the current settings.



82
83
84
85
# File 'lib/cplus2ruby/model.rb', line 82

def settings(h={})
  @settings.update(h)
  @settings
end