TaPAS  0.2
ccl-exception.h
Go to the documentation of this file.
1 /*
2  * ccl-exception.h -- An exception mechanism for C.
3  *
4  * This file is a part of the C Common Library (CCL) project.
5  *
6  * Copyright (C) 2010 CNRS UMR 5800 & Université Bordeaux I (see AUTHORS file).
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the AltaRica Public License that comes with this
10  * package.
11  *
12  * This program 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.
15  */
16 
121 #ifndef __CCL_EXCEPTION_H__
122 # define __CCL_EXCEPTION_H__
123 
124 # include <ccl/ccl-common.h>
125 # include <ccl/ccl-protos.h>
126 
127 BEGIN_C_DECLS
128 
129 # include <setjmp.h>
130 
134 typedef struct ccl_exception_st ccl_exception;
135 
139 typedef struct ccl_exception_type_st ccl_exception_type;
140 typedef struct ccl_exception_local_variable_st ccl_exception_local_variable;
141 
142 struct ccl_exception_st {
143  const ccl_exception_type *type;
144  jmp_buf context;
145  ccl_exception_local_variable *vars;
146  ccl_exception *next;
147 };
148 
149 struct ccl_exception_type_st {
150  const char *name;
151  const ccl_exception_type *super;
152 };
153 
159 # define CCL_THROW(_args_)
160 
167 # define CCL_DECLARE_EXCEPTION(exc,super) \
168  extern const ccl_exception_type CCL_EXCEPTION_NAME (exc)
169 
175 # define CCL_DEFINE_EXCEPTION(exc,super) \
176  const ccl_exception_type CCL_EXCEPTION_NAME (exc) = { \
177  # exc, &CCL_EXCEPTION_NAME (super) \
178  }
179 
185 # define CCL_EXCEPTION_NAME(exc) ccl_exception_ ## exc
186 
187 CCL_DECLARE_EXCEPTION (___toplevel_throwable, NULL);
188 
192 CCL_DECLARE_EXCEPTION (error, ___toplevel_throwable);
193 
197 CCL_DECLARE_EXCEPTION (exception, ___toplevel_throwable);
198 
203 CCL_DECLARE_EXCEPTION (internal_error, error);
204 
209 CCL_DECLARE_EXCEPTION (runtime_exception, exception);
210 
215 CCL_DECLARE_EXCEPTION (division_by_zero, runtime_exception);
216 
221 extern const char *ccl_exception_current_message;
222 
229 # define ccl_try(_extype_) \
230  do { \
231  ccl_exception here; \
232  ccl_exception_push (&here, &CCL_EXCEPTION_NAME (_extype_)); \
233  if (setjmp (here.context) == 0) \
234  {
235 
236 
242 # define ccl_catch \
243  ccl_exception_cleanup_local_variables (0); \
244  ccl_exception_pop (); \
245  } else { \
246  ccl_exception_cleanup_local_variables (1); \
247  ccl_exception_pop ();
248 
249 # define ccl_catch_rethrow ccl_catch { ccl_rethrow (); } ccl_end_try
250 
255 # define ccl_end_try \
256  } \
257  } while (0)
258 
263 # define ccl_trycatch(_extype_,_stmt_) \
264  ccl_try (_extype_) _stmt_ ccl_catch ccl_end_try
265 
271 # define ccl_no_catch \
272  } \
273  ccl_exception_pop (); \
274 } while (0)
275 
285 # define ccl_throw(_extype,_msg) \
286  ccl_throw__ (&CCL_EXCEPTION_NAME (_extype), _msg, __FILE__, __LINE__)
287 
292 # define ccl_throw_no_msg(_extype) ccl_throw (_extype, # _extype)
293 
294 
300 # define ccl_rethrow() ccl_rethrow__ (__FILE__, __LINE__)
301 
306 # define ccl_exception_is_raised(_e_) \
307  ccl_exception_is_raised__ (&CCL_EXCEPTION_NAME (_e_))
308 
313 extern void
314 ccl_exception_print (void);
315 
316 /*
317  * The following functions are used by the implementation
318  */
319 
320 extern void
321 ccl_throw__ (const ccl_exception_type *ex, const char *msg,
322  const char *file, int line);
323 
324 extern void
325 ccl_rethrow__ (const char *file, int line);
326 
327 extern void
328 ccl_exception_push (ccl_exception *e, const ccl_exception_type *type);
329 
330 extern void
331 ccl_exception_pop (void);
332 
333 
334 extern int
335 ccl_exception_is_raised__ (const ccl_exception_type *type);
336 
337 # define ccl_exception_local_variable(del, pvar) \
338  ccl_exception_local_variable__ ((ccl_delete_proc *)(del), (pvar))
339 
340 extern void
341 ccl_exception_local_variable__ (ccl_delete_proc *del, void *pvar);
342 
343 extern void
344 ccl_exception_cleanup_local_variables (int usedel);
345 
346 END_C_DECLS
347 
348 #endif /* ! __CCL_EXCEPTION_H__ */
Frequently used prototypes of generic functions.
const char * ccl_exception_current_message
Message of the last raised exception. If no exception has been raised the content of this variable is...
struct ccl_exception_st ccl_exception
Type of exception objects.
void ccl_delete_proc(void *ptr)
Prototype of procedures used to release the resources allocated to the object pointed by ptr...
Definition: ccl-protos.h:44
void ccl_exception_print(void)
Print a message on CCL_LOG_ERROR that locates the last exception and its associated error message...
Some useful and common macros.
#define CCL_DECLARE_EXCEPTION(exc, super)
Macro-function used to declare an exception exc.
struct ccl_exception_type_st ccl_exception_type
Type encoding the kind of an exception.