/* c_exceptions.h is part of CXCPT. CXCPT is a C exception-handling library. CXCPT is Copyright (C) 2002 Fergus Henderson. It may be copied under the terms of the GNU Library General Public License. The purpose of this library is two-fold: - to provide a mechanism for doing exception handling in C code - to enable other languages that support exceptions to do so in a manner that interoperates with C++ exceptions. Namespace cleanliness: all symbols exported by this module start with the prefix `CXCPT_'. */ #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ /* Rather than using the type to distinguish between different kinds of exceptions, as in C++, a distinct address (normally the address of a static object) is associated with each distinct kind of exception. */ typedef void *CXCPT_ExceptionKind; /* CXCPT_throw() throws an exception (of type CXCPT_Exception, see below) with the specified MESSAGE (which should be appropriate for display to the user), the specified KIND, and the appropriate DATA for that kind, if any. */ void CXCPT_throw(const char *message, CXCPT_ExceptionKind kind, void *data); /* CXCPT_try_catch() executes ACTION(ACTION_DATA). If this results in an exception being thrown, then the exception will be caught, and HANDLER(HANDLER_DATA, ...) will be called; the values of the remaining parameters to HANDLER() depend on what kind of exception it was. If the exception was thrown by CXCPT_throw(), then the MESSAGE, KIND, and EXCEPTION_DATA parameters passed to HANDLER() will be the values that were passed to CXCPT_throw(). If the exception is a C++ exception derived from std::exception, then the MESSAGE parameter will be the return value of std::exception::what(), and the KIND and EXCEPTION_DATA parameters will be null. Otherwise, the MESSAGE, KIND, and EXCEPTION_DATA parameters will all be null. HANDLER() may return, in which case the exception is considered handled, and so will not be propagated to the caller; or, if the exception is of a kind which cannot be handled, HANDLER() may call CXCPT_rethrow(), in which case the exception will be propagated to the caller of CXCPT_try_catch(). */ void CXCPT_try_catch( void (*action)(void *action_data), void *action_data, void (*handler)(void *handler_data, const char *message, CXCPT_ExceptionKind kind, void *exception_data), void *handler_data ); /* CXCPT_rethrow() re-throws the current exception. This should only be called from within a handler function called by try_catch(). */ void CXCPT_rethrow(void); #ifdef __cplusplus } // end extern "C" #include /* The CXCPT_Exception type is the C++ exception type used for the exceptions thrown by CXCPT_throw(). It is declared here so that it can be used by C++ code which wants to catch exceptions thrown by C code that calls CXCPT_throw(). */ struct CXCPT_Exception : public std::exception { const char *message; CXCPT_ExceptionKind kind; void *data; CXCPT_Exception(const char *m, CXCPT_ExceptionKind k, void *d) : message(m), kind(k), data(d) {} virtual const char *what() { return message; } }; #endif