Merge remote branch 'gvdb/master'
[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.
47  *
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.
52  */
53
54 typedef GInitableIface GInitableInterface;
55 G_DEFINE_INTERFACE (GInitable, g_initable, G_TYPE_OBJECT)
56
57 static void
58 g_initable_default_init (GInitableInterface *iface)
59 {
60 }
61
62 /**
63  * g_initable_init:
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
67  * ignore.
68  *
69  * Initializes the object implementing the interface. This must be
70  * done before any real use of the object after initial construction.
71  *
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.
78  *
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.
82  *
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.
88  *
89  * Returns: %TRUE if successful. If an error has occurred, this function will
90  *     return %FALSE and set @error appropriately if present.
91  *
92  * Since: 2.22
93  */
94 gboolean
95 g_initable_init (GInitable     *initable,
96                  GCancellable  *cancellable,
97                  GError       **error)
98 {
99   GInitableIface *iface;
100
101   g_return_val_if_fail (G_IS_INITABLE (initable), FALSE);
102
103   iface = G_INITABLE_GET_IFACE (initable);
104
105   return (* iface->init) (initable, cancellable, error);
106 }
107
108 /**
109  * g_initable_new:
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
113  *    ignore.
114  * @first_property_name: the name of the first property, or %NULL if no
115  *     properties
116  * @...:  the value if the first property, followed by and other property
117  *    value pairs, and ended by %NULL.
118  *
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.
122  *
123  * Return value: (transfer full): a newly allocated #GObject, or %NULL on error
124  *
125  * Since: 2.22
126  */
127 gpointer
128 g_initable_new (GType          object_type,
129                 GCancellable  *cancellable,
130                 GError       **error,
131                 const gchar   *first_property_name,
132                 ...)
133 {
134   GObject *object;
135   va_list var_args;
136
137   va_start (var_args, first_property_name);
138   object = g_initable_new_valist (object_type,
139                                   first_property_name, var_args,
140                                   cancellable, error);
141   va_end (var_args);
142
143   return object;
144 }
145
146 /**
147  * g_initable_newv:
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
153  *     ignore.
154  *
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.
158  *
159  * Return value: (transfer full): a newly allocated #GObject, or %NULL on error
160  *
161  * Since: 2.22
162  */
163 gpointer
164 g_initable_newv (GType          object_type,
165                  guint          n_parameters,
166                  GParameter    *parameters,
167                  GCancellable  *cancellable,
168                  GError       **error)
169 {
170   GObject *obj;
171
172   g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
173
174   obj = g_object_newv (object_type, n_parameters, parameters);
175
176   if (!g_initable_init (G_INITABLE (obj), cancellable, error))
177     {
178       g_object_unref (obj);
179       return NULL;
180     }
181
182   return (gpointer)obj;
183 }
184
185 /**
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
193  *     ignore.
194  *
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.
198  *
199  * Return value: (transfer full): a newly allocated #GObject, or %NULL on error
200  *
201  * Since: 2.22
202  */
203 GObject*
204 g_initable_new_valist (GType          object_type,
205                        const gchar   *first_property_name,
206                        va_list        var_args,
207                        GCancellable  *cancellable,
208                        GError       **error)
209 {
210   GObject *obj;
211
212   g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
213
214   obj = g_object_new_valist (object_type,
215                              first_property_name,
216                              var_args);
217
218   if (!g_initable_init (G_INITABLE (obj), cancellable, error))
219     {
220       g_object_unref (obj);
221       return NULL;
222     }
223
224   return obj;
225 }