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"
31 * @short_description: Failable object initialization interface
33 * @see_also: #GAsyncInitable
35 * #GInitable is implemented by objects that can fail during
36 * initialization. If an object implements this interface the
37 * g_initable_init() function must be called as the first thing
38 * after construction. If g_initable_init() is not called, or if
39 * it returns an error, all further operations on the object
40 * should fail, generally with a %G_IO_ERROR_NOT_INITIALIZED error.
42 * Users of objects implementing this are not intended to use
43 * the interface method directly, instead it will be used automatically
44 * in various ways. For C applications you generally just call
45 * g_initable_new() directly, or indirectly via a foo_thing_new() wrapper.
46 * This will call g_initable_init() under the cover, returning %NULL and
47 * setting a %GError on failure.
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.
56 g_initable_get_type (void)
58 static volatile gsize g_define_type_id__volatile = 0;
60 if (g_once_init_enter (&g_define_type_id__volatile))
62 const GTypeInfo initable_info =
64 sizeof (GInitableIface), /* class_size */
66 NULL, /* base_finalize */
68 NULL, /* class_finalize */
69 NULL, /* class_data */
74 GType g_define_type_id =
75 g_type_register_static (G_TYPE_INTERFACE, I_("GInitable"),
78 g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
80 g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
83 return g_define_type_id__volatile;
88 * @initable: a #GInitable.
89 * @cancellable: optional #GCancellable object, %NULL to ignore.
90 * @error: a #GError location to store the error occuring, or %NULL to
93 * Initializes the object implementing the interface. This must be
94 * done before any real use of the object after initial construction.
96 * Implementations may also support cancellation. If @cancellable is not %NULL,
97 * then initialization can be cancelled by triggering the cancellable object
98 * from another thread. If the operation was cancelled, the error
99 * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL and
100 * the object doesn't support cancellable initialization the error
101 * %G_IO_ERROR_NOT_SUPPORTED will be returned.
103 * If this function is not called, or returns with an error then all
104 * operations on the object should fail, generally returning the
105 * error %G_IO_ERROR_NOT_INITIALIZED.
107 * Implementations of this method must be idempotent, i.e. multiple calls
108 * to this function with the same argument should return the same results.
109 * Only the first call initializes the object, further calls return the result
110 * of the first call. This is so that its safe to implement the singleton
111 * pattern in the GObject constructor function.
113 * Returns: %TRUE if successful. If an error has occurred, this function will
114 * return %FALSE and set @error appropriately if present.
119 g_initable_init (GInitable *initable,
120 GCancellable *cancellable,
123 GInitableIface *iface;
125 g_return_val_if_fail (G_IS_INITABLE (initable), FALSE);
127 iface = G_INITABLE_GET_IFACE (initable);
129 return (* iface->init) (initable, cancellable, error);
134 * @object_type: a #GType supporting #GInitable.
135 * @cancellable: optional #GCancellable object, %NULL to ignore.
136 * @error: a #GError location to store the error occuring, or %NULL to
138 * @first_property_name: the name of the first property, or %NULL if no
140 * @...: the value if the first property, followed by and other property
141 * value pairs, and ended by %NULL.
143 * Helper function for constructing #GInitiable object. This is
144 * similar to g_object_new() but also initializes the object
145 * and returns %NULL, setting an error on failure.
147 * Return value: a newly allocated #GObject, or %NULL on error
152 g_initable_new (GType object_type,
153 GCancellable *cancellable,
155 const gchar *first_property_name,
161 va_start (var_args, first_property_name);
162 object = g_initable_new_valist (object_type,
163 first_property_name, var_args,
172 * @object_type: a #GType supporting #GInitable.
173 * @n_parameters: the number of parameters in @parameters
174 * @parameters: the parameters to use to construct the object
175 * @cancellable: optional #GCancellable object, %NULL to ignore.
176 * @error: a #GError location to store the error occuring, or %NULL to
179 * Helper function for constructing #GInitiable object. This is
180 * similar to g_object_newv() but also initializes the object
181 * and returns %NULL, setting an error on failure.
183 * Return value: a newly allocated #GObject, or %NULL on error
188 g_initable_newv (GType object_type,
190 GParameter *parameters,
191 GCancellable *cancellable,
196 g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
198 obj = g_object_newv (object_type, n_parameters, parameters);
200 if (!g_initable_init (G_INITABLE (obj), cancellable, error))
202 g_object_unref (obj);
206 return (gpointer)obj;
210 * g_initable_new_valist:
211 * @object_type: a #GType supporting #GInitable.
212 * @first_property_name: the name of the first property, followed by
213 * the value, and other property value pairs, and ended by %NULL.
214 * @var_args: The var args list generated from @first_property_name.
215 * @cancellable: optional #GCancellable object, %NULL to ignore.
216 * @error: a #GError location to store the error occuring, or %NULL to
219 * Helper function for constructing #GInitiable object. This is
220 * similar to g_object_new_valist() but also initializes the object
221 * and returns %NULL, setting an error on failure.
223 * Return value: a newly allocated #GObject, or %NULL on error
228 g_initable_new_valist (GType object_type,
229 const gchar *first_property_name,
231 GCancellable *cancellable,
236 g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
238 obj = g_object_new_valist (object_type,
242 if (!g_initable_init (G_INITABLE (obj), cancellable, error))
244 g_object_unref (obj);
251 #define __G_INITABLE_C__
252 #include "gioaliasdef.c"