Class: Comet::HeaderGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/comet-html/header-generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ HeaderGenerator

Returns a new instance of HeaderGenerator.



5
6
7
# File 'lib/comet-html/header-generator.rb', line 5

def initialize object
  @object = object
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



3
4
5
# File 'lib/comet-html/header-generator.rb', line 3

def object
  @object
end

#sourceObject (readonly)

Returns the value of attribute source.



3
4
5
# File 'lib/comet-html/header-generator.rb', line 3

def source
  @source
end

Instance Method Details

#bound_attributes_definitionObject



21
22
23
24
# File 'lib/comet-html/header-generator.rb', line 21

def bound_attributes_definition
  return "" if object.implements_ibindable_view?
  "Comet::BoundAttributes bound_attributes;"
end

#friends_defObject



61
62
63
64
65
66
67
# File 'lib/comet-html/header-generator.rb', line 61

def friends_def
  result = ""
  object.context.classes.each do |klass|
    result += indent + "friend class #{klass.typename};\n" unless klass == object
  end
  result
end

#generateObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/comet-html/header-generator.rb', line 114

def generate
  anchors_def = generate_anchors
  public_properties_def, protected_properties_def, private_properties_def = generate_properties

<<HEADER
class #{object.typename} : public #{object.superclass}
{
#{friends_def}
protected:
  #{bound_attributes_definition}
#{protected_properties_def}
private:
#{anchors_def}
#{private_properties_def}
public:
  #{signaler_definition}
#{public_properties_def}

  #{object.constructor_decl}
  virtual ~#{object.typename}() {}

  void bind_attributes();
  void trigger_binding_updates();
  
#{generate_getters_setters}

  #{object.inline_code}
};
HEADER
end

#generate_anchorsObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/comet-html/header-generator.rb', line 48

def generate_anchors
  result = ""
  anchor_count = 0
  object.collect_children.each do |child|
    if child.is_anchorable?
      anchor_count += 1
      child.anchor_name = "anchor_#{anchor_count}"
      result += indent + "Comet::CommentElement #{child.anchor_name};\n"
    end
  end
  result
end

#generate_getters_settersObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/comet-html/header-generator.rb', line 26

def generate_getters_setters
  result = ""
  object.refs.each do |ref|
    is_ptr = ref.type.end_with? '*'
    if ref.has_getter?
      if is_ptr
        result += indent + "#{ref.type} get_#{ref.name}() const { return #{ref.name}; }\n"
      else
        result += indent + "const #{ref.type}& get_#{ref.name}() const { return #{ref.name}; }\n"
      end
    end
    if ref.has_setter?
      if is_ptr
        result += indent + "virtual void set_#{ref.name}(#{ref.type} __v) { #{ref.name} = __v; }\n"
      else
        result += indent + "virtual void set_#{ref.name}(const #{ref.type}& __v) { #{ref.name} = __v; }\n"
      end
    end
  end
  result
end

#generate_propertiesObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/comet-html/header-generator.rb', line 69

def generate_properties
  protected_properties_def = ""
  public_properties_def    = ""
  private_properties_def   = ""
  if object.is_root?
    protected_properties_def += indent + "#{object.typename}* root;\n"
  else
    public_properties_def  += indent + "#{object.parent.typename}* parent;\n"
    protected_properties_def += indent + "#{object.root.typename}* root;\n"
  end
  object.refs.each do |ref|
    suffix = " = #{ref.default_value}" if ref.has_default_value?
    if ref.is_explicit?
      protected_properties_def += indent + "#{ref.type} #{ref.name}#{suffix};\n"
    elsif ref.is_implicit?
      private_properties_def   += indent + "#{ref.type} #{ref.name}#{suffix};\n"
    end
  end
  ## BEGIN Repeaters
  object.repeaters.each do |repeater|
    protected_properties_def += indent + "Comet::Repeater<#{repeater.list_type}, #{repeater.typename}> #{repeater.repeater_name};\n" 
  end
  if object.class == Repeater
    public_properties_def += indent + "#{object.value_type} #{object.value_name};\n"
  end
  ## END Repeaters
  ## BEGIN SLOTS
  # Slots emanate from the root object.
  if object.is_root?
    all_slots = object.slots
    object.recursively_collect_children.each do |child|
      all_slots += child.slots
    end
    all_slots.each do |slot|
      public_properties_def += indent + "Comet::SlotElement #{slot.slot_ref};\n"
    end
  else
    object.slots.each do |slot|
      public_properties_def += indent + "Comet::SlotElement& #{slot.slot_ref};\n"
    end
  end
  ## END SLOTS
  [public_properties_def, protected_properties_def, private_properties_def]
end

#indentObject



9
10
11
# File 'lib/comet-html/header-generator.rb', line 9

def indent
  "      "
end

#signaler_definitionObject



13
14
15
16
17
18
19
# File 'lib/comet-html/header-generator.rb', line 13

def signaler_definition
  return "" if object.implements_ibindable_view?
  # Signaler def. Must be a reference if the object isn't the root object
  signaler_type  = "Comet::Signal<std::string>"
  signaler_type += "&" unless object.parent.nil?
  "#{signaler_type} signaler;"
end