method_bind.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*************************************************************************/
  2. /* method_bind.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "core/list.h"
  33. #include "core/method_ptrcall.h"
  34. #include "core/object.h"
  35. #include "core/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. #include "core/type_info.h"
  44. enum MethodFlags {
  45. METHOD_FLAG_NORMAL = 1,
  46. METHOD_FLAG_EDITOR = 2,
  47. METHOD_FLAG_NOSCRIPT = 4,
  48. METHOD_FLAG_CONST = 8,
  49. METHOD_FLAG_REVERSE = 16, // used for events
  50. METHOD_FLAG_VIRTUAL = 32,
  51. METHOD_FLAG_FROM_SCRIPT = 64,
  52. METHOD_FLAG_VARARG = 128,
  53. METHOD_FLAGS_DEFAULT = METHOD_FLAG_NORMAL,
  54. };
  55. template <class T>
  56. struct VariantCaster {
  57. static _FORCE_INLINE_ T cast(const Variant &p_variant) {
  58. return p_variant;
  59. }
  60. };
  61. template <class T>
  62. struct VariantCaster<T &> {
  63. static _FORCE_INLINE_ T cast(const Variant &p_variant) {
  64. return p_variant;
  65. }
  66. };
  67. template <class T>
  68. struct VariantCaster<const T &> {
  69. static _FORCE_INLINE_ T cast(const Variant &p_variant) {
  70. return p_variant;
  71. }
  72. };
  73. #define _VC(m_idx) \
  74. (VariantCaster<P##m_idx>::cast((m_idx - 1) >= p_arg_count ? get_default_argument(m_idx - 1) : *p_args[m_idx - 1]))
  75. #ifdef PTRCALL_ENABLED
  76. #define VARIANT_ENUM_CAST(m_enum) \
  77. MAKE_ENUM_TYPE_INFO(m_enum) \
  78. template <> \
  79. struct VariantCaster<m_enum> { \
  80. \
  81. static _FORCE_INLINE_ m_enum cast(const Variant &p_variant) { \
  82. return (m_enum)p_variant.operator int(); \
  83. } \
  84. }; \
  85. template <> \
  86. struct PtrToArg<m_enum> { \
  87. _FORCE_INLINE_ static m_enum convert(const void *p_ptr) { \
  88. return m_enum(*reinterpret_cast<const int *>(p_ptr)); \
  89. } \
  90. _FORCE_INLINE_ static void encode(m_enum p_val, const void *p_ptr) { \
  91. *(int *)p_ptr = p_val; \
  92. } \
  93. };
  94. #else
  95. #define VARIANT_ENUM_CAST(m_enum) \
  96. MAKE_ENUM_TYPE_INFO(m_enum) \
  97. template <> \
  98. struct VariantCaster<m_enum> { \
  99. \
  100. static _FORCE_INLINE_ m_enum cast(const Variant &p_variant) { \
  101. return (m_enum)p_variant.operator int(); \
  102. } \
  103. };
  104. #endif
  105. // Object enum casts must go here
  106. VARIANT_ENUM_CAST(Object::ConnectFlags);
  107. template <typename T>
  108. struct VariantObjectClassChecker {
  109. static _FORCE_INLINE_ bool check(const Variant &p_variant) {
  110. return true;
  111. }
  112. };
  113. template <>
  114. struct VariantObjectClassChecker<Node *> {
  115. static _FORCE_INLINE_ bool check(const Variant &p_variant) {
  116. Object *obj = p_variant;
  117. Node *node = p_variant;
  118. return node || !obj;
  119. }
  120. };
  121. template <>
  122. struct VariantObjectClassChecker<Control *> {
  123. static _FORCE_INLINE_ bool check(const Variant &p_variant) {
  124. Object *obj = p_variant;
  125. Control *control = p_variant;
  126. return control || !obj;
  127. }
  128. };
  129. #define CHECK_ARG(m_arg) \
  130. if ((m_arg - 1) < p_arg_count) { \
  131. Variant::Type argtype = get_argument_type(m_arg - 1); \
  132. if (!Variant::can_convert_strict(p_args[m_arg - 1]->get_type(), argtype) || \
  133. !VariantObjectClassChecker<P##m_arg>::check(*p_args[m_arg - 1])) { \
  134. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; \
  135. r_error.argument = m_arg - 1; \
  136. r_error.expected = argtype; \
  137. return Variant(); \
  138. } \
  139. }
  140. #define CHECK_NOARG(m_arg) \
  141. { \
  142. if (p_arg##m_arg.get_type() != Variant::NIL) { \
  143. if (r_argerror) *r_argerror = (m_arg - 1); \
  144. return CALL_ERROR_EXTRA_ARGUMENT; \
  145. } \
  146. }
  147. // some helpers
  148. VARIANT_ENUM_CAST(Vector3::Axis);
  149. VARIANT_ENUM_CAST(Error);
  150. VARIANT_ENUM_CAST(Margin);
  151. VARIANT_ENUM_CAST(Corner);
  152. VARIANT_ENUM_CAST(Orientation);
  153. VARIANT_ENUM_CAST(HAlign);
  154. VARIANT_ENUM_CAST(VAlign);
  155. VARIANT_ENUM_CAST(PropertyHint);
  156. VARIANT_ENUM_CAST(PropertyUsageFlags);
  157. VARIANT_ENUM_CAST(MethodFlags);
  158. VARIANT_ENUM_CAST(Variant::Type);
  159. VARIANT_ENUM_CAST(Variant::Operator);
  160. template <>
  161. struct VariantCaster<wchar_t> {
  162. static _FORCE_INLINE_ wchar_t cast(const Variant &p_variant) {
  163. return (wchar_t)p_variant.operator int();
  164. }
  165. };
  166. #ifdef PTRCALL_ENABLED
  167. template <>
  168. struct PtrToArg<wchar_t> {
  169. _FORCE_INLINE_ static wchar_t convert(const void *p_ptr) {
  170. return wchar_t(*reinterpret_cast<const int *>(p_ptr));
  171. }
  172. _FORCE_INLINE_ static void encode(wchar_t p_val, const void *p_ptr) {
  173. *(int *)p_ptr = p_val;
  174. }
  175. };
  176. #endif
  177. class MethodBind {
  178. int method_id;
  179. uint32_t hint_flags;
  180. StringName name;
  181. Vector<Variant> default_arguments;
  182. int default_argument_count;
  183. int argument_count;
  184. bool _const;
  185. bool _returns;
  186. protected:
  187. #ifdef DEBUG_METHODS_ENABLED
  188. Variant::Type *argument_types;
  189. Vector<StringName> arg_names;
  190. #endif
  191. void _set_const(bool p_const);
  192. void _set_returns(bool p_returns);
  193. #ifdef DEBUG_METHODS_ENABLED
  194. virtual Variant::Type _gen_argument_type(int p_arg) const = 0;
  195. virtual PropertyInfo _gen_argument_type_info(int p_arg) const = 0;
  196. void _generate_argument_types(int p_count);
  197. #endif
  198. void set_argument_count(int p_count) { argument_count = p_count; }
  199. public:
  200. Vector<Variant> get_default_arguments() const { return default_arguments; }
  201. _FORCE_INLINE_ int get_default_argument_count() const { return default_argument_count; }
  202. _FORCE_INLINE_ Variant has_default_argument(int p_arg) const {
  203. int idx = argument_count - p_arg - 1;
  204. if (idx < 0 || idx >= default_arguments.size())
  205. return false;
  206. else
  207. return true;
  208. }
  209. _FORCE_INLINE_ Variant get_default_argument(int p_arg) const {
  210. int idx = argument_count - p_arg - 1;
  211. if (idx < 0 || idx >= default_arguments.size())
  212. return Variant();
  213. else
  214. return default_arguments[idx];
  215. }
  216. #ifdef DEBUG_METHODS_ENABLED
  217. _FORCE_INLINE_ Variant::Type get_argument_type(int p_argument) const {
  218. ERR_FAIL_COND_V(p_argument < -1 || p_argument > argument_count, Variant::NIL);
  219. return argument_types[p_argument + 1];
  220. }
  221. PropertyInfo get_argument_info(int p_argument) const;
  222. PropertyInfo get_return_info() const;
  223. void set_argument_names(const Vector<StringName> &p_names); //set by class, db, can't be inferred otherwise
  224. Vector<StringName> get_argument_names() const;
  225. #endif
  226. void set_hint_flags(uint32_t p_hint) { hint_flags = p_hint; }
  227. uint32_t get_hint_flags() const { return hint_flags | (is_const() ? METHOD_FLAG_CONST : 0) | (is_vararg() ? METHOD_FLAG_VARARG : 0); }
  228. virtual String get_instance_class() const = 0;
  229. _FORCE_INLINE_ int get_argument_count() const { return argument_count; };
  230. virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Variant::CallError &r_error) = 0;
  231. #ifdef PTRCALL_ENABLED
  232. virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) = 0;
  233. #endif
  234. StringName get_name() const;
  235. void set_name(const StringName &p_name);
  236. _FORCE_INLINE_ int get_method_id() const { return method_id; }
  237. _FORCE_INLINE_ bool is_const() const { return _const; }
  238. _FORCE_INLINE_ bool has_return() const { return _returns; }
  239. virtual bool is_vararg() const { return false; }
  240. void set_default_arguments(const Vector<Variant> &p_defargs);
  241. MethodBind();
  242. virtual ~MethodBind();
  243. };
  244. template <class T>
  245. class MethodBindVarArg : public MethodBind {
  246. public:
  247. typedef Variant (T::*NativeCall)(const Variant **, int, Variant::CallError &);
  248. protected:
  249. NativeCall call_method;
  250. #ifdef DEBUG_METHODS_ENABLED
  251. MethodInfo arguments;
  252. #endif
  253. public:
  254. #ifdef DEBUG_METHODS_ENABLED
  255. virtual PropertyInfo _gen_argument_type_info(int p_arg) const {
  256. if (p_arg < 0) {
  257. return arguments.return_val;
  258. } else if (p_arg < arguments.arguments.size()) {
  259. return arguments.arguments[p_arg];
  260. } else {
  261. return PropertyInfo(Variant::NIL, "arg_" + itos(p_arg), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT);
  262. }
  263. }
  264. virtual Variant::Type _gen_argument_type(int p_arg) const {
  265. return _gen_argument_type_info(p_arg).type;
  266. }
  267. #else
  268. virtual Variant::Type _gen_argument_type(int p_arg) const {
  269. return Variant::NIL;
  270. }
  271. #endif
  272. virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Variant::CallError &r_error) {
  273. T *instance = static_cast<T *>(p_object);
  274. return (instance->*call_method)(p_args, p_arg_count, r_error);
  275. }
  276. void set_method_info(const MethodInfo &p_info) {
  277. set_argument_count(p_info.arguments.size());
  278. #ifdef DEBUG_METHODS_ENABLED
  279. Variant::Type *at = memnew_arr(Variant::Type, p_info.arguments.size() + 1);
  280. at[0] = p_info.return_val.type;
  281. if (p_info.arguments.size()) {
  282. Vector<StringName> names;
  283. names.resize(p_info.arguments.size());
  284. for (int i = 0; i < p_info.arguments.size(); i++) {
  285. at[i + 1] = p_info.arguments[i].type;
  286. names.write[i] = p_info.arguments[i].name;
  287. }
  288. set_argument_names(names);
  289. }
  290. argument_types = at;
  291. arguments = p_info;
  292. arguments.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  293. #endif
  294. }
  295. #ifdef PTRCALL_ENABLED
  296. virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) {
  297. ERR_FAIL(); //can't call
  298. } //todo
  299. #endif
  300. void set_method(NativeCall p_method) { call_method = p_method; }
  301. virtual bool is_const() const { return false; }
  302. virtual String get_instance_class() const { return T::get_class_static(); }
  303. virtual bool is_vararg() const { return true; }
  304. MethodBindVarArg() {
  305. call_method = NULL;
  306. _set_returns(true);
  307. }
  308. };
  309. template <class T>
  310. MethodBind *create_vararg_method_bind(Variant (T::*p_method)(const Variant **, int, Variant::CallError &), const MethodInfo &p_info) {
  311. MethodBindVarArg<T> *a = memnew((MethodBindVarArg<T>));
  312. a->set_method(p_method);
  313. a->set_method_info(p_info);
  314. return a;
  315. }
  316. /** This amazing hack is based on the FastDelegates theory */
  317. // tale of an amazing hack.. //
  318. // if you declare a nonexistent class..
  319. class __UnexistingClass;
  320. #include "method_bind.gen.inc"
  321. #endif