Updated FSF's address
[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, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20
21 #include "config.h"
22 #include "ginitable.h"
23 #include "glibintl.h"
24
25
26 /**
27  * SECTION:ginitable
28  * @short_description: Failable object initialization interface
29  * @include: gio/gio.h
30  * @see_also: #GAsyncInitable
31  *
32  * #GInitable is implemented by objects that can fail during
33  * initialization. If an object implements this interface then
34  * it must be initialized as the first thing after construction,
35  * either via g_initable_init() or g_async_initable_init_async()
36  * (the latter is only available if it also implements #GAsyncInitable).
37  *
38  * If the object is not initialized, or initialization returns with an
39  * error, then all operations on the object except g_object_ref() and
40  * g_object_unref() are considered to be invalid, and have undefined
41  * behaviour. They will often fail with g_critical() or g_warning(), but
42  * this must not be relied on.
43  *
44  * Users of objects implementing this are not intended to use
45  * the interface method directly, instead it will be used automatically
46  * in various ways. For C applications you generally just call
47  * g_initable_new() directly, or indirectly via a foo_thing_new() wrapper.
48  * This will call g_initable_init() under the cover, returning %NULL and
49  * setting a #GError on failure (at which point the instance is
50  * unreferenced).
51  *
52  * For bindings in languages where the native constructor supports
53  * exceptions the binding could check for objects implemention %GInitable
54  * during normal construction and automatically initialize them, throwing
55  * an exception on failure.
56  */
57
58 typedef GInitableIface GInitableInterface;
59 G_DEFINE_INTERFACE (GInitable, g_initable, G_TYPE_OBJECT)
60
61 static void
62 g_initable_default_init (GInitableInterface *iface)
63 {
64 }
65
66 /**
67  * g_initable_init:
68  * @initable: a #GInitable.
69  * @cancellable: optional #GCancellable object, %NULL to ignore.
70  * @error: a #GError location to store the error occurring, or %NULL to
71  * ignore.
72  *
73  * Initializes the object implementing the interface.
74  *
75  * The object must be initialized before any real use after initial
76  * construction, either with this function or g_async_initable_init_async().
77  *
78  * Implementations may also support cancellation. If @cancellable is not %NULL,
79  * then initialization can be cancelled by triggering the cancellable object
80  * from another thread. If the operation was cancelled, the error
81  * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL and
82  * the object doesn't support cancellable initialization the error
83  * %G_IO_ERROR_NOT_SUPPORTED will be returned.
84  *
85  * If the object is not initialized, or initialization returns with an
86  * error, then all operations on the object except g_object_ref() and
87  * g_object_unref() are considered to be invalid, and have undefined
88  * behaviour. See the <xref linkend="ginitable"/> section introduction
89  * for more details.
90  *
91  * Implementations of this method must be idempotent, i.e. multiple calls
92  * to this function with the same argument should return the same results.
93  * Only the first call initializes the object, further calls return the result
94  * of the first call. This is so that it's safe to implement the singleton
95  * pattern in the GObject constructor function.
96  *
97  * Returns: %TRUE if successful. If an error has occurred, this function will
98  *     return %FALSE and set @error appropriately if present.
99  *
100  * Since: 2.22
101  */
102 gboolean
103 g_initable_init (GInitable     *initable,
104                  GCancellable  *cancellable,
105                  GError       **error)
106 {
107   GInitableIface *iface;
108
109   g_return_val_if_fail (G_IS_INITABLE (initable), FALSE);
110
111   iface = G_INITABLE_GET_IFACE (initable);
112
113   return (* iface->init) (initable, cancellable, error);
114 }
115
116 /**
117  * g_initable_new:
118  * @object_type: a #GType supporting #GInitable.
119  * @cancellable: optional #GCancellable object, %NULL to ignore.
120  * @error: a #GError location to store the error occurring, or %NULL to
121  *    ignore.
122  * @first_property_name: (allow-none): the name of the first property, or %NULL if no
123  *     properties
124  * @...:  the value if the first property, followed by and other property
125  *    value pairs, and ended by %NULL.
126  *
127  * Helper function for constructing #GInitable object. This is
128  * similar to g_object_new() but also initializes the object
129  * and returns %NULL, setting an error on failure.
130  *
131  * Return value: (type GObject.Object) (transfer full): a newly allocated
132  *      #GObject, or %NULL on error
133  *
134  * Since: 2.22
135  */
136 gpointer
137 g_initable_new (GType          object_type,
138                 GCancellable  *cancellable,
139                 GError       **error,
140                 const gchar   *first_property_name,
141                 ...)
142 {
143   GObject *object;
144   va_list var_args;
145
146   va_start (var_args, first_property_name);
147   object = g_initable_new_valist (object_type,
148                                   first_property_name, var_args,
149                                   cancellable, error);
150   va_end (var_args);
151
152   return object;
153 }
154
155 /**
156  * g_initable_newv:
157  * @object_type: a #GType supporting #GInitable.
158  * @n_parameters: the number of parameters in @parameters
159  * @parameters: (array length=n_parameters): the parameters to use to construct the object
160  * @cancellable: optional #GCancellable object, %NULL to ignore.
161  * @error: a #GError location to store the error occurring, or %NULL to
162  *     ignore.
163  *
164  * Helper function for constructing #GInitable object. This is
165  * similar to g_object_newv() but also initializes the object
166  * and returns %NULL, setting an error on failure.
167  *
168  * Return value: (type GObject.Object) (transfer full): a newly allocated
169  *      #GObject, or %NULL on error
170  *
171  * Since: 2.22
172  */
173 gpointer
174 g_initable_newv (GType          object_type,
175                  guint          n_parameters,
176                  GParameter    *parameters,
177                  GCancellable  *cancellable,
178                  GError       **error)
179 {
180   GObject *obj;
181
182   g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
183
184   obj = g_object_newv (object_type, n_parameters, parameters);
185
186   if (!g_initable_init (G_INITABLE (obj), cancellable, error))
187     {
188       g_object_unref (obj);
189       return NULL;
190     }
191
192   return (gpointer)obj;
193 }
194
195 /**
196  * g_initable_new_valist:
197  * @object_type: a #GType supporting #GInitable.
198  * @first_property_name: the name of the first property, followed by
199  * the value, and other property value pairs, and ended by %NULL.
200  * @var_args: The var args list generated from @first_property_name.
201  * @cancellable: optional #GCancellable object, %NULL to ignore.
202  * @error: a #GError location to store the error occurring, or %NULL to
203  *     ignore.
204  *
205  * Helper function for constructing #GInitable object. This is
206  * similar to g_object_new_valist() but also initializes the object
207  * and returns %NULL, setting an error on failure.
208  *
209  * Return value: (type GObject.Object) (transfer full): a newly allocated
210  *      #GObject, or %NULL on error
211  *
212  * Since: 2.22
213  */
214 GObject*
215 g_initable_new_valist (GType          object_type,
216                        const gchar   *first_property_name,
217                        va_list        var_args,
218                        GCancellable  *cancellable,
219                        GError       **error)
220 {
221   GObject *obj;
222
223   g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
224
225   obj = g_object_new_valist (object_type,
226                              first_property_name,
227                              var_args);
228
229   if (!g_initable_init (G_INITABLE (obj), cancellable, error))
230     {
231       g_object_unref (obj);
232       return NULL;
233     }
234
235   return obj;
236 }