callable_method_pointer.hpp 7.5 KB

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