callable_method_pointer.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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/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;
  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. template <class T, class... P>
  66. class CallableCustomMethodPointer : public CallableCustomMethodPointerBase {
  67. struct Data {
  68. T *instance;
  69. #ifdef DEBUG_ENABLED
  70. uint64_t object_id;
  71. #endif
  72. void (T::*method)(P...);
  73. } data;
  74. public:
  75. virtual ObjectID get_object() const {
  76. #ifdef DEBUG_ENABLED
  77. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  78. return ObjectID();
  79. }
  80. #endif
  81. return data.instance->get_instance_id();
  82. }
  83. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  84. #ifdef DEBUG_ENABLED
  85. ERR_FAIL_COND_MSG(ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr, "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  86. #endif
  87. call_with_variant_args(data.instance, data.method, p_arguments, p_argcount, r_call_error);
  88. }
  89. CallableCustomMethodPointer(T *p_instance, void (T::*p_method)(P...)) {
  90. memset(&data, 0, sizeof(Data)); // Clear beforehand, may have padding bytes.
  91. data.instance = p_instance;
  92. #ifdef DEBUG_ENABLED
  93. data.object_id = p_instance->get_instance_id();
  94. #endif
  95. data.method = p_method;
  96. _setup((uint32_t *)&data, sizeof(Data));
  97. }
  98. };
  99. template <class T, class... P>
  100. Callable create_custom_callable_function_pointer(T *p_instance,
  101. #ifdef DEBUG_METHODS_ENABLED
  102. const char *p_func_text,
  103. #endif
  104. void (T::*p_method)(P...)) {
  105. typedef CallableCustomMethodPointer<T, P...> CCMP; // Messes with memnew otherwise.
  106. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  107. #ifdef DEBUG_METHODS_ENABLED
  108. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  109. #endif
  110. return Callable(ccmp);
  111. }
  112. // VERSION WITH RETURN
  113. template <class T, class R, class... P>
  114. class CallableCustomMethodPointerRet : public CallableCustomMethodPointerBase {
  115. struct Data {
  116. T *instance;
  117. #ifdef DEBUG_ENABLED
  118. uint64_t object_id;
  119. #endif
  120. R(T::*method)
  121. (P...);
  122. } data;
  123. public:
  124. virtual ObjectID get_object() const {
  125. #ifdef DEBUG_ENABLED
  126. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  127. return ObjectID();
  128. }
  129. #endif
  130. return data.instance->get_instance_id();
  131. }
  132. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  133. #ifdef DEBUG_ENABLED
  134. ERR_FAIL_COND_MSG(ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr, "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  135. #endif
  136. call_with_variant_args_ret(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  137. }
  138. CallableCustomMethodPointerRet(T *p_instance, R (T::*p_method)(P...)) {
  139. memset(&data, 0, sizeof(Data)); // Clear beforehand, may have padding bytes.
  140. data.instance = p_instance;
  141. #ifdef DEBUG_ENABLED
  142. data.object_id = p_instance->get_instance_id();
  143. #endif
  144. data.method = p_method;
  145. _setup((uint32_t *)&data, sizeof(Data));
  146. }
  147. };
  148. template <class T, class R, class... P>
  149. Callable create_custom_callable_function_pointer(T *p_instance,
  150. #ifdef DEBUG_METHODS_ENABLED
  151. const char *p_func_text,
  152. #endif
  153. R (T::*p_method)(P...)) {
  154. typedef CallableCustomMethodPointerRet<T, R, P...> CCMP; // Messes with memnew otherwise.
  155. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  156. #ifdef DEBUG_METHODS_ENABLED
  157. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  158. #endif
  159. return Callable(ccmp);
  160. }
  161. // CONST VERSION WITH RETURN
  162. template <class T, class R, class... P>
  163. class CallableCustomMethodPointerRetC : public CallableCustomMethodPointerBase {
  164. struct Data {
  165. T *instance;
  166. #ifdef DEBUG_ENABLED
  167. uint64_t object_id;
  168. #endif
  169. R(T::*method)
  170. (P...) const;
  171. } data;
  172. public:
  173. virtual ObjectID get_object() const {
  174. #ifdef DEBUG_ENABLED
  175. if (ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr) {
  176. return ObjectID();
  177. }
  178. #endif
  179. return data.instance->get_instance_id();
  180. }
  181. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  182. #ifdef DEBUG_ENABLED
  183. ERR_FAIL_COND_MSG(ObjectDB::get_instance(ObjectID(data.object_id)) == nullptr, "Invalid Object id '" + uitos(data.object_id) + "', can't call method.");
  184. #endif
  185. call_with_variant_args_retc(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  186. }
  187. CallableCustomMethodPointerRetC(T *p_instance, R (T::*p_method)(P...) const) {
  188. memset(&data, 0, sizeof(Data)); // Clear beforehand, may have padding bytes.
  189. data.instance = p_instance;
  190. #ifdef DEBUG_ENABLED
  191. data.object_id = p_instance->get_instance_id();
  192. #endif
  193. data.method = p_method;
  194. _setup((uint32_t *)&data, sizeof(Data));
  195. }
  196. };
  197. template <class T, class R, class... P>
  198. Callable create_custom_callable_function_pointer(T *p_instance,
  199. #ifdef DEBUG_METHODS_ENABLED
  200. const char *p_func_text,
  201. #endif
  202. R (T::*p_method)(P...) const) {
  203. typedef CallableCustomMethodPointerRetC<T, R, P...> CCMP; // Messes with memnew otherwise.
  204. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  205. #ifdef DEBUG_METHODS_ENABLED
  206. ccmp->set_text(p_func_text + 1); // Try to get rid of the ampersand.
  207. #endif
  208. return Callable(ccmp);
  209. }
  210. #ifdef DEBUG_METHODS_ENABLED
  211. #define callable_mp(I, M) create_custom_callable_function_pointer(I, #M, M)
  212. #else
  213. #define callable_mp(I, M) create_custom_callable_function_pointer(I, M)
  214. #endif
  215. #endif // CALLABLE_METHOD_POINTER_H