callable_method_pointer.h 10 KB

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