Skip to content

Commit 0230024

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

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

include/godot_cpp/classes/wrapped.hpp

Lines changed: 32 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,16 @@ 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+
62+
void _initialize();
63+
5564
protected:
65+
virtual bool _is_extension_class() const { return false; }
66+
5667
#ifdef HOT_RELOAD_ENABLED
5768
struct RecreateInstance {
5869
GDExtensionClassInstancePtr wrapper;
@@ -62,9 +73,6 @@ class Wrapped {
6273
inline static RecreateInstance *recreate_instance = nullptr;
6374
#endif
6475

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-
6876
void _notification(int p_what) {}
6977
bool _set(const StringName &p_name, const Variant &p_property) { return false; }
7078
bool _get(const StringName &p_name, Variant &r_property) const { return false; }
@@ -95,6 +103,8 @@ class Wrapped {
95103
virtual ~Wrapped() {}
96104

97105
public:
106+
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.
107+
98108
static StringName &get_class_static() {
99109
static StringName string_name = StringName("Wrapped");
100110
return string_name;
@@ -108,6 +118,18 @@ class Wrapped {
108118
GodotObject *_owner = nullptr;
109119
};
110120

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

@@ -162,15 +184,7 @@ private:
162184
friend class ::godot::ClassDB; \
163185
\
164186
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-
\
187+
virtual bool _is_extension_class() const override { return true; } \
174188
static void (*_get_bind_methods())() { \
175189
return &m_class::_bind_methods; \
176190
} \
@@ -213,6 +227,11 @@ protected:
213227
} \
214228
\
215229
public: \
230+
static const ::godot::StringName *_get_extension_class_name() { \
231+
static ::godot::StringName string_name = get_class_static(); \
232+
return &string_name; \
233+
} \
234+
\
216235
typedef m_class self_type; \
217236
\
218237
static void initialize_class() { \
@@ -383,10 +402,6 @@ private:
383402
void operator=(const m_class &p_rval) {} \
384403
\
385404
protected: \
386-
virtual const GDExtensionInstanceBindingCallbacks *_get_bindings_callbacks() const override { \
387-
return &_gde_binding_callbacks; \
388-
} \
389-
\
390405
m_class(const char *p_godot_class) : m_inherits(p_godot_class) {} \
391406
m_class(GodotObject *p_godot_object) : m_inherits(p_godot_object) {} \
392407
\

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, typename std::enable_if_t<!std::is_base_of<::godot::Wrapped, T>::value> * = nullptr>
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_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

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_class_binding_callbacks) {
56+
godot::internal::gdextension_interface_object_set_instance_binding(_owner, godot::internal::token, this, _constructing_class_binding_callbacks);
57+
_constructing_class_binding_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)