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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "ginitable.h"
30 * @short_description: Failable object initialization interface
32 * @see_also: #GAsyncInitable
34 * #GInitable is implemented by objects that can fail during
35 * initialization. If an object implements this interface the
36 * g_initable_init() function must be called as the first thing
37 * after construction. If g_initable_init() is not called, or if
38 * it returns an error, all further operations on the object
39 * should fail, generally with a %G_IO_ERROR_NOT_INITIALIZED error.
41 * Users of objects implementing this are not intended to use
42 * the interface method directly, instead it will be used automatically
43 * in various ways. For C applications you generally just call
44 * g_initable_new() directly, or indirectly via a foo_thing_new() wrapper.
45 * This will call g_initable_init() under the cover, returning %NULL and
46 * setting a #GError on failure (at which point the instance is
49 * For bindings in languages where the native constructor supports
50 * exceptions the binding could check for objects implemention %GInitable
51 * during normal construction and automatically initialize them, throwing
52 * an exception on failure.
55 typedef GInitableIface GInitableInterface;
56 G_DEFINE_INTERFACE (GInitable, g_initable, G_TYPE_OBJECT)
59 g_initable_default_init (GInitableInterface *iface)
65 * @initable: a #GInitable.
66 * @cancellable: optional #GCancellable object, %NULL to ignore.
67 * @error: a #GError location to store the error occuring, or %NULL to
70 * Initializes the object implementing the interface. This must be
71 * done before any real use of the object after initial construction.
73 * Implementations may also support cancellation. If @cancellable is not %NULL,
74 * then initialization can be cancelled by triggering the cancellable object
75 * from another thread. If the operation was cancelled, the error
76 * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL and
77 * the object doesn't support cancellable initialization the error
78 * %G_IO_ERROR_NOT_SUPPORTED will be returned.
80 * If this function is not called, or returns with an error then all
81 * operations on the object should fail, generally returning the
82 * error %G_IO_ERROR_NOT_INITIALIZED.
84 * Implementations of this method must be idempotent, i.e. multiple calls
85 * to this function with the same argument should return the same results.
86 * Only the first call initializes the object, further calls return the result
87 * of the first call. This is so that its safe to implement the singleton
88 * pattern in the GObject constructor function.
90 * Returns: %TRUE if successful. If an error has occurred, this function will
91 * return %FALSE and set @error appropriately if present.
96 g_initable_init (GInitable *initable,
97 GCancellable *cancellable,
100 GInitableIface *iface;
102 g_return_val_if_fail (G_IS_INITABLE (initable), FALSE);
104 iface = G_INITABLE_GET_IFACE (initable);
106 return (* iface->init) (initable, cancellable, error);
111 * @object_type: a #GType supporting #GInitable.
112 * @cancellable: optional #GCancellable object, %NULL to ignore.
113 * @error: a #GError location to store the error occuring, or %NULL to
115 * @first_property_name: the name of the first property, or %NULL if no
117 * @...: the value if the first property, followed by and other property
118 * value pairs, and ended by %NULL.
120 * Helper function for constructing #GInitiable object. This is
121 * similar to g_object_new() but also initializes the object
122 * and returns %NULL, setting an error on failure.
124 * Return value: (transfer full): a newly allocated #GObject, or %NULL on error
129 g_initable_new (GType object_type,
130 GCancellable *cancellable,
132 const gchar *first_property_name,
138 va_start (var_args, first_property_name);
139 object = g_initable_new_valist (object_type,
140 first_property_name, var_args,
149 * @object_type: a #GType supporting #GInitable.
150 * @n_parameters: the number of parameters in @parameters
151 * @parameters: the parameters to use to construct the object
152 * @cancellable: optional #GCancellable object, %NULL to ignore.
153 * @error: a #GError location to store the error occuring, or %NULL to
156 * Helper function for constructing #GInitiable object. This is
157 * similar to g_object_newv() but also initializes the object
158 * and returns %NULL, setting an error on failure.
160 * Return value: (transfer full): a newly allocated #GObject, or %NULL on error
165 g_initable_newv (GType object_type,
167 GParameter *parameters,
168 GCancellable *cancellable,
173 g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
175 obj = g_object_newv (object_type, n_parameters, parameters);
177 if (!g_initable_init (G_INITABLE (obj), cancellable, error))
179 g_object_unref (obj);
183 return (gpointer)obj;
187 * g_initable_new_valist:
188 * @object_type: a #GType supporting #GInitable.
189 * @first_property_name: the name of the first property, followed by
190 * the value, and other property value pairs, and ended by %NULL.
191 * @var_args: The var args list generated from @first_property_name.
192 * @cancellable: optional #GCancellable object, %NULL to ignore.
193 * @error: a #GError location to store the error occuring, or %NULL to
196 * Helper function for constructing #GInitiable object. This is
197 * similar to g_object_new_valist() but also initializes the object
198 * and returns %NULL, setting an error on failure.
200 * Return value: (transfer full): a newly allocated #GObject, or %NULL on error
205 g_initable_new_valist (GType object_type,
206 const gchar *first_property_name,
208 GCancellable *cancellable,
213 g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
215 obj = g_object_new_valist (object_type,
219 if (!g_initable_init (G_INITABLE (obj), cancellable, error))
221 g_object_unref (obj);