1 /* gerror.h - Error reporting system
3 * Copyright 2000 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 Public License
18 * along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
25 #error "Only <glib.h> can be included directly."
30 #include <glib/gquark.h>
36 * @domain: error domain, e.g. %G_FILE_ERROR
37 * @code: error code, e.g. %G_FILE_ERROR_NOENT
38 * @message: human-readable informative error message
40 * The `GError` structure contains information about
41 * an error that has occurred.
43 typedef struct _GError GError;
53 * G_DEFINE_EXTENDED_ERROR:
54 * @ErrorType: name to return a #GQuark for
55 * @error_type: prefix for the function name
57 * A convenience macro which defines two functions. First, returning
58 * the #GQuark for the extended error type @ErrorType; it is called
59 * `error_type_quark()`. Second, returning the private data from a
60 * passed #GError; it is called `error_type_get_private()`.
62 * For this macro to work, a type named `ErrorTypePrivate` should be
63 * defined, `error_type_private_init()`, `error_type_private_copy()`
64 * and `error_type_private_clear()` functions need to be either
65 * declared or defined. The functions should be similar to
66 * #GErrorInitFunc, #GErrorCopyFunc and #GErrorClearFunc,
67 * respectively, but they should receive the private data type instead
70 * See [Extended #GError Domains][gerror-extended-domains] for an example.
74 #define G_DEFINE_EXTENDED_ERROR(ErrorType, error_type) \
75 static inline ErrorType ## Private * \
76 error_type ## _get_private (const GError *error) \
78 /* Copied from gtype.c (STRUCT_ALIGNMENT and ALIGN_STRUCT macros). */ \
79 const gsize sa = 2 * sizeof (gsize); \
80 const gsize as = (sizeof (ErrorType ## Private) + (sa - 1)) & -sa; \
81 g_return_val_if_fail (error != NULL, NULL); \
82 g_return_val_if_fail (error->domain == error_type ## _quark (), NULL); \
83 return (ErrorType ## Private *) (((guint8 *)error) - as); \
87 g_error_with_ ## error_type ## _private_init (GError *error) \
89 ErrorType ## Private *priv = error_type ## _get_private (error); \
90 error_type ## _private_init (priv); \
94 g_error_with_ ## error_type ## _private_copy (const GError *src_error, \
97 const ErrorType ## Private *src_priv = error_type ## _get_private (src_error); \
98 ErrorType ## Private *dest_priv = error_type ## _get_private (dest_error); \
99 error_type ## _private_copy (src_priv, dest_priv); \
103 g_error_with_ ## error_type ## _private_clear (GError *error) \
105 ErrorType ## Private *priv = error_type ## _get_private (error); \
106 error_type ## _private_clear (priv); \
110 error_type ## _quark (void) \
113 static gsize initialized = 0; \
115 if (g_once_init_enter (&initialized)) \
117 q = g_error_domain_register_static (#ErrorType, \
118 sizeof (ErrorType ## Private), \
119 g_error_with_ ## error_type ## _private_init, \
120 g_error_with_ ## error_type ## _private_copy, \
121 g_error_with_ ## error_type ## _private_clear); \
122 g_once_init_leave (&initialized, 1); \
130 * @error: extended error
132 * Specifies the type of function which is called just after an
133 * extended error instance is created and its fields filled. It should
134 * only initialize the fields in the private data, which can be
135 * received with the generated `*_get_private()` function.
137 * Normally, it is better to use G_DEFINE_EXTENDED_ERROR(), as it
138 * already takes care of getting the private data from @error.
142 typedef void (*GErrorInitFunc) (GError *error);
146 * @src_error: source extended error
147 * @dest_error: destination extended error
149 * Specifies the type of function which is called when an extended
150 * error instance is copied. It is passed the pointer to the
151 * destination error and source error, and should copy only the fields
152 * of the private data from @src_error to @dest_error.
154 * Normally, it is better to use G_DEFINE_EXTENDED_ERROR(), as it
155 * already takes care of getting the private data from @src_error and
160 typedef void (*GErrorCopyFunc) (const GError *src_error, GError *dest_error);
164 * @error: extended error to clear
166 * Specifies the type of function which is called when an extended
167 * error instance is freed. It is passed the error pointer about to be
168 * freed, and should free the error's private data fields.
170 * Normally, it is better to use G_DEFINE_EXTENDED_ERROR(), as it
171 * already takes care of getting the private data from @error.
175 typedef void (*GErrorClearFunc) (GError *error);
177 GLIB_AVAILABLE_IN_2_68
178 GQuark g_error_domain_register_static (const char *error_type_name,
179 gsize error_type_private_size,
180 GErrorInitFunc error_type_init,
181 GErrorCopyFunc error_type_copy,
182 GErrorClearFunc error_type_clear);
184 GLIB_AVAILABLE_IN_2_68
185 GQuark g_error_domain_register (const char *error_type_name,
186 gsize error_type_private_size,
187 GErrorInitFunc error_type_init,
188 GErrorCopyFunc error_type_copy,
189 GErrorClearFunc error_type_clear);
191 GLIB_AVAILABLE_IN_ALL
192 GError* g_error_new (GQuark domain,
195 ...) G_GNUC_PRINTF (3, 4);
197 GLIB_AVAILABLE_IN_ALL
198 GError* g_error_new_literal (GQuark domain,
200 const gchar *message);
201 GLIB_AVAILABLE_IN_ALL
202 GError* g_error_new_valist (GQuark domain,
205 va_list args) G_GNUC_PRINTF(3, 0);
207 GLIB_AVAILABLE_IN_ALL
208 void g_error_free (GError *error);
209 GLIB_AVAILABLE_IN_ALL
210 GError* g_error_copy (const GError *error);
212 GLIB_AVAILABLE_IN_ALL
213 gboolean g_error_matches (const GError *error,
217 /* if (err) *err = g_error_new(domain, code, format, ...), also has
218 * some sanity checks.
220 GLIB_AVAILABLE_IN_ALL
221 void g_set_error (GError **err,
225 ...) G_GNUC_PRINTF (4, 5);
227 GLIB_AVAILABLE_IN_ALL
228 void g_set_error_literal (GError **err,
231 const gchar *message);
233 /* if (dest) *dest = src; also has some sanity checks.
235 GLIB_AVAILABLE_IN_ALL
236 void g_propagate_error (GError **dest,
239 /* if (err && *err) { g_error_free(*err); *err = NULL; } */
240 GLIB_AVAILABLE_IN_ALL
241 void g_clear_error (GError **err);
243 /* if (err) prefix the formatted string to the ->message */
244 GLIB_AVAILABLE_IN_ALL
245 void g_prefix_error (GError **err,
247 ...) G_GNUC_PRINTF (2, 3);
249 /* if (err) prefix the string to the ->message */
250 GLIB_AVAILABLE_IN_2_70
251 void g_prefix_error_literal (GError **err,
252 const gchar *prefix);
254 /* g_propagate_error then g_error_prefix on dest */
255 GLIB_AVAILABLE_IN_ALL
256 void g_propagate_prefixed_error (GError **dest,
259 ...) G_GNUC_PRINTF (3, 4);
263 #endif /* __G_ERROR_H__ */