gkdbus: Fix underflow and unreachable code bug
[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  * SPDX-License-Identifier: LGPL-2.1-or-later
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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 implementing %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  * This method is intended for language bindings. If writing in C,
78  * g_initable_new() should typically be used instead.
79  *
80  * The object must be initialized before any real use after initial
81  * construction, either with this function or g_async_initable_init_async().
82  *
83  * Implementations may also support cancellation. If @cancellable is not %NULL,
84  * then initialization can be cancelled by triggering the cancellable object
85  * from another thread. If the operation was cancelled, the error
86  * %G_IO_ERROR_CANCELLED will be returned. If @cancellable is not %NULL and
87  * the object doesn't support cancellable initialization the error
88  * %G_IO_ERROR_NOT_SUPPORTED will be returned.
89  *
90  * If the object is not initialized, or initialization returns with an
91  * error, then all operations on the object except g_object_ref() and
92  * g_object_unref() are considered to be invalid, and have undefined
93  * behaviour. See the [introduction][ginitable] for more details.
94  *
95  * Callers should not assume that a class which implements #GInitable can be
96  * initialized multiple times, unless the class explicitly documents itself as
97  * supporting this. Generally, a class’ implementation of init() can assume
98  * (and assert) that it will only be called once. Previously, this documentation
99  * recommended all #GInitable implementations should be idempotent; that
100  * recommendation was relaxed in GLib 2.54.
101  *
102  * If a class explicitly supports being initialized multiple times, it is
103  * recommended that the method is idempotent: multiple calls with the same
104  * arguments should return the same results. Only the first call initializes
105  * the object; further calls return the result of the first call.
106  *
107  * One reason why a class might need to support idempotent initialization is if
108  * it is designed to be used via the singleton pattern, with a
109  * #GObjectClass.constructor that sometimes returns an existing instance.
110  * In this pattern, a caller would expect to be able to call g_initable_init()
111  * on the result of g_object_new(), regardless of whether it is in fact a new
112  * instance.
113  *
114  * Returns: %TRUE if successful. If an error has occurred, this function will
115  *     return %FALSE and set @error 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 occurring, or %NULL to
138  *    ignore.
139  * @first_property_name: (nullable): the name of the first property, or %NULL if no
140  *     properties
141  * @...:  the value if the first property, followed by and other property
142  *    value pairs, and ended by %NULL.
143  *
144  * Helper function for constructing #GInitable object. This is
145  * similar to g_object_new() but also initializes the object
146  * and returns %NULL, setting an error on failure.
147  *
148  * Returns: (type GObject.Object) (transfer full): a newly allocated
149  *      #GObject, or %NULL on error
150  *
151  * Since: 2.22
152  */
153 gpointer
154 g_initable_new (GType          object_type,
155                 GCancellable  *cancellable,
156                 GError       **error,
157                 const gchar   *first_property_name,
158                 ...)
159 {
160   GObject *object;
161   va_list var_args;
162
163   va_start (var_args, first_property_name);
164   object = g_initable_new_valist (object_type,
165                                   first_property_name, var_args,
166                                   cancellable, error);
167   va_end (var_args);
168
169   return object;
170 }
171
172 /**
173  * g_initable_newv:
174  * @object_type: a #GType supporting #GInitable.
175  * @n_parameters: the number of parameters in @parameters
176  * @parameters: (array length=n_parameters): the parameters to use to construct the object
177  * @cancellable: optional #GCancellable object, %NULL to ignore.
178  * @error: a #GError location to store the error occurring, or %NULL to
179  *     ignore.
180  *
181  * Helper function for constructing #GInitable object. This is
182  * similar to g_object_newv() but also initializes the object
183  * and returns %NULL, setting an error on failure.
184  *
185  * Returns: (type GObject.Object) (transfer full): a newly allocated
186  *      #GObject, or %NULL on error
187  *
188  * Since: 2.22
189  * Deprecated: 2.54: Use g_object_new_with_properties() and
190  * g_initable_init() instead. See #GParameter for more information.
191  */
192 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
193 gpointer
194 g_initable_newv (GType          object_type,
195                  guint          n_parameters,
196                  GParameter    *parameters,
197                  GCancellable  *cancellable,
198                  GError       **error)
199 {
200   GObject *obj;
201
202   g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
203
204   obj = g_object_newv (object_type, n_parameters, parameters);
205
206   if (!g_initable_init (G_INITABLE (obj), cancellable, error))
207     {
208       g_object_unref (obj);
209       return NULL;
210     }
211
212   return (gpointer)obj;
213 }
214 G_GNUC_END_IGNORE_DEPRECATIONS
215
216 /**
217  * g_initable_new_valist:
218  * @object_type: a #GType supporting #GInitable.
219  * @first_property_name: the name of the first property, followed by
220  * the value, and other property value pairs, and ended by %NULL.
221  * @var_args: The var args list generated from @first_property_name.
222  * @cancellable: optional #GCancellable object, %NULL to ignore.
223  * @error: a #GError location to store the error occurring, or %NULL to
224  *     ignore.
225  *
226  * Helper function for constructing #GInitable object. This is
227  * similar to g_object_new_valist() but also initializes the object
228  * and returns %NULL, setting an error on failure.
229  *
230  * Returns: (type GObject.Object) (transfer full): a newly allocated
231  *      #GObject, or %NULL on error
232  *
233  * Since: 2.22
234  */
235 GObject*
236 g_initable_new_valist (GType          object_type,
237                        const gchar   *first_property_name,
238                        va_list        var_args,
239                        GCancellable  *cancellable,
240                        GError       **error)
241 {
242   GObject *obj;
243
244   g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
245
246   obj = g_object_new_valist (object_type,
247                              first_property_name,
248                              var_args);
249
250   if (!g_initable_init (G_INITABLE (obj), cancellable, error))
251     {
252       g_object_unref (obj);
253       return NULL;
254     }
255
256   return obj;
257 }