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 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 General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include "camel-exception.h"
32 #include "e-util/e-memory.h"
34 /* i dont know why gthread_mutex stuff even exists, this is easier */
36 /* also, i'm not convinced mutexes are needed here. But it
37 doesn't really hurt either */
38 static pthread_mutex_t exception_mutex = PTHREAD_MUTEX_INITIALIZER;
40 #define CAMEL_EXCEPTION_LOCK(e) (pthread_mutex_lock(&exception_mutex))
41 #define CAMEL_EXCEPTION_UNLOCK(e) (pthread_mutex_unlock(&exception_mutex))
43 static EMemChunk *exception_chunks = NULL;
46 * camel_exception_new: allocate a new exception object.
48 * Create and returns a new exception object.
51 * Return value: The newly allocated exception object.
54 camel_exception_new (void)
58 CAMEL_EXCEPTION_LOCK(exception);
60 if (exception_chunks == NULL)
61 exception_chunks = e_memchunk_new(16, sizeof(CamelException));
63 ex = e_memchunk_alloc(exception_chunks);
66 /* set the Exception Id to NULL */
67 ex->id = CAMEL_EXCEPTION_NONE;
69 CAMEL_EXCEPTION_UNLOCK(exception);
75 * camel_exception_init: init a (statically allocated) exception.
77 * Init an exception. This routine is mainly
78 * useful when using a statically allocated
84 camel_exception_init (CamelException *ex)
88 /* set the Exception Id to NULL */
89 ex->id = CAMEL_EXCEPTION_NONE;
94 * camel_exception_clear: Clear an exception
95 * @exception: the exception object
97 * Clear an exception, that is, set the
98 * exception ID to CAMEL_EXCEPTION_NONE and
99 * free the description text.
100 * If the exception is NULL, this funtion just
104 camel_exception_clear (CamelException *exception)
109 CAMEL_EXCEPTION_LOCK(exception);
112 g_free (exception->desc);
113 exception->desc = NULL;
114 exception->id = CAMEL_EXCEPTION_NONE;
116 CAMEL_EXCEPTION_UNLOCK(exception);
120 * camel_exception_free: Free an exception
121 * @exception: The exception object to free
123 * Free an exception object. If the exception
124 * is NULL, nothing is done, the routine simply
128 camel_exception_free (CamelException *exception)
134 g_free (exception->desc);
136 CAMEL_EXCEPTION_LOCK(exception);
138 e_memchunk_free(exception_chunks, exception);
140 CAMEL_EXCEPTION_UNLOCK(exception);
144 * camel_exception_set: set an exception
145 * @ex: exception object
147 * @desc: textual description of the exception
149 * Set the value of an exception. The exception id is
150 * a unique number representing the exception. The
151 * textual description is a small text explaining
152 * what happened and provoked the exception.
154 * When @ex is NULL, nothing is done, this routine
159 camel_exception_set (CamelException *ex,
166 CAMEL_EXCEPTION_LOCK(exception);
170 if (desc != ex->desc) {
172 ex->desc = g_strdup (desc);
175 CAMEL_EXCEPTION_UNLOCK(exception);
179 * camel_exception_setv: set an exception
180 * @ex: exception object
182 * @format: format of the description string. The format string is
183 * used as in printf().
185 * Set the value of an exception. The exception id is
186 * a unique number representing the exception. The
187 * textual description is a small text explaining
188 * what happened and provoked the exception.
189 * In this version, the string is created from the format
190 * string and the variable argument list.
193 * camel_exception_setv (ex, ..., camel_exception_get_description (ex), ...);
195 * When @ex is NULL, nothing is done, this routine
200 camel_exception_setv (CamelException *ex,
211 CAMEL_EXCEPTION_LOCK(exception);
215 va_start(args, format);
216 ex->desc = g_strdup_vprintf (format, args);
223 CAMEL_EXCEPTION_UNLOCK(exception);
227 * camel_exception_xfer: transfer an exception
228 * @ex_dst: Destination exception object
229 * @ex_src: Source exception object
231 * Transfer the content of an exception from
232 * an exception object to another.
233 * The destination exception receives the id and
234 * the description text of the source exception.
237 camel_exception_xfer (CamelException *ex_dst,
238 CamelException *ex_src)
240 if (ex_src == NULL) {
241 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: get the exception id
267 * @ex: The exception object
269 * Return the id of an exception.
270 * If @ex is NULL, return CAMEL_EXCEPTION_NONE;
272 * Return value: Exception ID.
275 camel_exception_get_id (CamelException *ex)
280 g_warning ("camel_exception_get_id called with NULL parameter.");
281 return CAMEL_EXCEPTION_NONE;
286 * camel_exception_get_description: get the description of an exception.
287 * @ex: The exception object
289 * Return the exception description text.
290 * If @ex is NULL, return NULL;
293 * Return value: Exception description text.
296 camel_exception_get_description (CamelException *ex)
303 g_warning ("camel_exception_get_description called with NULL parameter.");