callable_method_pointer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*************************************************************************/
  2. /* callable_method_pointer.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 CALLABLE_METHOD_POINTER_H
  31. #define CALLABLE_METHOD_POINTER_H
  32. #include "core/callable.h"
  33. #include "core/hashfuncs.h"
  34. #include "core/object.h"
  35. #include "core/os/copymem.h"
  36. #include "core/simple_type.h"
  37. class CallableCustomMethodPointerBase : public CallableCustom {
  38. uint32_t *comp_ptr;
  39. uint32_t comp_size;
  40. uint32_t h;
  41. #ifdef DEBUG_METHODS_ENABLED
  42. const char *text = "";
  43. #endif
  44. static bool compare_equal(const CallableCustom *p_a, const CallableCustom *p_b);
  45. static bool compare_less(const CallableCustom *p_a, const CallableCustom *p_b);
  46. protected:
  47. void _setup(uint32_t *p_base_ptr, uint32_t p_ptr_size);
  48. public:
  49. #ifdef DEBUG_METHODS_ENABLED
  50. void set_text(const char *p_text) {
  51. text = p_text;
  52. }
  53. virtual String get_as_text() const {
  54. return text;
  55. }
  56. #else
  57. virtual String get_as_text() const {
  58. return String();
  59. }
  60. #endif
  61. virtual CompareEqualFunc get_compare_equal_func() const;
  62. virtual CompareLessFunc get_compare_less_func() const;
  63. virtual uint32_t hash() const;
  64. };
  65. #ifdef DEBUG_METHODS_ENABLED
  66. template <class T>
  67. struct VariantCasterAndValidate {
  68. static _FORCE_INLINE_ T cast(const Variant **p_args, uint32_t p_arg_idx, Callable::CallError &r_error) {
  69. Variant::Type argtype = GetTypeInfo<T>::VARIANT_TYPE;
  70. if (!Variant::can_convert_strict(p_args[p_arg_idx]->get_type(), argtype)) {
  71. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  72. r_error.argument = p_arg_idx;
  73. r_error.expected = argtype;
  74. }
  75. return VariantCaster<T>::cast(*p_args[p_arg_idx]);
  76. }
  77. };
  78. template <class T>
  79. struct VariantCasterAndValidate<T &> {
  80. static _FORCE_INLINE_ T cast(const Variant **p_args, uint32_t p_arg_idx, Callable::CallError &r_error) {
  81. Variant::Type argtype = GetTypeInfo<T>::VARIANT_TYPE;
  82. if (!Variant::can_convert_strict(p_args[p_arg_idx]->get_type(), argtype)) {
  83. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  84. r_error.argument = p_arg_idx;
  85. r_error.expected = argtype;
  86. }
  87. return VariantCaster<T>::cast(*p_args[p_arg_idx]);
  88. }
  89. };
  90. template <class T>
  91. struct VariantCasterAndValidate<const T &> {
  92. static _FORCE_INLINE_ T cast(const Variant **p_args, uint32_t p_arg_idx, Callable::CallError &r_error) {
  93. Variant::Type argtype = GetTypeInfo<T>::VARIANT_TYPE;
  94. if (!Variant::can_convert_strict(p_args[p_arg_idx]->get_type(), argtype)) {
  95. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  96. r_error.argument = p_arg_idx;
  97. r_error.expected = argtype;
  98. }
  99. return VariantCaster<T>::cast(*p_args[p_arg_idx]);
  100. }
  101. };
  102. #endif // DEBUG_METHODS_ENABLED
  103. // GCC 8 raises "parameter 'p_args' set but not used" here, probably using a
  104. // template version that does not have arguments and thus sees it unused, but
  105. // obviously the template can be used for functions with and without them, and
  106. // the optimizer will get rid of it anyway.
  107. #if defined(DEBUG_METHODS_ENABLED) && defined(__GNUC__) && !defined(__clang__)
  108. #pragma GCC diagnostic push
  109. #pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
  110. #endif
  111. template <class T, class... P, size_t... Is>
  112. void call_with_variant_args_helper(T *p_instance, void (T::*p_method)(P...), const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) {
  113. r_error.error = Callable::CallError::CALL_OK;
  114. #ifdef DEBUG_METHODS_ENABLED
  115. (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  116. #else
  117. (p_instance->*p_method)(VariantCaster<P>::cast(p_args[Is])...);
  118. #endif
  119. }
  120. #if defined(DEBUG_METHODS_ENABLED) && defined(__GNUC__) && !defined(__clang__)
  121. #pragma GCC diagnostic pop
  122. #endif
  123. template <class T, class... P>
  124. void call_with_variant_args(T *p_instance, void (T::*p_method)(P...), const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  125. #ifdef DEBUG_METHODS_ENABLED
  126. if ((size_t)p_argcount > sizeof...(P)) {
  127. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  128. r_error.argument = sizeof...(P);
  129. return;
  130. }
  131. if ((size_t)p_argcount < sizeof...(P)) {
  132. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  133. r_error.argument = sizeof...(P);
  134. return;
  135. }
  136. #endif
  137. call_with_variant_args_helper<T, P...>(p_instance, p_method, p_args, r_error, BuildIndexSequence<sizeof...(P)>{});
  138. }
  139. template <class T, class... P>
  140. class CallableCustomMethodPointer : public CallableCustomMethodPointerBase {
  141. struct Data {
  142. T *instance;
  143. #ifdef DEBUG_ENABLED
  144. uint64_t object_id;
  145. #endif
  146. void (T::*method)(P...);
  147. } data;
  148. public:
  149. virtual ObjectID get_object() const {
  150. #ifdef DEBUG_ENABLED
  151. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  152. return ObjectID();
  153. }
  154. #endif
  155. return data.instance->get_instance_id();
  156. }
  157. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  158. #ifdef DEBUG_ENABLED
  159. ERR_FAIL_COND_MSG(ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr, "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  160. #endif
  161. call_with_variant_args(data.instance, data.method, p_arguments, p_argcount, r_call_error);
  162. }
  163. CallableCustomMethodPointer(T *p_instance, void (T::*p_method)(P...)) {
  164. zeromem(&data, sizeof(Data)); // Clear beforehand, may have padding bytes.
  165. data.instance = p_instance;
  166. #ifdef DEBUG_ENABLED
  167. data.object_id = p_instance->get_instance_id();
  168. #endif
  169. data.method = p_method;
  170. _setup((uint32_t *)&data, sizeof(Data));
  171. }
  172. };
  173. template <class T, class... P>
  174. Callable create_custom_callable_function_pointer(T *p_instance,
  175. #ifdef DEBUG_METHODS_ENABLED
  176. const char *p_func_text,
  177. #endif
  178. void (T::*p_method)(P...)) {
  179. typedef CallableCustomMethodPointer<T, P...> CCMP; // Messes with memnew otherwise.
  180. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  181. #ifdef DEBUG_METHODS_ENABLED
  182. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  183. #endif
  184. return Callable(ccmp);
  185. }
  186. // VERSION WITH RETURN
  187. // GCC 8 raises "parameter 'p_args' set but not used" here, probably using a
  188. // template version that does not have arguments and thus sees it unused, but
  189. // obviously the template can be used for functions with and without them, and
  190. // the optimizer will get rid of it anyway.
  191. #if defined(DEBUG_METHODS_ENABLED) && defined(__GNUC__) && !defined(__clang__)
  192. #pragma GCC diagnostic push
  193. #pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
  194. #endif
  195. template <class T, class R, class... P, size_t... Is>
  196. void call_with_variant_args_ret_helper(T *p_instance, R (T::*p_method)(P...), const Variant **p_args, Variant &r_ret, Callable::CallError &r_error, IndexSequence<Is...>) {
  197. r_error.error = Callable::CallError::CALL_OK;
  198. #ifdef DEBUG_METHODS_ENABLED
  199. r_ret = (p_instance->*p_method)(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...);
  200. #else
  201. (p_instance->*p_method)(VariantCaster<P>::cast(p_args[Is])...);
  202. #endif
  203. }
  204. #if defined(DEBUG_METHODS_ENABLED) && defined(__GNUC__) && !defined(__clang__)
  205. #pragma GCC diagnostic pop
  206. #endif
  207. template <class T, class R, class... P>
  208. void call_with_variant_args_ret(T *p_instance, R (T::*p_method)(P...), const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) {
  209. #ifdef DEBUG_METHODS_ENABLED
  210. if ((size_t)p_argcount > sizeof...(P)) {
  211. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  212. r_error.argument = sizeof...(P);
  213. return;
  214. }
  215. if ((size_t)p_argcount < sizeof...(P)) {
  216. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  217. r_error.argument = sizeof...(P);
  218. return;
  219. }
  220. #endif
  221. call_with_variant_args_ret_helper<T, R, P...>(p_instance, p_method, p_args, r_ret, r_error, BuildIndexSequence<sizeof...(P)>{});
  222. }
  223. template <class T, class R, class... P>
  224. class CallableCustomMethodPointerRet : public CallableCustomMethodPointerBase {
  225. struct Data {
  226. T *instance;
  227. #ifdef DEBUG_ENABLED
  228. uint64_t object_id;
  229. #endif
  230. R(T::*method)
  231. (P...);
  232. } data;
  233. public:
  234. virtual ObjectID get_object() const {
  235. #ifdef DEBUG_ENABLED
  236. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  237. return ObjectID();
  238. }
  239. #endif
  240. return data.instance->get_instance_id();
  241. }
  242. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  243. #ifdef DEBUG_ENABLED
  244. ERR_FAIL_COND_MSG(ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr, "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  245. #endif
  246. call_with_variant_args_ret(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  247. }
  248. CallableCustomMethodPointerRet(T *p_instance, R (T::*p_method)(P...)) {
  249. zeromem(&data, sizeof(Data)); // Clear beforehand, may have padding bytes.
  250. data.instance = p_instance;
  251. #ifdef DEBUG_ENABLED
  252. data.object_id = p_instance->get_instance_id();
  253. #endif
  254. data.method = p_method;
  255. _setup((uint32_t *)&data, sizeof(Data));
  256. }
  257. };
  258. template <class T, class R, class... P>
  259. Callable create_custom_callable_function_pointer(T *p_instance,
  260. #ifdef DEBUG_METHODS_ENABLED
  261. const char *p_func_text,
  262. #endif
  263. R (T::*p_method)(P...)) {
  264. typedef CallableCustomMethodPointerRet<T, R, P...> CCMP; // Messes with memnew otherwise.
  265. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  266. #ifdef DEBUG_METHODS_ENABLED
  267. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  268. #endif
  269. return Callable(ccmp);
  270. }
  271. #ifdef DEBUG_METHODS_ENABLED
  272. #define callable_mp(I, M) create_custom_callable_function_pointer(I, #M, M)
  273. #else
  274. #define callable_mp(I, M) create_custom_callable_function_pointer(I, M)
  275. #endif
  276. #endif // CALLABLE_METHOD_POINTER_H