callable_method_pointer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**************************************************************************/
  2. /* callable_method_pointer.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 CALLABLE_METHOD_POINTER_H
  31. #define CALLABLE_METHOD_POINTER_H
  32. #include "core/object/object.h"
  33. #include "core/templates/hashfuncs.h"
  34. #include "core/templates/simple_type.h"
  35. #include "core/variant/binder_common.h"
  36. #include "core/variant/callable.h"
  37. class CallableCustomMethodPointerBase : public CallableCustom {
  38. uint32_t *comp_ptr = nullptr;
  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. virtual StringName get_method() const {
  50. #ifdef DEBUG_METHODS_ENABLED
  51. return StringName(text);
  52. #else
  53. return StringName();
  54. #endif
  55. }
  56. #ifdef DEBUG_METHODS_ENABLED
  57. void set_text(const char *p_text) {
  58. text = p_text;
  59. }
  60. virtual String get_as_text() const {
  61. return text;
  62. }
  63. #else
  64. virtual String get_as_text() const {
  65. return String();
  66. }
  67. #endif
  68. virtual CompareEqualFunc get_compare_equal_func() const;
  69. virtual CompareLessFunc get_compare_less_func() const;
  70. virtual uint32_t hash() const;
  71. };
  72. template <typename T, typename... P>
  73. class CallableCustomMethodPointer : public CallableCustomMethodPointerBase {
  74. struct Data {
  75. T *instance;
  76. uint64_t object_id;
  77. void (T::*method)(P...);
  78. } data;
  79. public:
  80. virtual ObjectID get_object() const {
  81. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  82. return ObjectID();
  83. }
  84. return data.instance->get_instance_id();
  85. }
  86. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  87. ERR_FAIL_NULL_MSG(ObjectDB::get_instance(ObjectID(data.object_id)), "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  88. call_with_variant_args(data.instance, data.method, p_arguments, p_argcount, r_call_error);
  89. }
  90. CallableCustomMethodPointer(T *p_instance, void (T::*p_method)(P...)) {
  91. memset(&data, 0, sizeof(Data)); // Clear beforehand, may have padding bytes.
  92. data.instance = p_instance;
  93. data.object_id = p_instance->get_instance_id();
  94. data.method = p_method;
  95. _setup((uint32_t *)&data, sizeof(Data));
  96. }
  97. };
  98. template <typename T, typename... P>
  99. Callable create_custom_callable_function_pointer(T *p_instance,
  100. #ifdef DEBUG_METHODS_ENABLED
  101. const char *p_func_text,
  102. #endif
  103. void (T::*p_method)(P...)) {
  104. typedef CallableCustomMethodPointer<T, P...> CCMP; // Messes with memnew otherwise.
  105. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  106. #ifdef DEBUG_METHODS_ENABLED
  107. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  108. #endif
  109. return Callable(ccmp);
  110. }
  111. // VERSION WITH RETURN
  112. template <typename T, typename R, typename... P>
  113. class CallableCustomMethodPointerRet : public CallableCustomMethodPointerBase {
  114. struct Data {
  115. T *instance;
  116. uint64_t object_id;
  117. R(T::*method)
  118. (P...);
  119. } data;
  120. public:
  121. virtual ObjectID get_object() const {
  122. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  123. return ObjectID();
  124. }
  125. return data.instance->get_instance_id();
  126. }
  127. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  128. ERR_FAIL_NULL_MSG(ObjectDB::get_instance(ObjectID(data.object_id)), "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  129. call_with_variant_args_ret(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  130. }
  131. CallableCustomMethodPointerRet(T *p_instance, R (T::*p_method)(P...)) {
  132. memset(&data, 0, sizeof(Data)); // Clear beforehand, may have padding bytes.
  133. data.instance = p_instance;
  134. data.object_id = p_instance->get_instance_id();
  135. data.method = p_method;
  136. _setup((uint32_t *)&data, sizeof(Data));
  137. }
  138. };
  139. template <typename T, typename R, typename... P>
  140. Callable create_custom_callable_function_pointer(T *p_instance,
  141. #ifdef DEBUG_METHODS_ENABLED
  142. const char *p_func_text,
  143. #endif
  144. R (T::*p_method)(P...)) {
  145. typedef CallableCustomMethodPointerRet<T, R, P...> CCMP; // Messes with memnew otherwise.
  146. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  147. #ifdef DEBUG_METHODS_ENABLED
  148. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  149. #endif
  150. return Callable(ccmp);
  151. }
  152. // CONST VERSION WITH RETURN
  153. template <typename T, typename R, typename... P>
  154. class CallableCustomMethodPointerRetC : public CallableCustomMethodPointerBase {
  155. struct Data {
  156. T *instance;
  157. uint64_t object_id;
  158. R(T::*method)
  159. (P...) const;
  160. } data;
  161. public:
  162. virtual ObjectID get_object() const override {
  163. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  164. return ObjectID();
  165. }
  166. return data.instance->get_instance_id();
  167. }
  168. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override {
  169. ERR_FAIL_NULL_MSG(ObjectDB::get_instance(ObjectID(data.object_id)), "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  170. call_with_variant_args_retc(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  171. }
  172. CallableCustomMethodPointerRetC(T *p_instance, R (T::*p_method)(P...) const) {
  173. memset(&data, 0, sizeof(Data)); // Clear beforehand, may have padding bytes.
  174. data.instance = p_instance;
  175. data.object_id = p_instance->get_instance_id();
  176. data.method = p_method;
  177. _setup((uint32_t *)&data, sizeof(Data));
  178. }
  179. };
  180. template <typename T, typename R, typename... P>
  181. Callable create_custom_callable_function_pointer(T *p_instance,
  182. #ifdef DEBUG_METHODS_ENABLED
  183. const char *p_func_text,
  184. #endif
  185. R (T::*p_method)(P...) const) {
  186. typedef CallableCustomMethodPointerRetC<T, R, P...> CCMP; // Messes with memnew otherwise.
  187. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  188. #ifdef DEBUG_METHODS_ENABLED
  189. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  190. #endif
  191. return Callable(ccmp);
  192. }
  193. #ifdef DEBUG_METHODS_ENABLED
  194. #define callable_mp(I, M) create_custom_callable_function_pointer(I, #M, M)
  195. #else
  196. #define callable_mp(I, M) create_custom_callable_function_pointer(I, M)
  197. #endif
  198. // STATIC VERSIONS
  199. template <typename... P>
  200. class CallableCustomStaticMethodPointer : public CallableCustomMethodPointerBase {
  201. struct Data {
  202. void (*method)(P...);
  203. } data;
  204. public:
  205. virtual bool is_valid() const override {
  206. return true;
  207. }
  208. virtual ObjectID get_object() const override {
  209. return ObjectID();
  210. }
  211. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override {
  212. call_with_variant_args_static_ret(data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  213. r_return_value = Variant();
  214. }
  215. CallableCustomStaticMethodPointer(void (*p_method)(P...)) {
  216. memset(&data, 0, sizeof(Data)); // Clear beforehand, may have padding bytes.
  217. data.method = p_method;
  218. _setup((uint32_t *)&data, sizeof(Data));
  219. }
  220. };
  221. template <typename T, typename... P>
  222. Callable create_custom_callable_static_function_pointer(
  223. #ifdef DEBUG_METHODS_ENABLED
  224. const char *p_func_text,
  225. #endif
  226. void (*p_method)(P...)) {
  227. typedef CallableCustomStaticMethodPointer<P...> CCMP; // Messes with memnew otherwise.
  228. CCMP *ccmp = memnew(CCMP(p_method));
  229. #ifdef DEBUG_METHODS_ENABLED
  230. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  231. #endif
  232. return Callable(ccmp);
  233. }
  234. template <typename R, typename... P>
  235. class CallableCustomStaticMethodPointerRet : public CallableCustomMethodPointerBase {
  236. struct Data {
  237. R(*method)
  238. (P...);
  239. } data;
  240. public:
  241. virtual bool is_valid() const override {
  242. return true;
  243. }
  244. virtual ObjectID get_object() const override {
  245. return ObjectID();
  246. }
  247. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override {
  248. call_with_variant_args_static_ret(data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  249. }
  250. CallableCustomStaticMethodPointerRet(R (*p_method)(P...)) {
  251. memset(&data, 0, sizeof(Data)); // Clear beforehand, may have padding bytes.
  252. data.method = p_method;
  253. _setup((uint32_t *)&data, sizeof(Data));
  254. }
  255. };
  256. template <typename R, typename... P>
  257. Callable create_custom_callable_static_function_pointer(
  258. #ifdef DEBUG_METHODS_ENABLED
  259. const char *p_func_text,
  260. #endif
  261. R (*p_method)(P...)) {
  262. typedef CallableCustomStaticMethodPointerRet<R, P...> CCMP; // Messes with memnew otherwise.
  263. CCMP *ccmp = memnew(CCMP(p_method));
  264. #ifdef DEBUG_METHODS_ENABLED
  265. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  266. #endif
  267. return Callable(ccmp);
  268. }
  269. #ifdef DEBUG_METHODS_ENABLED
  270. #define callable_mp_static(M) create_custom_callable_static_function_pointer(#M, M)
  271. #else
  272. #define callable_mp_static(M) create_custom_callable_static_function_pointer(M)
  273. #endif
  274. #endif // CALLABLE_METHOD_POINTER_H