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/. 4MAX_ARGS = 15 5 6boilerplate = "/* 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 10includes = "#include \"RefCounted.h\"\n" 11 12refcountclass = "class RefCountTaskWrapper : public gmp_args_base {\n\ 13public:\n\ 14 RefCountTaskWrapper(GMPTask* aTask, RefCounted* aRefCounted)\n\ 15 : mTask(aTask)\n\ 16 , mRefCounted(aRefCounted)\n\ 17 {}\n\ 18 virtual void Run() {\n\ 19 mTask->Run();\n\ 20 }\n\ 21 virtual void Destroy() {\n\ 22 mTask->Destroy();\n\ 23 gmp_args_base::Destroy();\n\ 24 }\n\ 25private:\n\ 26 ~RefCountTaskWrapper() {}\n\ 27\n\ 28 GMPTask* mTask;\n\ 29 RefPtr<RefCounted> mRefCounted;\n\ 30};\n" 31 32 33def gen_args_type(args, member): 34 if member: 35 ret = ["C o"] 36 else: 37 ret = [] 38 ret.append("M m") 39 for arg in range(0, args): 40 ret.append("A%d a%d"%(arg, arg)) 41 return ", ".join(ret) 42 43def gen_args(args, member): 44 if member: 45 ret = ["o"] 46 else: 47 ret = [] 48 ret.append("m") 49 for arg in range(0, args): 50 ret.append("a%d"%(arg)) 51 return ", ".join(ret) 52 53def gen_args_(args): 54 ret = [] 55 for arg in range(0, args): 56 ret.append("a%d_"%(arg)) 57 return ", ".join(ret) 58 59def gen_init(args, r = False, member = False): 60 if member: 61 ret = ["o_ (o)"] 62 else: 63 ret = [] 64 ret.append("m_ (m)") 65 66 if r: 67 ret.append("r_ (r)") 68 69 for arg in range(0, args): 70 ret.append("a%d_ (a%d)"%(arg, arg)) 71 return ", ".join(ret) 72 73def gen_typenames(args, member): 74 if member: 75 ret = ["typename C"] 76 else: 77 ret = [] 78 ret.append("typename M") 79 80 for arg in range(0, args): 81 ret.append("typename A%d"%(arg)) 82 return ", ".join(ret) 83 84def gen_types(args, member): 85 if member: 86 ret = ["C"] 87 else: 88 ret = [] 89 ret.append("M") 90 for arg in range(0, args): 91 ret.append("A%d"%(arg)) 92 return ", ".join(ret) 93 94 95def generate_class_template(args, ret = False, member = True): 96 print "// %d arguments --"%args 97 if member: 98 nm = "m" 99 else: 100 nm = "nm" 101 102 if not ret: 103 print "template<"+ gen_typenames(args, member) + "> class gmp_args_%s_%d : public gmp_args_base {"%(nm, args) 104 else: 105 print "template<"+ gen_typenames(args, member) + ", typename R> class gmp_args_%s_%d_ret : public gmp_args_base {"%(nm, args) 106 107 print " public:" 108 109 if not ret: 110 print " gmp_args_%s_%d ("%(nm, args) + gen_args_type(args, member) + ") :" 111 print " " + gen_init(args, False, member) + " {}" 112 else: 113 print " gmp_args_%s_%d_ret ("%(nm, args) + gen_args_type(args, member) + ", R* r) :" 114 print " " + gen_init(args, True, member) + " {}" 115 print " virtual bool returns_value() const {\n return true;\n }" 116 print 117 print " void Run() {" 118 if ret: 119 print " *r_ =", 120 else: 121 print " ", 122 if member: 123 print "((*o_).*m_) (" + gen_args_(args) + ");" 124 else: 125 print "m_ (" + gen_args_(args) + ");" 126 print " }" 127 print 128 print " private:" 129 if member: 130 print " C o_;" 131 print " M m_;" 132 if ret: 133 print " R* r_;" 134 for arg in range(0, args): 135 print " A%d a%d_;"%(arg, arg) 136 print "};" 137 print 138 print 139 print 140 141def generate_function_template(args, member): 142 if member: 143 nm = "m" 144 NM = ""; 145 else: 146 nm = "nm" 147 NM = "NM"; 148 149 print "// %d arguments --"%args 150 print "template<" + gen_typenames(args, member) + ">" 151 print "gmp_args_%s_%d<"%(nm, args) + gen_types(args, member) + ">* WrapTask%s ("%NM + gen_args_type(args, member) + ") {" 152 print " return new gmp_args_%s_%d<"%(nm, args) + gen_types(args, member) + ">" 153 print " (" + gen_args(args, member) + ");" 154 print "}" 155 print 156 if member: 157 print "template<" + gen_typenames(args, member) + ">" 158 print "GMPTask*" 159 print "WrapTaskRefCounted%s ("%NM + gen_args_type(args, member) + ") {" 160 print " GMPTask *t = WrapTask%s ("%NM + gen_args(args, member) + ");" 161 print " return new RefCountTaskWrapper(t, o);" 162 print "}" 163 print 164 165def generate_function_template_ret(args, member): 166 if member: 167 nm = "m" 168 NM = ""; 169 else: 170 nm = "nm" 171 NM = "NM"; 172 print "// %d arguments --"%args 173 print "template<" + gen_typenames(args, member) + ", typename R>" 174 print "gmp_args_%s_%d_ret<"%(nm, args) + gen_types(args, member) + ", R>* WrapTask%sRet ("%NM + gen_args_type(args, member) + ", R* r) {" 175 print " return new gmp_args_%s_%d_ret<"%(nm, args) + gen_types(args, member) + ", R>" 176 print " (" + gen_args(args, member) + ", r);" 177 print "}" 178 print 179 180 181 182print boilerplate 183print 184print includes 185print 186print refcountclass 187print 188 189for num_args in range (0, MAX_ARGS): 190 generate_class_template(num_args, False, False) 191 generate_class_template(num_args, True, False) 192 generate_class_template(num_args, False, True) 193 generate_class_template(num_args, True, True) 194 195 196print 197print 198print 199 200for num_args in range(0, MAX_ARGS): 201 generate_function_template(num_args, False) 202 generate_function_template_ret(num_args, False) 203 generate_function_template(num_args, True) 204 generate_function_template_ret(num_args, True) 205 206 207 208