method_bind.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*************************************************************************/
  2. /* method_bind.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef METHOD_BIND_H
  31. #define METHOD_BIND_H
  32. #include "list.h"
  33. #include "method_ptrcall.h"
  34. #include "object.h"
  35. #include "variant.h"
  36. #include <stdio.h>
  37. /**
  38. @author Juan Linietsky <reduzio@gmail.com>
  39. */
  40. #ifdef DEBUG_ENABLED
  41. #define DEBUG_METHODS_ENABLED
  42. #endif
  43. enum MethodFlags {
  44. METHOD_FLAG_NORMAL = 1,
  45. METHOD_FLAG_EDITOR = 2,
  46. METHOD_FLAG_NOSCRIPT = 4,
  47. METHOD_FLAG_CONST = 8,
  48. METHOD_FLAG_REVERSE = 16, // used for events
  49. METHOD_FLAG_VIRTUAL = 32,
  50. METHOD_FLAG_FROM_SCRIPT = 64,
  51. METHOD_FLAGS_DEFAULT = METHOD_FLAG_NORMAL,
  52. };
  53. template <class T>
  54. struct VariantCaster {
  55. static _FORCE_INLINE_ T cast(const Variant &p_variant) {
  56. return p_variant;
  57. }
  58. };
  59. template <class T>
  60. struct VariantCaster<T &> {
  61. static _FORCE_INLINE_ T cast(const Variant &p_variant) {
  62. return p_variant;
  63. }
  64. };
  65. template <class T>
  66. struct VariantCaster<const T &> {
  67. static _FORCE_INLINE_ T cast(const Variant &p_variant) {
  68. return p_variant;
  69. }
  70. };
  71. #define _VC(m_idx) \
  72. (VariantCaster<P##m_idx>::cast((m_idx - 1) >= p_arg_count ? get_default_argument(m_idx - 1) : *p_args[m_idx - 1]))
  73. //SIMPLE_NUMERIC_TYPE is used to avoid a warning on Variant::get_type_for
  74. #ifdef PTRCALL_ENABLED
  75. #define VARIANT_ENUM_CAST(m_enum) \
  76. SIMPLE_NUMERIC_TYPE(m_enum); \
  77. template <> \
  78. struct VariantCaster<m_enum> { \
  79. \
  80. static _FORCE_INLINE_ m_enum cast(const Variant &p_variant) { \
  81. return (m_enum)p_variant.operator int(); \
  82. } \
  83. }; \
  84. template <> \
  85. struct PtrToArg<m_enum> { \
  86. _FORCE_INLINE_ static m_enum convert(const void *p_ptr) { \
  87. return m_enum(*reinterpret_cast<const int *>(p_ptr)); \
  88. } \
  89. _FORCE_INLINE_ static void encode(m_enum p_val, const void *p_ptr) { \
  90. *(int *)p_ptr = p_val; \
  91. } \
  92. };
  93. #else
  94. #define VARIANT_ENUM_CAST(m_enum) \
  95. SIMPLE_NUMERIC_TYPE(m_enum); \
  96. template <> \
  97. struct VariantCaster<m_enum> { \
  98. \
  99. static _FORCE_INLINE_ m_enum cast(const Variant &p_variant) { \
  100. return (m_enum)p_variant.operator int(); \
  101. } \
  102. };
  103. #endif
  104. #define CHECK_ARG(m_arg) \
  105. if ((m_arg - 1) < p_arg_count) { \
  106. Variant::Type argtype = get_argument_type(m_arg - 1); \
  107. if (!Variant::can_convert_strict(p_args[m_arg - 1]->get_type(), argtype)) { \
  108. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; \
  109. r_error.argument = m_arg - 1; \
  110. r_error.expected = argtype; \
  111. return Variant(); \
  112. } \
  113. }
  114. #define CHECK_NOARG(m_arg) \
  115. { \
  116. if (p_arg##m_arg.get_type() != Variant::NIL) { \
  117. if (r_argerror) *r_argerror = (m_arg - 1); \
  118. return CALL_ERROR_EXTRA_ARGUMENT; \
  119. } \
  120. }
  121. // some helpers
  122. VARIANT_ENUM_CAST(Vector3::Axis);
  123. VARIANT_ENUM_CAST(Image::Format);
  124. VARIANT_ENUM_CAST(Error);
  125. VARIANT_ENUM_CAST(wchar_t);
  126. VARIANT_ENUM_CAST(Margin);
  127. VARIANT_ENUM_CAST(Orientation);
  128. VARIANT_ENUM_CAST(HAlign);
  129. class MethodBind {
  130. int method_id;
  131. uint32_t hint_flags;
  132. StringName name;
  133. Vector<Variant> default_arguments;
  134. int default_argument_count;
  135. int argument_count;
  136. #ifdef DEBUG_METHODS_ENABLED
  137. Vector<StringName> arg_names;
  138. Variant::Type *argument_types;
  139. StringName ret_type;
  140. #endif
  141. bool _const;
  142. protected:
  143. void _set_const(bool p_const);
  144. #ifdef DEBUG_METHODS_ENABLED
  145. virtual Variant::Type _gen_argument_type(int p_arg) const = 0;
  146. void _generate_argument_types(int p_count);
  147. void set_argument_types(Variant::Type *p_types) { argument_types = p_types; }
  148. #endif
  149. void set_argument_count(int p_count) { argument_count = p_count; }
  150. public:
  151. Vector<Variant> get_default_arguments() const { return default_arguments; }
  152. _FORCE_INLINE_ int get_default_argument_count() const { return default_argument_count; }
  153. _FORCE_INLINE_ Variant has_default_argument(int p_arg) const {
  154. int idx = argument_count - p_arg - 1;
  155. if (idx < 0 || idx >= default_arguments.size())
  156. return false;
  157. else
  158. return true;
  159. }
  160. _FORCE_INLINE_ Variant get_default_argument(int p_arg) const {
  161. int idx = argument_count - p_arg - 1;
  162. if (idx < 0 || idx >= default_arguments.size())
  163. return Variant();
  164. else
  165. return default_arguments[idx];
  166. }
  167. #ifdef DEBUG_METHODS_ENABLED
  168. _FORCE_INLINE_ void set_return_type(const StringName &p_type) { ret_type = p_type; }
  169. _FORCE_INLINE_ StringName get_return_type() const { return ret_type; }
  170. _FORCE_INLINE_ Variant::Type get_argument_type(int p_argument) const {
  171. ERR_FAIL_COND_V(p_argument < -1 || p_argument > argument_count, Variant::NIL);
  172. return argument_types[p_argument + 1];
  173. }
  174. PropertyInfo get_argument_info(int p_argument) const;
  175. void set_argument_names(const Vector<StringName> &p_names);
  176. Vector<StringName> get_argument_names() const;
  177. #endif
  178. void set_hint_flags(uint32_t p_hint) { hint_flags = p_hint; }
  179. uint32_t get_hint_flags() const { return hint_flags | (is_const() ? METHOD_FLAG_CONST : 0); }
  180. virtual String get_instance_type() const = 0;
  181. _FORCE_INLINE_ int get_argument_count() const { return argument_count; };
  182. #if 0
  183. _FORCE_INLINE_ Variant call_safe(const Variant** p_args,int p_arg_count, Variant::CallError& r_error) {
  184. r_error.error=Variant::CallError::CALL_OK;
  185. check_call( p_args, &errorarg );
  186. if (!err)
  187. return call(p_object, VARIANT_ARG_PASS );
  188. VARIANT_ARGPTRS
  189. String errstr;
  190. String methodname = get_instance_type()+"::"+name;
  191. if (err==CALL_ERROR_ARGUMENT_TYPE) {
  192. errstr="Invalid Argument to call: '"+methodname+"'. Cannot convert argument "+itos(errorarg+1)+" from "+Variant::get_type_name(get_argument_type(errorarg))+" to "+Variant::get_type_name(argptr[errorarg]->get_type())+".";
  193. }
  194. if (err==CALL_ERROR_EXTRA_ARGUMENT) {
  195. errstr="Invalid call. Member function '"+methodname+"' takes "+itos(get_argument_count())+" argument, but argument "+itos(errorarg+1)+" was received.";
  196. }
  197. ERR_PRINT(errstr.ascii().get_data());
  198. return Variant();
  199. }
  200. #endif
  201. virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Variant::CallError &r_error) = 0;
  202. #ifdef PTRCALL_ENABLED
  203. virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) = 0;
  204. #endif
  205. StringName get_name() const;
  206. void set_name(const StringName &p_name);
  207. _FORCE_INLINE_ int get_method_id() const { return method_id; }
  208. _FORCE_INLINE_ bool is_const() const { return _const; }
  209. void set_default_arguments(const Vector<Variant> &p_defargs);
  210. MethodBind();
  211. virtual ~MethodBind();
  212. };
  213. template <class T>
  214. class MethodBindNative : public MethodBind {
  215. public:
  216. typedef Variant (T::*NativeCall)(const Variant **, int, Variant::CallError &);
  217. protected:
  218. NativeCall call_method;
  219. public:
  220. virtual Variant::Type _gen_argument_type(int p_arg) const {
  221. return Variant::NIL;
  222. }
  223. virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Variant::CallError &r_error) {
  224. T *instance = static_cast<T *>(p_object);
  225. return (instance->*call_method)(p_args, p_arg_count, r_error);
  226. }
  227. void set_method_info(const MethodInfo &p_info) {
  228. set_argument_count(p_info.arguments.size());
  229. #ifdef DEBUG_METHODS_ENABLED
  230. Variant::Type *at = memnew_arr(Variant::Type, p_info.arguments.size() + 1);
  231. at[0] = p_info.return_val.type;
  232. if (p_info.arguments.size()) {
  233. Vector<StringName> names;
  234. names.resize(p_info.arguments.size());
  235. for (int i = 0; i < p_info.arguments.size(); i++) {
  236. at[i + 1] = p_info.arguments[i].type;
  237. names[i] = p_info.arguments[i].name;
  238. }
  239. set_argument_names(names);
  240. }
  241. set_argument_types(at);
  242. #endif
  243. }
  244. #ifdef PTRCALL_ENABLED
  245. virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) {} //todo
  246. #endif
  247. void set_method(NativeCall p_method) { call_method = p_method; }
  248. virtual bool is_const() const { return false; }
  249. virtual String get_instance_type() const { return T::get_type_static(); }
  250. MethodBindNative() { call_method = NULL; }
  251. };
  252. template <class T>
  253. MethodBind *create_native_method_bind(Variant (T::*p_method)(const Variant **, int, Variant::CallError &), const MethodInfo &p_info) {
  254. MethodBindNative<T> *a = memnew((MethodBindNative<T>));
  255. a->set_method(p_method);
  256. a->set_method_info(p_info);
  257. return a;
  258. }
  259. /** This amazing hack is based on the FastDelegates theory */
  260. // tale of an amazing hack.. //
  261. // if you declare an nonexistent class..
  262. class __UnexistingClass;
  263. #include "method_bind.gen.inc"
  264. #endif