1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
5 * Bertrand Guiheneuf <bertrand@helixcode.com>
7 * Copyright 1999-2003 Ximian, Inc. (www.ximian.com)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU Lesser General Public
11 * License as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
32 #include <glib/gi18n-lib.h>
34 #include <libedataserver/e-memory.h>
36 #include "camel-debug.h"
37 #include "camel-exception.h"
39 /* dont turn this off */
42 /* i dont know why gthread_mutex stuff even exists, this is easier */
44 /* also, i'm not convinced mutexes are needed here. But it
45 doesn't really hurt either */
46 static pthread_mutex_t exception_mutex = PTHREAD_MUTEX_INITIALIZER;
48 #define CAMEL_EXCEPTION_LOCK(e) (pthread_mutex_lock(&exception_mutex))
49 #define CAMEL_EXCEPTION_UNLOCK(e) (pthread_mutex_unlock(&exception_mutex))
51 static EMemChunk *exception_chunks = NULL;
54 * camel_exception_new: allocate a new exception object.
56 * Create and returns a new exception object.
58 * Returns the newly allocated exception object
61 camel_exception_new (void)
65 CAMEL_EXCEPTION_LOCK(exception);
67 if (exception_chunks == NULL)
68 exception_chunks = e_memchunk_new(16, sizeof(CamelException));
70 ex = e_memchunk_alloc(exception_chunks);
73 /* set the Exception Id to NULL */
74 ex->id = CAMEL_EXCEPTION_NONE;
76 CAMEL_EXCEPTION_UNLOCK(exception);
82 * camel_exception_init:
83 * @ex: a #CamelException
85 * Init an exception. This routine is mainly useful when using a
86 * statically allocated exception.
89 camel_exception_init (CamelException *ex)
93 /* set the Exception Id to NULL */
94 ex->id = CAMEL_EXCEPTION_NONE;
99 * camel_exception_clear:
100 * @ex: a #CamelException
102 * Clear an exception, that is, set the exception ID to
103 * #CAMEL_EXCEPTION_NONE and free the description text. If the
104 * exception is %NULL, this funtion just returns.
107 camel_exception_clear (CamelException *exception)
112 CAMEL_EXCEPTION_LOCK(exception);
115 g_free (exception->desc);
116 exception->desc = NULL;
117 exception->id = CAMEL_EXCEPTION_NONE;
119 CAMEL_EXCEPTION_UNLOCK(exception);
123 * camel_exception_free:
124 * @ex: a #CamelException
126 * Free an exception object. If the exception is %NULL, nothing is
127 * done, the routine simply returns.
130 camel_exception_free (CamelException *exception)
136 g_free (exception->desc);
138 CAMEL_EXCEPTION_LOCK(exception);
140 e_memchunk_free(exception_chunks, exception);
142 CAMEL_EXCEPTION_UNLOCK(exception);
146 * camel_exception_set: set an exception
147 * @ex: a #CamelException
149 * @desc: textual description of the exception
151 * Set the value of an exception. The exception id is
152 * a unique number representing the exception. The
153 * textual description is a small text explaining
154 * what happened and provoked the exception.
156 * When @ex is %NULL, nothing is done, this routine
160 camel_exception_set (CamelException *ex, ExceptionId id, const char *desc)
162 if (camel_debug("exception"))
163 printf("CamelException.set(%p, %u, '%s')\n", ex, id, desc);
168 CAMEL_EXCEPTION_LOCK(exception);
172 if (desc != ex->desc) {
174 ex->desc = g_strdup (desc);
177 CAMEL_EXCEPTION_UNLOCK(exception);
181 * camel_exception_setv: set an exception
182 * @ex: a #CamelException
184 * @format: format of the description string. The format string is
185 * used as in printf().
187 * Set the value of an exception. The exception id is
188 * a unique number representing the exception. The
189 * textual description is a small text explaining
190 * what happened and provoked the exception.
191 * In this version, the string is created from the format
192 * string and the variable argument list.
195 * camel_exception_setv (ex, ..., camel_exception_get_description (ex), ...);
197 * When @ex is %NULL, nothing is done, this routine
201 camel_exception_setv (CamelException *ex, ExceptionId id, const char *format, ...)
206 va_start(args, format);
207 desc = g_strdup_vprintf (format, args);
210 if (camel_debug("exception"))
211 printf("CamelException.setv(%p, %u, '%s')\n", ex, id, desc);
218 CAMEL_EXCEPTION_LOCK(exception);
224 CAMEL_EXCEPTION_UNLOCK(exception);
228 * camel_exception_xfer:
229 * @ex_dst: Destination exception object
230 * @ex_src: Source exception object
232 * Transfer the content of an exception from an exception object to
233 * another. The destination exception receives the id and the
234 * description text of the source exception.
237 camel_exception_xfer (CamelException *ex_dst,
238 CamelException *ex_src)
240 if (ex_src == NULL) {
241 w(g_warning ("camel_exception_xfer: trying to transfer NULL exception to %p\n", ex_dst));
245 if (ex_dst == NULL) {
246 /* must have same side-effects */
247 camel_exception_clear (ex_src);
251 CAMEL_EXCEPTION_LOCK(exception);
254 g_free (ex_dst->desc);
256 ex_dst->id = ex_src->id;
257 ex_dst->desc = ex_src->desc;
260 ex_src->id = CAMEL_EXCEPTION_NONE;
262 CAMEL_EXCEPTION_UNLOCK(exception);
266 * camel_exception_get_id:
267 * @ex: a #CamelException
269 * Get the id of an exception.
271 * Returns the exception id (#CAMEL_EXCEPTION_NONE will be returned if
272 * @ex is %NULL or unset)
275 camel_exception_get_id (CamelException *ex)
280 w(g_warning ("camel_exception_get_id called with NULL parameter."));
282 return CAMEL_EXCEPTION_NONE;
286 * camel_exception_get_description:
287 * @ex: a #CamelException
289 * Get the exception description text.
291 * Returns the exception description text (%NULL will be returned if
292 * @ex is %NULL or unset)
295 camel_exception_get_description (CamelException *ex)
302 w(g_warning ("camel_exception_get_description called with NULL parameter."));
304 return ret ? ret : (_("No description available"));