Make extension instances create the corresponding godot object in their constructor

pull/663/head
Gilles Roudière 2021-11-30 14:00:13 +01:00
parent 50512f0dee
commit 3fcb8a4d1e
9 changed files with 197 additions and 180 deletions

View File

@ -458,6 +458,7 @@ if should_generate_bindings:
# Sources to compile # Sources to compile
sources = [] sources = []
add_sources(sources, "src", "cpp") add_sources(sources, "src", "cpp")
add_sources(sources, "src/classes", "cpp")
add_sources(sources, "src/core", "cpp") add_sources(sources, "src/core", "cpp")
add_sources(sources, "src/variant", "cpp") add_sources(sources, "src/variant", "cpp")
add_sources(sources, "gen/src/variant", "cpp") add_sources(sources, "gen/src/variant", "cpp")

View File

@ -914,9 +914,6 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
result.append("") result.append("")
result.append("public:") result.append("public:")
# Constructor override, since parent Wrapped has protected constructor.
result.append(f"\t{class_name}() = default;")
result.append("") result.append("")
if "enums" in class_api: if "enums" in class_api:

@ -1 +1 @@
Subproject commit cfc1909edf55f21a8822dd088172dcf6c349a1d0 Subproject commit 68e989fce33a14fdd9e10c8adc88ae3c9b3aae85

View File

@ -34,6 +34,7 @@
#include <godot_cpp/core/memory.hpp> #include <godot_cpp/core/memory.hpp>
#include <godot_cpp/godot.hpp> #include <godot_cpp/godot.hpp>
namespace godot { namespace godot {
typedef void GodotObject; typedef void GodotObject;
@ -41,46 +42,22 @@ typedef void GodotObject;
// Base for all engine classes, to contain the pointer to the engine instance. // Base for all engine classes, to contain the pointer to the engine instance.
class Wrapped { class Wrapped {
friend class GDExtensionBinding; friend class GDExtensionBinding;
friend void postinitialize_handler(Wrapped *);
// Private constructor, this should not be created directly by users.
Wrapped(GodotObject *p_owner) :
_owner(p_owner) {}
protected: protected:
Wrapped() = default; virtual const char *_get_class() const = 0; // This is needed to retrieve the class name before the godot object has its _extension and _extension_instance members assigned.
virtual const GDNativeInstanceBindingCallbacks *_get_bindings_callbacks() const = 0;
void _postinitialize();
Wrapped(const char *p_godot_class);
Wrapped(GodotObject *p_godot_object);
public: public:
// Must be public but you should not touch this. // Must be public but you should not touch this.
GodotObject *_owner = nullptr; GodotObject *_owner = nullptr;
static Wrapped *_new() {
return nullptr;
}
}; };
namespace internal {
template <class T, class Enable = void>
struct Creator {
static T *_new() { return nullptr; }
};
template <class T>
struct Creator<T, typename std::enable_if<std::is_base_of_v<godot::Wrapped, T>>::type> {
static T *_new() { return T::_new(); }
};
// template <class T>
// struct Creator<T, std::false_type> {
// };
// template <class T>
// struct Creator<T, std::enable_if_t<std::is_base_of_v<godot::Wrapped, T>, bool>> {
// static T *_new() { return T::_new(); }
// };
}; // namespace internal
} // namespace godot } // namespace godot
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
@ -94,126 +71,126 @@ struct Creator<T, typename std::enable_if<std::is_base_of_v<godot::Wrapped, T>>:
#define CHECK_CLASS_CONSTRUCTOR(m_constructor, m_class) #define CHECK_CLASS_CONSTRUCTOR(m_constructor, m_class)
#endif #endif
#define GDCLASS(m_class, m_inherits) \ #define GDCLASS(m_class, m_inherits) \
private: \ private: \
friend class ClassDB; \ void operator=(const m_class &p_rval) {} \
\ friend class ClassDB; \
using SelfType = m_class; \ \
\ using SelfType = m_class; \
protected: \ \
static void (*_get_bind_methods())() { \ protected: \
return &m_class::_bind_methods; \ virtual const char *_get_class() const override { \
} \ return get_class_static(); \
\ } \
template <class T> \ \
static void register_virtuals() { \ virtual const GDNativeInstanceBindingCallbacks *_get_bindings_callbacks() const override { \
m_inherits::register_virtuals<T>(); \ return &___binding_callbacks; \
} \ } \
\ \
public: \ static void (*_get_bind_methods())() { \
static void initialize_class() { \ return &m_class::_bind_methods; \
static bool initialized = false; \ } \
if (initialized) { \ \
return; \ template <class T> \
} \ static void register_virtuals() { \
m_inherits::initialize_class(); \ m_inherits::register_virtuals<T>(); \
if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) { \ } \
_bind_methods(); \ \
m_inherits::register_virtuals<m_class>(); \ public: \
} \ static void initialize_class() { \
initialized = true; \ static bool initialized = false; \
} \ if (initialized) { \
\ return; \
static const char *get_class_static() { \ } \
return #m_class; \ m_inherits::initialize_class(); \
} \ if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) { \
\ _bind_methods(); \
static const char *get_parent_class_static() { \ m_inherits::register_virtuals<m_class>(); \
return #m_inherits; \ } \
} \ initialized = true; \
\ } \
static GDExtensionClassInstancePtr create(void *data) { \ \
return reinterpret_cast<GDExtensionClassInstancePtr>(new ("") m_class); \ static const char *get_class_static() { \
} \ return #m_class; \
\ } \
static void free(void *data, GDExtensionClassInstancePtr ptr) { \ \
if (ptr) { \ static const char *get_parent_class_static() { \
m_class *cls = reinterpret_cast<m_class *>(ptr); \ return #m_inherits; \
cls->~m_class(); \ } \
::godot::Memory::free_static(cls); \ \
} \ static GDNativeObjectPtr create(void *data) { \
} \ m_class *new_object = memnew(m_class); \
\ return new_object->_owner; \
static void set_object_instance(GDExtensionClassInstancePtr p_instance, GDNativeObjectPtr p_object_instance) { \ } \
godot::internal::gdn_interface->object_set_instance_binding(p_object_instance, godot::internal::token, p_instance, &m_class::___binding_callbacks); \ \
reinterpret_cast<m_class *>(p_instance)->_owner = reinterpret_cast<godot::GodotObject *>(p_object_instance); \ static void free(void *data, GDExtensionClassInstancePtr ptr) { \
} \ if (ptr) { \
\ m_class *cls = reinterpret_cast<m_class *>(ptr); \
static void *___binding_create_callback(void *p_token, void *p_instance) { \ cls->~m_class(); \
return nullptr; \ ::godot::Memory::free_static(cls); \
} \ } \
static void ___binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \ } \
} \ \
static GDNativeBool ___binding_reference_callback(void *p_token, void *p_instance, GDNativeBool p_reference) { \ static void *___binding_create_callback(void *p_token, void *p_instance) { \
return true; \ return nullptr; \
} \ } \
static constexpr GDNativeInstanceBindingCallbacks ___binding_callbacks = { \ static void ___binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \
___binding_create_callback, \ } \
___binding_free_callback, \ static GDNativeBool ___binding_reference_callback(void *p_token, void *p_instance, GDNativeBool p_reference) { \
___binding_reference_callback, \ return true; \
}; \ } \
\ static constexpr GDNativeInstanceBindingCallbacks ___binding_callbacks = { \
static m_class *_new() { \ ___binding_create_callback, \
static GDNativeExtensionPtr ___extension = nullptr; \ ___binding_free_callback, \
static GDNativeClassConstructor ___constructor = godot::internal::gdn_interface->classdb_get_constructor(#m_class, &___extension); \ ___binding_reference_callback, \
CHECK_CLASS_CONSTRUCTOR(___constructor, m_class); \ };
GDNativeObjectPtr obj = godot::internal::gdn_interface->classdb_construct_object(___constructor, ___extension); \
return reinterpret_cast<m_class *>(godot::internal::gdn_interface->object_get_instance_binding(obj, godot::internal::token, &m_class::___binding_callbacks)); \
} \
\
private:
// Don't use this for your classes, use GDCLASS() instead. // Don't use this for your classes, use GDCLASS() instead.
#define GDNATIVE_CLASS(m_class, m_inherits) \ #define GDNATIVE_CLASS(m_class, m_inherits) \
protected: \ private: \
static void (*_get_bind_methods())() { \ void operator=(const m_class &p_rval) {} \
return nullptr; \ \
} \ protected: \
\ virtual const char *_get_class() const override { \
public: \ return get_class_static(); \
static void initialize_class() {} \ } \
\ \
static const char *get_class_static() { \ virtual const GDNativeInstanceBindingCallbacks *_get_bindings_callbacks() const override { \
return #m_class; \ return &___binding_callbacks; \
} \ } \
\ \
static const char *get_parent_class_static() { \ m_class(const char *p_godot_class) : m_inherits(p_godot_class) {} \
return #m_inherits; \ m_class(GodotObject *p_godot_object) : m_inherits(p_godot_object) {} \
} \ \
\ static void (*_get_bind_methods())() { \
static void *___binding_create_callback(void *p_token, void *p_instance) { \ return nullptr; \
m_class *obj = new ("") m_class; \ } \
obj->_owner = (godot::GodotObject *)p_instance; \ \
return obj; \ public: \
} \ static void initialize_class() {} \
static void ___binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \ \
Memory::free_static(reinterpret_cast<m_class *>(p_binding)); \ static const char *get_class_static() { \
} \ return #m_class; \
static GDNativeBool ___binding_reference_callback(void *p_token, void *p_instance, GDNativeBool p_reference) { \ } \
return true; \ \
} \ static const char *get_parent_class_static() { \
static constexpr GDNativeInstanceBindingCallbacks ___binding_callbacks = { \ return #m_inherits; \
___binding_create_callback, \ } \
___binding_free_callback, \ \
___binding_reference_callback, \ static void *___binding_create_callback(void *p_token, void *p_instance) { \
}; \ return memnew(m_class((GodotObject *)p_instance)); \
static m_class *_new() { \ } \
static GDNativeClassConstructor ___constructor = godot::internal::gdn_interface->classdb_get_constructor(#m_class, nullptr); \ static void ___binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \
CHECK_CLASS_CONSTRUCTOR(___constructor, m_class); \ Memory::free_static(reinterpret_cast<m_class *>(p_binding)); \
GDNativeObjectPtr obj = ___constructor(); \ } \
return reinterpret_cast<m_class *>(godot::internal::gdn_interface->object_get_instance_binding(obj, godot::internal::token, &m_class::___binding_callbacks)); \ static GDNativeBool ___binding_reference_callback(void *p_token, void *p_instance, GDNativeBool p_reference) { \
} \ return true; \
\ } \
private: static constexpr GDNativeInstanceBindingCallbacks ___binding_callbacks = { \
___binding_create_callback, \
___binding_free_callback, \
___binding_reference_callback, \
}; \
m_class() : m_class(#m_class) {}
#endif // ! GODOT_CPP_WRAPPED_HPP #endif // ! GODOT_CPP_WRAPPED_HPP

View File

@ -158,9 +158,8 @@ void ClassDB::register_class() {
nullptr, // GDNativeExtensionClassUnreference nullptr, // GDNativeExtensionClassUnreference
T::create, // GDNativeExtensionClassCreateInstance create_instance_func; /* this one is mandatory */ T::create, // GDNativeExtensionClassCreateInstance create_instance_func; /* this one is mandatory */
T::free, // GDNativeExtensionClassFreeInstance free_instance_func; /* this one is mandatory */ T::free, // GDNativeExtensionClassFreeInstance free_instance_func; /* this one is mandatory */
T::set_object_instance, // GDNativeExtensionClassObjectInstance object_instance_func; /* this one is mandatory */
&ClassDB::get_virtual_func, // GDNativeExtensionClassGetVirtual get_virtual_func; &ClassDB::get_virtual_func, // GDNativeExtensionClassGetVirtual get_virtual_func;
(void *)cl.name, //void *class_userdata; (void *)cl.name, // void *class_userdata;
}; };
internal::gdn_interface->classdb_register_extension_class(internal::library, cl.name, cl.parent_name, &class_info); internal::gdn_interface->classdb_register_extension_class(internal::library, cl.name, cl.parent_name, &class_info);

View File

@ -68,16 +68,17 @@ public:
static void free_static(void *p_ptr); static void free_static(void *p_ptr);
}; };
#define memnew(m_v) \ _ALWAYS_INLINE_ void postinitialize_handler(void *) {}
([&]() { \
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>
_ALWAYS_INLINE_ T *_post_initialize(T *p_obj) {
postinitialize_handler(p_obj);
return p_obj;
}
#define memnew(m_class) _post_initialize(new ("") m_class)
#define memnew_placement(m_placement, m_class) _post_initialize(new (m_placement, sizeof(m_class), "") m_class)
template <class T> template <class T>
void memdelete(T *p_class, typename std::enable_if<!std::is_base_of_v<godot::Wrapped, T>>::type * = 0) { void memdelete(T *p_class, typename std::enable_if<!std::is_base_of_v<godot::Wrapped, T>>::type * = 0) {

View File

@ -55,9 +55,6 @@ public:
static void initialize_level(void *userdata, GDNativeInitializationLevel p_level); static void initialize_level(void *userdata, GDNativeInitializationLevel p_level);
static void deinitialize_level(void *userdata, GDNativeInitializationLevel p_level); static void deinitialize_level(void *userdata, GDNativeInitializationLevel p_level);
static void *create_instance_callback(void *p_token, void *p_instance);
static void free_instance_callback(void *p_token, void *p_instance, void *p_binding);
class InitObject { class InitObject {
const GDNativeInterface *gdn_interface; const GDNativeInterface *gdn_interface;
const GDNativeExtensionClassLibraryPtr library; const GDNativeExtensionClassLibraryPtr library;

56
src/classes/wrapped.cpp Normal file
View File

@ -0,0 +1,56 @@
/*************************************************************************/
/* wrapped.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include <godot_cpp/classes/wrapped.hpp>
#include <godot_cpp/variant/builtin_types.hpp>
#include <godot_cpp/classes/object.hpp>
namespace godot {
void Wrapped::_postinitialize() {
godot::internal::gdn_interface->object_set_instance(_owner, _get_class(), this);
godot::internal::gdn_interface->object_set_instance_binding(_owner, godot::internal::token, this, _get_bindings_callbacks());
}
Wrapped::Wrapped(const char *p_godot_class) {
_owner = godot::internal::gdn_interface->classdb_construct_object(p_godot_class);
}
Wrapped::Wrapped(GodotObject *p_godot_object) {
_owner = p_godot_object;
}
void postinitialize_handler(Wrapped *p_wrapped) {
p_wrapped->_postinitialize();
}
} // namespace godot

View File

@ -91,17 +91,6 @@ void GDExtensionBinding::deinitialize_level(void *userdata, GDNativeInitializati
} }
} }
void *GDExtensionBinding::create_instance_callback(void *p_token, void *p_instance) {
ERR_FAIL_COND_V_MSG(p_token != internal::library, nullptr, "Asking for creating instance with invalid token.");
Wrapped *wrapped = memnew(Wrapped(p_instance));
return wrapped;
}
void GDExtensionBinding::free_instance_callback(void *p_token, void *p_instance, void *p_binding) {
ERR_FAIL_COND_MSG(p_token != internal::library, "Asking for freeing instance with invalid token.");
memdelete((Wrapped *)p_binding);
}
void GDExtensionBinding::InitObject::register_core_initializer(Callback p_core_init) const { void GDExtensionBinding::InitObject::register_core_initializer(Callback p_core_init) const {
GDExtensionBinding::init_callbacks[GDNATIVE_INITIALIZATION_CORE] = p_core_init; GDExtensionBinding::init_callbacks[GDNATIVE_INITIALIZATION_CORE] = p_core_init;
} }