make_binders.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. # -*- coding: ibm850 -*-
  2. template_typed = """
  3. #ifdef TYPED_METHOD_BIND
  4. template<class T $ifret ,class R$ $ifargs ,$ $arg, class P@$>
  5. class MethodBind$argc$$ifret R$$ifconst C$ : public MethodBind {
  6. public:
  7. $ifret R$ $ifnoret void$ (T::*method)($arg, P@$) $ifconst const$;
  8. virtual Variant::Type _gen_argument_type(int p_arg) const { return _get_argument_type(p_arg); }
  9. #ifdef DEBUG_METHODS_ENABLED
  10. virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const {
  11. $ifret if (p_arg==-1) return GetTypeInfo<R>::METADATA;$
  12. $arg if (p_arg==(@-1)) return GetTypeInfo<P@>::METADATA;
  13. $
  14. return GodotTypeInfo::METADATA_NONE;
  15. }
  16. #endif
  17. Variant::Type _get_argument_type(int p_argument) const {
  18. $ifret if (p_argument==-1) return (Variant::Type)GetTypeInfo<R>::VARIANT_TYPE;$
  19. $arg if (p_argument==(@-1)) return (Variant::Type)GetTypeInfo<P@>::VARIANT_TYPE;
  20. $
  21. return Variant::NIL;
  22. }
  23. virtual PropertyInfo _gen_argument_type_info(int p_argument) const {
  24. $ifret if (p_argument==-1) return GetTypeInfo<R>::get_class_info();$
  25. $arg if (p_argument==(@-1)) return GetTypeInfo<P@>::get_class_info();
  26. $
  27. return PropertyInfo();
  28. }
  29. virtual String get_instance_class() const {
  30. return T::get_class_static();
  31. }
  32. virtual Variant call(Object* p_object,const Variant** p_args,int p_arg_count, Variant::CallError& r_error) {
  33. T *instance=Object::cast_to<T>(p_object);
  34. r_error.error=Variant::CallError::CALL_OK;
  35. #ifdef DEBUG_METHODS_ENABLED
  36. ERR_FAIL_COND_V(!instance,Variant());
  37. if (p_arg_count>get_argument_count()) {
  38. r_error.error=Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  39. r_error.argument=get_argument_count();
  40. return Variant();
  41. }
  42. if (p_arg_count<(get_argument_count()-get_default_argument_count())) {
  43. r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  44. r_error.argument=get_argument_count()-get_default_argument_count();
  45. return Variant();
  46. }
  47. $arg CHECK_ARG(@);
  48. $
  49. #endif
  50. $ifret Variant ret = $(instance->*method)($arg, _VC(@)$);
  51. $ifret return Variant(ret);$
  52. $ifnoret return Variant();$
  53. }
  54. #ifdef PTRCALL_ENABLED
  55. virtual void ptrcall(Object*p_object,const void** p_args,void *r_ret) {
  56. T *instance=Object::cast_to<T>(p_object);
  57. $ifret PtrToArg<R>::encode( $ (instance->*method)($arg, PtrToArg<P@>::convert(p_args[@-1])$) $ifret ,r_ret)$ ;
  58. }
  59. #endif
  60. MethodBind$argc$$ifret R$$ifconst C$ () {
  61. #ifdef DEBUG_METHODS_ENABLED
  62. _set_const($ifconst true$$ifnoconst false$);
  63. #endif
  64. _generate_argument_types($argc$);
  65. $ifret _set_returns(true); $
  66. };
  67. };
  68. template<class T $ifret ,class R$ $ifargs ,$ $arg, class P@$>
  69. MethodBind* create_method_bind($ifret R$ $ifnoret void$ (T::*p_method)($arg, P@$) $ifconst const$ ) {
  70. MethodBind$argc$$ifret R$$ifconst C$<T $ifret ,R$ $ifargs ,$ $arg, P@$> * a = memnew( (MethodBind$argc$$ifret R$$ifconst C$<T $ifret ,R$ $ifargs ,$ $arg, P@$>) );
  71. a->method=p_method;
  72. return a;
  73. }
  74. #endif
  75. """
  76. template = """#pragma once
  77. #ifndef TYPED_METHOD_BIND
  78. $iftempl template<$ $ifret class R$ $ifretargs ,$ $arg, class P@$ $iftempl >$
  79. class MethodBind$argc$$ifret R$$ifconst C$ : public MethodBind {
  80. public:
  81. StringName type_name;
  82. $ifret R$ $ifnoret void$ (__UnexistingClass::*method)($arg, P@$) $ifconst const$;
  83. virtual Variant::Type _gen_argument_type(int p_arg) const { return _get_argument_type(p_arg); }
  84. #ifdef DEBUG_METHODS_ENABLED
  85. virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const {
  86. $ifret if (p_arg==-1) return GetTypeInfo<R>::METADATA;$
  87. $arg if (p_arg==(@-1)) return GetTypeInfo<P@>::METADATA;
  88. $
  89. return GodotTypeInfo::METADATA_NONE;
  90. }
  91. #endif
  92. Variant::Type _get_argument_type(int p_argument) const {
  93. $ifret if (p_argument==-1) return (Variant::Type)GetTypeInfo<R>::VARIANT_TYPE;$
  94. $arg if (p_argument==(@-1)) return (Variant::Type)GetTypeInfo<P@>::VARIANT_TYPE;
  95. $
  96. return Variant::NIL;
  97. }
  98. virtual PropertyInfo _gen_argument_type_info(int p_argument) const {
  99. $ifret if (p_argument==-1) return GetTypeInfo<R>::get_class_info();$
  100. $arg if (p_argument==(@-1)) return GetTypeInfo<P@>::get_class_info();
  101. $
  102. return PropertyInfo();
  103. }
  104. virtual String get_instance_class() const {
  105. return type_name;
  106. }
  107. virtual Variant call(Object* p_object,const Variant** p_args,int p_arg_count, Variant::CallError& r_error) {
  108. __UnexistingClass *instance = (__UnexistingClass*)p_object;
  109. r_error.error=Variant::CallError::CALL_OK;
  110. #ifdef DEBUG_METHODS_ENABLED
  111. ERR_FAIL_COND_V(!instance,Variant());
  112. if (p_arg_count>get_argument_count()) {
  113. r_error.error=Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  114. r_error.argument=get_argument_count();
  115. return Variant();
  116. }
  117. if (p_arg_count<(get_argument_count()-get_default_argument_count())) {
  118. r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  119. r_error.argument=get_argument_count()-get_default_argument_count();
  120. return Variant();
  121. }
  122. $arg CHECK_ARG(@);
  123. $
  124. #endif
  125. $ifret Variant ret = $(instance->*method)($arg, _VC(@)$);
  126. $ifret return Variant(ret);$
  127. $ifnoret return Variant();$
  128. }
  129. #ifdef PTRCALL_ENABLED
  130. virtual void ptrcall(Object*p_object,const void** p_args,void *r_ret) {
  131. __UnexistingClass *instance = (__UnexistingClass*)p_object;
  132. $ifret PtrToArg<R>::encode( $ (instance->*method)($arg, PtrToArg<P@>::convert(p_args[@-1])$) $ifret ,r_ret) $ ;
  133. }
  134. #endif
  135. MethodBind$argc$$ifret R$$ifconst C$ () {
  136. #ifdef DEBUG_METHODS_ENABLED
  137. _set_const($ifconst true$$ifnoconst false$);
  138. #endif
  139. _generate_argument_types($argc$);
  140. $ifret _set_returns(true); $
  141. };
  142. };
  143. template<class T $ifret ,class R$ $ifargs ,$ $arg, class P@$>
  144. MethodBind* create_method_bind($ifret R$ $ifnoret void$ (T::*p_method)($arg, P@$) $ifconst const$ ) {
  145. MethodBind$argc$$ifret R$$ifconst C$ $iftempl <$ $ifret R$ $ifretargs ,$ $arg, P@$ $iftempl >$ * a = memnew( (MethodBind$argc$$ifret R$$ifconst C$ $iftempl <$ $ifret R$ $ifretargs ,$ $arg, P@$ $iftempl >$) );
  146. union {
  147. $ifret R$ $ifnoret void$ (T::*sm)($arg, P@$) $ifconst const$;
  148. $ifret R$ $ifnoret void$ (__UnexistingClass::*dm)($arg, P@$) $ifconst const$;
  149. } u;
  150. u.sm=p_method;
  151. a->method=u.dm;
  152. a->type_name=T::get_class_static();
  153. return a;
  154. }
  155. #endif
  156. """
  157. template_typed_free_func = """
  158. #ifdef TYPED_METHOD_BIND
  159. template<class T $ifret ,class R$ $ifargs ,$ $arg, class P@$>
  160. class FunctionBind$argc$$ifret R$$ifconst C$ : public MethodBind {
  161. public:
  162. $ifret R$ $ifnoret void$ (*method) ($ifconst const$ T *$ifargs , $$arg, P@$);
  163. virtual Variant::Type _gen_argument_type(int p_arg) const { return _get_argument_type(p_arg); }
  164. virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const {
  165. $ifret if (p_arg==-1) return GetTypeInfo<R>::METADATA;$
  166. $arg if (p_arg==(@-1)) return GetTypeInfo<P@>::METADATA;
  167. $
  168. return GodotTypeInfo::METADATA_NONE;
  169. }
  170. Variant::Type _get_argument_type(int p_argument) const {
  171. $ifret if (p_argument==-1) return (Variant::Type)GetTypeInfo<R>::VARIANT_TYPE;$
  172. $arg if (p_argument==(@-1)) return (Variant::Type)GetTypeInfo<P@>::VARIANT_TYPE;
  173. $
  174. return Variant::NIL;
  175. }
  176. virtual PropertyInfo _gen_argument_type_info(int p_argument) const {
  177. $ifret if (p_argument==-1) return GetTypeInfo<R>::get_class_info();$
  178. $arg if (p_argument==(@-1)) return GetTypeInfo<P@>::get_class_info();
  179. $
  180. return PropertyInfo();
  181. }
  182. virtual String get_instance_class() const {
  183. return T::get_class_static();
  184. }
  185. virtual Variant call(Object* p_object,const Variant** p_args,int p_arg_count, Variant::CallError& r_error) {
  186. T *instance=Object::cast_to<T>(p_object);
  187. r_error.error=Variant::CallError::CALL_OK;
  188. #ifdef DEBUG_METHODS_ENABLED
  189. ERR_FAIL_COND_V(!instance,Variant());
  190. if (p_arg_count>get_argument_count()) {
  191. r_error.error=Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  192. r_error.argument=get_argument_count();
  193. return Variant();
  194. }
  195. if (p_arg_count<(get_argument_count()-get_default_argument_count())) {
  196. r_error.error=Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  197. r_error.argument=get_argument_count()-get_default_argument_count();
  198. return Variant();
  199. }
  200. $arg CHECK_ARG(@);
  201. $
  202. #endif
  203. $ifret Variant ret = $(method)(instance$ifargs , $$arg, _VC(@)$);
  204. $ifret return Variant(ret);$
  205. $ifnoret return Variant();$
  206. }
  207. #ifdef PTRCALL_ENABLED
  208. virtual void ptrcall(Object*p_object,const void** p_args,void *r_ret) {
  209. T *instance=Object::cast_to<T>(p_object);
  210. $ifret PtrToArg<R>::encode( $ (method)(instance$ifargs , $$arg, PtrToArg<P@>::convert(p_args[@-1])$) $ifret ,r_ret)$ ;
  211. }
  212. #endif
  213. FunctionBind$argc$$ifret R$$ifconst C$ () {
  214. #ifdef DEBUG_METHODS_ENABLED
  215. _set_const($ifconst true$$ifnoconst false$);
  216. #endif
  217. _generate_argument_types($argc$);
  218. $ifret _set_returns(true); $
  219. };
  220. };
  221. template<class T $ifret ,class R$ $ifargs ,$ $arg, class P@$>
  222. MethodBind* create_method_bind($ifret R$ $ifnoret void$ (*p_method)($ifconst const$ T *$ifargs , $$arg, P@$) ) {
  223. FunctionBind$argc$$ifret R$$ifconst C$<T $ifret ,R$ $ifargs ,$ $arg, P@$> * a = memnew( (FunctionBind$argc$$ifret R$$ifconst C$<T $ifret ,R$ $ifargs ,$ $arg, P@$>) );
  224. a->method=p_method;
  225. return a;
  226. }
  227. #endif
  228. """
  229. def make_version(template, nargs, argmax, const, ret):
  230. intext = template
  231. from_pos = 0
  232. outtext = ""
  233. while True:
  234. to_pos = intext.find("$", from_pos)
  235. if to_pos == -1:
  236. outtext += intext[from_pos:]
  237. break
  238. else:
  239. outtext += intext[from_pos:to_pos]
  240. end = intext.find("$", to_pos + 1)
  241. if end == -1:
  242. break # ignore
  243. macro = intext[to_pos + 1 : end]
  244. cmd = ""
  245. data = ""
  246. if macro.find(" ") != -1:
  247. cmd = macro[0 : macro.find(" ")]
  248. data = macro[macro.find(" ") + 1 :]
  249. else:
  250. cmd = macro
  251. if cmd == "argc":
  252. outtext += str(nargs)
  253. if cmd == "ifret" and ret:
  254. outtext += data
  255. if cmd == "ifargs" and nargs:
  256. outtext += data
  257. if cmd == "ifretargs" and nargs and ret:
  258. outtext += data
  259. if cmd == "ifconst" and const:
  260. outtext += data
  261. elif cmd == "ifnoconst" and not const:
  262. outtext += data
  263. elif cmd == "ifnoret" and not ret:
  264. outtext += data
  265. elif cmd == "iftempl" and (nargs > 0 or ret):
  266. outtext += data
  267. elif cmd == "arg,":
  268. for i in range(1, nargs + 1):
  269. if i > 1:
  270. outtext += ", "
  271. outtext += data.replace("@", str(i))
  272. elif cmd == "arg":
  273. for i in range(1, nargs + 1):
  274. outtext += data.replace("@", str(i))
  275. elif cmd == "noarg":
  276. for i in range(nargs + 1, argmax + 1):
  277. outtext += data.replace("@", str(i))
  278. from_pos = end + 1
  279. return outtext
  280. def run(target, source, env):
  281. versions = 13
  282. versions_ext = 6
  283. text = ""
  284. text_ext = ""
  285. text_free_func = "#ifndef METHOD_BIND_FREE_FUNC_H\n#define METHOD_BIND_FREE_FUNC_H\n"
  286. text_free_func += "\n//including this header file allows method binding to use free functions\n"
  287. text_free_func += (
  288. "//note that the free function must have a pointer to an instance of the class as its first parameter\n"
  289. )
  290. for i in range(0, versions + 1):
  291. t = ""
  292. t += make_version(template, i, versions, False, False)
  293. t += make_version(template_typed, i, versions, False, False)
  294. t += make_version(template, i, versions, False, True)
  295. t += make_version(template_typed, i, versions, False, True)
  296. t += make_version(template, i, versions, True, False)
  297. t += make_version(template_typed, i, versions, True, False)
  298. t += make_version(template, i, versions, True, True)
  299. t += make_version(template_typed, i, versions, True, True)
  300. if i >= versions_ext:
  301. text_ext += t
  302. else:
  303. text += t
  304. text_free_func += make_version(template_typed_free_func, i, versions, False, False)
  305. text_free_func += make_version(template_typed_free_func, i, versions, False, True)
  306. text_free_func += make_version(template_typed_free_func, i, versions, True, False)
  307. text_free_func += make_version(template_typed_free_func, i, versions, True, True)
  308. text_free_func += "#endif"
  309. with open(target[0], "w") as f:
  310. f.write(text)
  311. with open(target[1], "w") as f:
  312. f.write(text_ext)
  313. with open(target[2], "w") as f:
  314. f.write(text_free_func)
  315. if __name__ == "__main__":
  316. from platform_methods import subprocess_main
  317. subprocess_main(globals())