From 575bb7f3399fc7d267ab2b0f200fbc5611535795 Mon Sep 17 00:00:00 2001 From: Karroffel Date: Thu, 9 Mar 2017 03:03:21 +0100 Subject: [PATCH] reworked object system a little bit --- .gitignore | 1 + binding_generator/src/main.rs | 59 ++++++++++++++++++++++++-------- include/SConstruct | 17 ++++++--- include/godot_cpp/Godot.hpp | 34 +++++++----------- include/godot_cpp/core/Color.hpp | 2 +- 5 files changed, 70 insertions(+), 43 deletions(-) diff --git a/.gitignore b/.gitignore index 52f6c7d4..d21af468 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ include/godot_cpp/impl/ include/godot.h include/godot *.os +*.so diff --git a/binding_generator/src/main.rs b/binding_generator/src/main.rs index ea0c7a22..5a987f1d 100644 --- a/binding_generator/src/main.rs +++ b/binding_generator/src/main.rs @@ -71,6 +71,9 @@ fn main() { let mut icalls: HashSet<(String, Vec)> = HashSet::new(); for class in json { + if class.api_type == "tools" { + continue + } let used_classes = get_used_classes(&class); let mut header = File::create((base_dir.to_string() + strip_name(&class.name) + ".hpp").as_str()).unwrap(); @@ -158,7 +161,7 @@ fn generate_class_header(used_classes: &HashSet<&String>, class: &GodotClass) -> contents = contents + " {\n"; if class.base_class == "" { - contents = contents + "protected:\n\tgodot_object *__core_object = 0;\n\n"; + contents = contents + "public:\n\tgodot_object *__core_object = 0;\n\n"; } if class.singleton { @@ -180,12 +183,22 @@ fn generate_class_header(used_classes: &HashSet<&String>, class: &GodotClass) -> contents = contents + "\t" + strip_name(&class.name) + "(godot_object *ptr);\n\n"; } + // object constructor + if !class.singleton { + contents = contents + "\t" + strip_name(&class.name) + "(const Object& ptr);\n\n"; + } + + // object cast + { + contents = contents + "\toperator godot_object*();\n\n"; + } + if class.base_class != "" { contents = contents + "\tvoid _init();\n\n"; } if class.instanciable { - contents = contents + "\tstatic " + strip_name(&class.name) + "& __new();\n"; + contents = contents + "\tstatic " + strip_name(&class.name) + " __new();\n"; contents = contents + "\tvoid __destroy();\n\n"; } @@ -196,10 +209,10 @@ fn generate_class_header(used_classes: &HashSet<&String>, class: &GodotClass) -> contents += "\n\n"; for method in &class.methods { - contents = contents + "\t" + (if class.singleton { "static " } else if method.is_virtual { "virtual " } else { "" }) + strip_name(&method.return_type) + (if !is_core_type(&method.return_type) && !is_primitive(&method.return_type) { " &" } else { " " }) + escape_cpp(&method.name) + "("; + contents = contents + "\t" + (if class.singleton { "static " } else if method.is_virtual { "virtual " } else { "" }) + strip_name(&method.return_type) + (if !is_core_type(&method.return_type) && !is_primitive(&method.return_type) { " " } else { " " }) + escape_cpp(&method.name) + "("; for (i, argument) in (&method.arguments).iter().enumerate() { - if !is_primitive(&argument._type) { + if !is_primitive(&argument._type) && !is_core_type(&argument._type) { contents = contents + "const " + argument._type.as_str() + "&"; } else { contents = contents + "const " + argument._type.as_str() + ""; @@ -241,6 +254,8 @@ fn generate_class_implementation(icalls: &mut HashSet<(String, Vec)>, us contents = contents + "\n#include \"core/CoreTypes.hpp\"\n"; + contents = contents + "\n#include \"Godot.hpp\"\n\n"; + if class.instanciable { contents = contents + "#include \"ClassDB.hpp\"\n"; } @@ -294,6 +309,20 @@ fn generate_class_implementation(icalls: &mut HashSet<(String, Vec)>, us contents = contents + "}\n\n\n"; } + // Object constructor + if !class.singleton { + contents = contents + "" + strip_name(&class.name) + "::" + strip_name(&class.name) + "(const Object& ptr)\n{\n"; + contents = contents + "\t__core_object = ptr.__core_object;\n"; + contents = contents + "}\n\n\n"; + } + + // Object constructor + { + contents = contents + "" + strip_name(&class.name) + "::operator godot_object*()\n{\n"; + contents = contents + "\treturn __core_object;\n"; + contents = contents + "}\n\n\n"; + } + if class.base_class != "" { contents = contents + "void " + strip_name(&class.name) + "::" + "_init()\n{\n"; contents = contents + "\t\n"; @@ -304,10 +333,10 @@ fn generate_class_implementation(icalls: &mut HashSet<(String, Vec)>, us contents += "\n\n"; for method in &class.methods { - contents = contents + strip_name(&method.return_type) + (if !is_core_type(&method.return_type) && !is_primitive(&method.return_type) { " &" } else { " " }) + strip_name(&class.name) + "::" + escape_cpp(&method.name) + "("; + contents = contents + strip_name(&method.return_type) + (if !is_core_type(&method.return_type) && !is_primitive(&method.return_type) { " " } else { " " }) + strip_name(&class.name) + "::" + escape_cpp(&method.name) + "("; for (i, argument) in (&method.arguments).iter().enumerate() { - if !is_primitive(&argument._type) { + if !is_primitive(&argument._type) && !is_core_type(&argument._type) { contents = contents + "const " + argument._type.as_str() + "&"; } else { contents = contents + "const " + argument._type.as_str() + ""; @@ -337,7 +366,7 @@ fn generate_class_implementation(icalls: &mut HashSet<(String, Vec)>, us if method.return_type != "void" { contents = contents + "return "; if !is_primitive(&method.return_type) && !is_core_type(&method.return_type) { - contents = contents + "reinterpret_cast<" + strip_name(&method.return_type) + "&>("; + contents = contents + strip_name(&method.return_type) + "("; } } @@ -379,9 +408,9 @@ fn generate_class_implementation(icalls: &mut HashSet<(String, Vec)>, us if class.instanciable { - contents = contents + strip_name(&class.name) + "& " + strip_name(&class.name) + "::__new() {\n"; + contents = contents + strip_name(&class.name) + " " + strip_name(&class.name) + "::__new() {\n"; contents = contents + "\tObject ptr = ClassDB::instance(\"" + class.name.as_str() + "\");\n"; - contents = contents + "\treturn reinterpret_cast<" + strip_name(&class.name) + "&>(ptr);\n"; + contents = contents + "\treturn ptr;\n"; contents = contents + "}\n\n"; contents = contents + "void " + strip_name(&class.name) + "::__destroy() {\n"; @@ -443,7 +472,7 @@ fn generate_icall_header(icalls: &HashSet<(String, Vec)>) -> String { if is_primitive(t) || is_core_type(t) { t.as_str() } else { - "Object& " + "godot_object* " } } @@ -464,7 +493,7 @@ fn generate_icall_header(icalls: &HashSet<(String, Vec)>) -> String { contents = contents + return_type(ret) + " " + get_icall_name_ref((ret, args)).as_str() + "(godot_method_bind *mb, godot_object *inst"; for arg in args { contents = contents + ", "; - if !is_primitive(&arg) { + if !is_primitive(&arg) && is_core_type(&arg) { contents = contents + "const " + arg.as_str() + "&"; } else { contents = contents + "const " + arg.as_str() + ""; @@ -484,7 +513,7 @@ fn generate_icall_implementation(icalls: &HashSet<(String, Vec)>) -> Str if is_primitive(t) || is_core_type(t) { t.as_str() } else { - "Object& " + "godot_object* " } } @@ -504,7 +533,7 @@ fn generate_icall_implementation(icalls: &HashSet<(String, Vec)>) -> Str let mut i = 0; for arg in args { contents = contents + ", "; - if !is_primitive(&arg) { + if !is_primitive(&arg) && is_core_type(&arg) { contents = contents + "const " + arg.as_str() + "&"; } else { contents = contents + "const " + arg.as_str() + ""; @@ -530,7 +559,7 @@ fn generate_icall_implementation(icalls: &HashSet<(String, Vec)>) -> Str } else if is_core_type(arg) { contents = contents + "(void *) &arg" + j.to_string().as_str(); } else { - contents = contents + "(void *) &arg" + j.to_string().as_str(); + contents = contents + "(void *) *(godot_object**) &arg" + j.to_string().as_str(); } contents = contents + ",\n"; j = j + 1; @@ -541,7 +570,7 @@ fn generate_icall_implementation(icalls: &HashSet<(String, Vec)>) -> Str contents = contents + "\tgodot_method_bind_ptrcall(mb, inst, args, " + if ret == "void" { "NULL" } else { "&ret" } + ");\n"; if !is_primitive(ret) && !is_core_type(ret) { - contents = contents + "\treturn reinterpret_cast(ret);\n"; + contents = contents + "\treturn ret;\n"; } else if ret != "void" { contents = contents + "\treturn ret;\n"; } diff --git a/include/SConstruct b/include/SConstruct index 2287e944..c48e8c48 100644 --- a/include/SConstruct +++ b/include/SConstruct @@ -2,7 +2,9 @@ import os env = Environment() -env["CXX"] = "clang++" + +if ARGUMENTS.get("use_llvm", "yes") == "yes": + env["CXX"] = "clang++" target = ARGUMENTS.get("target", "core") @@ -35,13 +37,18 @@ if (target == "core"): library = env.SharedLibrary(target='godot_cpp_core', source=sources) Default(library) -elif target == "binding": - env.Append(CCFLAGS = ['-Wno-writable-strings']) +elif target == "bindings": + if env["CXX"] == "clang++": + env.Append(CCFLAGS = ['-Wno-writable-strings']) + else: + env.Append(CCFLAGS = ['-Wno-write-strings', '-Wno-return-local-addr']) + env.Append(CCFLAGS = ['-g','-O3', '-std=c++14']) env.Append(CPPPATH=['.', './godot', './godot_cpp']) - env.Append(RPATH=['.']) - env.Append(LINKFLAGS=['-Rgodot_cpp_core']) + env.Append(LINKFLAGS = ['-Wl,-R,\'$$ORIGIN/\'']) + env.Append(LIBS=['godot_cpp_core']) + env.Append(LIBPATH=["."]) sources = [os.path.join("godot_cpp/impl/", f) for f in os.listdir("godot_cpp/impl/") if f.endswith('.cpp')] diff --git a/include/godot_cpp/Godot.hpp b/include/godot_cpp/Godot.hpp index d07fa7b1..cd569e20 100644 --- a/include/godot_cpp/Godot.hpp +++ b/include/godot_cpp/Godot.hpp @@ -20,16 +20,6 @@ namespace godot { -template -A object_cast(B b) -{ - A *a = (A*) &b; - return *a; -} - - - - // instance and destroy funcs @@ -66,7 +56,7 @@ void register_class() template struct WrappedMethod5 { - static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + static godot_variant wrapped_method(godot_object *_, void *data, void *, int num_args, godot_variant **args) { godot_variant _variant; godot_variant_new_nil(&_variant); @@ -80,7 +70,7 @@ struct WrappedMethod5 { template struct WrappedMethod5 { - static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + static godot_variant wrapped_method(godot_object *_, void *data, void *, int num_args, godot_variant **args) { godot_variant _variant; godot_variant_new_nil(&_variant); @@ -93,7 +83,7 @@ struct WrappedMethod5 { template struct WrappedMethod4 { - static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + static godot_variant wrapped_method(godot_object *_, void *data, void *, int num_args, godot_variant **args) { godot_variant _variant; godot_variant_new_nil(&_variant); @@ -107,7 +97,7 @@ struct WrappedMethod4 { template struct WrappedMethod4 { - static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + static godot_variant wrapped_method(godot_object *_, void *data, void *, int num_args, godot_variant **args) { godot_variant _variant; godot_variant_new_nil(&_variant); @@ -121,7 +111,7 @@ struct WrappedMethod4 { template struct WrappedMethod3 { - static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + static godot_variant wrapped_method(godot_object *_, void *data, void *, int num_args, godot_variant **args) { godot_variant _variant; godot_variant_new_nil(&_variant); @@ -135,7 +125,7 @@ struct WrappedMethod3 { template struct WrappedMethod3 { - static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + static godot_variant wrapped_method(godot_object *_, void *data, void *, int num_args, godot_variant **args) { godot_variant _variant; godot_variant_new_nil(&_variant); @@ -148,7 +138,7 @@ struct WrappedMethod3 { template struct WrappedMethod2 { - static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + static godot_variant wrapped_method(godot_object *_, void *data, void *, int num_args, godot_variant **args) { godot_variant _variant; godot_variant_new_nil(&_variant); @@ -162,7 +152,7 @@ struct WrappedMethod2 { template struct WrappedMethod2 { - static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + static godot_variant wrapped_method(godot_object *_, void *data, void *, int num_args, godot_variant **args) { godot_variant _variant; godot_variant_new_nil(&_variant); @@ -175,7 +165,7 @@ struct WrappedMethod2 { template struct WrappedMethod1 { - static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + static godot_variant wrapped_method(godot_object *_, void *data, void *, int num_args, godot_variant **args) { godot_variant _variant; godot_variant_new_nil(&_variant); @@ -189,7 +179,7 @@ struct WrappedMethod1 { template struct WrappedMethod1 { - static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + static godot_variant wrapped_method(godot_object *_, void *data, void *, int num_args, godot_variant **args) { godot_variant _variant; godot_variant_new_nil(&_variant); @@ -204,7 +194,7 @@ struct WrappedMethod1 { template struct WrappedMethod0 { - static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + static godot_variant wrapped_method(godot_object *_, void *data, void *, int num_args, godot_variant **args) { godot_variant _variant; godot_variant_new_nil(&_variant); @@ -217,7 +207,7 @@ struct WrappedMethod0 { template struct WrappedMethod0 { - static godot_variant wrapped_method(godot_object *_, void *data, int num_args, godot_variant **args) + static godot_variant wrapped_method(godot_object *_, void *data, void *, int num_args, godot_variant **args) { godot_variant _variant; godot_variant_new_nil(&_variant); diff --git a/include/godot_cpp/core/Color.hpp b/include/godot_cpp/core/Color.hpp index d0f834db..528e6744 100644 --- a/include/godot_cpp/core/Color.hpp +++ b/include/godot_cpp/core/Color.hpp @@ -14,7 +14,7 @@ struct Color { private: - static float _parse_col(const String& p_str, int p_ofs); + // static float _parse_col(const String& p_str, int p_ofs); public: union {