reworked object system a little bit

pull/7/head
Karroffel 2017-03-09 03:03:21 +01:00
parent c772d7f656
commit 575bb7f339
5 changed files with 70 additions and 43 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ include/godot_cpp/impl/
include/godot.h include/godot.h
include/godot include/godot
*.os *.os
*.so

View File

@ -71,6 +71,9 @@ fn main() {
let mut icalls: HashSet<(String, Vec<String>)> = HashSet::new(); let mut icalls: HashSet<(String, Vec<String>)> = HashSet::new();
for class in json { for class in json {
if class.api_type == "tools" {
continue
}
let used_classes = get_used_classes(&class); let used_classes = get_used_classes(&class);
let mut header = File::create((base_dir.to_string() + strip_name(&class.name) + ".hpp").as_str()).unwrap(); 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"; contents = contents + " {\n";
if class.base_class == "" { 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 { 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"; 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 != "" { if class.base_class != "" {
contents = contents + "\tvoid _init();\n\n"; contents = contents + "\tvoid _init();\n\n";
} }
if class.instanciable { 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"; contents = contents + "\tvoid __destroy();\n\n";
} }
@ -196,10 +209,10 @@ fn generate_class_header(used_classes: &HashSet<&String>, class: &GodotClass) ->
contents += "\n\n"; contents += "\n\n";
for method in &class.methods { 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() { 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() + "&"; contents = contents + "const " + argument._type.as_str() + "&";
} else { } else {
contents = contents + "const " + argument._type.as_str() + ""; contents = contents + "const " + argument._type.as_str() + "";
@ -241,6 +254,8 @@ fn generate_class_implementation(icalls: &mut HashSet<(String, Vec<String>)>, us
contents = contents + "\n#include \"core/CoreTypes.hpp\"\n"; contents = contents + "\n#include \"core/CoreTypes.hpp\"\n";
contents = contents + "\n#include \"Godot.hpp\"\n\n";
if class.instanciable { if class.instanciable {
contents = contents + "#include \"ClassDB.hpp\"\n"; contents = contents + "#include \"ClassDB.hpp\"\n";
} }
@ -294,6 +309,20 @@ fn generate_class_implementation(icalls: &mut HashSet<(String, Vec<String>)>, us
contents = contents + "}\n\n\n"; 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 != "" { if class.base_class != "" {
contents = contents + "void " + strip_name(&class.name) + "::" + "_init()\n{\n"; contents = contents + "void " + strip_name(&class.name) + "::" + "_init()\n{\n";
contents = contents + "\t\n"; contents = contents + "\t\n";
@ -304,10 +333,10 @@ fn generate_class_implementation(icalls: &mut HashSet<(String, Vec<String>)>, us
contents += "\n\n"; contents += "\n\n";
for method in &class.methods { 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() { 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() + "&"; contents = contents + "const " + argument._type.as_str() + "&";
} else { } else {
contents = contents + "const " + argument._type.as_str() + ""; contents = contents + "const " + argument._type.as_str() + "";
@ -337,7 +366,7 @@ fn generate_class_implementation(icalls: &mut HashSet<(String, Vec<String>)>, us
if method.return_type != "void" { if method.return_type != "void" {
contents = contents + "return "; contents = contents + "return ";
if !is_primitive(&method.return_type) && !is_core_type(&method.return_type) { 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<String>)>, us
if class.instanciable { 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 + "\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 + "}\n\n";
contents = contents + "void " + strip_name(&class.name) + "::__destroy() {\n"; contents = contents + "void " + strip_name(&class.name) + "::__destroy() {\n";
@ -443,7 +472,7 @@ fn generate_icall_header(icalls: &HashSet<(String, Vec<String>)>) -> String {
if is_primitive(t) || is_core_type(t) { if is_primitive(t) || is_core_type(t) {
t.as_str() t.as_str()
} else { } else {
"Object& " "godot_object* "
} }
} }
@ -464,7 +493,7 @@ fn generate_icall_header(icalls: &HashSet<(String, Vec<String>)>) -> String {
contents = contents + return_type(ret) + " " + get_icall_name_ref((ret, args)).as_str() + "(godot_method_bind *mb, godot_object *inst"; contents = contents + return_type(ret) + " " + get_icall_name_ref((ret, args)).as_str() + "(godot_method_bind *mb, godot_object *inst";
for arg in args { for arg in args {
contents = contents + ", "; contents = contents + ", ";
if !is_primitive(&arg) { if !is_primitive(&arg) && is_core_type(&arg) {
contents = contents + "const " + arg.as_str() + "&"; contents = contents + "const " + arg.as_str() + "&";
} else { } else {
contents = contents + "const " + arg.as_str() + ""; contents = contents + "const " + arg.as_str() + "";
@ -484,7 +513,7 @@ fn generate_icall_implementation(icalls: &HashSet<(String, Vec<String>)>) -> Str
if is_primitive(t) || is_core_type(t) { if is_primitive(t) || is_core_type(t) {
t.as_str() t.as_str()
} else { } else {
"Object& " "godot_object* "
} }
} }
@ -504,7 +533,7 @@ fn generate_icall_implementation(icalls: &HashSet<(String, Vec<String>)>) -> Str
let mut i = 0; let mut i = 0;
for arg in args { for arg in args {
contents = contents + ", "; contents = contents + ", ";
if !is_primitive(&arg) { if !is_primitive(&arg) && is_core_type(&arg) {
contents = contents + "const " + arg.as_str() + "&"; contents = contents + "const " + arg.as_str() + "&";
} else { } else {
contents = contents + "const " + arg.as_str() + ""; contents = contents + "const " + arg.as_str() + "";
@ -530,7 +559,7 @@ fn generate_icall_implementation(icalls: &HashSet<(String, Vec<String>)>) -> Str
} else if is_core_type(arg) { } else if is_core_type(arg) {
contents = contents + "(void *) &arg" + j.to_string().as_str(); contents = contents + "(void *) &arg" + j.to_string().as_str();
} else { } else {
contents = contents + "(void *) &arg" + j.to_string().as_str(); contents = contents + "(void *) *(godot_object**) &arg" + j.to_string().as_str();
} }
contents = contents + ",\n"; contents = contents + ",\n";
j = j + 1; j = j + 1;
@ -541,7 +570,7 @@ fn generate_icall_implementation(icalls: &HashSet<(String, Vec<String>)>) -> Str
contents = contents + "\tgodot_method_bind_ptrcall(mb, inst, args, " + if ret == "void" { "NULL" } else { "&ret" } + ");\n"; contents = contents + "\tgodot_method_bind_ptrcall(mb, inst, args, " + if ret == "void" { "NULL" } else { "&ret" } + ");\n";
if !is_primitive(ret) && !is_core_type(ret) { if !is_primitive(ret) && !is_core_type(ret) {
contents = contents + "\treturn reinterpret_cast<Object&>(ret);\n"; contents = contents + "\treturn ret;\n";
} else if ret != "void" { } else if ret != "void" {
contents = contents + "\treturn ret;\n"; contents = contents + "\treturn ret;\n";
} }

View File

@ -2,6 +2,8 @@
import os import os
env = Environment() env = Environment()
if ARGUMENTS.get("use_llvm", "yes") == "yes":
env["CXX"] = "clang++" env["CXX"] = "clang++"
target = ARGUMENTS.get("target", "core") target = ARGUMENTS.get("target", "core")
@ -35,13 +37,18 @@ if (target == "core"):
library = env.SharedLibrary(target='godot_cpp_core', source=sources) library = env.SharedLibrary(target='godot_cpp_core', source=sources)
Default(library) Default(library)
elif target == "binding": elif target == "bindings":
if env["CXX"] == "clang++":
env.Append(CCFLAGS = ['-Wno-writable-strings']) 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(CCFLAGS = ['-g','-O3', '-std=c++14'])
env.Append(CPPPATH=['.', './godot', './godot_cpp']) env.Append(CPPPATH=['.', './godot', './godot_cpp'])
env.Append(RPATH=['.']) env.Append(LINKFLAGS = ['-Wl,-R,\'$$ORIGIN/\''])
env.Append(LINKFLAGS=['-Rgodot_cpp_core']) 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')] sources = [os.path.join("godot_cpp/impl/", f) for f in os.listdir("godot_cpp/impl/") if f.endswith('.cpp')]

View File

@ -20,16 +20,6 @@ namespace godot {
template<class A, class B>
A object_cast(B b)
{
A *a = (A*) &b;
return *a;
}
// instance and destroy funcs // instance and destroy funcs
@ -66,7 +56,7 @@ void register_class()
template<class T, class R, class A0, class A1, class A2, class A3, class A4, R (T::*p)(A0, A1, A2, A3, A4)> template<class T, class R, class A0, class A1, class A2, class A3, class A4, R (T::*p)(A0, A1, A2, A3, A4)>
struct WrappedMethod5 { 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 _variant;
godot_variant_new_nil(&_variant); godot_variant_new_nil(&_variant);
@ -80,7 +70,7 @@ struct WrappedMethod5 {
template<class T, class A0, class A1, class A2, class A3, class A4, void (T::*p)(A0, A1, A2, A3, A4)> template<class T, class A0, class A1, class A2, class A3, class A4, void (T::*p)(A0, A1, A2, A3, A4)>
struct WrappedMethod5<T, void, A0, A1, A2, A3, A4, p> { struct WrappedMethod5<T, void, A0, A1, A2, A3, A4, p> {
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 _variant;
godot_variant_new_nil(&_variant); godot_variant_new_nil(&_variant);
@ -93,7 +83,7 @@ struct WrappedMethod5<T, void, A0, A1, A2, A3, A4, p> {
template<class T, class R, class A0, class A1, class A2, class A3, R (T::*p)(A0, A1, A2, A3)> template<class T, class R, class A0, class A1, class A2, class A3, R (T::*p)(A0, A1, A2, A3)>
struct WrappedMethod4 { 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 _variant;
godot_variant_new_nil(&_variant); godot_variant_new_nil(&_variant);
@ -107,7 +97,7 @@ struct WrappedMethod4 {
template<class T, class A0, class A1, class A2, class A3, void (T::*p)(A0, A1, A2, A3)> template<class T, class A0, class A1, class A2, class A3, void (T::*p)(A0, A1, A2, A3)>
struct WrappedMethod4<T, void, A0, A1, A2, A3, p> { struct WrappedMethod4<T, void, A0, A1, A2, A3, p> {
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 _variant;
godot_variant_new_nil(&_variant); godot_variant_new_nil(&_variant);
@ -121,7 +111,7 @@ struct WrappedMethod4<T, void, A0, A1, A2, A3, p> {
template<class T, class R, class A0, class A1, class A2, R (T::*p)(A0, A1, A2)> template<class T, class R, class A0, class A1, class A2, R (T::*p)(A0, A1, A2)>
struct WrappedMethod3 { 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 _variant;
godot_variant_new_nil(&_variant); godot_variant_new_nil(&_variant);
@ -135,7 +125,7 @@ struct WrappedMethod3 {
template<class T, class A0, class A1, class A2, void (T::*p)(A0, A1, A2)> template<class T, class A0, class A1, class A2, void (T::*p)(A0, A1, A2)>
struct WrappedMethod3<T, void, A0, A1, A2, p> { struct WrappedMethod3<T, void, A0, A1, A2, p> {
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 _variant;
godot_variant_new_nil(&_variant); godot_variant_new_nil(&_variant);
@ -148,7 +138,7 @@ struct WrappedMethod3<T, void, A0, A1, A2, p> {
template<class T, class R, class A0, class A1, R (T::*p)(A0, A1)> template<class T, class R, class A0, class A1, R (T::*p)(A0, A1)>
struct WrappedMethod2 { 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 _variant;
godot_variant_new_nil(&_variant); godot_variant_new_nil(&_variant);
@ -162,7 +152,7 @@ struct WrappedMethod2 {
template<class T, class A0, class A1, void (T::*p)(A0, A1)> template<class T, class A0, class A1, void (T::*p)(A0, A1)>
struct WrappedMethod2<T, void, A0, A1, p> { struct WrappedMethod2<T, void, A0, A1, p> {
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 _variant;
godot_variant_new_nil(&_variant); godot_variant_new_nil(&_variant);
@ -175,7 +165,7 @@ struct WrappedMethod2<T, void, A0, A1, p> {
template<class T, class R, class I, R (T::*p)(I)> template<class T, class R, class I, R (T::*p)(I)>
struct WrappedMethod1 { 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 _variant;
godot_variant_new_nil(&_variant); godot_variant_new_nil(&_variant);
@ -189,7 +179,7 @@ struct WrappedMethod1 {
template <class T, class I, void (T::*p)(I)> template <class T, class I, void (T::*p)(I)>
struct WrappedMethod1<T, void, I, p> { struct WrappedMethod1<T, void, I, p> {
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 _variant;
godot_variant_new_nil(&_variant); godot_variant_new_nil(&_variant);
@ -204,7 +194,7 @@ struct WrappedMethod1<T, void, I, p> {
template<class T, class R, R (T::*p)()> template<class T, class R, R (T::*p)()>
struct WrappedMethod0 { 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 _variant;
godot_variant_new_nil(&_variant); godot_variant_new_nil(&_variant);
@ -217,7 +207,7 @@ struct WrappedMethod0 {
template<class T, void (T::*p)()> template<class T, void (T::*p)()>
struct WrappedMethod0<T, void, p> { struct WrappedMethod0<T, void, p> {
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 _variant;
godot_variant_new_nil(&_variant); godot_variant_new_nil(&_variant);

View File

@ -14,7 +14,7 @@ struct Color {
private: private:
static float _parse_col(const String& p_str, int p_ofs); // static float _parse_col(const String& p_str, int p_ofs);
public: public:
union { union {