Skip to content

Commit 73f7a8b

Browse files
committed
Add CallableCustom that devs can use in their GDExtensions
1 parent 0a6a19e commit 73f7a8b

File tree

10 files changed

+221
-13
lines changed

10 files changed

+221
-13
lines changed

binding_generator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,9 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
416416
if class_name == "Array":
417417
result.append("#include <godot_cpp/variant/array_helpers.hpp>")
418418

419+
if class_name == "Callable":
420+
result.append("#include <godot_cpp/variant/callable_custom.hpp>")
421+
419422
for include in fully_used_classes:
420423
if include == "TypedArray":
421424
result.append("#include <godot_cpp/variant/typed_array.hpp>")
@@ -525,6 +528,9 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
525528
result.append(f"\t{class_name}(const wchar_t *from);")
526529
result.append(f"\t{class_name}(const char16_t *from);")
527530
result.append(f"\t{class_name}(const char32_t *from);")
531+
if class_name == "Callable":
532+
result.append("\tCallable(CallableCustom *p_custom);")
533+
result.append("\tCallableCustom *get_custom() const;")
528534

529535
if "constants" in builtin_api:
530536
axis_constants_count = 0

include/godot_cpp/godot.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ extern "C" GDExtensionInterfaceObjectCastTo gdextension_interface_object_cast_to
166166
extern "C" GDExtensionInterfaceObjectGetInstanceFromId gdextension_interface_object_get_instance_from_id;
167167
extern "C" GDExtensionInterfaceObjectGetInstanceId gdextension_interface_object_get_instance_id;
168168
extern "C" GDExtensionInterfaceCallableCustomCreate gdextension_interface_callable_custom_create;
169+
extern "C" GDExtensionInterfaceCallableCustomGetUserData gdextension_interface_callable_custom_get_userdata;
169170
extern "C" GDExtensionInterfaceRefGetObject gdextension_interface_ref_get_object;
170171
extern "C" GDExtensionInterfaceRefSetObject gdextension_interface_ref_set_object;
171172
extern "C" GDExtensionInterfaceScriptInstanceCreate2 gdextension_interface_script_instance_create2;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**************************************************************************/
2+
/* callable_custom.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_CUSTOM_HPP
32+
#define GODOT_CALLABLE_CUSTOM_HPP
33+
34+
#include <godot_cpp/variant/string_name.hpp>
35+
36+
namespace godot {
37+
38+
class Object;
39+
40+
class CallableCustom {
41+
public:
42+
typedef GDExtensionBool (*CompareEqualFunc)(const CallableCustom *p_a, const CallableCustom *p_b);
43+
typedef GDExtensionBool (*CompareLessFunc)(const CallableCustom *p_a, const CallableCustom *p_b);
44+
45+
virtual uint32_t hash() const = 0;
46+
virtual String get_as_text() const = 0;
47+
virtual CompareEqualFunc get_compare_equal_func() const = 0;
48+
virtual CompareLessFunc get_compare_less_func() const = 0;
49+
virtual bool is_valid() const = 0;
50+
virtual Object *get_object() const = 0;
51+
virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const = 0;
52+
53+
virtual ~CallableCustom() {}
54+
};
55+
56+
} // namespace godot
57+
58+
#endif // GODOT_CALLABLE_CUSTOM_HPP

include/godot_cpp/variant/callable_method_pointer.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class CallableCustomMethodPointerBase {
4545

4646
namespace internal {
4747

48-
Callable create_custom_callable(CallableCustomMethodPointerBase *p_callable_method_pointer);
48+
Callable create_callable_from_ccmp(CallableCustomMethodPointerBase *p_callable_method_pointer);
4949

5050
} // namespace internal
5151

@@ -77,7 +77,7 @@ template <class T, class... P>
7777
Callable create_custom_callable_function_pointer(T *p_instance, void (T::*p_method)(P...)) {
7878
typedef CallableCustomMethodPointer<T, P...> CCMP;
7979
CCMP *ccmp = memnew(CCMP(p_instance, p_method));
80-
return ::godot::internal::create_custom_callable(ccmp);
80+
return ::godot::internal::create_callable_from_ccmp(ccmp);
8181
}
8282

8383
//
@@ -109,7 +109,7 @@ template <class T, class R, class... P>
109109
Callable create_custom_callable_function_pointer(T *p_instance, R (T::*p_method)(P...)) {
110110
typedef CallableCustomMethodPointerRet<T, R, P...> CCMP; // Messes with memnew otherwise.
111111
CCMP *ccmp = memnew(CCMP(p_instance, p_method));
112-
return ::godot::internal::create_custom_callable(ccmp);
112+
return ::godot::internal::create_callable_from_ccmp(ccmp);
113113
}
114114

115115
//
@@ -141,7 +141,7 @@ template <class T, class R, class... P>
141141
Callable create_custom_callable_function_pointer(const T *p_instance, R (T::*p_method)(P...) const) {
142142
typedef CallableCustomMethodPointerRetC<T, R, P...> CCMP; // Messes with memnew otherwise.
143143
CCMP *ccmp = memnew(CCMP(p_instance, p_method));
144-
return ::godot::internal::create_custom_callable(ccmp);
144+
return ::godot::internal::create_callable_from_ccmp(ccmp);
145145
}
146146

147147
//
@@ -171,7 +171,7 @@ template <class... P>
171171
Callable create_custom_callable_static_function_pointer(void (*p_method)(P...)) {
172172
typedef CallableCustomStaticMethodPointer<P...> CCMP;
173173
CCMP *ccmp = memnew(CCMP(p_method));
174-
return ::godot::internal::create_custom_callable(ccmp);
174+
return ::godot::internal::create_callable_from_ccmp(ccmp);
175175
}
176176

177177
//
@@ -201,7 +201,7 @@ template <class R, class... P>
201201
Callable create_custom_callable_static_function_pointer(R (*p_method)(P...)) {
202202
typedef CallableCustomStaticMethodPointerRet<R, P...> CCMP;
203203
CCMP *ccmp = memnew(CCMP(p_method));
204-
return ::godot::internal::create_custom_callable(ccmp);
204+
return ::godot::internal::create_callable_from_ccmp(ccmp);
205205
}
206206

207207
//

src/godot.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ GDExtensionInterfaceObjectCastTo gdextension_interface_object_cast_to = nullptr;
172172
GDExtensionInterfaceObjectGetInstanceFromId gdextension_interface_object_get_instance_from_id = nullptr;
173173
GDExtensionInterfaceObjectGetInstanceId gdextension_interface_object_get_instance_id = nullptr;
174174
GDExtensionInterfaceCallableCustomCreate gdextension_interface_callable_custom_create = nullptr;
175+
GDExtensionInterfaceCallableCustomGetUserData gdextension_interface_callable_custom_get_userdata = nullptr;
175176
GDExtensionInterfaceRefGetObject gdextension_interface_ref_get_object = nullptr;
176177
GDExtensionInterfaceRefSetObject gdextension_interface_ref_set_object = nullptr;
177178
GDExtensionInterfaceScriptInstanceCreate2 gdextension_interface_script_instance_create2 = nullptr;
@@ -390,6 +391,7 @@ GDExtensionBool GDExtensionBinding::init(GDExtensionInterfaceGetProcAddress p_ge
390391
LOAD_PROC_ADDRESS(object_get_instance_from_id, GDExtensionInterfaceObjectGetInstanceFromId);
391392
LOAD_PROC_ADDRESS(object_get_instance_id, GDExtensionInterfaceObjectGetInstanceId);
392393
LOAD_PROC_ADDRESS(callable_custom_create, GDExtensionInterfaceCallableCustomCreate);
394+
LOAD_PROC_ADDRESS(callable_custom_get_userdata, GDExtensionInterfaceCallableCustomGetUserData);
393395
LOAD_PROC_ADDRESS(ref_get_object, GDExtensionInterfaceRefGetObject);
394396
LOAD_PROC_ADDRESS(ref_set_object, GDExtensionInterfaceRefSetObject);
395397
LOAD_PROC_ADDRESS(script_instance_create2, GDExtensionInterfaceScriptInstanceCreate2);

src/variant/callable_custom.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**************************************************************************/
2+
/* callable_custom.cpp */
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+
#include <godot_cpp/variant/callable_custom.hpp>
32+
33+
#include <godot_cpp/classes/object.hpp>
34+
#include <godot_cpp/variant/callable.hpp>
35+
36+
namespace godot {
37+
38+
static void callable_custom_call(void *p_userdata, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_argument_count, GDExtensionVariantPtr r_return, GDExtensionCallError *r_error) {
39+
CallableCustom *callable_custom = (CallableCustom *)p_userdata;
40+
callable_custom->call((const Variant **)p_args, p_argument_count, *(Variant *)r_return, *r_error);
41+
}
42+
43+
static GDExtensionBool callable_custom_is_valid(void *p_userdata) {
44+
CallableCustom *callable_custom = (CallableCustom *)p_userdata;
45+
return callable_custom->is_valid();
46+
}
47+
48+
static void callable_custom_free(void *p_userdata) {
49+
CallableCustom *callable_custom = (CallableCustom *)p_userdata;
50+
memdelete(callable_custom);
51+
}
52+
53+
static uint32_t callable_custom_hash(void *p_userdata) {
54+
CallableCustom *callable_custom = (CallableCustom *)p_userdata;
55+
return callable_custom->hash();
56+
}
57+
58+
static void callable_custom_to_string(void *p_userdata, GDExtensionBool *r_is_valid, GDExtensionStringPtr r_out) {
59+
CallableCustom *callable_custom = (CallableCustom *)p_userdata;
60+
*((String *)r_out) = callable_custom->get_as_text();
61+
*r_is_valid = true;
62+
}
63+
64+
Callable::Callable(CallableCustom *p_callable_custom) {
65+
Object *object = p_callable_custom->get_object();
66+
67+
GDExtensionCallableCustomInfo info = {};
68+
info.callable_userdata = p_callable_custom;
69+
info.token = internal::token;
70+
info.object = object != nullptr ? object->_owner : nullptr;
71+
info.call_func = &callable_custom_call;
72+
info.is_valid_func = &callable_custom_is_valid;
73+
info.free_func = &callable_custom_free;
74+
info.hash_func = &callable_custom_hash;
75+
info.equal_func = (GDExtensionCallableCustomEqual)p_callable_custom->get_compare_equal_func();
76+
info.less_than_func = (GDExtensionCallableCustomLessThan)p_callable_custom->get_compare_less_func();
77+
info.to_string_func = &callable_custom_to_string;
78+
79+
::godot::internal::gdextension_interface_callable_custom_create(_native_ptr(), &info);
80+
}
81+
82+
CallableCustom *Callable::get_custom() const {
83+
// @todo This isn't a safe cast if this callable was created via `callable_mp()` - but if the classes shared a base class, we could use `dynamic_cast()` here.
84+
return (CallableCustom *)::godot::internal::gdextension_interface_callable_custom_get_userdata(_native_ptr(), internal::token);
85+
}
86+
87+
} // namespace godot

src/variant/callable_method_pointer.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,29 @@
3030

3131
#include <godot_cpp/variant/callable_method_pointer.hpp>
3232

33-
//#include <godot_cpp/godot.hpp>
34-
3533
namespace godot {
3634

37-
static void call_custom_callable(void *userdata, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_argument_count, GDExtensionVariantPtr r_return, GDExtensionCallError *r_error) {
35+
static void custom_callable_mp_call(void *userdata, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_argument_count, GDExtensionVariantPtr r_return, GDExtensionCallError *r_error) {
3836
CallableCustomMethodPointerBase *callable_method_pointer = (CallableCustomMethodPointerBase *)userdata;
3937
callable_method_pointer->call((const Variant **)p_args, p_argument_count, *(Variant *)r_return, *r_error);
4038
}
4139

42-
static void free_custom_callable(void *userdata) {
40+
static void custom_callable_mp_free(void *userdata) {
4341
CallableCustomMethodPointerBase *callable_method_pointer = (CallableCustomMethodPointerBase *)userdata;
4442
memdelete(callable_method_pointer);
4543
}
4644

4745
namespace internal {
4846

49-
Callable create_custom_callable(CallableCustomMethodPointerBase *p_callable_method_pointer) {
47+
Callable create_callable_from_ccmp(CallableCustomMethodPointerBase *p_callable_method_pointer) {
5048
Object *object = p_callable_method_pointer->get_object();
5149

5250
GDExtensionCallableCustomInfo info = {};
5351
info.callable_userdata = p_callable_method_pointer;
5452
info.token = internal::token;
5553
info.object = object != nullptr ? object->_owner : nullptr;
56-
info.call_func = &call_custom_callable;
57-
info.free_func = &free_custom_callable;
54+
info.call_func = &custom_callable_mp_call;
55+
info.free_func = &custom_callable_mp_free;
5856

5957
Callable callable;
6058
::godot::internal::gdextension_interface_callable_custom_create(callable._native_ptr(), &info);

test/project/main.gd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ func _ready():
118118
var mp_callable_static_ret: Callable = example.test_callable_mp_static_ret()
119119
assert_equal(mp_callable_static_ret.call(example, "static-ret", 84), "unbound_static_method2: Example - static-ret - 84")
120120

121+
# CallableCustom.
122+
var custom_callable: Callable = example.test_custom_callable();
123+
assert_equal(custom_callable.is_custom(), true);
124+
assert_equal(custom_callable.is_valid(), true);
125+
assert_equal(custom_callable.call(), "Hi")
126+
assert_equal(custom_callable.hash(), 27);
127+
assert_equal(custom_callable.get_object(), null);
128+
assert_equal(custom_callable.get_method(), "");
129+
assert_equal(str(custom_callable), "<MyCallableCustom>");
130+
121131
# PackedArray iterators
122132
assert_equal(example.test_vector_ops(), 105)
123133

test/src/example.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,46 @@
1515

1616
using namespace godot;
1717

18+
class MyCallableCustom : public CallableCustom {
19+
public:
20+
virtual uint32_t hash() const {
21+
return 27;
22+
}
23+
24+
virtual String get_as_text() const {
25+
return "<MyCallableCustom>";
26+
}
27+
28+
static GDExtensionBool compare_equal_func(const CallableCustom *p_a, const CallableCustom *p_b) {
29+
return p_a == p_b;
30+
}
31+
32+
virtual CompareEqualFunc get_compare_equal_func() const {
33+
return &MyCallableCustom::compare_equal_func;
34+
}
35+
36+
static GDExtensionBool compare_less_func(const CallableCustom *p_a, const CallableCustom *p_b) {
37+
return (void *)p_a < (void *)p_b;
38+
}
39+
40+
virtual CompareLessFunc get_compare_less_func() const {
41+
return &MyCallableCustom::compare_less_func;
42+
}
43+
44+
bool is_valid() const {
45+
return true;
46+
}
47+
48+
virtual Object *get_object() const {
49+
return nullptr;
50+
}
51+
52+
virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const {
53+
r_return_value = "Hi";
54+
r_call_error.error = GDEXTENSION_CALL_OK;
55+
}
56+
};
57+
1858
void ExampleRef::set_id(int p_id) {
1959
id = p_id;
2060
}
@@ -168,6 +208,7 @@ void Example::_bind_methods() {
168208
ClassDB::bind_method(D_METHOD("test_callable_mp_retc"), &Example::test_callable_mp_retc);
169209
ClassDB::bind_method(D_METHOD("test_callable_mp_static"), &Example::test_callable_mp_static);
170210
ClassDB::bind_method(D_METHOD("test_callable_mp_static_ret"), &Example::test_callable_mp_static_ret);
211+
ClassDB::bind_method(D_METHOD("test_custom_callable"), &Example::test_custom_callable);
171212

172213
ClassDB::bind_method(D_METHOD("test_bitfield", "flags"), &Example::test_bitfield);
173214

@@ -375,6 +416,10 @@ Callable Example::test_callable_mp_static_ret() const {
375416
return callable_mp_static(&Example::unbound_static_method2);
376417
}
377418

419+
Callable Example::test_custom_callable() const {
420+
return Callable(memnew(MyCallableCustom));
421+
}
422+
378423
void Example::unbound_method1(Object *p_object, String p_string, int p_int) {
379424
String test = "unbound_method1: ";
380425
test += p_object->get_class();

test/src/example.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class Example : public Control {
143143
Callable test_callable_mp_retc() const;
144144
Callable test_callable_mp_static() const;
145145
Callable test_callable_mp_static_ret() const;
146+
Callable test_custom_callable() const;
146147

147148
void unbound_method1(Object *p_object, String p_string, int p_int);
148149
String unbound_method2(Object *p_object, String p_string, int p_int);

0 commit comments

Comments
 (0)