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.



21
22
23
24
25
26
27
28
# File 'lib/cplus2ruby/model.rb', line 21

def initialize
  @typing = Cplus2Ruby::Typing.new
  @code = ""
  @includes = []
  @settings = default_settings()
  @order_cnt = 0
  @prop_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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cplus2ruby/model.rb', line 53

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



36
37
38
39
40
41
42
# File 'lib/cplus2ruby/model.rb', line 36

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

#entities_orderedObject



75
76
77
78
79
80
81
82
# File 'lib/cplus2ruby/model.rb', line 75

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



44
45
46
47
48
49
50
51
# File 'lib/cplus2ruby/model.rb', line 44

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



30
31
32
33
34
# File 'lib/cplus2ruby/model.rb', line 30

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

#next_prop_cntObject



17
18
19
# File 'lib/cplus2ruby/model.rb', line 17

def next_prop_cnt
  @prop_cnt += 1
end

#settings(h = {}) ⇒ Object

Update or retrieve the current settings.



87
88
89
90
# File 'lib/cplus2ruby/model.rb', line 87

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