1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2009 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "ginitable.h"
30 * @short_description: Failable object initialization interface
32 * @see_also: #GAsyncInitable
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).
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.
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
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.
60 typedef GInitableIface GInitableInterface;
61 G_DEFINE_INTERFACE (GInitable, g_initable, G_TYPE_OBJECT)
64 g_initable_default_init (GInitableInterface *iface)
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
75 * Initializes the object implementing the interface.
77 * This method is intended for language bindings. If writing in C,
78 * g_initable_new() should typically be used instead.
80 * The object must be initialized before any real use after initial
81 * construction, either with this function or g_async_initable_init_async().
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.
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.
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.
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.
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
114 * Returns: %TRUE if successful. If an error has occurred, this function will
115 * return %FALSE and set @error appropriately if present.
120 g_initable_init (GInitable *initable,
121 GCancellable *cancellable,
124 GInitableIface *iface;
126 g_return_val_if_fail (G_IS_INITABLE (initable), FALSE);
128 iface = G_INITABLE_GET_IFACE (initable);
130 return (* iface->init) (initable, cancellable, error);
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
139 * @first_property_name: (nullable): the name of the first property, or %NULL if no
141 * @...: the value if the first property, followed by and other property
142 * value pairs, and ended by %NULL.
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.
148 * Returns: (type GObject.Object) (transfer full): a newly allocated
149 * #GObject, or %NULL on error
154 g_initable_new (GType object_type,
155 GCancellable *cancellable,
157 const gchar *first_property_name,
163 va_start (var_args, first_property_name);
164 object = g_initable_new_valist (object_type,
165 first_property_name, var_args,
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
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.
185 * Returns: (type GObject.Object) (transfer full): a newly allocated
186 * #GObject, or %NULL on error
189 * Deprecated: 2.54: Use g_object_new_with_properties() and
190 * g_initable_init() instead. See #GParameter for more information.
192 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
194 g_initable_newv (GType object_type,
196 GParameter *parameters,
197 GCancellable *cancellable,
202 g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
204 obj = g_object_newv (object_type, n_parameters, parameters);
206 if (!g_initable_init (G_INITABLE (obj), cancellable, error))
208 g_object_unref (obj);
212 return (gpointer)obj;
214 G_GNUC_END_IGNORE_DEPRECATIONS
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
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.
230 * Returns: (type GObject.Object) (transfer full): a newly allocated
231 * #GObject, or %NULL on error
236 g_initable_new_valist (GType object_type,
237 const gchar *first_property_name,
239 GCancellable *cancellable,
244 g_return_val_if_fail (G_TYPE_IS_INITABLE (object_type), NULL);
246 obj = g_object_new_valist (object_type,
250 if (!g_initable_init (G_INITABLE (obj), cancellable, error))
252 g_object_unref (obj);