FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
dispatch_gen.py
1 # ############################################################
2 # Original Author: Georg Fritzsche
3 #
4 # Created: November 6, 2009
5 # License: Dual license model; choose one of two:
6 # New BSD License
7 # http://www.opensource.org/licenses/bsd-license.php
8 # - or -
9 # GNU Lesser General Public License, version 2.1
10 # http://www.gnu.org/licenses/lgpl-2.1.html
11 #
12 # Copyright 2009 Georg Fritzsche, Firebreath development team
13 # ############################################################
14 
15 #!/usr/bin/python
16 
17 import time
18 from datetime import date
19 
20 def tern(a,b,c):
21  i = 0
22  if(a): i = 1
23  return (b,c)[i]
24 
25 # ######################################
26 
27 max_args = 10
28 tab = "\t"
29 endl = "\n"
30 headerFileName = "MethodConverter.h"
31 includeGuardName = "METHOD_CONVERTER_H"
32 methodWrapStructName = "method_wrapper"
33 makeWrapperFunctionName = "make_method"
34 callMethodFunctorName = "FB::CallMethodFunctor"
35 
36 indent = 0;
37 def ind_in():
38  global indent
39  indent += 1
40 def ind_out():
41  global indent
42  indent -= 1
43 def ind():
44  return tab*indent
45 
46 # ######################################
47 
48 f = open(headerFileName, "w")
49 
50 def wl(s):
51  f.write(ind()+s+endl)
52 
53 # legal
54 
55 wl("/**********************************************************\\")
56 wl("Original Author: Georg Fritzsche")
57 wl("Generated on: " + date.today().isoformat())
58 wl("License: Dual license model; choose one of two:")
59 wl(" New BSD License")
60 wl(" http://www.opensource.org/licenses/bsd-license.php")
61 wl(" - or -")
62 wl(" GNU Lesser General Public License, version 2.1")
63 wl(" http://www.gnu.org/licenses/lgpl-2.1.html")
64 wl("")
65 wl("Copyright 2009 Georg Fritzsche, Firebreath development team")
66 wl("\\**********************************************************/")
67 wl("")
68 
69 # start
70 
71 wl("")
72 wl("#if !defined("+includeGuardName+")")
73 wl("#define "+includeGuardName)
74 wl("")
75 wl("#if defined(_MSC_VER)")
76 wl("# pragma once")
77 wl("#endif")
78 wl("")
79 
80 # includes
81 
82 wl("#include <boost/function.hpp>")
83 wl("#include <boost/bind.hpp>")
84 wl("#include \"ConverterUtils.h\"")
85 wl("")
86 
87 # prologue
88 
89 wl("namespace FB")
90 wl("{")
91 ind_in()
92 
93 # wrapper
94 
95 wl("namespace detail { namespace methods")
96 wl("{")
97 ind_in()
98 
99 wl("using FB::detail::plain_type;")
100 wl("using FB::convertArgument;")
101 wl("")
102 
103 for i in range(max_args+1):
104  for voidReturn in [False, True]:
105  s = "template<class C"
106  if not voidReturn:
107  s += ", class R"
108  for i2 in range(i):
109  s += ", class T"+str(i2)
110  wl(s + ", class F>")
111 
112  s = "struct "+methodWrapStructName+str(i)
113  if voidReturn: # specializing on void return type
114  s += "<C, void"
115  for i2 in range(i):
116  s += ", T"+str(i2)
117  s += ", F>"
118  wl(s)
119 
120  wl("{")
121  ind_in()
122 
123  wl("typedef FB::variant result_type;")
124  wl("F f;")
125  wl(methodWrapStructName+str(i)+"(F f) : f(f) {}")
126 
127  wl("FB::variant operator()(C* instance, const FB::VariantList& in)")
128  wl("{")
129  ind_in()
130  if i<1:
131  wl("if(in.size() != 0)")
132  else:
133  wl("typedef typename plain_type<T"+str(i-1)+">::type TLast;")
134  wl("if(!checkArgumentCount<TLast>(in, "+str(i)+"))")
135  wl(tab+"return FB::variant();")
136 
137  if voidReturn: # specializing on void return type
138  s = "(instance->*f)("
139  else:
140  s = "return (instance->*f)(";
141  # if i>0:
142  # s+="in[0].convert_cast<typename FB::detail::plain_type<T0>::type>()"
143  # for i2 in range(1,i):
144  # s+= ", in["+str(i2)+"].convert_cast<typename FB::detail::plain_type<T"+str(i2)+">::type>()"
145  if i<1:
146  wl(s+");")
147  else:
148  wl(s)
149  ind_in()
150  s = "convertArgument<typename plain_type<T0>::type>(in[0], 1)"
151  if i>1:
152  for i2 in range(1,i-1):
153  wl(s+",")
154  s = "convertArgument<typename plain_type<T"+str(i2)+">::type>(in["+str(i2)+"], "+str(i2+1)+")"
155  wl(s+",")
156  wl("convertLastArgument<TLast>(in, "+str(i)+"));")
157  ind_out()
158  if voidReturn: # specializing on void return type
159  wl("return FB::variant();")
160  ind_out()
161  wl("}")
162  ind_out()
163  wl("};")
164 
165 ind_out()
166 wl("} } // namespace detail::methods")
167 wl("")
168 
169 # make_wrapper
170 
171 for i in range(max_args+1):
172  for constness in ['', ' const']:
173  typenames = ""
174  if i>0:
175  typenames += "class T0"
176  for i2 in range(1,i):
177  typenames += ", class T"+str(i2)
178  typenames_app = ""
179  if i>0:
180  typenames_app = ", "+typenames
181  types = ""
182  if i>0:
183  types += "T0"
184  for i2 in range(1,i):
185  types += ", T"+str(i2)
186  print " * "+types
187  types_app = ""
188  if i>0:
189  types_app = ", "+types
190 
191  wl("template<class C, class R"+typenames_app+">")
192  wl("inline "+callMethodFunctorName)
193  wl(makeWrapperFunctionName+"(C* instance, R (C::*function)("+types+")"+constness+")")
194  wl("{")
195  ind_in()
196  wl("return boost::bind(FB::detail::methods::"+methodWrapStructName+str(i)+"<C, R"+types_app+", R (C::*)("+types+")"+constness+">(function), instance, _1);")
197  ind_out()
198  wl("}")
199  wl("")
200 
201 # epilogue
202 
203 ind_out()
204 wl("} // namespace FB")
205 wl("")
206 
207 wl("#endif //"+includeGuardName)
208 wl("")
209 
210 f.close()