Skip to content

Commit 2c6ef7a

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

File tree

6 files changed

+77
-29
lines changed

6 files changed

+77
-29
lines changed

include/godot_cpp/classes/wrapped.hpp

Lines changed: 30 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,14 @@ 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+
virtual bool _is_extension_class() const { return false; }
64+
5665
#ifdef HOT_RELOAD_ENABLED
5766
struct RecreateInstance {
5867
GDExtensionClassInstancePtr wrapper;
@@ -62,9 +71,6 @@ class Wrapped {
6271
inline static RecreateInstance *recreate_instance = nullptr;
6372
#endif
6473

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-
6874
void _notification(int p_what) {}
6975
bool _set(const StringName &p_name, const Variant &p_property) { return false; }
7076
bool _get(const StringName &p_name, Variant &r_property) const { return false; }
@@ -95,6 +101,8 @@ class Wrapped {
95101
virtual ~Wrapped() {}
96102

97103
public:
104+
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.
105+
98106
static StringName &get_class_static() {
99107
static StringName string_name = StringName("Wrapped");
100108
return string_name;
@@ -108,6 +116,18 @@ class Wrapped {
108116
GodotObject *_owner = nullptr;
109117
};
110118

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

@@ -162,15 +182,7 @@ private:
162182
friend class ::godot::ClassDB; \
163183
\
164184
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-
\
185+
virtual bool _is_extension_class() const override { return true; } \
174186
static void (*_get_bind_methods())() { \
175187
return &m_class::_bind_methods; \
176188
} \
@@ -213,6 +225,11 @@ protected:
213225
} \
214226
\
215227
public: \
228+
static const ::godot::StringName *_get_extension_class_name() { \
229+
const ::godot::StringName &string_name = get_class_static(); \
230+
return &string_name; \
231+
} \
232+
\
216233
typedef m_class self_type; \
217234
\
218235
static void initialize_class() { \
@@ -383,10 +400,6 @@ private:
383400
void operator=(const m_class &p_rval) {} \
384401
\
385402
protected: \
386-
virtual const GDExtensionInstanceBindingCallbacks *_get_bindings_callbacks() const override { \
387-
return &_gde_binding_callbacks; \
388-
} \
389-
\
390403
m_class(const char *p_godot_class) : m_inherits(p_godot_class) {} \
391404
m_class(GodotObject *p_godot_object) : m_inherits(p_godot_object) {} \
392405
\

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: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,16 @@
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+
const StringName *Wrapped::_get_extension_class_name() {
4446
return nullptr;
4547
}
4648

4749
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) {
50+
// Only send NOTIFICATION_POSTINITIALIZE for extension classes.
51+
if (_is_extension_class()) {
5452
Object *obj = dynamic_cast<Object *>(this);
5553
if (obj) {
5654
obj->notification(Object::NOTIFICATION_POSTINITIALIZE);
@@ -79,6 +77,16 @@ Wrapped::Wrapped(const StringName p_godot_class) {
7977
}
8078
#endif
8179
_owner = godot::internal::gdextension_interface_classdb_construct_object(reinterpret_cast<GDExtensionConstStringNamePtr>(p_godot_class._native_ptr()));
80+
81+
if (_constructing_extension_class_name) {
82+
godot::internal::gdextension_interface_object_set_instance(_owner, reinterpret_cast<GDExtensionConstStringNamePtr>(_constructing_extension_class_name), this);
83+
_constructing_extension_class_name = nullptr;
84+
}
85+
86+
if (_constructing_class_binding_callbacks) {
87+
godot::internal::gdextension_interface_object_set_instance_binding(_owner, godot::internal::token, this, _constructing_class_binding_callbacks);
88+
_constructing_class_binding_callbacks = nullptr;
89+
}
8290
}
8391

8492
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: 17 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,17 @@ void Example::_bind_methods() {
287289
BIND_ENUM_CONSTANT(OUTSIDE_OF_CLASS);
288290
}
289291

290-
Example::Example() {
292+
bool Example::has_object_instance_binding() const {
293+
return internal::gdextension_interface_object_get_instance_binding(_owner, internal::token, nullptr);
294+
}
295+
296+
Example::Example() :
297+
object_instance_binding_set_by_parent_constructor(has_object_instance_binding()) {
298+
// Test conversion, to ensure users can use all parent calss functions at this time.
299+
// It would crash if instance binding still not be initialized.
300+
Variant v = Variant(this);
301+
Object *o = (Object *)v;
302+
291303
//UtilityFunctions::print("Constructor.");
292304
}
293305

@@ -367,6 +379,10 @@ void Example::emit_custom_signal(const String &name, int value) {
367379
emit_signal("custom_signal", name, value);
368380
}
369381

382+
bool Example::is_object_binding_set_by_parent_constructor() const {
383+
return object_instance_binding_set_by_parent_constructor;
384+
}
385+
370386
Array Example::test_array() const {
371387
Array arr;
372388

test/src/example.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class ExampleMin : public Control {
5757
GDCLASS(ExampleMin, Control);
5858

5959
protected:
60-
static void _bind_methods(){};
60+
static void _bind_methods() {};
6161
};
6262

6363
class Example : public Control {
@@ -82,6 +82,9 @@ 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+
bool has_object_instance_binding() const;
87+
8588
public:
8689
// Constants.
8790
enum Constants {
@@ -120,6 +123,8 @@ class Example : public Control {
120123
void emit_custom_signal(const String &name, int value);
121124
int def_args(int p_a = 100, int p_b = 200);
122125

126+
bool is_object_binding_set_by_parent_constructor() const;
127+
123128
Array test_array() const;
124129
int test_tarray_arg(const TypedArray<int64_t> &p_array);
125130
TypedArray<Vector2> test_tarray() const;

0 commit comments

Comments
 (0)