[vala] D-Bus: Use proper source_object in callbacks of async calls with GDBus



commit 0f8d470d31347fb14ac584b951e6becd527f6166
Author: Jürg Billeter <j bitron ch>
Date:   Fri Oct 22 11:52:28 2010 +0200

    D-Bus: Use proper source_object in callbacks of async calls with GDBus

 codegen/valagasyncmodule.vala      |   46 ++++++++++++++++++++++++++++++++++-
 codegen/valagdbusclientmodule.vala |   19 ++++++++++++--
 2 files changed, 60 insertions(+), 5 deletions(-)
---
diff --git a/codegen/valagasyncmodule.vala b/codegen/valagasyncmodule.vala
index fc5a64d..70c3cbe 100644
--- a/codegen/valagasyncmodule.vala
+++ b/codegen/valagasyncmodule.vala
@@ -643,6 +643,48 @@ public class Vala.GAsyncModule : GSignalModule {
 		}
 		base.generate_cparameters (m, decl_space, cparam_map, func, vdeclarator, carg_map, vcall, direction);
 	}
-}
 
-// vim:sw=8 noet
+	public string generate_async_callback_wrapper () {
+		string async_callback_wrapper_func = "_vala_g_async_ready_callback";
+
+		if (!add_wrapper (async_callback_wrapper_func)) {
+			return async_callback_wrapper_func;
+		}
+
+		var function = new CCodeFunction (async_callback_wrapper_func, "void");
+		function.modifiers = CCodeModifiers.STATIC;
+
+		function.add_parameter (new CCodeFormalParameter ("*source_object", "GObject"));
+		function.add_parameter (new CCodeFormalParameter ("*res", "GAsyncResult"));
+		function.add_parameter (new CCodeFormalParameter ("*user_data", "void"));
+
+		push_function (function);
+
+		var res_ref = new CCodeFunctionCall (new CCodeIdentifier ("g_object_ref"));
+		res_ref.add_argument (new CCodeIdentifier ("res"));
+
+		// store reference to async result of inner async function in out async result
+		var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_simple_async_result_set_op_res_gpointer"));
+		ccall.add_argument (new CCodeIdentifier ("user_data"));
+		ccall.add_argument (res_ref);
+		ccall.add_argument (new CCodeIdentifier ("g_object_unref"));
+		ccode.add_expression (ccall);
+
+		// call user-provided callback
+		ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_simple_async_result_complete"));
+		ccall.add_argument (new CCodeIdentifier ("user_data"));
+		ccode.add_expression (ccall);
+
+		// free async result
+		ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_object_unref"));
+		ccall.add_argument (new CCodeIdentifier ("user_data"));
+		ccode.add_expression (ccall);
+
+		pop_function ();
+
+		cfile.add_function_declaration (function);
+		cfile.add_function (function);
+
+		return async_callback_wrapper_func;
+	}
+}
diff --git a/codegen/valagdbusclientmodule.vala b/codegen/valagdbusclientmodule.vala
index 2519135..069b4e4 100644
--- a/codegen/valagdbusclientmodule.vala
+++ b/codegen/valagdbusclientmodule.vala
@@ -506,8 +506,16 @@ public class Vala.GDBusClientModule : GDBusModule {
 				ccall.add_argument (timeout);
 				ccall.add_argument (new CCodeConstant ("NULL"));
 				ccall.add_argument (new CCodeConstant ("NULL"));
-				ccall.add_argument (new CCodeIdentifier ("_callback_"));
-				ccall.add_argument (new CCodeIdentifier ("_user_data_"));
+
+				// use wrapper as source_object wouldn't be correct otherwise
+				ccall.add_argument (new CCodeIdentifier (generate_async_callback_wrapper ()));
+				var res_wrapper = new CCodeFunctionCall (new CCodeIdentifier ("g_simple_async_result_new"));
+				res_wrapper.add_argument (new CCodeIdentifier ("self"));
+				res_wrapper.add_argument (new CCodeIdentifier ("_callback_"));
+				res_wrapper.add_argument (new CCodeIdentifier ("_user_data_"));
+				res_wrapper.add_argument (new CCodeConstant ("NULL"));
+				ccall.add_argument (res_wrapper);
+
 				ccode.add_expression (ccall);
 			}
 
@@ -519,7 +527,12 @@ public class Vala.GDBusClientModule : GDBusModule {
 		} else {
 			var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_dbus_connection_send_message_with_reply_finish"));
 			ccall.add_argument (connection);
-			ccall.add_argument (new CCodeIdentifier ("_res_"));
+
+			// unwrap async result
+			var inner_res = new CCodeFunctionCall (new CCodeIdentifier ("g_simple_async_result_get_op_res_gpointer"));
+			inner_res.add_argument (new CCodeIdentifier ("_res_"));
+			ccall.add_argument (inner_res);
+
 			ccall.add_argument (new CCodeConstant ("error"));
 			ccode.add_expression (new CCodeAssignment (new CCodeIdentifier ("_reply_message"), ccall));
 		}



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