Update the docs for the new network APIs
[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 "glibintl.h"
28
29 #include "gioalias.h"
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  * or both.
43  *
44  * Users of objects implementing this are not intended to use
45  * the interface method directly, instead it will be used automatically
46  * in various ways. For C applications you generally just call
47  * g_async_initable_new() directly, or indirectly via a foo_thing_new_async() wrapper.
48  * This will call g_async_initable_init() under the cover, calling back with %NULL and
49  * a set %GError on failure.
50  **/
51
52 static void     g_async_initable_base_init        (gpointer              g_iface);
53 static void     g_async_initable_real_init_async  (GAsyncInitable       *initable,
54                                                    int                   io_priority,
55                                                    GCancellable         *cancellable,
56                                                    GAsyncReadyCallback   callback,
57                                                    gpointer              user_data);
58 static gboolean g_async_initable_real_init_finish (GAsyncInitable       *initable,
59                                                    GAsyncResult         *res,
60                                                    GError              **error);
61
62 GType
63 g_async_initable_get_type (void)
64 {
65   static volatile gsize g_define_type_id__volatile = 0;
66
67   if (g_once_init_enter (&g_define_type_id__volatile))
68     {
69       const GTypeInfo initable_info =
70       {
71         sizeof (GAsyncInitableIface), /* class_size */
72         g_async_initable_base_init,   /* base_init */
73         NULL,           /* base_finalize */
74         NULL,
75         NULL,           /* class_finalize */
76         NULL,           /* class_data */
77         0,
78         0,              /* n_preallocs */
79         NULL
80       };
81       GType g_define_type_id =
82         g_type_register_static (G_TYPE_INTERFACE, I_("GAsyncInitable"),
83                                 &initable_info, 0);
84
85       g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
86
87       g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
88     }
89
90   return g_define_type_id__volatile;
91 }
92
93 static void
94 g_async_initable_base_init (gpointer g_iface)
95 {
96   GAsyncInitableIface *iface = g_iface;
97
98   iface->init_async = g_async_initable_real_init_async;
99   iface->init_finish = g_async_initable_real_init_finish;
100 }
101
102 /**
103  * g_async_initable_init_async:
104  * @initable: a #GAsyncInitable.
105  * @io_priority: the <link linkend="io-priority">I/O priority</link>
106  *     of the operation.
107  * @cancellable: optional #GCancellable object, %NULL to ignore.
108  * @callback: a #GAsyncReadyCallback to call when the request is satisfied
109  * @user_data: the data to pass to callback function
110  *
111  * Starts asynchronous initialization of the object implementing the interface.
112  * This must be done before any real use of the object after initial construction.
113  * If the object also implements #GInitable you can optionally call g_initable_init()
114  * instead.
115  *
116  * When the initialization is finished, @callback will be called. You can then call
117  * g_async_initable_init_finish() to get the result of the initialization.
118  *
119  * Implementations may also support cancellation. If @cancellable is not %NULL,
120  * then initialization can be cancelled by triggering the cancellable object
121  * from another thread. If the operation was cancelled, the error
122  * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL and
123  * the object doesn't support cancellable initialization the error
124  * %G_IO_ERROR_NOT_SUPPORTED will be returned.
125  *
126  * If this function is not called, or returns with an error then all
127  * operations on the object should fail, generally returning the
128  * error %G_IO_ERROR_NOT_INITIALIZED.
129  *
130  * Implementations of this method must be idempotent, i.e. multiple calls
131  * to this function with the same argument should return the same results.
132  * Only the first call initializes the object, further calls return the result
133  * of the first call. This is so that its safe to implement the singleton
134  * pattern in the GObject constructor function.
135  *
136  * For classes that also support the #GInitable interface the default
137  * implementation of this method will run the g_initable_init() function
138  * in a thread, so if you want to support asynchronous initialization via
139  * threads, just implement the #GAsyncInitable interface without overriding
140  * any interface methods.
141  *
142  * Since: 2.22
143  **/
144 void
145 g_async_initable_init_async (GAsyncInitable     *initable,
146                              int                 io_priority,
147                              GCancellable       *cancellable,
148                              GAsyncReadyCallback callback,
149                              gpointer            user_data)
150 {
151   GAsyncInitableIface *iface;
152
153   g_return_if_fail (G_IS_ASYNC_INITABLE (initable));
154
155   iface = G_ASYNC_INITABLE_GET_IFACE (initable);
156
157   return (* iface->init_async) (initable, io_priority, cancellable, callback, user_data);
158 }
159
160 /**
161  * g_async_initable_init_finish:
162  * @initable: a #GAsyncInitable.
163  * @res: a #GAsyncResult.
164  * @error: a #GError location to store the error occuring, or %NULL to
165  * ignore.
166  *
167  * Finishes asynchronous initialization and returns the result.
168  * See g_async_initable_init_async().
169  *
170  * Returns: %TRUE if successful. If an error
171  *     has occurred, this function will return %FALSE and set @error
172  *     appropriately if present.
173  *
174  * Since: 2.22
175  **/
176 gboolean
177 g_async_initable_init_finish (GAsyncInitable       *initable,
178                               GAsyncResult         *res,
179                               GError              **error)
180 {
181   GAsyncInitableIface *iface;
182
183   g_return_val_if_fail (G_IS_ASYNC_INITABLE (initable), FALSE);
184   g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
185
186   if (G_IS_SIMPLE_ASYNC_RESULT (res))
187     {
188       GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
189       if (g_simple_async_result_propagate_error (simple, error))
190         return FALSE;
191     }
192
193   iface = G_ASYNC_INITABLE_GET_IFACE (initable);
194
195   return (* iface->init_finish) (initable, res, error);
196 }
197
198 static void
199 async_init_thread (GSimpleAsyncResult *res,
200                    GObject            *object,
201                    GCancellable       *cancellable)
202 {
203   GError *error = NULL;
204
205   if (!g_initable_init (G_INITABLE (res), cancellable, &error))
206     {
207       g_simple_async_result_set_from_error (res, error);
208       g_error_free (error);
209     }
210 }
211
212 static void
213 g_async_initable_real_init_async (GAsyncInitable     *initable,
214                                   int                 io_priority,
215                                   GCancellable       *cancellable,
216                                   GAsyncReadyCallback callback,
217                                   gpointer            user_data)
218 {
219   GSimpleAsyncResult *res;
220
221   g_return_if_fail (G_IS_INITABLE (initable));
222
223   res = g_simple_async_result_new (G_OBJECT (initable), callback, user_data,
224                                    g_async_initable_real_init_async);
225   g_simple_async_result_run_in_thread (res, async_init_thread,
226                                        io_priority, cancellable);
227   g_object_unref (res);
228 }
229
230 static gboolean
231 g_async_initable_real_init_finish (GAsyncInitable *initable,
232                                    GAsyncResult *res,
233                                    GError **error)
234 {
235   return TRUE; /* Errors handled by base impl */
236 }
237
238 /**
239  * g_async_initable_new_async:
240  * @object_type: a #GType supporting #GAsyncInitable.
241  * @io_priority: the <link linkend="io-priority">I/O priority</link>
242  *     of the operation.
243  * @cancellable: optional #GCancellable object, %NULL to ignore.
244  * @callback: a #GAsyncReadyCallback to call when the initialization is finished
245  * @user_data: the data to pass to callback function
246  * @first_property_name: the name of the first property, or %NULL if no properties
247  * @...:  the value if the first property, followed by and other property
248  *    value pairs, and ended by %NULL.
249  *
250  * Helper function for constructing #GAsyncInitiable object. This is
251  * similar to g_object_new() but also initializes the object asyncronously.
252  *
253  * When the initialization is finished, @callback will be called. You can then call
254  * g_async_initable_new_finish() to get new object and check for any errors.
255  *
256  * Since: 2.22
257  **/
258 void
259 g_async_initable_new_async (GType object_type,
260                             int io_priority,
261                             GCancellable  *cancellable,
262                             GAsyncReadyCallback callback,
263                             gpointer user_data,
264                             const gchar *first_property_name,
265                             ...)
266 {
267   va_list var_args;
268
269   va_start (var_args, first_property_name);
270   g_async_initable_new_valist_async (object_type,
271                                      first_property_name, var_args,
272                                      io_priority, cancellable,
273                                      callback, user_data);
274   va_end (var_args);
275 }
276
277 /**
278  * g_async_initable_newv_async:
279  * @object_type: a #GType supporting #GAsyncInitable.
280  * @n_parameters: the number of parameters in @parameters
281  * @parameters: the parameters to use to construct the object
282  * @io_priority: the <link linkend="io-priority">I/O priority</link>
283  *     of the operation.
284  * @cancellable: optional #GCancellable object, %NULL to ignore.
285  * @callback: a #GAsyncReadyCallback to call when the initialization is finished
286  * @user_data: the data to pass to callback function
287  *
288  * Helper function for constructing #GAsyncInitiable object. This is
289  * similar to g_object_newv() but also initializes the object asyncronously.
290  *
291  * When the initialization is finished, @callback will be called. You can then call
292  * g_async_initable_new_finish() to get new object and check for any errors.
293  *
294  * Since: 2.22
295  **/
296 void
297 g_async_initable_newv_async (GType object_type,
298                              guint n_parameters,
299                              GParameter *parameters,
300                              int io_priority,
301                              GCancellable *cancellable,
302                              GAsyncReadyCallback callback,
303                              gpointer user_data)
304 {
305   GObject *obj;
306
307   g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
308
309   obj = g_object_newv (object_type, n_parameters, parameters);
310
311   g_async_initable_init_async (G_ASYNC_INITABLE (obj),
312                                io_priority, cancellable,
313                                callback, user_data);
314 }
315
316 /**
317  * g_async_initable_new_valist_async:
318  * @object_type: a #GType supporting #GAsyncInitable.
319  * @first_property_name: the name of the first property, followed by
320  * the value, and other property value pairs, and ended by %NULL.
321  * @var_args: The var args list generated from @first_property_name.
322  * @io_priority: the <link linkend="io-priority">I/O priority</link>
323  *     of the operation.
324  * @cancellable: optional #GCancellable object, %NULL to ignore.
325  * @callback: a #GAsyncReadyCallback to call when the initialization is finished
326  * @user_data: the data to pass to callback function
327  *
328  * Helper function for constructing #GAsyncInitiable object. This is
329  * similar to g_object_new_valist() but also initializes the object asyncronously.
330  *
331  * When the initialization is finished, @callback will be called. You can then call
332  * g_async_initable_new_finish() to get new object and check for any errors.
333  *
334  * Since: 2.22
335  **/
336 void
337 g_async_initable_new_valist_async (GType object_type,
338                                    const gchar *first_property_name,
339                                    va_list var_args,
340                                    int io_priority,
341                                    GCancellable *cancellable,
342                                    GAsyncReadyCallback callback,
343                                    gpointer user_data)
344 {
345   GObject *obj;
346
347   g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
348
349   obj = g_object_new_valist (object_type,
350                              first_property_name,
351                              var_args);
352
353   g_async_initable_init_async (G_ASYNC_INITABLE (obj),
354                                io_priority, cancellable,
355                                callback, user_data);
356   g_object_unref (obj); /* Passed ownership to async call */
357 }
358 /**
359  * g_async_initable_new_finish:
360  * @initable: the #GAsyncInitable from the callback
361  * @res: the #GAsyncResult.from the callback
362  * @error: a #GError location to store the error occuring, or %NULL to
363  * ignore.
364  *
365  * Finishes the async construction for the various g_async_initable_new calls,
366  * returning the created object or %NULL on error.
367  *
368  * Returns: a newly created #GObject, or %NULL on error. Free with g_object_unref().
369  *
370  * Since: 2.22
371  **/
372 GObject *
373 g_async_initable_new_finish (GAsyncInitable       *initable,
374                              GAsyncResult         *res,
375                              GError              **error)
376 {
377   if (g_async_initable_init_finish (initable, res, error))
378     return g_object_ref (initable);
379   else
380     return NULL;
381 }
382
383 #define __G_ASYNC_INITABLE_C__
384 #include "gioaliasdef.c"