]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
D-Bus: Use proper source_object in callbacks of async calls with GDBus
authorJürg Billeter <j@bitron.ch>
Fri, 22 Oct 2010 09:52:28 +0000 (11:52 +0200)
committerJürg Billeter <j@bitron.ch>
Sat, 23 Oct 2010 16:11:46 +0000 (18:11 +0200)
codegen/valagasyncmodule.vala
codegen/valagdbusclientmodule.vala

index fc5a64d508ae2fe861460ab05d03bcf1a618e7dd..70c3cbe64fe57d48d7a93064b78df4ad52f021ec 100644 (file)
@@ -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;
+       }
+}
index 25191351cea279cac9365b12fb64ad1798c3e9fc..069b4e42b8d84ed0cd029b1f83ee6ffae8147b4e 100644 (file)
@@ -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));
                }