method_bind.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**************************************************************************/
  2. /* method_bind.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. static _FORCE_INLINE_ m_enum cast(const Variant &p_variant) { \
  78. return (m_enum)p_variant.operator int(); \
  79. } \
  80. }; \
  81. template <> \
  82. struct PtrToArg<m_enum> { \
  83. _FORCE_INLINE_ static m_enum convert(const void *p_ptr) { \
  84. return m_enum(*reinterpret_cast<const int *>(p_ptr)); \
  85. } \
  86. _FORCE_INLINE_ static void encode(m_enum p_val, const void *p_ptr) { \
  87. *(int *)p_ptr = p_val; \
  88. } \
  89. };
  90. #else
  91. #define VARIANT_ENUM_CAST(m_enum) \
  92. MAKE_ENUM_TYPE_INFO(m_enum) \
  93. template <> \
  94. struct VariantCaster<m_enum> { \
  95. static _FORCE_INLINE_ m_enum cast(const Variant &p_variant) { \
  96. return (m_enum)p_variant.operator int(); \
  97. } \
  98. };
  99. #endif
  100. // Object enum casts must go here
  101. VARIANT_ENUM_CAST(Object::ConnectFlags);
  102. template <typename T>
  103. struct VariantObjectClassChecker {
  104. static _FORCE_INLINE_ bool check(const Variant &p_variant) {
  105. return true;
  106. }
  107. };
  108. template <>
  109. struct VariantObjectClassChecker<Node *> {
  110. static _FORCE_INLINE_ bool check(const Variant &p_variant) {
  111. Object *obj = p_variant;
  112. Node *node = p_variant;
  113. return node || !obj;
  114. }
  115. };
  116. template <>
  117. struct VariantObjectClassChecker<Control *> {
  118. static _FORCE_INLINE_ bool check(const Variant &p_variant) {
  119. Object *obj = p_variant;
  120. Control *control = p_variant;
  121. return control || !obj;
  122. }
  123. };
  124. #define CHECK_ARG(m_arg) \
  125. if ((m_arg - 1) < p_arg_count) { \
  126. Variant::Type argtype = get_argument_type(m_arg - 1); \
  127. if (!Variant::can_convert_strict(p_args[m_arg - 1]->get_type(), argtype) || \
  128. !VariantObjectClassChecker<P##m_arg>::check(*p_args[m_arg - 1])) { \
  129. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; \
  130. r_error.argument = m_arg - 1; \
  131. r_error.expected = argtype; \
  132. return Variant(); \
  133. } \
  134. }
  135. #define CHECK_NOARG(m_arg) \
  136. { \
  137. if (p_arg##m_arg.get_type() != Variant::NIL) { \
  138. if (r_argerror) \
  139. *r_argerror = (m_arg - 1); \
  140. return CALL_ERROR_EXTRA_ARGUMENT; \
  141. } \
  142. }
  143. // some helpers
  144. VARIANT_ENUM_CAST(Vector3::Axis);
  145. VARIANT_ENUM_CAST(Error);
  146. VARIANT_ENUM_CAST(Margin);
  147. VARIANT_ENUM_CAST(Corner);
  148. VARIANT_ENUM_CAST(Orientation);
  149. VARIANT_ENUM_CAST(HAlign);
  150. VARIANT_ENUM_CAST(VAlign);
  151. VARIANT_ENUM_CAST(PropertyHint);
  152. VARIANT_ENUM_CAST(PropertyUsageFlags);
  153. VARIANT_ENUM_CAST(MethodFlags);
  154. VARIANT_ENUM_CAST(Variant::Type);
  155. VARIANT_ENUM_CAST(Variant::Operator);
  156. template <>
  157. struct VariantCaster<wchar_t> {
  158. static _FORCE_INLINE_ wchar_t cast(const Variant &p_variant) {
  159. return (wchar_t)p_variant.operator int();
  160. }
  161. };
  162. #ifdef PTRCALL_ENABLED
  163. template <>
  164. struct PtrToArg<wchar_t> {
  165. _FORCE_INLINE_ static wchar_t convert(const void *p_ptr) {
  166. return wchar_t(*reinterpret_cast<const int *>(p_ptr));
  167. }
  168. _FORCE_INLINE_ static void encode(wchar_t p_val, const void *p_ptr) {
  169. *(int *)p_ptr = p_val;
  170. }
  171. };
  172. #endif
  173. class MethodBind {
  174. int method_id;
  175. uint32_t hint_flags;
  176. StringName name;
  177. Vector<Variant> default_arguments;
  178. int default_argument_count;
  179. int argument_count;
  180. bool _const;
  181. bool _returns;
  182. protected:
  183. Variant::Type *argument_types;
  184. #ifdef DEBUG_METHODS_ENABLED
  185. Vector<StringName> arg_names;
  186. #endif
  187. void _set_const(bool p_const);
  188. void _set_returns(bool p_returns);
  189. virtual Variant::Type _gen_argument_type(int p_arg) const = 0;
  190. virtual PropertyInfo _gen_argument_type_info(int p_arg) const = 0;
  191. void _generate_argument_types(int p_count);
  192. void set_argument_count(int p_count) { argument_count = p_count; }
  193. public:
  194. Vector<Variant> get_default_arguments() const { return default_arguments; }
  195. _FORCE_INLINE_ int get_default_argument_count() const { return default_argument_count; }
  196. _FORCE_INLINE_ Variant has_default_argument(int p_arg) const {
  197. int idx = argument_count - p_arg - 1;
  198. if (idx < 0 || idx >= default_arguments.size()) {
  199. return false;
  200. } else {
  201. return true;
  202. }
  203. }
  204. _FORCE_INLINE_ Variant get_default_argument(int p_arg) const {
  205. int idx = argument_count - p_arg - 1;
  206. if (idx < 0 || idx >= default_arguments.size()) {
  207. return Variant();
  208. } else {
  209. return default_arguments[idx];
  210. }
  211. }
  212. _FORCE_INLINE_ Variant::Type get_argument_type(int p_argument) const {
  213. ERR_FAIL_COND_V(p_argument < -1 || p_argument > argument_count, Variant::NIL);
  214. return argument_types[p_argument + 1];
  215. }
  216. PropertyInfo get_argument_info(int p_argument) const;
  217. PropertyInfo get_return_info() const;
  218. #ifdef DEBUG_METHODS_ENABLED
  219. void set_argument_names(const Vector<StringName> &p_names); //set by class, db, can't be inferred otherwise
  220. Vector<StringName> get_argument_names() const;
  221. virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const = 0;
  222. #endif
  223. void set_hint_flags(uint32_t p_hint) { hint_flags = p_hint; }
  224. uint32_t get_hint_flags() const { return hint_flags | (is_const() ? METHOD_FLAG_CONST : 0) | (is_vararg() ? METHOD_FLAG_VARARG : 0); }
  225. virtual String get_instance_class() const = 0;
  226. _FORCE_INLINE_ int get_argument_count() const { return argument_count; };
  227. virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Variant::CallError &r_error) = 0;
  228. #ifdef PTRCALL_ENABLED
  229. virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) = 0;
  230. #endif
  231. StringName get_name() const;
  232. void set_name(const StringName &p_name);
  233. _FORCE_INLINE_ int get_method_id() const { return method_id; }
  234. _FORCE_INLINE_ bool is_const() const { return _const; }
  235. _FORCE_INLINE_ bool has_return() const { return _returns; }
  236. virtual bool is_vararg() const { return false; }
  237. void set_default_arguments(const Vector<Variant> &p_defargs);
  238. MethodBind();
  239. virtual ~MethodBind();
  240. };
  241. template <class T>
  242. class MethodBindVarArg : public MethodBind {
  243. public:
  244. typedef Variant (T::*NativeCall)(const Variant **, int, Variant::CallError &);
  245. protected:
  246. NativeCall call_method;
  247. MethodInfo arguments;
  248. public:
  249. virtual PropertyInfo _gen_argument_type_info(int p_arg) const {
  250. if (p_arg < 0) {
  251. return arguments.return_val;
  252. } else if (p_arg < arguments.arguments.size()) {
  253. return arguments.arguments[p_arg];
  254. } else {
  255. return PropertyInfo(Variant::NIL, "arg_" + itos(p_arg), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT);
  256. }
  257. }
  258. virtual Variant::Type _gen_argument_type(int p_arg) const {
  259. return _gen_argument_type_info(p_arg).type;
  260. }
  261. #ifdef DEBUG_METHODS_ENABLED
  262. virtual GodotTypeInfo::Metadata get_argument_meta(int) const {
  263. return GodotTypeInfo::METADATA_NONE;
  264. }
  265. #endif
  266. virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Variant::CallError &r_error) {
  267. T *instance = static_cast<T *>(p_object);
  268. return (instance->*call_method)(p_args, p_arg_count, r_error);
  269. }
  270. void set_method_info(const MethodInfo &p_info, bool p_return_nil_is_variant) {
  271. set_argument_count(p_info.arguments.size());
  272. Variant::Type *at = memnew_arr(Variant::Type, p_info.arguments.size() + 1);
  273. at[0] = p_info.return_val.type;
  274. if (p_info.arguments.size()) {
  275. #ifdef DEBUG_METHODS_ENABLED
  276. Vector<StringName> names;
  277. names.resize(p_info.arguments.size());
  278. #endif
  279. for (int i = 0; i < p_info.arguments.size(); i++) {
  280. at[i + 1] = p_info.arguments[i].type;
  281. #ifdef DEBUG_METHODS_ENABLED
  282. names.write[i] = p_info.arguments[i].name;
  283. #endif
  284. }
  285. #ifdef DEBUG_METHODS_ENABLED
  286. set_argument_names(names);
  287. #endif
  288. }
  289. argument_types = at;
  290. arguments = p_info;
  291. if (p_return_nil_is_variant) {
  292. arguments.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  293. }
  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 = nullptr;
  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, bool p_return_nil_is_variant) {
  311. MethodBindVarArg<T> *a = memnew((MethodBindVarArg<T>));
  312. a->set_method(p_method);
  313. a->set_method_info(p_info, p_return_nil_is_variant);
  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 // METHOD_BIND_H