|  | 
|  | 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 | + | 
|  | 31 | +#ifndef GODOT_CALLABLE_METHOD_POINTER_HPP | 
|  | 32 | +#define GODOT_CALLABLE_METHOD_POINTER_HPP | 
|  | 33 | + | 
|  | 34 | +#include <godot_cpp/core/binder_common.hpp> | 
|  | 35 | +#include <godot_cpp/variant/variant.hpp> | 
|  | 36 | + | 
|  | 37 | +namespace godot { | 
|  | 38 | + | 
|  | 39 | +class CallableCustomMethodPointerBase { | 
|  | 40 | +public: | 
|  | 41 | +virtual Object *get_object() const = 0; | 
|  | 42 | +virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const = 0; | 
|  | 43 | +}; | 
|  | 44 | + | 
|  | 45 | +namespace internal { | 
|  | 46 | + | 
|  | 47 | +Callable create_custom_callable(CallableCustomMethodPointerBase *p_callable_method_pointer); | 
|  | 48 | + | 
|  | 49 | +} // namespace internal | 
|  | 50 | + | 
|  | 51 | +// | 
|  | 52 | +// No return value. | 
|  | 53 | +// | 
|  | 54 | + | 
|  | 55 | +template <class T, class... P> | 
|  | 56 | +class CallableCustomMethodPointer : public CallableCustomMethodPointerBase { | 
|  | 57 | +T *instance; | 
|  | 58 | +void (T::*method)(P...); | 
|  | 59 | + | 
|  | 60 | +public: | 
|  | 61 | +virtual Object *get_object() const override { | 
|  | 62 | +return instance; | 
|  | 63 | +} | 
|  | 64 | + | 
|  | 65 | +virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override { | 
|  | 66 | +call_with_variant_args(instance, method, p_arguments, p_argcount, r_call_error); | 
|  | 67 | +} | 
|  | 68 | + | 
|  | 69 | +CallableCustomMethodPointer(T *p_instance, void (T::*p_method)(P...)) { | 
|  | 70 | +instance = p_instance; | 
|  | 71 | +method = p_method; | 
|  | 72 | +} | 
|  | 73 | +}; | 
|  | 74 | + | 
|  | 75 | +template <class T, class... P> | 
|  | 76 | +Callable create_custom_callable_function_pointer(T *p_instance, void (T::*p_method)(P...)) { | 
|  | 77 | +typedef CallableCustomMethodPointer<T, P...> CCMP; | 
|  | 78 | +CCMP *ccmp = memnew(CCMP(p_instance, p_method)); | 
|  | 79 | +return ::godot::internal::create_custom_callable(ccmp); | 
|  | 80 | +} | 
|  | 81 | + | 
|  | 82 | +// | 
|  | 83 | +// With return value. | 
|  | 84 | +// | 
|  | 85 | + | 
|  | 86 | +template <class T, class R, class... P> | 
|  | 87 | +class CallableCustomMethodPointerRet : public CallableCustomMethodPointerBase { | 
|  | 88 | +T *instance; | 
|  | 89 | +R(T::*method) | 
|  | 90 | +(P...); | 
|  | 91 | + | 
|  | 92 | +public: | 
|  | 93 | +virtual Object *get_object() const override { | 
|  | 94 | +return instance; | 
|  | 95 | +} | 
|  | 96 | + | 
|  | 97 | +virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override { | 
|  | 98 | +call_with_variant_args_ret(instance, method, p_arguments, p_argcount, r_return_value, r_call_error); | 
|  | 99 | +} | 
|  | 100 | + | 
|  | 101 | +CallableCustomMethodPointerRet(T *p_instance, R (T::*p_method)(P...)) { | 
|  | 102 | +instance = p_instance; | 
|  | 103 | +method = p_method; | 
|  | 104 | +} | 
|  | 105 | +}; | 
|  | 106 | + | 
|  | 107 | +template <class T, class R, class... P> | 
|  | 108 | +Callable create_custom_callable_function_pointer(T *p_instance, R (T::*p_method)(P...)) { | 
|  | 109 | +typedef CallableCustomMethodPointerRet<T, R, P...> CCMP; // Messes with memnew otherwise. | 
|  | 110 | +CCMP *ccmp = memnew(CCMP(p_instance, p_method)); | 
|  | 111 | +return ::godot::internal::create_custom_callable(ccmp); | 
|  | 112 | +} | 
|  | 113 | + | 
|  | 114 | +// | 
|  | 115 | +// Const with return value. | 
|  | 116 | +// | 
|  | 117 | + | 
|  | 118 | +template <class T, class R, class... P> | 
|  | 119 | +class CallableCustomMethodPointerRetC : public CallableCustomMethodPointerBase { | 
|  | 120 | +T *instance; | 
|  | 121 | +R(T::*method) | 
|  | 122 | +(P...) const; | 
|  | 123 | + | 
|  | 124 | +public: | 
|  | 125 | +virtual Object *get_object() const override { | 
|  | 126 | +return instance; | 
|  | 127 | +} | 
|  | 128 | + | 
|  | 129 | +virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const override { | 
|  | 130 | +call_with_variant_args_retc(instance, method, p_arguments, p_argcount, r_return_value, r_call_error); | 
|  | 131 | +} | 
|  | 132 | + | 
|  | 133 | +CallableCustomMethodPointerRetC(T *p_instance, R (T::*p_method)(P...) const) { | 
|  | 134 | +instance = p_instance; | 
|  | 135 | +method = p_method; | 
|  | 136 | +} | 
|  | 137 | +}; | 
|  | 138 | + | 
|  | 139 | +template <class T, class R, class... P> | 
|  | 140 | +Callable create_custom_callable_function_pointer(T *p_instance, R (T::*p_method)(P...) const) { | 
|  | 141 | +typedef CallableCustomMethodPointerRetC<T, R, P...> CCMP; // Messes with memnew otherwise. | 
|  | 142 | +CCMP *ccmp = memnew(CCMP(p_instance, p_method)); | 
|  | 143 | +return ::godot::internal::create_custom_callable(ccmp); | 
|  | 144 | +} | 
|  | 145 | + | 
|  | 146 | +// The actual API: | 
|  | 147 | + | 
|  | 148 | +#define callable_mp(I, M) ::godot::create_custom_callable_function_pointer(I, M) | 
|  | 149 | + | 
|  | 150 | +} // namespace godot | 
|  | 151 | + | 
|  | 152 | +#endif // GODOT_CALLABLE_METHOD_POINTER_HPP | 
0 commit comments