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