Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make extension instances create the corresponding godot object in their constructor #663

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ if should_generate_bindings:
# Sources to compile
sources = []
add_sources(sources, "src", "cpp")
add_sources(sources, "src/classes", "cpp")
add_sources(sources, "src/core", "cpp")
add_sources(sources, "src/variant", "cpp")
add_sources(sources, "gen/src/variant", "cpp")
Expand Down
3 changes: 0 additions & 3 deletions binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,9 +914,6 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
result.append("")

result.append("public:")

# Constructor override, since parent Wrapped has protected constructor.
result.append(f"\t{class_name}() = default;")
result.append("")

if "enums" in class_api:
Expand Down
2 changes: 1 addition & 1 deletion godot-headers
279 changes: 128 additions & 151 deletions include/godot_cpp/classes/wrapped.hpp

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions include/godot_cpp/core/class_db.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@ void ClassDB::register_class() {
nullptr, // GDNativeExtensionClassUnreference
T::create, // GDNativeExtensionClassCreateInstance create_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;
(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);
Expand Down
21 changes: 11 additions & 10 deletions include/godot_cpp/core/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,17 @@ class Memory {
static void free_static(void *p_ptr);
};

#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)
_ALWAYS_INLINE_ void postinitialize_handler(void *) {}

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>
void memdelete(T *p_class, typename std::enable_if<!std::is_base_of_v<godot::Wrapped, T>>::type * = 0) {
Expand Down
3 changes: 0 additions & 3 deletions include/godot_cpp/godot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ class GDExtensionBinding {
static void initialize_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 {
const GDNativeInterface *gdn_interface;
const GDNativeExtensionClassLibraryPtr library;
Expand Down
56 changes: 56 additions & 0 deletions src/classes/wrapped.cpp
Original file line number Diff line number Diff line change
@@ -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
11 changes: 0 additions & 11 deletions src/godot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
GDExtensionBinding::init_callbacks[GDNATIVE_INITIALIZATION_CORE] = p_core_init;
}
Expand Down