gerror.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* gerror.h - Error reporting system
  2. *
  3. * Copyright 2000 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 Public License
  18. * along with this library; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef __G_ERROR_H__
  21. #define __G_ERROR_H__
  22. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  23. #error "Only <glib.h> can be included directly."
  24. #endif
  25. #include <stdarg.h>
  26. #include <glib/gquark.h>
  27. G_BEGIN_DECLS
  28. /**
  29. * GError:
  30. * @domain: error domain, e.g. %G_FILE_ERROR
  31. * @code: error code, e.g. %G_FILE_ERROR_NOENT
  32. * @message: human-readable informative error message
  33. *
  34. * The `GError` structure contains information about
  35. * an error that has occurred.
  36. */
  37. typedef struct _GError GError;
  38. struct _GError
  39. {
  40. GQuark domain;
  41. gint code;
  42. gchar *message;
  43. };
  44. /**
  45. * G_DEFINE_EXTENDED_ERROR:
  46. * @ErrorType: name to return a #GQuark for
  47. * @error_type: prefix for the function name
  48. *
  49. * A convenience macro which defines two functions. First, returning
  50. * the #GQuark for the extended error type @ErrorType; it is called
  51. * `error_type_quark()`. Second, returning the private data from a
  52. * passed #GError; it is called `error_type_get_private()`.
  53. *
  54. * For this macro to work, a type named `ErrorTypePrivate` should be
  55. * defined, `error_type_private_init()`, `error_type_private_copy()`
  56. * and `error_type_private_clear()` functions need to be either
  57. * declared or defined. The functions should be similar to
  58. * #GErrorInitFunc, #GErrorCopyFunc and #GErrorClearFunc,
  59. * respectively, but they should receive the private data type instead
  60. * of #GError.
  61. *
  62. * See [Extended #GError Domains][gerror-extended-domains] for an example.
  63. *
  64. * Since: 2.68
  65. */
  66. #define G_DEFINE_EXTENDED_ERROR(ErrorType, error_type) \
  67. static inline ErrorType ## Private * \
  68. error_type ## _get_private (const GError *error) \
  69. { \
  70. /* Copied from gtype.c (STRUCT_ALIGNMENT and ALIGN_STRUCT macros). */ \
  71. const gsize sa = 2 * sizeof (gsize); \
  72. const gsize as = (sizeof (ErrorType ## Private) + (sa - 1)) & -sa; \
  73. g_return_val_if_fail (error != NULL, NULL); \
  74. g_return_val_if_fail (error->domain == error_type ## _quark (), NULL); \
  75. return (ErrorType ## Private *) (((guint8 *)error) - as); \
  76. } \
  77. \
  78. static void \
  79. g_error_with_ ## error_type ## _private_init (GError *error) \
  80. { \
  81. ErrorType ## Private *priv = error_type ## _get_private (error); \
  82. error_type ## _private_init (priv); \
  83. } \
  84. \
  85. static void \
  86. g_error_with_ ## error_type ## _private_copy (const GError *src_error, \
  87. GError *dest_error) \
  88. { \
  89. const ErrorType ## Private *src_priv = error_type ## _get_private (src_error); \
  90. ErrorType ## Private *dest_priv = error_type ## _get_private (dest_error); \
  91. error_type ## _private_copy (src_priv, dest_priv); \
  92. } \
  93. \
  94. static void \
  95. g_error_with_ ## error_type ## _private_clear (GError *error) \
  96. { \
  97. ErrorType ## Private *priv = error_type ## _get_private (error); \
  98. error_type ## _private_clear (priv); \
  99. } \
  100. \
  101. GQuark \
  102. error_type ## _quark (void) \
  103. { \
  104. static GQuark q; \
  105. static gsize initialized = 0; \
  106. \
  107. if (g_once_init_enter (&initialized)) \
  108. { \
  109. q = g_error_domain_register_static (#ErrorType, \
  110. sizeof (ErrorType ## Private), \
  111. g_error_with_ ## error_type ## _private_init, \
  112. g_error_with_ ## error_type ## _private_copy, \
  113. g_error_with_ ## error_type ## _private_clear); \
  114. g_once_init_leave (&initialized, 1); \
  115. } \
  116. \
  117. return q; \
  118. }
  119. /**
  120. * GErrorInitFunc:
  121. * @error: extended error
  122. *
  123. * Specifies the type of function which is called just after an
  124. * extended error instance is created and its fields filled. It should
  125. * only initialize the fields in the private data, which can be
  126. * received with the generated `*_get_private()` function.
  127. *
  128. * Normally, it is better to use G_DEFINE_EXTENDED_ERROR(), as it
  129. * already takes care of getting the private data from @error.
  130. *
  131. * Since: 2.68
  132. */
  133. typedef void (*GErrorInitFunc) (GError *error);
  134. /**
  135. * GErrorCopyFunc:
  136. * @src_error: source extended error
  137. * @dest_error: destination extended error
  138. *
  139. * Specifies the type of function which is called when an extended
  140. * error instance is copied. It is passed the pointer to the
  141. * destination error and source error, and should copy only the fields
  142. * of the private data from @src_error to @dest_error.
  143. *
  144. * Normally, it is better to use G_DEFINE_EXTENDED_ERROR(), as it
  145. * already takes care of getting the private data from @src_error and
  146. * @dest_error.
  147. *
  148. * Since: 2.68
  149. */
  150. typedef void (*GErrorCopyFunc) (const GError *src_error, GError *dest_error);
  151. /**
  152. * GErrorClearFunc:
  153. * @error: extended error to clear
  154. *
  155. * Specifies the type of function which is called when an extended
  156. * error instance is freed. It is passed the error pointer about to be
  157. * freed, and should free the error's private data fields.
  158. *
  159. * Normally, it is better to use G_DEFINE_EXTENDED_ERROR(), as it
  160. * already takes care of getting the private data from @error.
  161. *
  162. * Since: 2.68
  163. */
  164. typedef void (*GErrorClearFunc) (GError *error);
  165. GLIB_AVAILABLE_IN_2_68
  166. GQuark g_error_domain_register_static (const char *error_type_name,
  167. gsize error_type_private_size,
  168. GErrorInitFunc error_type_init,
  169. GErrorCopyFunc error_type_copy,
  170. GErrorClearFunc error_type_clear);
  171. GLIB_AVAILABLE_IN_2_68
  172. GQuark g_error_domain_register (const char *error_type_name,
  173. gsize error_type_private_size,
  174. GErrorInitFunc error_type_init,
  175. GErrorCopyFunc error_type_copy,
  176. GErrorClearFunc error_type_clear);
  177. GLIB_AVAILABLE_IN_ALL
  178. GError* g_error_new (GQuark domain,
  179. gint code,
  180. const gchar *format,
  181. ...) G_GNUC_PRINTF (3, 4);
  182. GLIB_AVAILABLE_IN_ALL
  183. GError* g_error_new_literal (GQuark domain,
  184. gint code,
  185. const gchar *message);
  186. GLIB_AVAILABLE_IN_ALL
  187. GError* g_error_new_valist (GQuark domain,
  188. gint code,
  189. const gchar *format,
  190. va_list args) G_GNUC_PRINTF(3, 0);
  191. GLIB_AVAILABLE_IN_ALL
  192. void g_error_free (GError *error);
  193. GLIB_AVAILABLE_IN_ALL
  194. GError* g_error_copy (const GError *error);
  195. GLIB_AVAILABLE_IN_ALL
  196. gboolean g_error_matches (const GError *error,
  197. GQuark domain,
  198. gint code);
  199. /* if (err) *err = g_error_new(domain, code, format, ...), also has
  200. * some sanity checks.
  201. */
  202. GLIB_AVAILABLE_IN_ALL
  203. void g_set_error (GError **err,
  204. GQuark domain,
  205. gint code,
  206. const gchar *format,
  207. ...) G_GNUC_PRINTF (4, 5);
  208. GLIB_AVAILABLE_IN_ALL
  209. void g_set_error_literal (GError **err,
  210. GQuark domain,
  211. gint code,
  212. const gchar *message);
  213. /* if (dest) *dest = src; also has some sanity checks.
  214. */
  215. GLIB_AVAILABLE_IN_ALL
  216. void g_propagate_error (GError **dest,
  217. GError *src);
  218. /* if (err && *err) { g_error_free(*err); *err = NULL; } */
  219. GLIB_AVAILABLE_IN_ALL
  220. void g_clear_error (GError **err);
  221. /* if (err) prefix the formatted string to the ->message */
  222. GLIB_AVAILABLE_IN_ALL
  223. void g_prefix_error (GError **err,
  224. const gchar *format,
  225. ...) G_GNUC_PRINTF (2, 3);
  226. /* if (err) prefix the string to the ->message */
  227. GLIB_AVAILABLE_IN_2_70
  228. void g_prefix_error_literal (GError **err,
  229. const gchar *prefix);
  230. /* g_propagate_error then g_error_prefix on dest */
  231. GLIB_AVAILABLE_IN_ALL
  232. void g_propagate_prefixed_error (GError **dest,
  233. GError *src,
  234. const gchar *format,
  235. ...) G_GNUC_PRINTF (3, 4);
  236. G_END_DECLS
  237. #endif /* __G_ERROR_H__ */