Change constructor/destructor management of extension classes
This makes sure custom constructors are always called on extension classes. However, note that constructors should not take any parameters, since Godot doesn't support that. Parameters are ignore in memnew macro. Use memnew(MyClass()) instead of memnew(MyClass) since it now needs a value instead of a class name. This macro calls MyClass::_new() (define in GDCLASS macro) which ultimately calls Godot to create the object, ensuring that both the Godot and the extension instances are created. Non Godot classes (that don't derive godot::Object) are constructed as usual an can have parameters. memdelete is also changed for the same reason, as it needs to destroy the Godot object as well, and that automatically frees the bound extension instance.
This commit is contained in:
committed by
Bastiaan Olij
parent
e839199848
commit
38ee8bfcf7
@@ -36,6 +36,9 @@
|
||||
|
||||
#include <godot_cpp/core/defs.hpp>
|
||||
#include <godot_cpp/core/error_macros.hpp>
|
||||
#include <godot_cpp/godot.hpp>
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
void *operator new(size_t p_size, const char *p_description); ///< operator new that takes a description and uses MemoryStaticPool
|
||||
void *operator new(size_t p_size, void *p_pointer, size_t check, const char *p_description); ///< operator new that takes a description and uses a pointer to the preallocated memory
|
||||
@@ -54,6 +57,8 @@ void operator delete(void *p_mem, void *p_pointer, size_t check, const char *p_d
|
||||
|
||||
namespace godot {
|
||||
|
||||
class Wrapped;
|
||||
|
||||
class Memory {
|
||||
Memory();
|
||||
|
||||
@@ -63,11 +68,19 @@ public:
|
||||
static void free_static(void *p_ptr);
|
||||
};
|
||||
|
||||
#define memnew(m_v) (new ("") m_v)
|
||||
#define memnew(m_v) \
|
||||
([&]() { \
|
||||
if constexpr (std::is_base_of<godot::Object, decltype(m_v)>::value) { \
|
||||
return godot::internal::Creator<decltype(m_v)>::_new(); \
|
||||
} else { \
|
||||
return new ("") m_v; \
|
||||
} \
|
||||
}())
|
||||
|
||||
#define memnew_placement(m_placement, m_class) (new (m_placement, sizeof(m_class), "") m_class)
|
||||
|
||||
template <class T>
|
||||
void memdelete(T *p_class) {
|
||||
void memdelete(T *p_class, typename std::enable_if<!std::is_base_of_v<godot::Wrapped, T>>::type * = 0) {
|
||||
if (!__has_trivial_destructor(T)) {
|
||||
p_class->~T();
|
||||
}
|
||||
@@ -75,6 +88,11 @@ void memdelete(T *p_class) {
|
||||
Memory::free_static(p_class);
|
||||
}
|
||||
|
||||
template <class T, std::enable_if_t<std::is_base_of_v<godot::Wrapped, T>, bool> = true>
|
||||
void memdelete(T *p_class) {
|
||||
godot::internal::interface->object_destroy(p_class->_owner);
|
||||
}
|
||||
|
||||
#define memnew_arr(m_class, m_count) memnew_arr_template<m_class>(m_count)
|
||||
|
||||
template <typename T>
|
||||
|
||||
@@ -194,7 +194,7 @@ public:
|
||||
|
||||
template <class T>
|
||||
MethodBind *create_vararg_method_bind(Variant (T::*p_method)(const Variant **, GDNativeInt, GDNativeCallError &), const MethodInfo &p_info, bool p_return_nil_is_variant) {
|
||||
MethodBindVarArg<T> *a = memnew((MethodBindVarArg<T>));
|
||||
MethodBindVarArg<T> *a = memnew(MethodBindVarArg<T>());
|
||||
a->set_method(p_method);
|
||||
a->set_method_info(p_info, p_return_nil_is_variant);
|
||||
a->set_instance_class(T::get_class_static());
|
||||
|
||||
Reference in New Issue
Block a user