Import GInitable, GSocket and dependencies from gnio
[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 #include "gioalias.h"
28
29 /**
30  * SECTION:ginitable
31  * @short_description: Failable object initialization interface
32  * @include: gio/gio.h
33  * @see_also: #GAsyncInitable
34  *
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.
41  *
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.
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 GType
56 g_initable_get_type (void)
57 {
58   static volatile gsize g_define_type_id__volatile = 0;
59
60   if (g_once_init_enter (&g_define_type_id__volatile))
61     {
62       const GTypeInfo initable_info =
63       {
64         sizeof (GInitableIface), /* class_size */
65         NULL,           /* base_init */
66         NULL,           /* base_finalize */
67         NULL,
68         NULL,           /* class_finalize */
69         NULL,           /* class_data */
70         0,
71         0,              /* n_preallocs */
72         NULL
73       };
74       GType g_define_type_id =
75         g_type_register_static (G_TYPE_INTERFACE, I_("GInitable"),
76                                 &initable_info, 0);
77
78       g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
79
80       g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
81     }
82
83   return g_define_type_id__volatile;
84 }
85
86 /**
87  * g_initable_init:
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
91  * ignore.
92  *
93  * Initializes the object implementing the interface. This must be
94  * done before any real use of the object after initial construction.
95  *
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.
102  *
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.
106  *
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.
112  *
113  * Returns: %TRUE if successful. If an error
114  *     has occurred, this function will return %FALSE and set @error
115  *     appropriately if present.
116  *
117  * Since: 2.22
118  **/
119 gboolean
120 g_initable_init (GInitable     *initable,
121                  GCancellable  *cancellable,
122                  GError       **error)
123 {
124   GInitableIface *iface;
125
126   g_return_val_if_fail (G_IS_INITABLE (initable), FALSE);
127
128   iface = G_INITABLE_GET_IFACE (initable);
129
130   return (* iface->init) (initable, cancellable, error);
131 }
132
133 /**
134  * g_initable_new:
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
138  * ignore.
139  * @first_property_name: the name of the first property, followed by
140  * the value, and other property value pairs, and ended by %NULL.
141  *
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.
145  *
146  * Return value: a newly allocated #GObject, or %NULL on error
147  *
148  * Since: 2.22
149  **/
150 gpointer
151 g_initable_new (GType          object_type,
152                 GCancellable  *cancellable,
153                 GError       **error,
154                 const gchar   *first_property_name,
155                 ...)
156 {
157   GObject *object;
158   va_list var_args;
159
160   va_start (var_args, first_property_name);
161   object = g_initable_new_valist (object_type,
162                                   first_property_name, var_args,
163                                   cancellable, error);
164   va_end (var_args);
165
166   return object;
167 }
168
169 /**
170  * g_initable_new:
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
176  * ignore.
177  *
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.
181  *
182  * Return value: a newly allocated #GObject, or %NULL on error
183  *
184  * Since: 2.22
185  **/
186 gpointer
187 g_initable_newv (GType          object_type,
188                  guint          n_parameters,
189                  GParameter    *parameters,
190                  GCancellable  *cancellable,
191                  GError       **error)
192 {
193   GObject *obj;
194
195   g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
196
197   obj = g_object_newv (object_type, n_parameters, parameters);
198
199   if (!g_initable_init (G_INITABLE (obj), cancellable, error))
200     {
201       g_object_unref (obj);
202       return NULL;
203     }
204
205   return (gpointer)obj;
206 }
207
208 /**
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
216  * ignore.
217  *
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.
221  *
222  * Return value: a newly allocated #GObject, or %NULL on error
223  *
224  * Since: 2.22
225  **/
226 GObject*
227 g_initable_new_valist (GType          object_type,
228                        const gchar   *first_property_name,
229                        va_list        var_args,
230                        GCancellable  *cancellable,
231                        GError       **error)
232 {
233   GObject *obj;
234
235   g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
236
237   obj = g_object_new_valist (object_type,
238                              first_property_name,
239                              var_args);
240
241   if (!g_initable_init (G_INITABLE (obj), cancellable, error))
242     {
243       g_object_unref (obj);
244       return NULL;
245     }
246
247   return obj;
248 }
249
250 #define __G_INITABLE_C__
251 #include "gioaliasdef.c"