[vala] D-Bus: Support get_proxy with generic type in GDBus clients



commit 1e68b9ae371c00c89d0893b9c67ad0c00f5b5a28
Author: Jürg Billeter <j bitron ch>
Date:   Sat Jan 15 17:50:28 2011 +0100

    D-Bus: Support get_proxy with generic type in GDBus clients
    
    Fixes bug 612517.

 codegen/Makefile.am                            |    1 -
 codegen/valaccodebasemodule.vala               |    2 +-
 codegen/valaclassregisterfunction.vala         |   10 +--
 codegen/valadbusclientmodule.vala              |   30 ++++++---
 codegen/valadbusinterfaceregisterfunction.vala |   52 ----------------
 codegen/valadbusservermodule.vala              |   24 ++++---
 codegen/valaenumregisterfunction.vala          |    4 -
 codegen/valagdbusclientmodule.vala             |   78 +++++++++++++++++++++---
 codegen/valagdbusservermodule.vala             |    8 ++-
 codegen/valagtypemodule.vala                   |   12 +---
 codegen/valainterfaceregisterfunction.vala     |   13 ++--
 codegen/valastructregisterfunction.vala        |    4 -
 codegen/valatyperegisterfunction.vala          |    5 +-
 13 files changed, 126 insertions(+), 117 deletions(-)
---
diff --git a/codegen/Makefile.am b/codegen/Makefile.am
index 7da6689..3232849 100644
--- a/codegen/Makefile.am
+++ b/codegen/Makefile.am
@@ -28,7 +28,6 @@ libvala_la_VALASOURCES = \
 	valaclassregisterfunction.vala \
 	valactype.vala \
 	valadbusclientmodule.vala \
-	valadbusinterfaceregisterfunction.vala \
 	valadbusmodule.vala \
 	valadbusservermodule.vala \
 	valadovaarraymodule.vala \
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index b24e839..0b37387 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -5849,7 +5849,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		return new CCodeFunctionCall (new CCodeIdentifier (""));
 	}
 
-	public virtual void register_dbus_info (ObjectTypeSymbol bindable) {
+	public virtual void register_dbus_info (CCodeBlock block, ObjectTypeSymbol bindable) {
 	}
 
 	public virtual string get_dynamic_property_getter_cname (DynamicProperty node) {
diff --git a/codegen/valaclassregisterfunction.vala b/codegen/valaclassregisterfunction.vala
index 64f0725..8e8cb5c 100644
--- a/codegen/valaclassregisterfunction.vala
+++ b/codegen/valaclassregisterfunction.vala
@@ -171,9 +171,7 @@ public class Vala.ClassRegisterFunction : TypeRegisterFunction {
 		return frag;
 	}
 
-	public override CCodeFragment get_type_interface_init_statements (bool plugin) {
-		var frag = new CCodeFragment ();
-		
+	public override void get_type_interface_init_statements (CCodeBlock block, bool plugin) {
 		foreach (DataType base_type in class_reference.get_base_types ()) {
 			if (!(base_type.data_type is Interface)) {
 				continue;
@@ -187,17 +185,17 @@ public class Vala.ClassRegisterFunction : TypeRegisterFunction {
 				reg_call.add_argument (new CCodeIdentifier ("%s_type_id".printf (class_reference.get_lower_case_cname (null))));
 				reg_call.add_argument (new CCodeIdentifier (iface.get_type_id ()));
 				reg_call.add_argument (new CCodeIdentifier ("&%s".printf (iface_info_name)));
-				frag.append (new CCodeExpressionStatement (reg_call));
+				block.add_statement (new CCodeExpressionStatement (reg_call));
 			} else {
 				var reg_call = new CCodeFunctionCall (new CCodeIdentifier ("g_type_module_add_interface"));
 				reg_call.add_argument (new CCodeIdentifier ("module"));
 				reg_call.add_argument (new CCodeIdentifier ("%s_type_id".printf (class_reference.get_lower_case_cname (null))));
 				reg_call.add_argument (new CCodeIdentifier (iface.get_type_id ()));
 				reg_call.add_argument (new CCodeIdentifier ("&%s".printf (iface_info_name)));
-				frag.append (new CCodeExpressionStatement (reg_call));
+				block.add_statement (new CCodeExpressionStatement (reg_call));
 			}
 		}
 		
-		return frag;
+		((CCodeBaseModule) context.codegen).register_dbus_info (block, class_reference);
 	}
 }
diff --git a/codegen/valadbusclientmodule.vala b/codegen/valadbusclientmodule.vala
index 4bd52d0..04004df 100644
--- a/codegen/valadbusclientmodule.vala
+++ b/codegen/valadbusclientmodule.vala
@@ -1300,15 +1300,6 @@ public class Vala.DBusClientModule : DBusModule {
 		cfile.add_function (set_prop);
 	}
 
-	public override TypeRegisterFunction create_interface_register_function (Interface iface) {
-		string dbus_iface_name = get_dbus_name (iface);
-		if (dbus_iface_name == null) {
-			return new InterfaceRegisterFunction (iface, context);
-		}
-
-		return new DBusInterfaceRegisterFunction (iface, context);
-	}
-
 	string generate_get_all_function (Method m) {
 		string get_all_func = "_dbus_g_proxy_get_all";
 
@@ -2820,4 +2811,25 @@ public class Vala.DBusClientModule : DBusModule {
 
 		return proxy_name;
 	}
+
+	public override void register_dbus_info (CCodeBlock block, ObjectTypeSymbol sym) {
+		if (!(sym is Interface)) {
+			return;
+		}
+
+		string dbus_iface_name = get_dbus_name (sym);
+		if (dbus_iface_name == null) {
+			return;
+		}
+
+		var quark_dbus_proxy = new CCodeFunctionCall (new CCodeIdentifier ("g_quark_from_string"));
+		quark_dbus_proxy.add_argument (new CCodeConstant ("\"ValaDBusInterfaceProxyType\""));
+
+		var func = new CCodeFunctionCall (new CCodeIdentifier ("g_type_set_qdata"));
+		func.add_argument (new CCodeIdentifier ("%s_type_id".printf (sym.get_lower_case_cname (null))));
+		func.add_argument (quark_dbus_proxy);
+		func.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("%s_dbus_proxy_get_type".printf (sym.get_lower_case_cname (null)))));
+
+		block.add_statement (new CCodeExpressionStatement (func));
+	}
 }
diff --git a/codegen/valadbusservermodule.vala b/codegen/valadbusservermodule.vala
index d9acbd4..c938489 100644
--- a/codegen/valadbusservermodule.vala
+++ b/codegen/valadbusservermodule.vala
@@ -1604,15 +1604,17 @@ public class Vala.DBusServerModule : DBusClientModule {
 	CCodeExpression get_vtable (ObjectType object_type) {
 		var sym = object_type.type_symbol;
 
-		var vtable = new CCodeInitializerList ();
-		vtable.append (new CCodeIdentifier (sym.get_lower_case_cprefix () + "dbus_register_object"));
+		if (add_wrapper ("_" + sym.get_lower_case_cprefix () + "dbus_vtable")) {
+			var vtable = new CCodeInitializerList ();
+			vtable.append (new CCodeIdentifier (sym.get_lower_case_cprefix () + "dbus_register_object"));
 
-		generate_register_function (object_type);
+			generate_register_function (object_type);
 
-		var cdecl = new CCodeDeclaration ("const _DBusObjectVTable");
-		cdecl.add_declarator (new CCodeVariableDeclarator ("_" + sym.get_lower_case_cprefix () + "dbus_vtable", vtable));
-		cdecl.modifiers = CCodeModifiers.STATIC;
-		cfile.add_constant_declaration (cdecl);
+			var cdecl = new CCodeDeclaration ("const _DBusObjectVTable");
+			cdecl.add_declarator (new CCodeVariableDeclarator ("_" + sym.get_lower_case_cprefix () + "dbus_vtable", vtable));
+			cdecl.modifiers = CCodeModifiers.STATIC;
+			cfile.add_constant_declaration (cdecl);
+		}
 
 		return new CCodeIdentifier ("_" + sym.get_lower_case_cprefix () + "dbus_vtable");
 	}
@@ -1687,19 +1689,21 @@ public class Vala.DBusServerModule : DBusClientModule {
 		return false;
 	}
 
-	public override void register_dbus_info (ObjectTypeSymbol sym) {
+	public override void register_dbus_info (CCodeBlock block, ObjectTypeSymbol sym) {
 		if (!type_implements_dbus_interface (sym)) {
 			return;
 		}
 
+		base.register_dbus_info (block, sym);
+
 		var quark = new CCodeFunctionCall (new CCodeIdentifier ("g_quark_from_static_string"));
 		quark.add_argument (new CCodeConstant ("\"DBusObjectVTable\""));
 
 		var set_qdata = new CCodeFunctionCall (new CCodeIdentifier ("g_type_set_qdata"));
-		set_qdata.add_argument (new CCodeIdentifier (sym.get_upper_case_cname ("TYPE_")));
+		set_qdata.add_argument (new CCodeIdentifier ("%s_type_id".printf (sym.get_lower_case_cname (null))));
 		set_qdata.add_argument (quark);
 		set_qdata.add_argument (new CCodeCastExpression (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, get_vtable (new ObjectType (sym))), "void*"));
 
-		ccode.add_expression (set_qdata);
+		block.add_statement (new CCodeExpressionStatement (set_qdata));
 	}
 }
diff --git a/codegen/valaenumregisterfunction.vala b/codegen/valaenumregisterfunction.vala
index e54098c..ee6ab69 100644
--- a/codegen/valaenumregisterfunction.vala
+++ b/codegen/valaenumregisterfunction.vala
@@ -50,8 +50,4 @@ public class Vala.EnumRegisterFunction : TypeRegisterFunction {
 	public override SymbolAccessibility get_accessibility () {
 		return enum_reference.access;
 	}
-
-	public override CCodeFragment get_type_interface_init_statements (bool plugin) {
-		return new CCodeFragment ();
-	}
 }
diff --git a/codegen/valagdbusclientmodule.vala b/codegen/valagdbusclientmodule.vala
index 5d884cd..acba1fc 100644
--- a/codegen/valagdbusclientmodule.vala
+++ b/codegen/valagdbusclientmodule.vala
@@ -216,13 +216,42 @@ public class Vala.GDBusClientModule : GDBusModule {
 		}
 
 		var ma = (MemberAccess) expr.call;
-		var type_arg = (ObjectType) ma.get_type_arguments ().get (0);
-		var iface = (Interface) type_arg.type_symbol;
+		var type_arg = ma.get_type_arguments ().get (0);
 
-		string dbus_iface_name = get_dbus_name (iface);
-		if (dbus_iface_name == null) {
-			Report.error (expr.source_reference, "`%s' is not a D-Bus interface".printf (iface.get_full_name ()));
-			return;
+		CCodeExpression proxy_type;
+		CCodeExpression dbus_iface_name;
+
+		var object_type = type_arg as ObjectType;
+		if (object_type != null) {
+			var iface = (Interface) object_type.type_symbol;
+
+			if (get_dbus_name (iface) == null) {
+				Report.error (expr.source_reference, "`%s' is not a D-Bus interface".printf (iface.get_full_name ()));
+				return;
+			}
+
+			proxy_type = new CCodeIdentifier ("%s_PROXY".printf (iface.get_type_id ()));
+			dbus_iface_name = new CCodeConstant ("\"%s\"".printf (get_dbus_name (iface)));
+		} else {
+			// use runtime type information for generic methods
+
+			var quark = new CCodeFunctionCall (new CCodeIdentifier ("g_quark_from_static_string"));
+			quark.add_argument (new CCodeConstant ("\"vala-dbus-proxy-type\""));
+
+			var get_qdata = new CCodeFunctionCall (new CCodeIdentifier ("g_type_get_qdata"));
+			get_qdata.add_argument (get_type_id_expression (type_arg));
+			get_qdata.add_argument (quark);
+
+			proxy_type = new CCodeFunctionCall (new CCodeCastExpression (get_qdata, "GType (*) (void)"));
+
+			quark = new CCodeFunctionCall (new CCodeIdentifier ("g_quark_from_static_string"));
+			quark.add_argument (new CCodeConstant ("\"vala-dbus-interface-name\""));
+
+			get_qdata = new CCodeFunctionCall (new CCodeIdentifier ("g_type_get_qdata"));
+			get_qdata.add_argument (get_type_id_expression (type_arg));
+			get_qdata.add_argument (quark);
+
+			dbus_iface_name = get_qdata;
 		}
 
 		var base_arg_index = 0;
@@ -244,7 +273,7 @@ public class Vala.GDBusClientModule : GDBusModule {
 		} else {
 			ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_initable_new"));
 		}
-		ccall.add_argument (new CCodeIdentifier ("%s_PROXY".printf (iface.get_type_id ())));
+		ccall.add_argument (proxy_type);
 		if (bus_get_proxy_async || conn_get_proxy_async) {
 			// I/O priority
 			ccall.add_argument (new CCodeConstant ("0"));
@@ -280,7 +309,7 @@ public class Vala.GDBusClientModule : GDBusModule {
 		object_path.emit (this);
 		ccall.add_argument (get_cvalue (object_path));
 		ccall.add_argument (new CCodeConstant ("\"g-interface-name\""));
-		ccall.add_argument (new CCodeConstant ("\"%s\"".printf (get_dbus_name (iface))));
+		ccall.add_argument (dbus_iface_name);
 		ccall.add_argument (new CCodeConstant ("NULL"));
 
 		if (bus_get_proxy_async || conn_get_proxy_async) {
@@ -1016,4 +1045,37 @@ public class Vala.GDBusClientModule : GDBusModule {
 
 		return proxy_name;
 	}
+
+	public override void register_dbus_info (CCodeBlock block, ObjectTypeSymbol sym) {
+		if (!(sym is Interface)) {
+			return;
+		}
+
+		string dbus_iface_name = get_dbus_name (sym);
+		if (dbus_iface_name == null) {
+			return;
+		}
+
+		var quark = new CCodeFunctionCall (new CCodeIdentifier ("g_quark_from_static_string"));
+		quark.add_argument (new CCodeConstant ("\"vala-dbus-proxy-type\""));
+
+		var proxy_type = new CCodeIdentifier (sym.get_lower_case_cprefix () + "proxy_get_type");
+
+		var set_qdata = new CCodeFunctionCall (new CCodeIdentifier ("g_type_set_qdata"));
+		set_qdata.add_argument (new CCodeIdentifier ("%s_type_id".printf (sym.get_lower_case_cname (null))));
+		set_qdata.add_argument (quark);
+		set_qdata.add_argument (new CCodeCastExpression (proxy_type, "void*"));
+
+		block.add_statement (new CCodeExpressionStatement (set_qdata));
+
+		quark = new CCodeFunctionCall (new CCodeIdentifier ("g_quark_from_static_string"));
+		quark.add_argument (new CCodeConstant ("\"vala-dbus-interface-name\""));
+
+		set_qdata = new CCodeFunctionCall (new CCodeIdentifier ("g_type_set_qdata"));
+		set_qdata.add_argument (new CCodeIdentifier ("%s_type_id".printf (sym.get_lower_case_cname (null))));
+		set_qdata.add_argument (quark);
+		set_qdata.add_argument (new CCodeConstant ("\"%s\"".printf (dbus_iface_name)));
+
+		block.add_statement (new CCodeExpressionStatement (set_qdata));
+	}
 }
diff --git a/codegen/valagdbusservermodule.vala b/codegen/valagdbusservermodule.vala
index c9c7c03..c121728 100644
--- a/codegen/valagdbusservermodule.vala
+++ b/codegen/valagdbusservermodule.vala
@@ -1323,20 +1323,22 @@ public class Vala.GDBusServerModule : GDBusClientModule {
 		cfile.add_function (cfunc);
 	}
 
-	public override void register_dbus_info (ObjectTypeSymbol sym) {
+	public override void register_dbus_info (CCodeBlock block, ObjectTypeSymbol sym) {
 		string dbus_iface_name = get_dbus_name (sym);
 		if (dbus_iface_name == null) {
 			return;
 		}
 
+		base.register_dbus_info (block, sym);
+
 		var quark = new CCodeFunctionCall (new CCodeIdentifier ("g_quark_from_static_string"));
 		quark.add_argument (new CCodeConstant ("\"vala-dbus-register-object\""));
 
 		var set_qdata = new CCodeFunctionCall (new CCodeIdentifier ("g_type_set_qdata"));
-		set_qdata.add_argument (new CCodeIdentifier (sym.get_upper_case_cname ("TYPE_")));
+		set_qdata.add_argument (new CCodeIdentifier ("%s_type_id".printf (sym.get_lower_case_cname (null))));
 		set_qdata.add_argument (quark);
 		set_qdata.add_argument (new CCodeCastExpression (new CCodeIdentifier (sym.get_lower_case_cprefix () + "register_object"), "void*"));
 
-		ccode.add_expression (set_qdata);
+		block.add_statement (new CCodeExpressionStatement (set_qdata));
 	}
 }
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala
index 2e021d4..1095442 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -1302,8 +1302,6 @@ public class Vala.GTypeModule : GErrorModule {
 			}
 		}
 
-		register_dbus_info (cl);
-
 		pop_context ();
 	}
 
@@ -1972,7 +1970,7 @@ public class Vala.GTypeModule : GErrorModule {
 
 		decl_space.add_type_definition (type_struct);
 
-		var type_fun = create_interface_register_function (iface);
+		var type_fun = new InterfaceRegisterFunction (iface, context);
 		type_fun.init_from_type (in_plugin);
 		decl_space.add_type_member_declaration (type_fun.get_declaration ());
 	}
@@ -2002,7 +2000,7 @@ public class Vala.GTypeModule : GErrorModule {
 			cfile.add_type_member_definition (new CCodeComment (iface.comment.content));
 		}
 
-		var type_fun = create_interface_register_function (iface);
+		var type_fun = new InterfaceRegisterFunction (iface, context);
 		type_fun.init_from_type (in_plugin);
 		cfile.add_type_member_declaration (type_fun.get_source_declaration ());
 		cfile.add_type_member_definition (type_fun.get_definition ());
@@ -2010,10 +2008,6 @@ public class Vala.GTypeModule : GErrorModule {
 		pop_context ();
 	}
 
-	public virtual TypeRegisterFunction create_interface_register_function (Interface iface) {
-		return new InterfaceRegisterFunction (iface, context);
-	}
-
 	private void add_interface_base_init_function (Interface iface) {
 		push_context (new EmitContext (iface));
 
@@ -2068,8 +2062,6 @@ public class Vala.GTypeModule : GErrorModule {
 			}
 		}
 
-		register_dbus_info (iface);
-
 		ccode.close ();
 
 		pop_context ();
diff --git a/codegen/valainterfaceregisterfunction.vala b/codegen/valainterfaceregisterfunction.vala
index 500e7ee..cec7cbd 100644
--- a/codegen/valainterfaceregisterfunction.vala
+++ b/codegen/valainterfaceregisterfunction.vala
@@ -1,6 +1,7 @@
 /* valainterfaceregisterfunction.vala
  *
- * Copyright (C) 2006-2007  Jürg Billeter, Raffaele Sandrini
+ * Copyright (C) 2006-2011  Jürg Billeter
+ * Copyright (C) 2006-2007  Raffaele Sandrini
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -77,9 +78,7 @@ public class Vala.InterfaceRegisterFunction : TypeRegisterFunction {
 		return interface_reference.access;
 	}
 
-	public override CCodeFragment get_type_interface_init_statements (bool plugin) {
-		var frag = new CCodeFragment ();
-		
+	public override void get_type_interface_init_statements (CCodeBlock block, bool plugin) {
 		/* register all prerequisites */
 		foreach (DataType prereq_ref in interface_reference.get_prerequisites ()) {
 			var prereq = prereq_ref.data_type;
@@ -88,9 +87,9 @@ public class Vala.InterfaceRegisterFunction : TypeRegisterFunction {
 			func.add_argument (new CCodeIdentifier ("%s_type_id".printf (interface_reference.get_lower_case_cname (null))));
 			func.add_argument (new CCodeIdentifier (prereq.get_type_id()));
 			
-			frag.append (new CCodeExpressionStatement (func));
+			block.add_statement (new CCodeExpressionStatement (func));
 		}
-		
-		return frag;
+
+		((CCodeBaseModule) context.codegen).register_dbus_info (block, interface_reference);
 	}
 }
diff --git a/codegen/valastructregisterfunction.vala b/codegen/valastructregisterfunction.vala
index 2684b48..404e412 100644
--- a/codegen/valastructregisterfunction.vala
+++ b/codegen/valastructregisterfunction.vala
@@ -49,8 +49,4 @@ public class Vala.StructRegisterFunction : TypeRegisterFunction {
 	public override SymbolAccessibility get_accessibility () {
 		return struct_reference.access;
 	}
-
-	public override CCodeFragment get_type_interface_init_statements (bool plugin) {
-		return new CCodeFragment ();
-	}
 }
diff --git a/codegen/valatyperegisterfunction.vala b/codegen/valatyperegisterfunction.vala
index 7b561f7..be0f212 100644
--- a/codegen/valatyperegisterfunction.vala
+++ b/codegen/valatyperegisterfunction.vala
@@ -222,7 +222,7 @@ public abstract class Vala.TypeRegisterFunction {
 			type_init.add_statement (new CCodeExpressionStatement (add_class_private_call));
 		}
 
-		type_init.add_statement (get_type_interface_init_statements (plugin));
+		get_type_interface_init_statements (type_init, plugin);
 
 		if (!plugin) {
 			CCodeExpression condition; // the condition that guards the type initialisation
@@ -426,7 +426,8 @@ public abstract class Vala.TypeRegisterFunction {
 	 *
 	 * @return C statements
 	 */
-	public abstract CCodeFragment get_type_interface_init_statements (bool plugin);
+	public virtual void get_type_interface_init_statements (CCodeBlock block, bool plugin) {
+	}
 	
 	public CCodeFragment get_source_declaration () {
 		return source_declaration_fragment;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]