callable_method_pointer.hpp 8.6 KB

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