Skip to content

Commit c49b699

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

File tree

3 files changed

+48
-26
lines changed

3 files changed

+48
-26
lines changed

include/godot_cpp/classes/wrapped.hpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,17 @@ class Wrapped {
5252
friend class ClassDB;
5353
friend void postinitialize_handler(Wrapped *);
5454

55+
template <typename T, std::enable_if_t<std::is_base_of<::godot::Wrapped, T>::value, bool>>
56+
friend _ALWAYS_INLINE_ void _pre_initialize();
57+
58+
thread_local static const StringName *constructing_extension_class_name;
59+
thread_local static const GDExtensionInstanceBindingCallbacks *constructing_extension_class_binds_callbacks;
60+
61+
void _initialize();
62+
5563
protected:
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,12 @@ class Wrapped {
108117
GodotObject *_owner = nullptr;
109118
};
110119

120+
template <typename T, std::enable_if_t<std::is_base_of<::godot::Wrapped, T>::value, bool> = true>
121+
_ALWAYS_INLINE_ void _pre_initialize() {
122+
Wrapped::constructing_extension_class_name = T::_get_extension_class_name();
123+
Wrapped::constructing_extension_class_binds_callbacks = &T::_gde_binding_callbacks;
124+
}
125+
111126
_FORCE_INLINE_ void snarray_add_str(Vector<StringName> &arr) {
112127
}
113128

@@ -162,15 +177,7 @@ private:
162177
friend class ::godot::ClassDB; \
163178
\
164179
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-
\
180+
virtual bool _is_extension_class() const override { return true; } \
174181
static void (*_get_bind_methods())() { \
175182
return &m_class::_bind_methods; \
176183
} \
@@ -213,6 +220,11 @@ protected:
213220
} \
214221
\
215222
public: \
223+
static const ::godot::StringName *_get_extension_class_name() { \
224+
static ::godot::StringName string_name = get_class_static(); \
225+
return &string_name; \
226+
} \
227+
\
216228
typedef m_class self_type; \
217229
\
218230
static void initialize_class() { \
@@ -383,10 +395,6 @@ private:
383395
void operator=(const m_class &p_rval) {} \
384396
\
385397
protected: \
386-
virtual const GDExtensionInstanceBindingCallbacks *_get_bindings_callbacks() const override { \
387-
return &_gde_binding_callbacks; \
388-
} \
389-
\
390398
m_class(const char *p_godot_class) : m_inherits(p_godot_class) {} \
391399
m_class(GodotObject *p_godot_object) : m_inherits(p_godot_object) {} \
392400
\

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_v<Wrapped, T>, 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: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,28 @@
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_extension_class_binds_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

47-
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);
49+
void Wrapped::_initialize() {
50+
if (constructing_extension_class_name) {
51+
godot::internal::gdextension_interface_object_set_instance(_owner, reinterpret_cast<GDExtensionConstStringNamePtr>(constructing_extension_class_name), this);
52+
constructing_extension_class_name = nullptr;
53+
}
54+
55+
if (constructing_extension_class_binds_callbacks) {
56+
godot::internal::gdextension_interface_object_set_instance_binding(_owner, godot::internal::token, this, constructing_extension_class_binds_callbacks);
57+
constructing_extension_class_binds_callbacks = nullptr;
5158
}
52-
godot::internal::gdextension_interface_object_set_instance_binding(_owner, godot::internal::token, this, _get_bindings_callbacks());
53-
if (extension_class) {
59+
}
60+
61+
void Wrapped::_postinitialize() {
62+
// Only sent NOTIFICATION_POSTINITIALIZE for extension classes.
63+
if (_is_extension_class()) {
5464
Object *obj = dynamic_cast<Object *>(this);
5565
if (obj) {
5666
obj->notification(Object::NOTIFICATION_POSTINITIALIZE);
@@ -79,6 +89,7 @@ Wrapped::Wrapped(const StringName p_godot_class) {
7989
}
8090
#endif
8191
_owner = godot::internal::gdextension_interface_classdb_construct_object(reinterpret_cast<GDExtensionConstStringNamePtr>(p_godot_class._native_ptr()));
92+
_initialize();
8293
}
8394

8495
Wrapped::Wrapped(GodotObject *p_godot_object) {

0 commit comments

Comments
 (0)