callable_method_pointer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 <class T, class... P>
  73. class CallableCustomMethodPointer : public CallableCustomMethodPointerBase {
  74. struct Data {
  75. T *instance;
  76. #ifdef DEBUG_ENABLED
  77. uint64_t object_id;
  78. #endif
  79. void (T::*method)(P...);
  80. } data;
  81. public:
  82. virtual ObjectID get_object() const {
  83. #ifdef DEBUG_ENABLED
  84. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  85. return ObjectID();
  86. }
  87. #endif
  88. return data.instance->get_instance_id();
  89. }
  90. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  91. #ifdef DEBUG_ENABLED
  92. ERR_FAIL_COND_MSG(ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr, "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  93. #endif
  94. call_with_variant_args(data.instance, data.method, p_arguments, p_argcount, r_call_error);
  95. }
  96. CallableCustomMethodPointer(T *p_instance, void (T::*p_method)(P...)) {
  97. memset(&data, 0, sizeof(Data)); // Clear beforehand, may have padding bytes.
  98. data.instance = p_instance;
  99. #ifdef DEBUG_ENABLED
  100. data.object_id = p_instance->get_instance_id();
  101. #endif
  102. data.method = p_method;
  103. _setup((uint32_t *)&data, sizeof(Data));
  104. }
  105. };
  106. template <class T, class... P>
  107. Callable create_custom_callable_function_pointer(T *p_instance,
  108. #ifdef DEBUG_METHODS_ENABLED
  109. const char *p_func_text,
  110. #endif
  111. void (T::*p_method)(P...)) {
  112. typedef CallableCustomMethodPointer<T, P...> CCMP; // Messes with memnew otherwise.
  113. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  114. #ifdef DEBUG_METHODS_ENABLED
  115. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  116. #endif
  117. return Callable(ccmp);
  118. }
  119. // VERSION WITH RETURN
  120. template <class T, class R, class... P>
  121. class CallableCustomMethodPointerRet : public CallableCustomMethodPointerBase {
  122. struct Data {
  123. T *instance;
  124. #ifdef DEBUG_ENABLED
  125. uint64_t object_id;
  126. #endif
  127. R(T::*method)
  128. (P...);
  129. } data;
  130. public:
  131. virtual ObjectID get_object() const {
  132. #ifdef DEBUG_ENABLED
  133. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  134. return ObjectID();
  135. }
  136. #endif
  137. return data.instance->get_instance_id();
  138. }
  139. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  140. #ifdef DEBUG_ENABLED
  141. ERR_FAIL_COND_MSG(ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr, "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  142. #endif
  143. call_with_variant_args_ret(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  144. }
  145. CallableCustomMethodPointerRet(T *p_instance, R (T::*p_method)(P...)) {
  146. memset(&data, 0, sizeof(Data)); // Clear beforehand, may have padding bytes.
  147. data.instance = p_instance;
  148. #ifdef DEBUG_ENABLED
  149. data.object_id = p_instance->get_instance_id();
  150. #endif
  151. data.method = p_method;
  152. _setup((uint32_t *)&data, sizeof(Data));
  153. }
  154. };
  155. template <class T, class R, class... P>
  156. Callable create_custom_callable_function_pointer(T *p_instance,
  157. #ifdef DEBUG_METHODS_ENABLED
  158. const char *p_func_text,
  159. #endif
  160. R (T::*p_method)(P...)) {
  161. typedef CallableCustomMethodPointerRet<T, R, P...> CCMP; // Messes with memnew otherwise.
  162. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  163. #ifdef DEBUG_METHODS_ENABLED
  164. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  165. #endif
  166. return Callable(ccmp);
  167. }
  168. // CONST VERSION WITH RETURN
  169. template <class T, class R, class... P>
  170. class CallableCustomMethodPointerRetC : public CallableCustomMethodPointerBase {
  171. struct Data {
  172. T *instance;
  173. #ifdef DEBUG_ENABLED
  174. uint64_t object_id;
  175. #endif
  176. R(T::*method)
  177. (P...) const;
  178. } data;
  179. public:
  180. virtual ObjectID get_object() const {
  181. #ifdef DEBUG_ENABLED
  182. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  183. return ObjectID();
  184. }
  185. #endif
  186. return data.instance->get_instance_id();
  187. }
  188. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  189. #ifdef DEBUG_ENABLED
  190. ERR_FAIL_COND_MSG(ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr, "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  191. #endif
  192. call_with_variant_args_retc(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  193. }
  194. CallableCustomMethodPointerRetC(T *p_instance, R (T::*p_method)(P...) const) {
  195. memset(&data, 0, sizeof(Data)); // Clear beforehand, may have padding bytes.
  196. data.instance = p_instance;
  197. #ifdef DEBUG_ENABLED
  198. data.object_id = p_instance->get_instance_id();
  199. #endif
  200. data.method = p_method;
  201. _setup((uint32_t *)&data, sizeof(Data));
  202. }
  203. };
  204. template <class T, class R, class... P>
  205. Callable create_custom_callable_function_pointer(T *p_instance,
  206. #ifdef DEBUG_METHODS_ENABLED
  207. const char *p_func_text,
  208. #endif
  209. R (T::*p_method)(P...) const) {
  210. typedef CallableCustomMethodPointerRetC<T, R, P...> CCMP; // Messes with memnew otherwise.
  211. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  212. #ifdef DEBUG_METHODS_ENABLED
  213. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  214. #endif
  215. return Callable(ccmp);
  216. }
  217. #ifdef DEBUG_METHODS_ENABLED
  218. #define callable_mp(I, M) create_custom_callable_function_pointer(I, #M, M)
  219. #else
  220. #define callable_mp(I, M) create_custom_callable_function_pointer(I, M)
  221. #endif
  222. // STATIC VERSIONS
  223. template <class... P>
  224. class CallableCustomStaticMethodPointer : public CallableCustomMethodPointerBase {
  225. struct Data {
  226. void (*method)(P...);
  227. } data;
  228. public:
  229. virtual ObjectID get_object() const {
  230. return ObjectID();
  231. }
  232. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  233. call_with_variant_args_static_ret(data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  234. r_return_value = Variant();
  235. }
  236. CallableCustomStaticMethodPointer(void (*p_method)(P...)) {
  237. memset(&data, 0, sizeof(Data)); // Clear beforehand, may have padding bytes.
  238. data.method = p_method;
  239. _setup((uint32_t *)&data, sizeof(Data));
  240. }
  241. };
  242. template <class T, class... P>
  243. Callable create_custom_callable_static_function_pointer(
  244. #ifdef DEBUG_METHODS_ENABLED
  245. const char *p_func_text,
  246. #endif
  247. void (*p_method)(P...)) {
  248. typedef CallableCustomStaticMethodPointer<P...> CCMP; // Messes with memnew otherwise.
  249. CCMP *ccmp = memnew(CCMP(p_method));
  250. #ifdef DEBUG_METHODS_ENABLED
  251. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  252. #endif
  253. return Callable(ccmp);
  254. }
  255. template <class R, class... P>
  256. class CallableCustomStaticMethodPointerRet : public CallableCustomMethodPointerBase {
  257. struct Data {
  258. R(*method)
  259. (P...);
  260. } data;
  261. public:
  262. virtual ObjectID get_object() const {
  263. return ObjectID();
  264. }
  265. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  266. call_with_variant_args_static_ret(data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  267. }
  268. CallableCustomStaticMethodPointerRet(R (*p_method)(P...)) {
  269. memset(&data, 0, sizeof(Data)); // Clear beforehand, may have padding bytes.
  270. data.method = p_method;
  271. _setup((uint32_t *)&data, sizeof(Data));
  272. }
  273. };
  274. template <class R, class... P>
  275. Callable create_custom_callable_static_function_pointer(
  276. #ifdef DEBUG_METHODS_ENABLED
  277. const char *p_func_text,
  278. #endif
  279. R (*p_method)(P...)) {
  280. typedef CallableCustomStaticMethodPointerRet<R, P...> CCMP; // Messes with memnew otherwise.
  281. CCMP *ccmp = memnew(CCMP(p_method));
  282. #ifdef DEBUG_METHODS_ENABLED
  283. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  284. #endif
  285. return Callable(ccmp);
  286. }
  287. #ifdef DEBUG_METHODS_ENABLED
  288. #define callable_mp_static(M) create_custom_callable_static_function_pointer(#M, M)
  289. #else
  290. #define callable_mp_static(M) create_custom_callable_static_function_pointer(M)
  291. #endif
  292. #endif // CALLABLE_METHOD_POINTER_H