callable_method_pointer.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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)(P...);
  90. } data;
  91. static_assert(sizeof(Data) % 4 == 0);
  92. public:
  93. virtual ObjectID get_object() const override {
  94. return ObjectID(data.instance->get_instance_id());
  95. }
  96. virtual int get_argument_count(bool &r_is_valid) const override {
  97. r_is_valid = true;
  98. return sizeof...(P);
  99. }
  100. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
  101. call_with_variant_args_ret(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  102. }
  103. CallableCustomMethodPointerRet(T *p_instance, R (T::*p_method)(P...)) {
  104. memset(&data, 0, sizeof(Data));
  105. data.instance = p_instance;
  106. data.method = p_method;
  107. _setup((uint32_t *)&data, sizeof(Data));
  108. }
  109. };
  110. template <typename T, typename R, typename... P>
  111. Callable create_custom_callable_function_pointer(T *p_instance, R (T::*p_method)(P...)) {
  112. typedef CallableCustomMethodPointerRet<T, R, P...> CCMP; // Messes with memnew otherwise.
  113. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  114. return ::godot::internal::create_callable_from_ccmp(ccmp);
  115. }
  116. //
  117. // Const with return value.
  118. //
  119. template <typename T, typename R, typename... P>
  120. class CallableCustomMethodPointerRetC : public CallableCustomMethodPointerBase {
  121. struct Data {
  122. T *instance;
  123. R (T::*method)(P...) const;
  124. } data;
  125. static_assert(sizeof(Data) % 4 == 0);
  126. public:
  127. virtual ObjectID get_object() const override {
  128. return ObjectID(data.instance->get_instance_id());
  129. }
  130. virtual int get_argument_count(bool &r_is_valid) const override {
  131. r_is_valid = true;
  132. return sizeof...(P);
  133. }
  134. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
  135. call_with_variant_args_retc(data.instance, data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  136. }
  137. CallableCustomMethodPointerRetC(const T *p_instance, R (T::*p_method)(P...) const) {
  138. memset(&data, 0, sizeof(Data));
  139. data.instance = const_cast<T *>(p_instance);
  140. data.method = p_method;
  141. _setup((uint32_t *)&data, sizeof(Data));
  142. }
  143. };
  144. template <typename T, typename R, typename... P>
  145. Callable create_custom_callable_function_pointer(const T *p_instance, R (T::*p_method)(P...) const) {
  146. typedef CallableCustomMethodPointerRetC<T, R, P...> CCMP; // Messes with memnew otherwise.
  147. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  148. return ::godot::internal::create_callable_from_ccmp(ccmp);
  149. }
  150. //
  151. // Static method with no return value.
  152. //
  153. template <typename... P>
  154. class CallableCustomStaticMethodPointer : public CallableCustomMethodPointerBase {
  155. struct Data {
  156. void (*method)(P...);
  157. } data;
  158. static_assert(sizeof(Data) % 4 == 0);
  159. public:
  160. virtual ObjectID get_object() const override {
  161. return ObjectID();
  162. }
  163. virtual int get_argument_count(bool &r_is_valid) const override {
  164. r_is_valid = true;
  165. return sizeof...(P);
  166. }
  167. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
  168. call_with_variant_args_static_ret(data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  169. r_return_value = Variant();
  170. }
  171. CallableCustomStaticMethodPointer(void (*p_method)(P...)) {
  172. memset(&data, 0, sizeof(Data));
  173. data.method = p_method;
  174. _setup((uint32_t *)&data, sizeof(Data));
  175. }
  176. };
  177. template <typename... P>
  178. Callable create_custom_callable_static_function_pointer(void (*p_method)(P...)) {
  179. typedef CallableCustomStaticMethodPointer<P...> CCMP;
  180. CCMP *ccmp = memnew(CCMP(p_method));
  181. return ::godot::internal::create_callable_from_ccmp(ccmp);
  182. }
  183. //
  184. // Static method with return value.
  185. //
  186. template <typename R, typename... P>
  187. class CallableCustomStaticMethodPointerRet : public CallableCustomMethodPointerBase {
  188. struct Data {
  189. R (*method)(P...);
  190. } data;
  191. static_assert(sizeof(Data) % 4 == 0);
  192. public:
  193. virtual ObjectID get_object() const override {
  194. return ObjectID();
  195. }
  196. virtual int get_argument_count(bool &r_is_valid) const override {
  197. r_is_valid = true;
  198. return sizeof...(P);
  199. }
  200. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
  201. call_with_variant_args_static_ret(data.method, p_arguments, p_argcount, r_return_value, r_call_error);
  202. }
  203. CallableCustomStaticMethodPointerRet(R (*p_method)(P...)) {
  204. memset(&data, 0, sizeof(Data));
  205. data.method = p_method;
  206. _setup((uint32_t *)&data, sizeof(Data));
  207. }
  208. };
  209. template <typename R, typename... P>
  210. Callable create_custom_callable_static_function_pointer(R (*p_method)(P...)) {
  211. typedef CallableCustomStaticMethodPointerRet<R, P...> CCMP;
  212. CCMP *ccmp = memnew(CCMP(p_method));
  213. return ::godot::internal::create_callable_from_ccmp(ccmp);
  214. }
  215. //
  216. // The API:
  217. //
  218. #define callable_mp(I, M) ::godot::create_custom_callable_function_pointer(I, M)
  219. #define callable_mp_static(M) ::godot::create_custom_callable_static_function_pointer(M)
  220. } // namespace godot