callable_method_pointer.h 11 KB

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