callable_method_pointer.h 10 KB

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