callable_method_pointer.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**************************************************************************/
  2. /* callable_method_pointer.hpp */
  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 <godot_cpp/core/binder_common.hpp>
  32. #include <godot_cpp/variant/variant.hpp>
  33. namespace godot {
  34. class CallableCustomMethodPointerBase : public CallableCustomBase {
  35. uint32_t *comp_ptr = nullptr;
  36. uint32_t comp_size;
  37. uint32_t h;
  38. protected:
  39. void _setup(uint32_t *p_base_ptr, uint32_t p_ptr_size);
  40. public:
  41. _FORCE_INLINE_ const uint32_t *get_comp_ptr() const { return comp_ptr; }
  42. _FORCE_INLINE_ uint32_t get_comp_size() const { return comp_size; }
  43. _FORCE_INLINE_ uint32_t get_hash() const { return h; }
  44. };
  45. namespace internal {
  46. Callable create_callable_from_ccmp(CallableCustomMethodPointerBase *p_callable_method_pointer);
  47. } // namespace internal
  48. //
  49. // No return value.
  50. //
  51. template <typename T, typename... P>
  52. class CallableCustomMethodPointer : public CallableCustomMethodPointerBase {
  53. struct Data {
  54. T *instance;
  55. void (T::*method)(P...);
  56. } data;
  57. static_assert(sizeof(Data) % 4 == 0);
  58. public:
  59. virtual ObjectID get_object() const override {
  60. return ObjectID(data.instance->get_instance_id());
  61. }
  62. virtual int get_argument_count(bool &r_is_valid) const override {
  63. r_is_valid = true;
  64. return sizeof...(P);
  65. }
  66. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
  67. call_with_variant_args(data.instance, data.method, p_arguments, p_argcount, r_call_error);
  68. }
  69. CallableCustomMethodPointer(T *p_instance, void (T::*p_method)(P...)) {
  70. memset(&data, 0, sizeof(Data));
  71. data.instance = p_instance;
  72. data.method = p_method;
  73. _setup((uint32_t *)&data, sizeof(Data));
  74. }
  75. };
  76. template <typename T, typename... P>
  77. Callable create_custom_callable_function_pointer(T *p_instance, void (T::*p_method)(P...)) {
  78. typedef CallableCustomMethodPointer<T, P...> CCMP;
  79. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  80. return ::godot::internal::create_callable_from_ccmp(ccmp);
  81. }
  82. //
  83. // With return value.
  84. //
  85. template <typename T, typename R, typename... P>
  86. class CallableCustomMethodPointerRet : public CallableCustomMethodPointerBase {
  87. struct Data {
  88. T *instance;
  89. R(T::*method)
  90. (P...);
  91. } data;
  92. static_assert(sizeof(Data) % 4 == 0);
  93. public:
  94. virtual ObjectID get_object() const override {
  95. return ObjectID(data.instance->get_instance_id());
  96. }
  97. virtual int get_argument_count(bool &r_is_valid) const override {
  98. r_is_valid = true;
  99. return sizeof...(P);
  100. }
  101. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
  102. call_with_variant_args_ret(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  103. }
  104. CallableCustomMethodPointerRet(T *p_instance, R (T::*p_method)(P...)) {
  105. memset(&data, 0, sizeof(Data));
  106. data.instance = p_instance;
  107. data.method = p_method;
  108. _setup((uint32_t *)&data, sizeof(Data));
  109. }
  110. };
  111. template <typename T, typename R, typename... P>
  112. Callable create_custom_callable_function_pointer(T *p_instance, R (T::*p_method)(P...)) {
  113. typedef CallableCustomMethodPointerRet<T, R, P...> CCMP; // Messes with memnew otherwise.
  114. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  115. return ::godot::internal::create_callable_from_ccmp(ccmp);
  116. }
  117. //
  118. // Const with return value.
  119. //
  120. template <typename T, typename R, typename... P>
  121. class CallableCustomMethodPointerRetC : public CallableCustomMethodPointerBase {
  122. struct Data {
  123. T *instance;
  124. R(T::*method)
  125. (P...) const;
  126. } data;
  127. static_assert(sizeof(Data) % 4 == 0);
  128. public:
  129. virtual ObjectID get_object() const override {
  130. return ObjectID(data.instance->get_instance_id());
  131. }
  132. virtual int get_argument_count(bool &r_is_valid) const override {
  133. r_is_valid = true;
  134. return sizeof...(P);
  135. }
  136. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
  137. call_with_variant_args_retc(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  138. }
  139. CallableCustomMethodPointerRetC(const T *p_instance, R (T::*p_method)(P...) const) {
  140. memset(&data, 0, sizeof(Data));
  141. data.instance = const_cast<T *>(p_instance);
  142. data.method = p_method;
  143. _setup((uint32_t *)&data, sizeof(Data));
  144. }
  145. };
  146. template <typename T, typename R, typename... P>
  147. Callable create_custom_callable_function_pointer(const T *p_instance, R (T::*p_method)(P...) const) {
  148. typedef CallableCustomMethodPointerRetC<T, R, P...> CCMP; // Messes with memnew otherwise.
  149. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  150. return ::godot::internal::create_callable_from_ccmp(ccmp);
  151. }
  152. //
  153. // Static method with no return value.
  154. //
  155. template <typename... P>
  156. class CallableCustomStaticMethodPointer : public CallableCustomMethodPointerBase {
  157. struct Data {
  158. void (*method)(P...);
  159. } data;
  160. static_assert(sizeof(Data) % 4 == 0);
  161. public:
  162. virtual ObjectID get_object() const override {
  163. return ObjectID();
  164. }
  165. virtual int get_argument_count(bool &r_is_valid) const override {
  166. r_is_valid = true;
  167. return sizeof...(P);
  168. }
  169. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
  170. call_with_variant_args_static_ret(data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  171. r_return_value = Variant();
  172. }
  173. CallableCustomStaticMethodPointer(void (*p_method)(P...)) {
  174. memset(&data, 0, sizeof(Data));
  175. data.method = p_method;
  176. _setup((uint32_t *)&data, sizeof(Data));
  177. }
  178. };
  179. template <typename... P>
  180. Callable create_custom_callable_static_function_pointer(void (*p_method)(P...)) {
  181. typedef CallableCustomStaticMethodPointer<P...> CCMP;
  182. CCMP *ccmp = memnew(CCMP(p_method));
  183. return ::godot::internal::create_callable_from_ccmp(ccmp);
  184. }
  185. //
  186. // Static method with return value.
  187. //
  188. template <typename R, typename... P>
  189. class CallableCustomStaticMethodPointerRet : public CallableCustomMethodPointerBase {
  190. struct Data {
  191. R(*method)
  192. (P...);
  193. } data;
  194. static_assert(sizeof(Data) % 4 == 0);
  195. public:
  196. virtual ObjectID get_object() const override {
  197. return ObjectID();
  198. }
  199. virtual int get_argument_count(bool &r_is_valid) const override {
  200. r_is_valid = true;
  201. return sizeof...(P);
  202. }
  203. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
  204. call_with_variant_args_static_ret(data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  205. }
  206. CallableCustomStaticMethodPointerRet(R (*p_method)(P...)) {
  207. memset(&data, 0, sizeof(Data));
  208. data.method = p_method;
  209. _setup((uint32_t *)&data, sizeof(Data));
  210. }
  211. };
  212. template <typename R, typename... P>
  213. Callable create_custom_callable_static_function_pointer(R (*p_method)(P...)) {
  214. typedef CallableCustomStaticMethodPointerRet<R, P...> CCMP;
  215. CCMP *ccmp = memnew(CCMP(p_method));
  216. return ::godot::internal::create_callable_from_ccmp(ccmp);
  217. }
  218. //
  219. // The API:
  220. //
  221. #define callable_mp(I, M) ::godot::create_custom_callable_function_pointer(I, M)
  222. #define callable_mp_static(M) ::godot::create_custom_callable_static_function_pointer(M)
  223. } // namespace godot