b2df3a961bd944f216786ad04e0fa045497438b7
[platform/upstream/glib.git] / gio / ginitable.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2009 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24 #include "ginitable.h"
25 #include "glibintl.h"
26
27
28 /**
29  * SECTION:ginitable
30  * @short_description: Failable object initialization interface
31  * @include: gio/gio.h
32  * @see_also: #GAsyncInitable
33  *
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.
40  *
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
47  * unreferenced).
48  *
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.
53  */
54
55 typedef GInitableIface GInitableInterface;
56 G_DEFINE_INTERFACE (GInitable, g_initable, G_TYPE_OBJECT)
57
58 static void
59 g_initable_default_init (GInitableInterface *iface)
60 {
61 }
62
63 /**
64  * g_initable_init:
65  * @initable: a #GInitable.
66  * @cancellable: optional #GCancellable object, %NULL to ignore.
67  * @error: a #GError location to store the error occurring, or %NULL to
68  * ignore.
69  *
70  * Initializes the object implementing the interface. This must be
71  * done before any real use of the object after initial construction.
72  *
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.
79  *
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.
83  *
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.
89  *
90  * Returns: %TRUE if successful. If an error has occurred, this function will
91  *     return %FALSE and set @error appropriately if present.
92  *
93  * Since: 2.22
94  */
95 gboolean
96 g_initable_init (GInitable     *initable,
97                  GCancellable  *cancellable,
98                  GError       **error)
99 {
100   GInitableIface *iface;
101
102   g_return_val_if_fail (G_IS_INITABLE (initable), FALSE);
103
104   iface = G_INITABLE_GET_IFACE (initable);
105
106   return (* iface->init) (initable, cancellable, error);
107 }
108
109 /**
110  * g_initable_new:
111  * @object_type: a #GType supporting #GInitable.
112  * @cancellable: optional #GCancellable object, %NULL to ignore.
113  * @error: a #GError location to store the error occurring, or %NULL to
114  *    ignore.
115  * @first_property_name: the name of the first property, or %NULL if no
116  *     properties
117  * @...:  the value if the first property, followed by and other property
118  *    value pairs, and ended by %NULL.
119  *
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.
123  *
124  * Return value: (transfer full): a newly allocated #GObject, or %NULL on error
125  *
126  * Since: 2.22
127  */
128 gpointer
129 g_initable_new (GType          object_type,
130                 GCancellable  *cancellable,
131                 GError       **error,
132                 const gchar   *first_property_name,
133                 ...)
134 {
135   GObject *object;
136   va_list var_args;
137
138   va_start (var_args, first_property_name);
139   object = g_initable_new_valist (object_type,
140                                   first_property_name, var_args,
141                                   cancellable, error);
142   va_end (var_args);
143
144   return object;
145 }
146
147 /**
148  * g_initable_newv:
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 occurring, or %NULL to
154  *     ignore.
155  *
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.
159  *
160  * Return value: (transfer full): a newly allocated #GObject, or %NULL on error
161  *
162  * Since: 2.22
163  */
164 gpointer
165 g_initable_newv (GType          object_type,
166                  guint          n_parameters,
167                  GParameter    *parameters,
168                  GCancellable  *cancellable,
169                  GError       **error)
170 {
171   GObject *obj;
172
173   g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
174
175   obj = g_object_newv (object_type, n_parameters, parameters);
176
177   if (!g_initable_init (G_INITABLE (obj), cancellable, error))
178     {
179       g_object_unref (obj);
180       return NULL;
181     }
182
183   return (gpointer)obj;
184 }
185
186 /**
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 occurring, or %NULL to
194  *     ignore.
195  *
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.
199  *
200  * Return value: (transfer full): a newly allocated #GObject, or %NULL on error
201  *
202  * Since: 2.22
203  */
204 GObject*
205 g_initable_new_valist (GType          object_type,
206                        const gchar   *first_property_name,
207                        va_list        var_args,
208                        GCancellable  *cancellable,
209                        GError       **error)
210 {
211   GObject *obj;
212
213   g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
214
215   obj = g_object_new_valist (object_type,
216                              first_property_name,
217                              var_args);
218
219   if (!g_initable_init (G_INITABLE (obj), cancellable, error))
220     {
221       g_object_unref (obj);
222       return NULL;
223     }
224
225   return obj;
226 }