callable_method_pointer.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 {
  36. public:
  37. virtual Object *get_object() const = 0;
  38. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const = 0;
  39. virtual ~CallableCustomMethodPointerBase() {}
  40. };
  41. namespace internal {
  42. Callable create_custom_callable(CallableCustomMethodPointerBase *p_callable_method_pointer);
  43. } // namespace internal
  44. //
  45. // No return value.
  46. //
  47. template <class T, class... P>
  48. class CallableCustomMethodPointer : public CallableCustomMethodPointerBase {
  49. T *instance;
  50. void (T::*method)(P...);
  51. public:
  52. virtual Object *get_object() const override {
  53. return instance;
  54. }
  55. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
  56. call_with_variant_args(instance, method, p_arguments, p_argcount, r_call_error);
  57. }
  58. CallableCustomMethodPointer(T *p_instance, void (T::*p_method)(P...)) {
  59. instance = p_instance;
  60. method = p_method;
  61. }
  62. };
  63. template <class T, class... P>
  64. Callable create_custom_callable_function_pointer(T *p_instance, void (T::*p_method)(P...)) {
  65. typedef CallableCustomMethodPointer<T, P...> CCMP;
  66. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  67. return ::godot::internal::create_custom_callable(ccmp);
  68. }
  69. //
  70. // With return value.
  71. //
  72. template <class T, class R, class... P>
  73. class CallableCustomMethodPointerRet : public CallableCustomMethodPointerBase {
  74. T *instance;
  75. R(T::*method)
  76. (P...);
  77. public:
  78. virtual Object *get_object() const override {
  79. return instance;
  80. }
  81. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
  82. call_with_variant_args_ret(instance, method, p_arguments, p_argcount, r_return_value, r_call_error);
  83. }
  84. CallableCustomMethodPointerRet(T *p_instance, R (T::*p_method)(P...)) {
  85. instance = p_instance;
  86. method = p_method;
  87. }
  88. };
  89. template <class T, class R, class... P>
  90. Callable create_custom_callable_function_pointer(T *p_instance, R (T::*p_method)(P...)) {
  91. typedef CallableCustomMethodPointerRet<T, R, P...> CCMP; // Messes with memnew otherwise.
  92. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  93. return ::godot::internal::create_custom_callable(ccmp);
  94. }
  95. //
  96. // Const with return value.
  97. //
  98. template <class T, class R, class... P>
  99. class CallableCustomMethodPointerRetC : public CallableCustomMethodPointerBase {
  100. T *instance;
  101. R(T::*method)
  102. (P...) const;
  103. public:
  104. virtual Object *get_object() const override {
  105. return instance;
  106. }
  107. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
  108. call_with_variant_args_retc(instance, method, p_arguments, p_argcount, r_return_value, r_call_error);
  109. }
  110. CallableCustomMethodPointerRetC(const T *p_instance, R (T::*p_method)(P...) const) {
  111. instance = const_cast<T *>(p_instance);
  112. method = p_method;
  113. }
  114. };
  115. template <class T, class R, class... P>
  116. Callable create_custom_callable_function_pointer(const T *p_instance, R (T::*p_method)(P...) const) {
  117. typedef CallableCustomMethodPointerRetC<T, R, P...> CCMP; // Messes with memnew otherwise.
  118. CCMP *ccmp = memnew(CCMP(p_instance, p_method));
  119. return ::godot::internal::create_custom_callable(ccmp);
  120. }
  121. //
  122. // Static method with no return value.
  123. //
  124. template <class... P>
  125. class CallableCustomStaticMethodPointer : public CallableCustomMethodPointerBase {
  126. void (*method)(P...);
  127. public:
  128. virtual Object *get_object() const override {
  129. return nullptr;
  130. }
  131. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
  132. call_with_variant_args_static_ret(method, p_arguments, p_argcount, r_return_value, r_call_error);
  133. r_return_value = Variant();
  134. }
  135. CallableCustomStaticMethodPointer(void (*p_method)(P...)) {
  136. method = p_method;
  137. }
  138. };
  139. template <class... P>
  140. Callable create_custom_callable_static_function_pointer(void (*p_method)(P...)) {
  141. typedef CallableCustomStaticMethodPointer<P...> CCMP;
  142. CCMP *ccmp = memnew(CCMP(p_method));
  143. return ::godot::internal::create_custom_callable(ccmp);
  144. }
  145. //
  146. // Static method with return value.
  147. //
  148. template <class R, class... P>
  149. class CallableCustomStaticMethodPointerRet : public CallableCustomMethodPointerBase {
  150. R(*method)
  151. (P...);
  152. public:
  153. virtual Object *get_object() const override {
  154. return nullptr;
  155. }
  156. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
  157. call_with_variant_args_static_ret(method, p_arguments, p_argcount, r_return_value, r_call_error);
  158. }
  159. CallableCustomStaticMethodPointerRet(R (*p_method)(P...)) {
  160. method = p_method;
  161. }
  162. };
  163. template <class R, class... P>
  164. Callable create_custom_callable_static_function_pointer(R (*p_method)(P...)) {
  165. typedef CallableCustomStaticMethodPointerRet<R, P...> CCMP;
  166. CCMP *ccmp = memnew(CCMP(p_method));
  167. return ::godot::internal::create_custom_callable(ccmp);
  168. }
  169. //
  170. // The API:
  171. //
  172. #define callable_mp(I, M) ::godot::create_custom_callable_function_pointer(I, M)
  173. #define callable_mp_static(M) ::godot::create_custom_callable_static_function_pointer(M)
  174. } // namespace godot
  175. #endif // GODOT_CALLABLE_METHOD_POINTER_HPP