|
1 # This Source Code Form is subject to the terms of the Mozilla Public |
|
2 # License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
|
4 MAX_ARGS = 15 |
|
5 |
|
6 boilerplate = "/* This Source Code Form is subject to the terms of the Mozilla Public\n\ |
|
7 * License, v. 2.0. If a copy of the MPL was not distributed with this\n\ |
|
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */\n" |
|
9 |
|
10 def gen_args_type(args, member): |
|
11 if member: |
|
12 ret = ["C o"] |
|
13 else: |
|
14 ret = [] |
|
15 ret.append("M m") |
|
16 for arg in range(0, args): |
|
17 ret.append("A%d a%d"%(arg, arg)) |
|
18 return ", ".join(ret) |
|
19 |
|
20 def gen_args(args, member): |
|
21 if member: |
|
22 ret = ["o"] |
|
23 else: |
|
24 ret = [] |
|
25 ret.append("m") |
|
26 for arg in range(0, args): |
|
27 ret.append("a%d"%(arg)) |
|
28 return ", ".join(ret) |
|
29 |
|
30 def gen_args_(args): |
|
31 ret = [] |
|
32 for arg in range(0, args): |
|
33 ret.append("a%d_"%(arg)) |
|
34 return ", ".join(ret) |
|
35 |
|
36 def gen_init(args, r = False, member = False): |
|
37 if member: |
|
38 ret = ["o_(o)"] |
|
39 else: |
|
40 ret = [] |
|
41 ret.append("m_(m)") |
|
42 |
|
43 if r: |
|
44 ret.append("r_(r)") |
|
45 |
|
46 for arg in range(0, args): |
|
47 ret.append("a%d_(a%d)"%(arg, arg)) |
|
48 return ", ".join(ret) |
|
49 |
|
50 def gen_typenames(args, member): |
|
51 if member: |
|
52 ret = ["typename C"] |
|
53 else: |
|
54 ret = [] |
|
55 ret.append("typename M") |
|
56 |
|
57 for arg in range(0, args): |
|
58 ret.append("typename A%d"%(arg)) |
|
59 return ", ".join(ret) |
|
60 |
|
61 def gen_types(args, member): |
|
62 if member: |
|
63 ret = ["C"] |
|
64 else: |
|
65 ret = [] |
|
66 ret.append("M") |
|
67 for arg in range(0, args): |
|
68 ret.append("A%d"%(arg)) |
|
69 return ", ".join(ret) |
|
70 |
|
71 |
|
72 def generate_class_template(args, ret = False, member = True): |
|
73 print "// %d arguments --"%args |
|
74 if member: |
|
75 nm = "m" |
|
76 else: |
|
77 nm = "nm" |
|
78 |
|
79 if not ret: |
|
80 print "template<"+ gen_typenames(args, member) + "> class runnable_args_%s_%d : public runnable_args_base {"%(nm, args) |
|
81 else: |
|
82 print "template<"+ gen_typenames(args, member) + ", typename R> class runnable_args_%s_%d_ret : public runnable_args_base {"%(nm, args) |
|
83 |
|
84 print " public:" |
|
85 |
|
86 if not ret: |
|
87 print " runnable_args_%s_%d("%(nm, args) + gen_args_type(args, member) + ") :" |
|
88 print " " + gen_init(args, False, member) + " {}" |
|
89 else: |
|
90 print " runnable_args_%s_%d_ret("%(nm, args) + gen_args_type(args, member) + ", R *r) :" |
|
91 print " " + gen_init(args, True, member) + " {}" |
|
92 print " virtual bool returns_value() const { return true; }" |
|
93 print |
|
94 print " NS_IMETHOD Run() {" |
|
95 if ret: |
|
96 print " *r_ =", |
|
97 else: |
|
98 print " ", |
|
99 if member: |
|
100 print "((*o_).*m_)(" + gen_args_(args) + ");" |
|
101 else: |
|
102 print "m_(" + gen_args_(args) + ");" |
|
103 |
|
104 print " return NS_OK;" |
|
105 print " }" |
|
106 print |
|
107 print " private:" |
|
108 if member: |
|
109 print " C o_;" |
|
110 print " M m_;" |
|
111 if ret: |
|
112 print " R* r_;" |
|
113 for arg in range(0, args): |
|
114 print " A%d a%d_;"%(arg, arg) |
|
115 print "};" |
|
116 print |
|
117 print |
|
118 print |
|
119 |
|
120 def generate_function_template(args, member): |
|
121 if member: |
|
122 nm = "m" |
|
123 NM = ""; |
|
124 else: |
|
125 nm = "nm" |
|
126 NM = "NM"; |
|
127 |
|
128 print "// %d arguments --"%args |
|
129 print "template<" + gen_typenames(args, member) + ">" |
|
130 print "runnable_args_%s_%d<"%(nm, args) + gen_types(args, member) + ">* WrapRunnable%s("%NM + gen_args_type(args, member) + ") {" |
|
131 print " return new runnable_args_%s_%d<"%(nm, args) + gen_types(args, member) + ">" |
|
132 print " (" + gen_args(args, member) + ");" |
|
133 print "}" |
|
134 print |
|
135 |
|
136 def generate_function_template_ret(args, member): |
|
137 if member: |
|
138 nm = "m" |
|
139 NM = ""; |
|
140 else: |
|
141 nm = "nm" |
|
142 NM = "NM"; |
|
143 print "// %d arguments --"%args |
|
144 print "template<" + gen_typenames(args, member) + ", typename R>" |
|
145 print "runnable_args_%s_%d_ret<"%(nm, args) + gen_types(args, member) + ", R>* WrapRunnable%sRet("%NM + gen_args_type(args, member) + ", R* r) {" |
|
146 print " return new runnable_args_%s_%d_ret<"%(nm, args) + gen_types(args, member) + ", R>" |
|
147 print " (" + gen_args(args, member) + ", r);" |
|
148 print "}" |
|
149 print |
|
150 |
|
151 |
|
152 print boilerplate |
|
153 print |
|
154 |
|
155 for num_args in range (0, MAX_ARGS): |
|
156 generate_class_template(num_args, False, False) |
|
157 generate_class_template(num_args, True, False) |
|
158 generate_class_template(num_args, False, True) |
|
159 generate_class_template(num_args, True, True) |
|
160 |
|
161 |
|
162 print |
|
163 print |
|
164 print |
|
165 |
|
166 for num_args in range(0, MAX_ARGS): |
|
167 generate_function_template(num_args, False) |
|
168 generate_function_template_ret(num_args, False) |
|
169 generate_function_template(num_args, True) |
|
170 generate_function_template_ret(num_args, True) |
|
171 |
|
172 |
|
173 |