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.
48 * For bindings in languages where the native constructor supports
49 * exceptions the binding could check for objects implemention %GInitable
50 * during normal construction and automatically initialize them, throwing
51 * an exception on failure.
54 typedef GInitableIface GInitableInterface;
55 G_DEFINE_INTERFACE (GInitable, g_initable, G_TYPE_OBJECT)
58 g_initable_default_init (GInitableInterface *iface)
64 * @initable: a #GInitable.
65 * @cancellable: optional #GCancellable object, %NULL to ignore.
66 * @error: a #GError location to store the error occuring, or %NULL to
69 * Initializes the object implementing the interface. This must be
70 * done before any real use of the object after initial construction.
72 * Implementations may also support cancellation. If @cancellable is not %NULL,
73 * then initialization can be cancelled by triggering the cancellable object
74 * from another thread. If the operation was cancelled, the error
75 * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL and
76 * the object doesn't support cancellable initialization the error
77 * %G_IO_ERROR_NOT_SUPPORTED will be returned.
79 * If this function is not called, or returns with an error then all
80 * operations on the object should fail, generally returning the
81 * error %G_IO_ERROR_NOT_INITIALIZED.
83 * Implementations of this method must be idempotent, i.e. multiple calls
84 * to this function with the same argument should return the same results.
85 * Only the first call initializes the object, further calls return the result
86 * of the first call. This is so that its safe to implement the singleton
87 * pattern in the GObject constructor function.
89 * Returns: %TRUE if successful. If an error has occurred, this function will
90 * return %FALSE and set @error appropriately if present.
95 g_initable_init (GInitable *initable,
96 GCancellable *cancellable,
99 GInitableIface *iface;
101 g_return_val_if_fail (G_IS_INITABLE (initable), FALSE);
103 iface = G_INITABLE_GET_IFACE (initable);
105 return (* iface->init) (initable, cancellable, error);
110 * @object_type: a #GType supporting #GInitable.
111 * @cancellable: optional #GCancellable object, %NULL to ignore.
112 * @error: a #GError location to store the error occuring, or %NULL to
114 * @first_property_name: the name of the first property, or %NULL if no
116 * @...: the value if the first property, followed by and other property
117 * value pairs, and ended by %NULL.
119 * Helper function for constructing #GInitiable object. This is
120 * similar to g_object_new() but also initializes the object
121 * and returns %NULL, setting an error on failure.
123 * Return value: a newly allocated #GObject, or %NULL on error
128 g_initable_new (GType object_type,
129 GCancellable *cancellable,
131 const gchar *first_property_name,
137 va_start (var_args, first_property_name);
138 object = g_initable_new_valist (object_type,
139 first_property_name, var_args,
148 * @object_type: a #GType supporting #GInitable.
149 * @n_parameters: the number of parameters in @parameters
150 * @parameters: the parameters to use to construct the object
151 * @cancellable: optional #GCancellable object, %NULL to ignore.
152 * @error: a #GError location to store the error occuring, or %NULL to
155 * Helper function for constructing #GInitiable object. This is
156 * similar to g_object_newv() but also initializes the object
157 * and returns %NULL, setting an error on failure.
159 * Return value: a newly allocated #GObject, or %NULL on error
164 g_initable_newv (GType object_type,
166 GParameter *parameters,
167 GCancellable *cancellable,
172 g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
174 obj = g_object_newv (object_type, n_parameters, parameters);
176 if (!g_initable_init (G_INITABLE (obj), cancellable, error))
178 g_object_unref (obj);
182 return (gpointer)obj;
186 * g_initable_new_valist:
187 * @object_type: a #GType supporting #GInitable.
188 * @first_property_name: the name of the first property, followed by
189 * the value, and other property value pairs, and ended by %NULL.
190 * @var_args: The var args list generated from @first_property_name.
191 * @cancellable: optional #GCancellable object, %NULL to ignore.
192 * @error: a #GError location to store the error occuring, or %NULL to
195 * Helper function for constructing #GInitiable object. This is
196 * similar to g_object_new_valist() but also initializes the object
197 * and returns %NULL, setting an error on failure.
199 * Return value: a newly allocated #GObject, or %NULL on error
204 g_initable_new_valist (GType object_type,
205 const gchar *first_property_name,
207 GCancellable *cancellable,
212 g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
214 obj = g_object_new_valist (object_type,
218 if (!g_initable_init (G_INITABLE (obj), cancellable, error))
220 g_object_unref (obj);