callable_method_pointer.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/object/object.h"
  33. #include "core/os/copymem.h"
  34. #include "core/templates/hashfuncs.h"
  35. #include "core/templates/simple_type.h"
  36. #include "core/variant/binder_common.h"
  37. #include "core/variant/callable.h"
  38. class CallableCustomMethodPointerBase : public CallableCustom {
  39. uint32_t *comp_ptr;
  40. uint32_t comp_size;
  41. uint32_t h;
  42. #ifdef DEBUG_METHODS_ENABLED
  43. const char *text = "";
  44. #endif
  45. static bool compare_equal(const CallableCustom *p_a, const CallableCustom *p_b);
  46. static bool compare_less(const CallableCustom *p_a, const CallableCustom *p_b);
  47. protected:
  48. void _setup(uint32_t *p_base_ptr, uint32_t p_ptr_size);
  49. public:
  50. #ifdef DEBUG_METHODS_ENABLED
  51. void set_text(const char *p_text) {
  52. text = p_text;
  53. }
  54. virtual String get_as_text() const {
  55. return text;
  56. }
  57. #else
  58. virtual String get_as_text() const {
  59. return String();
  60. }
  61. #endif
  62. virtual CompareEqualFunc get_compare_equal_func() const;
  63. virtual CompareLessFunc get_compare_less_func() const;
  64. virtual uint32_t hash() const;
  65. };
  66. template <class T, class... P>
  67. class CallableCustomMethodPointer : public CallableCustomMethodPointerBase {
  68. struct Data {
  69. T *instance;
  70. #ifdef DEBUG_ENABLED
  71. uint64_t object_id;
  72. #endif
  73. void (T::*method)(P...);
  74. } data;
  75. public:
  76. virtual ObjectID get_object() const {
  77. #ifdef DEBUG_ENABLED
  78. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  79. return ObjectID();
  80. }
  81. #endif
  82. return data.instance->get_instance_id();
  83. }
  84. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  85. #ifdef DEBUG_ENABLED
  86. ERR_FAIL_COND_MSG(ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr, "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  87. #endif
  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. zeromem(&data, sizeof(Data)); // Clear beforehand, may have padding bytes.
  92. data.instance = p_instance;
  93. #ifdef DEBUG_ENABLED
  94. data.object_id = p_instance->get_instance_id();
  95. #endif
  96. data.method = p_method;
  97. _setup((uint32_t *)&data, sizeof(Data));
  98. }
  99. };
  100. template <class T, class... P>
  101. Callable create_custom_callable_function_pointer(T *p_instance,
  102. #ifdef DEBUG_METHODS_ENABLED
  103. const char *p_func_text,
  104. #endif
  105. void (T::*p_method)(P...)) {
  106. typedef CallableCustomMethodPointer<T, P...> CCMP; // Messes with memnew otherwise.
  107. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  108. #ifdef DEBUG_METHODS_ENABLED
  109. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  110. #endif
  111. return Callable(ccmp);
  112. }
  113. // VERSION WITH RETURN
  114. template <class T, class R, class... P>
  115. class CallableCustomMethodPointerRet : public CallableCustomMethodPointerBase {
  116. struct Data {
  117. T *instance;
  118. #ifdef DEBUG_ENABLED
  119. uint64_t object_id;
  120. #endif
  121. R(T::*method)
  122. (P...);
  123. } data;
  124. public:
  125. virtual ObjectID get_object() const {
  126. #ifdef DEBUG_ENABLED
  127. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  128. return ObjectID();
  129. }
  130. #endif
  131. return data.instance->get_instance_id();
  132. }
  133. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  134. #ifdef DEBUG_ENABLED
  135. ERR_FAIL_COND_MSG(ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr, "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  136. #endif
  137. call_with_variant_args_ret(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  138. }
  139. CallableCustomMethodPointerRet(T *p_instance, R (T::*p_method)(P...)) {
  140. zeromem(&data, sizeof(Data)); // Clear beforehand, may have padding bytes.
  141. data.instance = p_instance;
  142. #ifdef DEBUG_ENABLED
  143. data.object_id = p_instance->get_instance_id();
  144. #endif
  145. data.method = p_method;
  146. _setup((uint32_t *)&data, sizeof(Data));
  147. }
  148. };
  149. template <class T, class R, class... P>
  150. Callable create_custom_callable_function_pointer(T *p_instance,
  151. #ifdef DEBUG_METHODS_ENABLED
  152. const char *p_func_text,
  153. #endif
  154. R (T::*p_method)(P...)) {
  155. typedef CallableCustomMethodPointerRet<T, R, P...> CCMP; // Messes with memnew otherwise.
  156. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  157. #ifdef DEBUG_METHODS_ENABLED
  158. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  159. #endif
  160. return Callable(ccmp);
  161. }
  162. // CONST VERSION WITH RETURN
  163. template <class T, class R, class... P>
  164. class CallableCustomMethodPointerRetC : public CallableCustomMethodPointerBase {
  165. struct Data {
  166. T *instance;
  167. #ifdef DEBUG_ENABLED
  168. uint64_t object_id;
  169. #endif
  170. R(T::*method)
  171. (P...) const;
  172. } data;
  173. public:
  174. virtual ObjectID get_object() const {
  175. #ifdef DEBUG_ENABLED
  176. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  177. return ObjectID();
  178. }
  179. #endif
  180. return data.instance->get_instance_id();
  181. }
  182. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  183. #ifdef DEBUG_ENABLED
  184. ERR_FAIL_COND_MSG(ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr, "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  185. #endif
  186. call_with_variant_args_retc(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  187. }
  188. CallableCustomMethodPointerRetC(T *p_instance, R (T::*p_method)(P...) const) {
  189. zeromem(&data, sizeof(Data)); // Clear beforehand, may have padding bytes.
  190. data.instance = p_instance;
  191. #ifdef DEBUG_ENABLED
  192. data.object_id = p_instance->get_instance_id();
  193. #endif
  194. data.method = p_method;
  195. _setup((uint32_t *)&data, sizeof(Data));
  196. }
  197. };
  198. template <class T, class R, class... P>
  199. Callable create_custom_callable_function_pointer(T *p_instance,
  200. #ifdef DEBUG_METHODS_ENABLED
  201. const char *p_func_text,
  202. #endif
  203. R (T::*p_method)(P...) const) {
  204. typedef CallableCustomMethodPointerRetC<T, R, P...> CCMP; // Messes with memnew otherwise.
  205. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  206. #ifdef DEBUG_METHODS_ENABLED
  207. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  208. #endif
  209. return Callable(ccmp);
  210. }
  211. #ifdef DEBUG_METHODS_ENABLED
  212. #define callable_mp(I, M) create_custom_callable_function_pointer(I, #M, M)
  213. #else
  214. #define callable_mp(I, M) create_custom_callable_function_pointer(I, M)
  215. #endif
  216. #endif // CALLABLE_METHOD_POINTER_H