gtestdbus: Fix non-const use of const variables
[platform/upstream/glib.git] / gio / gasyncinitable.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2009 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24 #include "gasyncinitable.h"
25 #include "gasyncresult.h"
26 #include "gsimpleasyncresult.h"
27 #include "gtask.h"
28 #include "glibintl.h"
29
30
31 /**
32  * SECTION:gasyncinitable
33  * @short_description: Asynchronously failable object initialization interface
34  * @include: gio/gio.h
35  * @see_also: #GInitable
36  *
37  * This is the asynchronous version of #GInitable; it behaves the same
38  * in all ways except that initialization is asynchronous. For more details
39  * see the descriptions on #GInitable.
40  *
41  * A class may implement both the #GInitable and #GAsyncInitable interfaces.
42  *
43  * Users of objects implementing this are not intended to use the interface
44  * method directly; instead it will be used automatically in various ways.
45  * For C applications you generally just call g_async_initable_new_async()
46  * directly, or indirectly via a foo_thing_new_async() wrapper. This will call
47  * g_async_initable_init_async() under the cover, calling back with %NULL and
48  * a set %GError on failure.
49  *
50  * A typical implementation might look something like this:
51  *
52  * |[
53  * enum {
54  *    NOT_INITIALIZED,
55  *    INITIALIZING,
56  *    INITIALIZED
57  * };
58  *
59  * static void
60  * _foo_ready_cb (Foo *self)
61  * {
62  *   GList *l;
63  *
64  *   self->priv->state = INITIALIZED;
65  *
66  *   for (l = self->priv->init_results; l != NULL; l = l->next)
67  *     {
68  *       GTask *task = l->data;
69  *
70  *       if (self->priv->success)
71  *         g_task_return_boolean (task, TRUE);
72  *       else
73  *         g_task_return_new_error (task, ...);
74  *       g_object_unref (task);
75  *     }
76  *
77  *   g_list_free (self->priv->init_results);
78  *   self->priv->init_results = NULL;
79  * }
80  *
81  * static void
82  * foo_init_async (GAsyncInitable       *initable,
83  *                 int                   io_priority,
84  *                 GCancellable         *cancellable,
85  *                 GAsyncReadyCallback   callback,
86  *                 gpointer              user_data)
87  * {
88  *   Foo *self = FOO (initable);
89  *   GTask *task;
90  *
91  *   task = g_task_new (initable, cancellable, callback, user_data);
92  *
93  *   switch (self->priv->state)
94  *     {
95  *       case NOT_INITIALIZED:
96  *         _foo_get_ready (self);
97  *         self->priv->init_results = g_list_append (self->priv->init_results,
98  *                                                   task);
99  *         self->priv->state = INITIALIZING;
100  *         break;
101  *       case INITIALIZING:
102  *         self->priv->init_results = g_list_append (self->priv->init_results,
103  *                                                   task);
104  *         break;
105  *       case INITIALIZED:
106  *         if (!self->priv->success)
107  *           g_task_return_new_error (task, ...);
108  *         else
109  *           g_task_return_boolean (task, TRUE);
110  *         g_object_unref (task);
111  *         break;
112  *     }
113  * }
114  *
115  * static gboolean
116  * foo_init_finish (GAsyncInitable       *initable,
117  *                  GAsyncResult         *result,
118  *                  GError              **error)
119  * {
120  *   g_return_val_if_fail (g_task_is_valid (result, initable), FALSE);
121  *
122  *   return g_task_propagate_boolean (G_TASK (result), error);
123  * }
124  *
125  * static void
126  * foo_async_initable_iface_init (gpointer g_iface,
127  *                                gpointer data)
128  * {
129  *   GAsyncInitableIface *iface = g_iface;
130  *
131  *   iface->init_async = foo_init_async;
132  *   iface->init_finish = foo_init_finish;
133  * }
134  * ]|
135  */
136
137 static void     g_async_initable_real_init_async  (GAsyncInitable       *initable,
138                                                    int                   io_priority,
139                                                    GCancellable         *cancellable,
140                                                    GAsyncReadyCallback   callback,
141                                                    gpointer              user_data);
142 static gboolean g_async_initable_real_init_finish (GAsyncInitable       *initable,
143                                                    GAsyncResult         *res,
144                                                    GError              **error);
145
146
147 typedef GAsyncInitableIface GAsyncInitableInterface;
148 G_DEFINE_INTERFACE (GAsyncInitable, g_async_initable, G_TYPE_OBJECT)
149
150
151 static void
152 g_async_initable_default_init (GAsyncInitableInterface *iface)
153 {
154   iface->init_async = g_async_initable_real_init_async;
155   iface->init_finish = g_async_initable_real_init_finish;
156 }
157
158 /**
159  * g_async_initable_init_async:
160  * @initable: a #GAsyncInitable.
161  * @io_priority: the <link linkend="io-priority">I/O priority</link>
162  *     of the operation.
163  * @cancellable: optional #GCancellable object, %NULL to ignore.
164  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
165  * @user_data: the data to pass to callback function
166  *
167  * Starts asynchronous initialization of the object implementing the
168  * interface. This must be done before any real use of the object after
169  * initial construction. If the object also implements #GInitable you can
170  * optionally call g_initable_init() instead.
171  *
172  * When the initialization is finished, @callback will be called. You can
173  * then call g_async_initable_init_finish() to get the result of the
174  * initialization.
175  *
176  * Implementations may also support cancellation. If @cancellable is not
177  * %NULL, then initialization can be cancelled by triggering the cancellable
178  * object from another thread. If the operation was cancelled, the error
179  * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL, and
180  * the object doesn't support cancellable initialization, the error
181  * %G_IO_ERROR_NOT_SUPPORTED will be returned.
182  *
183  * As with #GInitable, if the object is not initialized, or initialization
184  * returns with an error, then all operations on the object except
185  * g_object_ref() and g_object_unref() are considered to be invalid, and
186  * have undefined behaviour. They will often fail with g_critical() or
187  * g_warning(), but this must not be relied on.
188  *
189  * Implementations of this method must be idempotent: i.e. multiple calls
190  * to this function with the same argument should return the same results.
191  * Only the first call initializes the object; further calls return the result
192  * of the first call. This is so that it's safe to implement the singleton
193  * pattern in the GObject constructor function.
194  *
195  * For classes that also support the #GInitable interface, the default
196  * implementation of this method will run the g_initable_init() function
197  * in a thread, so if you want to support asynchronous initialization via
198  * threads, just implement the #GAsyncInitable interface without overriding
199  * any interface methods.
200  *
201  * Since: 2.22
202  */
203 void
204 g_async_initable_init_async (GAsyncInitable      *initable,
205                              int                  io_priority,
206                              GCancellable        *cancellable,
207                              GAsyncReadyCallback  callback,
208                              gpointer             user_data)
209 {
210   GAsyncInitableIface *iface;
211
212   g_return_if_fail (G_IS_ASYNC_INITABLE (initable));
213
214   iface = G_ASYNC_INITABLE_GET_IFACE (initable);
215
216   (* iface->init_async) (initable, io_priority, cancellable, callback, user_data);
217 }
218
219 /**
220  * g_async_initable_init_finish:
221  * @initable: a #GAsyncInitable.
222  * @res: a #GAsyncResult.
223  * @error: a #GError location to store the error occurring, or %NULL to
224  * ignore.
225  *
226  * Finishes asynchronous initialization and returns the result.
227  * See g_async_initable_init_async().
228  *
229  * Returns: %TRUE if successful. If an error has occurred, this function
230  * will return %FALSE and set @error appropriately if present.
231  *
232  * Since: 2.22
233  */
234 gboolean
235 g_async_initable_init_finish (GAsyncInitable  *initable,
236                               GAsyncResult    *res,
237                               GError         **error)
238 {
239   GAsyncInitableIface *iface;
240
241   g_return_val_if_fail (G_IS_ASYNC_INITABLE (initable), FALSE);
242   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
243
244   if (g_async_result_legacy_propagate_error (res, error))
245     return FALSE;
246
247   iface = G_ASYNC_INITABLE_GET_IFACE (initable);
248
249   return (* iface->init_finish) (initable, res, error);
250 }
251
252 static void
253 async_init_thread (GTask        *task,
254                    gpointer      source_object,
255                    gpointer      task_data,
256                    GCancellable *cancellable)
257 {
258   GError *error = NULL;
259
260   if (g_initable_init (G_INITABLE (source_object), cancellable, &error))
261     g_task_return_boolean (task, TRUE);
262   else
263     g_task_return_error (task, error);
264 }
265
266 static void
267 g_async_initable_real_init_async (GAsyncInitable      *initable,
268                                   int                  io_priority,
269                                   GCancellable        *cancellable,
270                                   GAsyncReadyCallback  callback,
271                                   gpointer             user_data)
272 {
273   GTask *task;
274
275   g_return_if_fail (G_IS_INITABLE (initable));
276
277   task = g_task_new (initable, cancellable, callback, user_data);
278   g_task_set_priority (task, io_priority);
279   g_task_run_in_thread (task, async_init_thread);
280   g_object_unref (task);
281 }
282
283 static gboolean
284 g_async_initable_real_init_finish (GAsyncInitable  *initable,
285                                    GAsyncResult    *res,
286                                    GError         **error)
287 {
288   /* For backward compatibility we have to process GSimpleAsyncResults
289    * even though g_async_initable_real_init_async doesn't generate
290    * them any more.
291    */
292   if (G_IS_SIMPLE_ASYNC_RESULT (res))
293     {
294       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
295       if (g_simple_async_result_propagate_error (simple, error))
296         return FALSE;
297       else
298         return TRUE;
299     }
300
301   g_return_val_if_fail (g_task_is_valid (res, initable), FALSE);
302
303   return g_task_propagate_boolean (G_TASK (res), error);
304 }
305
306 /**
307  * g_async_initable_new_async:
308  * @object_type: a #GType supporting #GAsyncInitable.
309  * @io_priority: the <link linkend="io-priority">I/O priority</link>
310  *     of the operation.
311  * @cancellable: optional #GCancellable object, %NULL to ignore.
312  * @callback: a #GAsyncReadyCallback to call when the initialization is
313  *     finished
314  * @user_data: the data to pass to callback function
315  * @first_property_name: (allow-none): the name of the first property, or %NULL if no
316  *     properties
317  * @...:  the value of the first property, followed by other property
318  *    value pairs, and ended by %NULL.
319  *
320  * Helper function for constructing #GAsyncInitable object. This is
321  * similar to g_object_new() but also initializes the object asynchronously.
322  *
323  * When the initialization is finished, @callback will be called. You can
324  * then call g_async_initable_new_finish() to get the new object and check
325  * for any errors.
326  *
327  * Since: 2.22
328  */
329 void
330 g_async_initable_new_async (GType                object_type,
331                             int                  io_priority,
332                             GCancellable        *cancellable,
333                             GAsyncReadyCallback  callback,
334                             gpointer             user_data,
335                             const gchar         *first_property_name,
336                             ...)
337 {
338   va_list var_args;
339
340   va_start (var_args, first_property_name);
341   g_async_initable_new_valist_async (object_type,
342                                      first_property_name, var_args,
343                                      io_priority, cancellable,
344                                      callback, user_data);
345   va_end (var_args);
346 }
347
348 /**
349  * g_async_initable_newv_async:
350  * @object_type: a #GType supporting #GAsyncInitable.
351  * @n_parameters: the number of parameters in @parameters
352  * @parameters: the parameters to use to construct the object
353  * @io_priority: the <link linkend="io-priority">I/O priority</link>
354  *     of the operation.
355  * @cancellable: optional #GCancellable object, %NULL to ignore.
356  * @callback: a #GAsyncReadyCallback to call when the initialization is
357  *     finished
358  * @user_data: the data to pass to callback function
359  *
360  * Helper function for constructing #GAsyncInitable object. This is
361  * similar to g_object_newv() but also initializes the object asynchronously.
362  *
363  * When the initialization is finished, @callback will be called. You can
364  * then call g_async_initable_new_finish() to get the new object and check
365  * for any errors.
366  *
367  * Since: 2.22
368  */
369 void
370 g_async_initable_newv_async (GType                object_type,
371                              guint                n_parameters,
372                              GParameter          *parameters,
373                              int                  io_priority,
374                              GCancellable        *cancellable,
375                              GAsyncReadyCallback  callback,
376                              gpointer             user_data)
377 {
378   GObject *obj;
379
380   g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
381
382   obj = g_object_newv (object_type, n_parameters, parameters);
383
384   g_async_initable_init_async (G_ASYNC_INITABLE (obj),
385                                io_priority, cancellable,
386                                callback, user_data);
387 }
388
389 /**
390  * g_async_initable_new_valist_async:
391  * @object_type: a #GType supporting #GAsyncInitable.
392  * @first_property_name: the name of the first property, followed by
393  * the value, and other property value pairs, and ended by %NULL.
394  * @var_args: The var args list generated from @first_property_name.
395  * @io_priority: the <link linkend="io-priority">I/O priority</link>
396  *     of the operation.
397  * @cancellable: optional #GCancellable object, %NULL to ignore.
398  * @callback: a #GAsyncReadyCallback to call when the initialization is
399  *     finished
400  * @user_data: the data to pass to callback function
401  *
402  * Helper function for constructing #GAsyncInitable object. This is
403  * similar to g_object_new_valist() but also initializes the object
404  * asynchronously.
405  *
406  * When the initialization is finished, @callback will be called. You can
407  * then call g_async_initable_new_finish() to get the new object and check
408  * for any errors.
409  *
410  * Since: 2.22
411  */
412 void
413 g_async_initable_new_valist_async (GType                object_type,
414                                    const gchar         *first_property_name,
415                                    va_list              var_args,
416                                    int                  io_priority,
417                                    GCancellable        *cancellable,
418                                    GAsyncReadyCallback  callback,
419                                    gpointer             user_data)
420 {
421   GObject *obj;
422
423   g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
424
425   obj = g_object_new_valist (object_type,
426                              first_property_name,
427                              var_args);
428
429   g_async_initable_init_async (G_ASYNC_INITABLE (obj),
430                                io_priority, cancellable,
431                                callback, user_data);
432   g_object_unref (obj); /* Passed ownership to async call */
433 }
434
435 /**
436  * g_async_initable_new_finish:
437  * @initable: the #GAsyncInitable from the callback
438  * @res: the #GAsyncResult from the callback
439  * @error: return location for errors, or %NULL to ignore
440  *
441  * Finishes the async construction for the various g_async_initable_new
442  * calls, returning the created object or %NULL on error.
443  *
444  * Returns: (type GObject.Object) (transfer full): a newly created #GObject,
445  *      or %NULL on error. Free with g_object_unref().
446  *
447  * Since: 2.22
448  */
449 GObject *
450 g_async_initable_new_finish (GAsyncInitable  *initable,
451                              GAsyncResult    *res,
452                              GError         **error)
453 {
454   if (g_async_initable_init_finish (initable, res, error))
455     return g_object_ref (initable);
456   else
457     return NULL;
458 }