callable_method_pointer.h 10 KB

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