blob: 031c0335c62a75196068be349e0e3ab112f0cf1b [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001class Target::QueueDec < Target::Node
2 attr_accessor :name,:loc
3 def initialize(type,name)
4 @type,@name = type,name
5 end
6 def msg_hash()
7 return @type.msg_hash
8 end
brians343bc112013-02-10 01:53:46 +00009 def full_message_name(cpp_tree)
10 type = cpp_tree.get(@type)
11 return @type.loc.to_cpp_id(type.name)
12 end
brians343bc112013-02-10 01:53:46 +000013 def full_builder_name(cpp_tree)
14 return "::aos::MessageBuilder< #{full_message_name(cpp_tree)}>"
15 end
16 def full_type_name(cpp_tree)
17 return "::aos::Queue< #{full_message_name(cpp_tree)}>"
18 end
19 def type_name(cpp_tree,queue_type = "::aos::Queue")
20 type = cpp_tree.get(@type)
21 if(@type.loc == @loc) #use relative name
22 return "#{queue_type}<#{type.name}>"
23 else #use full name
24 return "#{queue_type}< #{@type.loc.to_cpp_id(type.name)}>"
25 end
26 end
27 def create_usage(cpp_tree)
28 "#{type_name(cpp_tree)} #{@name}"
29 end
30 def create(cpp_tree)
31 namespace = cpp_tree.get(@loc)
32 type = cpp_tree.get(@type)
33 cpp_tree.set(self,self)
34 type_name = type_name(cpp_tree)
35 full_type_name = full_type_name(cpp_tree)
36 namespace.add_cc("static #{type_name} *#{@name}_ptr")
37 counter = "#{@name}_counter"
38 namespace.add_cc("static int #{counter}")
39
40 str = <<COMMENT_END
41Schwarz counter to construct and destruct the #{@name} queue.
42Must be constructed before &#{@name} is constructed so that #{@name}_ptr
43is valid so we can store a reference to the object.
44COMMENT_END
45 str.split(/\n/).each do |str_sec|
46 namespace.class_comment(str_sec)
47 end
48 init_class = namespace.add_class("InitializerFor_" + @name)
49
50 init_class.add_cc_comment(
51 "The counter is initialized at load-time, i.e., before any of the static",
52 "objects are initialized.")
53
54
55 cons = CPP::Constructor.new(init_class)
56 init_class.add_member(:public,cons)
57 cons.suite << if_stmt = CPP::If.new("0 == #{counter}++")
brians343bc112013-02-10 01:53:46 +000058
59 cons_call = CPP::FuncCall.new("new #{type_name}")
60 cons_call.args.push(@loc.queue_name(@name).inspect)
61 if_stmt.suite << CPP::Assign.new("#{@name}_ptr",cons_call)
brians343bc112013-02-10 01:53:46 +000062
63
64 destruct = CPP::Destructor.new(init_class)
65 init_class.add_member(:public,destruct)
66 destruct.suite << if_stmt = CPP::If.new("0 == --#{counter}")
brians343bc112013-02-10 01:53:46 +000067 if_stmt.suite << "delete #{@name}_ptr"
68 if_stmt.suite << CPP::Assign.new("#{@name}_ptr","NULL")
69
70 init_class.static = true
71 init_class.dec = @name + "_initializer";
72
73 str = <<COMMENT_END
74Create a reference to the new object in the pointer. Since we have already
75created the initializer
76COMMENT_END
Brian Silverman04fdc232014-02-12 14:51:11 -080077 str.split(/\n/).map{|str_sec| CPP::Comment.new(str_sec)}.each do |comment|
78 namespace.add(comment)
79 end
80 namespace.add("static UNUSED_VARIABLE #{type_name} &#{@name}" +
81 " = #{@name}_initializer.get()")
brians343bc112013-02-10 01:53:46 +000082
83 get = init_class.def_func(full_type_name,"get")
84 get.pre_func_types = "&"
85 get.suite << CPP::Return.new("*#{@name}_ptr")
86
87 end
88end
89