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
114 * has occurred, this function will return %FALSE and set @error
115 * appropriately if present.
120 g_initable_init (GInitable *initable,
121 GCancellable *cancellable,
124 GInitableIface *iface;
126 g_return_val_if_fail (G_IS_INITABLE (initable), FALSE);
128 iface = G_INITABLE_GET_IFACE (initable);
130 return (* iface->init) (initable, cancellable, error);
135 * @object_type: a #GType supporting #GInitable.
136 * @cancellable: optional #GCancellable object, %NULL to ignore.
137 * @error: a #GError location to store the error occuring, or %NULL to
139 * @first_property_name: the name of the first property, followed by
140 * the value, and other property value pairs, and ended by %NULL.
142 * Helper function for constructing #GInitiable object. This is
143 * similar to g_object_new() but also initializes the object
144 * and returns %NULL, setting an error on failure.
146 * Return value: a newly allocated #GObject, or %NULL on error
151 g_initable_new (GType object_type,
152 GCancellable *cancellable,
154 const gchar *first_property_name,
160 va_start (var_args, first_property_name);
161 object = g_initable_new_valist (object_type,
162 first_property_name, var_args,
171 * @object_type: a #GType supporting #GInitable.
172 * @n_parameters: the number of parameters in @parameters
173 * @parameters: the parameters to use to construct the object
174 * @cancellable: optional #GCancellable object, %NULL to ignore.
175 * @error: a #GError location to store the error occuring, or %NULL to
178 * Helper function for constructing #GInitiable object. This is
179 * similar to g_object_newv() but also initializes the object
180 * and returns %NULL, setting an error on failure.
182 * Return value: a newly allocated #GObject, or %NULL on error
187 g_initable_newv (GType object_type,
189 GParameter *parameters,
190 GCancellable *cancellable,
195 g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
197 obj = g_object_newv (object_type, n_parameters, parameters);
199 if (!g_initable_init (G_INITABLE (obj), cancellable, error))
201 g_object_unref (obj);
205 return (gpointer)obj;
209 * g_initable_new_valist:
210 * @object_type: a #GType supporting #GInitable.
211 * @first_property_name: the name of the first property, followed by
212 * the value, and other property value pairs, and ended by %NULL.
213 * @var_args: The var args list generated from @first_property_name.
214 * @cancellable: optional #GCancellable object, %NULL to ignore.
215 * @error: a #GError location to store the error occuring, or %NULL to
218 * Helper function for constructing #GInitiable object. This is
219 * similar to g_object_new_valist() but also initializes the object
220 * and returns %NULL, setting an error on failure.
222 * Return value: a newly allocated #GObject, or %NULL on error
227 g_initable_new_valist (GType object_type,
228 const gchar *first_property_name,
230 GCancellable *cancellable,
235 g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
237 obj = g_object_new_valist (object_type,
241 if (!g_initable_init (G_INITABLE (obj), cancellable, error))
243 g_object_unref (obj);
250 #define __G_INITABLE_C__
251 #include "gioaliasdef.c"