callable_method_pointer.hpp 9.1 KB

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