Skip to content

Commit fbaf79a

Browse files
Set instance and instance binding in Wrapped constructor.
1 parent ad307e4 commit fbaf79a

File tree

6 files changed

+71
-28
lines changed

6 files changed

+71
-28
lines changed

include/godot_cpp/classes/wrapped.hpp

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
#include <godot_cpp/godot.hpp>
4242

4343
namespace godot {
44-
44+
namespace internal {
45+
_ALWAYS_INLINE_ void _set_construct_info(const StringName *p_extension_class_name, const GDExtensionInstanceBindingCallbacks *p_binding_callbacks);
46+
} //namespace internal
4547
class ClassDB;
4648

4749
typedef void GodotObject;
@@ -52,7 +54,15 @@ class Wrapped {
5254
friend class ClassDB;
5355
friend void postinitialize_handler(Wrapped *);
5456

57+
friend _ALWAYS_INLINE_ void internal::_set_construct_info(const StringName *p_extension_class_name, const GDExtensionInstanceBindingCallbacks *p_binding_callbacks);
58+
59+
thread_local static const StringName *_constructing_extension_class_name;
60+
thread_local static const GDExtensionInstanceBindingCallbacks *_constructing_class_binding_callbacks;
61+
5562
protected:
63+
bool has_object_instance_binding() const;
64+
virtual bool _is_extension_class() const { return false; }
65+
5666
#ifdef HOT_RELOAD_ENABLED
5767
struct RecreateInstance {
5868
GDExtensionClassInstancePtr wrapper;
@@ -62,9 +72,6 @@ class Wrapped {
6272
inline static RecreateInstance *recreate_instance = nullptr;
6373
#endif
6474

65-
virtual const StringName *_get_extension_class_name() const; // This is needed to retrieve the class name before the godot object has its _extension and _extension_instance members assigned.
66-
virtual const GDExtensionInstanceBindingCallbacks *_get_bindings_callbacks() const = 0;
67-
6875
void _notification(int p_what) {}
6976
bool _set(const StringName &p_name, const Variant &p_property) { return false; }
7077
bool _get(const StringName &p_name, Variant &r_property) const { return false; }
@@ -95,6 +102,8 @@ class Wrapped {
95102
virtual ~Wrapped() {}
96103

97104
public:
105+
static const StringName *_get_extension_class_name(); // This is needed to retrieve the class name before the godot object has its _extension and _extension_instance members assigned.
106+
98107
static StringName &get_class_static() {
99108
static StringName string_name = StringName("Wrapped");
100109
return string_name;
@@ -108,6 +117,18 @@ class Wrapped {
108117
GodotObject *_owner = nullptr;
109118
};
110119

120+
namespace internal {
121+
_ALWAYS_INLINE_ void _set_construct_info(const StringName *p_extension_class_name, const GDExtensionInstanceBindingCallbacks *p_binding_callbacks) {
122+
Wrapped::_constructing_extension_class_name = p_extension_class_name;
123+
Wrapped::_constructing_class_binding_callbacks = p_binding_callbacks;
124+
}
125+
} //namespace internal
126+
127+
template <typename T, std::enable_if_t<std::is_base_of<::godot::Wrapped, T>::value, bool> = true>
128+
_ALWAYS_INLINE_ void _pre_initialize() {
129+
internal::_set_construct_info(T::_get_extension_class_name(), &T::_gde_binding_callbacks);
130+
}
131+
111132
_FORCE_INLINE_ void snarray_add_str(Vector<StringName> &arr) {
112133
}
113134

@@ -162,15 +183,7 @@ private:
162183
friend class ::godot::ClassDB; \
163184
\
164185
protected: \
165-
virtual const ::godot::StringName *_get_extension_class_name() const override { \
166-
static ::godot::StringName string_name = get_class_static(); \
167-
return &string_name; \
168-
} \
169-
\
170-
virtual const GDExtensionInstanceBindingCallbacks *_get_bindings_callbacks() const override { \
171-
return &_gde_binding_callbacks; \
172-
} \
173-
\
186+
virtual bool _is_extension_class() const override { return true; } \
174187
static void (*_get_bind_methods())() { \
175188
return &m_class::_bind_methods; \
176189
} \
@@ -213,6 +226,11 @@ protected:
213226
} \
214227
\
215228
public: \
229+
static const ::godot::StringName *_get_extension_class_name() { \
230+
static ::godot::StringName string_name = get_class_static(); \
231+
return &string_name; \
232+
} \
233+
\
216234
typedef m_class self_type; \
217235
\
218236
static void initialize_class() { \
@@ -383,10 +401,6 @@ private:
383401
void operator=(const m_class &p_rval) {} \
384402
\
385403
protected: \
386-
virtual const GDExtensionInstanceBindingCallbacks *_get_bindings_callbacks() const override { \
387-
return &_gde_binding_callbacks; \
388-
} \
389-
\
390404
m_class(const char *p_godot_class) : m_inherits(p_godot_class) {} \
391405
m_class(GodotObject *p_godot_object) : m_inherits(p_godot_object) {} \
392406
\

include/godot_cpp/core/memory.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ class Memory {
8282
static void free_static(void *p_ptr, bool p_pad_align = false);
8383
};
8484

85+
template <typename T, std::enable_if_t<!std::is_base_of<::godot::Wrapped, T>::value, bool> = true>
86+
_ALWAYS_INLINE_ void _pre_initialize() {}
87+
8588
_ALWAYS_INLINE_ void postinitialize_handler(void *) {}
8689

8790
template <typename T>
@@ -94,10 +97,10 @@ _ALWAYS_INLINE_ T *_post_initialize(T *p_obj) {
9497
#define memrealloc(m_mem, m_size) ::godot::Memory::realloc_static(m_mem, m_size)
9598
#define memfree(m_mem) ::godot::Memory::free_static(m_mem)
9699

97-
#define memnew(m_class) ::godot::_post_initialize(new ("", "") m_class)
100+
#define memnew(m_class) (::godot::_pre_initialize<std::remove_pointer_t<decltype(new ("", "") m_class)>>(), ::godot::_post_initialize(new ("", "") m_class))
98101

99-
#define memnew_allocator(m_class, m_allocator) ::godot::_post_initialize(new ("", m_allocator::alloc) m_class)
100-
#define memnew_placement(m_placement, m_class) ::godot::_post_initialize(new ("", m_placement, sizeof(m_class), "") m_class)
102+
#define memnew_allocator(m_class, m_allocator) (::godot::_pre_initialize<std::remove_pointer_t<decltype(new ("", "") m_class)>>(), ::godot::_post_initialize(new ("", m_allocator::alloc) m_class))
103+
#define memnew_placement(m_placement, m_class) (::godot::_pre_initialize<std::remove_pointer_t<decltype(new ("", "") m_class)>>(), ::godot::_post_initialize(new ("", m_placement, sizeof(m_class), "") m_class))
101104

102105
// Generic comparator used in Map, List, etc.
103106
template <typename T>

src/classes/wrapped.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@
3939
#include <godot_cpp/core/class_db.hpp>
4040

4141
namespace godot {
42+
thread_local const StringName *Wrapped::_constructing_extension_class_name = nullptr;
43+
thread_local const GDExtensionInstanceBindingCallbacks *Wrapped::_constructing_class_binding_callbacks = nullptr;
4244

43-
const StringName *Wrapped::_get_extension_class_name() const {
45+
bool Wrapped::has_object_instance_binding() const {
46+
return internal::gdextension_interface_object_get_instance_binding(_owner, internal::token, nullptr);
47+
}
48+
49+
const StringName *Wrapped::_get_extension_class_name() {
4450
return nullptr;
4551
}
4652

4753
void Wrapped::_postinitialize() {
48-
const StringName *extension_class = _get_extension_class_name();
49-
if (extension_class) {
50-
godot::internal::gdextension_interface_object_set_instance(_owner, reinterpret_cast<GDExtensionConstStringNamePtr>(extension_class), this);
51-
}
52-
godot::internal::gdextension_interface_object_set_instance_binding(_owner, godot::internal::token, this, _get_bindings_callbacks());
53-
if (extension_class) {
54+
// Only send NOTIFICATION_POSTINITIALIZE for extension classes.
55+
if (_is_extension_class()) {
5456
Object *obj = dynamic_cast<Object *>(this);
5557
if (obj) {
5658
obj->notification(Object::NOTIFICATION_POSTINITIALIZE);
@@ -79,6 +81,16 @@ Wrapped::Wrapped(const StringName p_godot_class) {
7981
}
8082
#endif
8183
_owner = godot::internal::gdextension_interface_classdb_construct_object(reinterpret_cast<GDExtensionConstStringNamePtr>(p_godot_class._native_ptr()));
84+
85+
if (_constructing_extension_class_name) {
86+
godot::internal::gdextension_interface_object_set_instance(_owner, reinterpret_cast<GDExtensionConstStringNamePtr>(_constructing_extension_class_name), this);
87+
_constructing_extension_class_name = nullptr;
88+
}
89+
90+
if (_constructing_class_binding_callbacks) {
91+
godot::internal::gdextension_interface_object_set_instance_binding(_owner, godot::internal::token, this, _constructing_class_binding_callbacks);
92+
_constructing_class_binding_callbacks = nullptr;
93+
}
8294
}
8395

8496
Wrapped::Wrapped(GodotObject *p_godot_object) {

test/project/main.gd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class TestClass:
99
func _ready():
1010
var example: Example = $Example
1111

12+
# Timing of set instance binding.
13+
assert_equal(example.is_object_binding_set_by_parent_constructor(), true)
14+
1215
# Signal.
1316
example.emit_custom_signal("Button", 42)
1417
assert_equal(custom_signal_emitted, ["Button", 42])

test/src/example.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ void Example::_bind_methods() {
192192
ClassDB::bind_method(D_METHOD("return_extended_ref"), &Example::return_extended_ref);
193193
ClassDB::bind_method(D_METHOD("extended_ref_checks", "ref"), &Example::extended_ref_checks);
194194

195+
ClassDB::bind_method(D_METHOD("is_object_binding_set_by_parent_constructor"), &Example::is_object_binding_set_by_parent_constructor);
196+
195197
ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array);
196198
ClassDB::bind_method(D_METHOD("test_tarray_arg", "array"), &Example::test_tarray_arg);
197199
ClassDB::bind_method(D_METHOD("test_tarray"), &Example::test_tarray);
@@ -287,7 +289,8 @@ void Example::_bind_methods() {
287289
BIND_ENUM_CONSTANT(OUTSIDE_OF_CLASS);
288290
}
289291

290-
Example::Example() {
292+
Example::Example() :
293+
object_instance_binding_set_by_parent_constructor(has_object_instance_binding()) {
291294
//UtilityFunctions::print("Constructor.");
292295
}
293296

@@ -367,6 +370,10 @@ void Example::emit_custom_signal(const String &name, int value) {
367370
emit_signal("custom_signal", name, value);
368371
}
369372

373+
bool Example::is_object_binding_set_by_parent_constructor() const {
374+
return object_instance_binding_set_by_parent_constructor;
375+
}
376+
370377
Array Example::test_array() const {
371378
Array arr;
372379

test/src/example.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ class Example : public Control {
8282
Vector2 dprop[3];
8383
int last_rpc_arg = 0;
8484

85+
const bool object_instance_binding_set_by_parent_constructor;
86+
8587
public:
8688
// Constants.
8789
enum Constants {
@@ -120,6 +122,8 @@ class Example : public Control {
120122
void emit_custom_signal(const String &name, int value);
121123
int def_args(int p_a = 100, int p_b = 200);
122124

125+
bool is_object_binding_set_by_parent_constructor() const;
126+
123127
Array test_array() const;
124128
int test_tarray_arg(const TypedArray<int64_t> &p_array);
125129
TypedArray<Vector2> test_tarray() const;

0 commit comments

Comments
 (0)