method_bind.h 13 KB

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