gosxappinfo: fix typo in url_escape_hostname
[glib.git] / gio / gasyncinitable.c
blob29943291889fb613ae772ebf245264f3c5696094
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2009 Red Hat, Inc.
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.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
21 #include "config.h"
22 #include "gasyncinitable.h"
23 #include "gasyncresult.h"
24 #include "gsimpleasyncresult.h"
25 #include "gtask.h"
26 #include "glibintl.h"
29 /**
30 * SECTION:gasyncinitable
31 * @short_description: Asynchronously failable object initialization interface
32 * @include: gio/gio.h
33 * @see_also: #GInitable
35 * This is the asynchronous version of #GInitable; it behaves the same
36 * in all ways except that initialization is asynchronous. For more details
37 * see the descriptions on #GInitable.
39 * A class may implement both the #GInitable and #GAsyncInitable interfaces.
41 * Users of objects implementing this are not intended to use the interface
42 * method directly; instead it will be used automatically in various ways.
43 * For C applications you generally just call g_async_initable_new_async()
44 * directly, or indirectly via a foo_thing_new_async() wrapper. This will call
45 * g_async_initable_init_async() under the cover, calling back with %NULL and
46 * a set %GError on failure.
48 * A typical implementation might look something like this:
50 * |[<!-- language="C" -->
51 * enum {
52 * NOT_INITIALIZED,
53 * INITIALIZING,
54 * INITIALIZED
55 * };
57 * static void
58 * _foo_ready_cb (Foo *self)
59 * {
60 * GList *l;
62 * self->priv->state = INITIALIZED;
64 * for (l = self->priv->init_results; l != NULL; l = l->next)
65 * {
66 * GTask *task = l->data;
68 * if (self->priv->success)
69 * g_task_return_boolean (task, TRUE);
70 * else
71 * g_task_return_new_error (task, ...);
72 * g_object_unref (task);
73 * }
75 * g_list_free (self->priv->init_results);
76 * self->priv->init_results = NULL;
77 * }
79 * static void
80 * foo_init_async (GAsyncInitable *initable,
81 * int io_priority,
82 * GCancellable *cancellable,
83 * GAsyncReadyCallback callback,
84 * gpointer user_data)
85 * {
86 * Foo *self = FOO (initable);
87 * GTask *task;
89 * task = g_task_new (initable, cancellable, callback, user_data);
91 * switch (self->priv->state)
92 * {
93 * case NOT_INITIALIZED:
94 * _foo_get_ready (self);
95 * self->priv->init_results = g_list_append (self->priv->init_results,
96 * task);
97 * self->priv->state = INITIALIZING;
98 * break;
99 * case INITIALIZING:
100 * self->priv->init_results = g_list_append (self->priv->init_results,
101 * task);
102 * break;
103 * case INITIALIZED:
104 * if (!self->priv->success)
105 * g_task_return_new_error (task, ...);
106 * else
107 * g_task_return_boolean (task, TRUE);
108 * g_object_unref (task);
109 * break;
113 * static gboolean
114 * foo_init_finish (GAsyncInitable *initable,
115 * GAsyncResult *result,
116 * GError **error)
118 * g_return_val_if_fail (g_task_is_valid (result, initable), FALSE);
120 * return g_task_propagate_boolean (G_TASK (result), error);
123 * static void
124 * foo_async_initable_iface_init (gpointer g_iface,
125 * gpointer data)
127 * GAsyncInitableIface *iface = g_iface;
129 * iface->init_async = foo_init_async;
130 * iface->init_finish = foo_init_finish;
132 * ]|
135 static void g_async_initable_real_init_async (GAsyncInitable *initable,
136 int io_priority,
137 GCancellable *cancellable,
138 GAsyncReadyCallback callback,
139 gpointer user_data);
140 static gboolean g_async_initable_real_init_finish (GAsyncInitable *initable,
141 GAsyncResult *res,
142 GError **error);
145 typedef GAsyncInitableIface GAsyncInitableInterface;
146 G_DEFINE_INTERFACE (GAsyncInitable, g_async_initable, G_TYPE_OBJECT)
149 static void
150 g_async_initable_default_init (GAsyncInitableInterface *iface)
152 iface->init_async = g_async_initable_real_init_async;
153 iface->init_finish = g_async_initable_real_init_finish;
157 * g_async_initable_init_async:
158 * @initable: a #GAsyncInitable.
159 * @io_priority: the [I/O priority][io-priority] of the operation
160 * @cancellable: optional #GCancellable object, %NULL to ignore.
161 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
162 * @user_data: the data to pass to callback function
164 * Starts asynchronous initialization of the object implementing the
165 * interface. This must be done before any real use of the object after
166 * initial construction. If the object also implements #GInitable you can
167 * optionally call g_initable_init() instead.
169 * This method is intended for language bindings. If writing in C,
170 * g_async_initable_new_async() should typically be used instead.
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.
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.
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.
189 * Callers should not assume that a class which implements #GAsyncInitable can
190 * be initialized multiple times; for more information, see g_initable_init().
191 * If a class explicitly supports being initialized multiple times,
192 * implementation requires yielding all subsequent calls to init_async() on the
193 * results of the first call.
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.
201 * Since: 2.22
203 void
204 g_async_initable_init_async (GAsyncInitable *initable,
205 int io_priority,
206 GCancellable *cancellable,
207 GAsyncReadyCallback callback,
208 gpointer user_data)
210 GAsyncInitableIface *iface;
212 g_return_if_fail (G_IS_ASYNC_INITABLE (initable));
214 iface = G_ASYNC_INITABLE_GET_IFACE (initable);
216 (* iface->init_async) (initable, io_priority, cancellable, callback, user_data);
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.
226 * Finishes asynchronous initialization and returns the result.
227 * See g_async_initable_init_async().
229 * Returns: %TRUE if successful. If an error has occurred, this function
230 * will return %FALSE and set @error appropriately if present.
232 * Since: 2.22
234 gboolean
235 g_async_initable_init_finish (GAsyncInitable *initable,
236 GAsyncResult *res,
237 GError **error)
239 GAsyncInitableIface *iface;
241 g_return_val_if_fail (G_IS_ASYNC_INITABLE (initable), FALSE);
242 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
244 if (g_async_result_legacy_propagate_error (res, error))
245 return FALSE;
247 iface = G_ASYNC_INITABLE_GET_IFACE (initable);
249 return (* iface->init_finish) (initable, res, error);
252 static void
253 async_init_thread (GTask *task,
254 gpointer source_object,
255 gpointer task_data,
256 GCancellable *cancellable)
258 GError *error = NULL;
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);
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)
273 GTask *task;
275 g_return_if_fail (G_IS_INITABLE (initable));
277 task = g_task_new (initable, cancellable, callback, user_data);
278 g_task_set_source_tag (task, g_async_initable_real_init_async);
279 g_task_set_priority (task, io_priority);
280 g_task_run_in_thread (task, async_init_thread);
281 g_object_unref (task);
284 static gboolean
285 g_async_initable_real_init_finish (GAsyncInitable *initable,
286 GAsyncResult *res,
287 GError **error)
289 /* For backward compatibility we have to process GSimpleAsyncResults
290 * even though g_async_initable_real_init_async doesn't generate
291 * them any more.
293 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
294 if (G_IS_SIMPLE_ASYNC_RESULT (res))
296 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
297 if (g_simple_async_result_propagate_error (simple, error))
298 return FALSE;
299 else
300 return TRUE;
302 G_GNUC_END_IGNORE_DEPRECATIONS
304 g_return_val_if_fail (g_task_is_valid (res, initable), FALSE);
306 return g_task_propagate_boolean (G_TASK (res), error);
310 * g_async_initable_new_async:
311 * @object_type: a #GType supporting #GAsyncInitable.
312 * @io_priority: the [I/O priority][io-priority] of the operation
313 * @cancellable: optional #GCancellable object, %NULL to ignore.
314 * @callback: a #GAsyncReadyCallback to call when the initialization is
315 * finished
316 * @user_data: the data to pass to callback function
317 * @first_property_name: (nullable): the name of the first property, or %NULL if no
318 * properties
319 * @...: the value of the first property, followed by other property
320 * value pairs, and ended by %NULL.
322 * Helper function for constructing #GAsyncInitable object. This is
323 * similar to g_object_new() but also initializes the object asynchronously.
325 * When the initialization is finished, @callback will be called. You can
326 * then call g_async_initable_new_finish() to get the new object and check
327 * for any errors.
329 * Since: 2.22
331 void
332 g_async_initable_new_async (GType object_type,
333 int io_priority,
334 GCancellable *cancellable,
335 GAsyncReadyCallback callback,
336 gpointer user_data,
337 const gchar *first_property_name,
338 ...)
340 va_list var_args;
342 va_start (var_args, first_property_name);
343 g_async_initable_new_valist_async (object_type,
344 first_property_name, var_args,
345 io_priority, cancellable,
346 callback, user_data);
347 va_end (var_args);
351 * g_async_initable_newv_async:
352 * @object_type: a #GType supporting #GAsyncInitable.
353 * @n_parameters: the number of parameters in @parameters
354 * @parameters: the parameters to use to construct the object
355 * @io_priority: the [I/O priority][io-priority] of the operation
356 * @cancellable: optional #GCancellable object, %NULL to ignore.
357 * @callback: a #GAsyncReadyCallback to call when the initialization is
358 * finished
359 * @user_data: the data to pass to callback function
361 * Helper function for constructing #GAsyncInitable object. This is
362 * similar to g_object_newv() but also initializes the object asynchronously.
364 * When the initialization is finished, @callback will be called. You can
365 * then call g_async_initable_new_finish() to get the new object and check
366 * for any errors.
368 * Since: 2.22
370 void
371 g_async_initable_newv_async (GType object_type,
372 guint n_parameters,
373 GParameter *parameters,
374 int io_priority,
375 GCancellable *cancellable,
376 GAsyncReadyCallback callback,
377 gpointer user_data)
379 GObject *obj;
381 g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
383 obj = g_object_newv (object_type, n_parameters, parameters);
385 g_async_initable_init_async (G_ASYNC_INITABLE (obj),
386 io_priority, cancellable,
387 callback, user_data);
388 g_object_unref (obj); /* Passed ownership to async call */
392 * g_async_initable_new_valist_async:
393 * @object_type: a #GType supporting #GAsyncInitable.
394 * @first_property_name: the name of the first property, followed by
395 * the value, and other property value pairs, and ended by %NULL.
396 * @var_args: The var args list generated from @first_property_name.
397 * @io_priority: the [I/O priority][io-priority] of the operation
398 * @cancellable: optional #GCancellable object, %NULL to ignore.
399 * @callback: a #GAsyncReadyCallback to call when the initialization is
400 * finished
401 * @user_data: the data to pass to callback function
403 * Helper function for constructing #GAsyncInitable object. This is
404 * similar to g_object_new_valist() but also initializes the object
405 * asynchronously.
407 * When the initialization is finished, @callback will be called. You can
408 * then call g_async_initable_new_finish() to get the new object and check
409 * for any errors.
411 * Since: 2.22
413 void
414 g_async_initable_new_valist_async (GType object_type,
415 const gchar *first_property_name,
416 va_list var_args,
417 int io_priority,
418 GCancellable *cancellable,
419 GAsyncReadyCallback callback,
420 gpointer user_data)
422 GObject *obj;
424 g_return_if_fail (G_TYPE_IS_ASYNC_INITABLE (object_type));
426 obj = g_object_new_valist (object_type,
427 first_property_name,
428 var_args);
430 g_async_initable_init_async (G_ASYNC_INITABLE (obj),
431 io_priority, cancellable,
432 callback, user_data);
433 g_object_unref (obj); /* Passed ownership to async call */
437 * g_async_initable_new_finish:
438 * @initable: the #GAsyncInitable from the callback
439 * @res: the #GAsyncResult from the callback
440 * @error: return location for errors, or %NULL to ignore
442 * Finishes the async construction for the various g_async_initable_new
443 * calls, returning the created object or %NULL on error.
445 * Returns: (type GObject.Object) (transfer full): a newly created #GObject,
446 * or %NULL on error. Free with g_object_unref().
448 * Since: 2.22
450 GObject *
451 g_async_initable_new_finish (GAsyncInitable *initable,
452 GAsyncResult *res,
453 GError **error)
455 if (g_async_initable_init_finish (initable, res, error))
456 return g_object_ref (initable);
457 else
458 return NULL;