Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #ifndef __PJ_EXCEPTION_H__ |
| 20 | #define __PJ_EXCEPTION_H__ |
| 21 | |
| 22 | /** |
| 23 | * @file except.h |
| 24 | * @brief Exception Handling in C. |
| 25 | */ |
| 26 | |
| 27 | #include <pj/types.h> |
| 28 | #include <pj/compat/setjmp.h> |
| 29 | |
| 30 | |
| 31 | PJ_BEGIN_DECL |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * @defgroup PJ_EXCEPT Exception Handling |
| 36 | * @ingroup PJ_MISC |
| 37 | * @{ |
| 38 | * |
| 39 | * \section pj_except_sample_sec Quick Example |
| 40 | * |
| 41 | * For the impatient, take a look at some examples: |
| 42 | * - @ref page_pjlib_samples_except_c |
| 43 | * - @ref page_pjlib_exception_test |
| 44 | * |
| 45 | * \section pj_except_except Exception Handling |
| 46 | * |
| 47 | * This module provides exception handling syntactically similar to C++ in |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 48 | * C language. In Win32 systems, it uses Windows Structured Exception |
| 49 | * Handling (SEH) if macro PJ_EXCEPTION_USE_WIN32_SEH is non-zero. |
| 50 | * Otherwise it will use setjmp() and longjmp(). |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 51 | * |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 52 | * On some platforms where setjmp/longjmp is not available, setjmp/longjmp |
| 53 | * implementation is provided. See <pj/compat/setjmp.h> for compatibility. |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 54 | * |
| 55 | * The exception handling mechanism is completely thread safe, so the exception |
| 56 | * thrown by one thread will not interfere with other thread. |
| 57 | * |
| 58 | * CAVEATS: |
| 59 | * - unlike C++ exception, the scheme here won't call destructors of local |
| 60 | * objects if exception is thrown. Care must be taken when a function |
| 61 | * hold some resorce such as pool or mutex etc. |
| 62 | * - You CAN NOT make nested exception in one single function without using |
| 63 | * a nested PJ_USE_EXCEPTION. |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 64 | * - You can not provide more than PJ_CATCH or PJ_CATCH_ANY nor use PJ_CATCH |
| 65 | * and PJ_CATCH_ANY for a single PJ_TRY. |
| 66 | * - Exceptions will always be caught by the first handler (unlike C++ where |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 67 | * exception is only caught if the type matches. |
| 68 | * |
| 69 | * The exception handling constructs are similar to C++. The blocks will be |
| 70 | * constructed similar to the following sample: |
| 71 | * |
| 72 | * \verbatim |
| 73 | #define NO_MEMORY 1 |
| 74 | #define SYNTAX_ERROR 2 |
| 75 | |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 76 | int sample1() |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 77 | { |
| 78 | PJ_USE_EXCEPTION; // declare local exception stack. |
| 79 | |
| 80 | PJ_TRY { |
| 81 | ...// do something.. |
| 82 | } |
| 83 | PJ_CATCH(NO_MEMORY) { |
| 84 | ... // handle exception 1 |
| 85 | } |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 86 | PJ_END; |
| 87 | } |
| 88 | |
| 89 | int sample2() |
| 90 | { |
| 91 | PJ_USE_EXCEPTION; // declare local exception stack. |
| 92 | |
| 93 | PJ_TRY { |
| 94 | ...// do something.. |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 95 | } |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 96 | PJ_CATCH_ANY { |
| 97 | if (PJ_GET_EXCEPTION() == NO_MEMORY) |
| 98 | ...; // handle no memory situation |
| 99 | else if (PJ_GET_EXCEPTION() == SYNTAX_ERROR) |
| 100 | ...; // handle syntax error |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 101 | } |
| 102 | PJ_END; |
| 103 | } |
| 104 | \endverbatim |
| 105 | * |
| 106 | * The above sample uses hard coded exception ID. It is @b strongly |
| 107 | * recommended that applications request a unique exception ID instead |
| 108 | * of hard coded value like above. |
| 109 | * |
| 110 | * \section pj_except_reg Exception ID Allocation |
| 111 | * |
| 112 | * To ensure that exception ID (number) are used consistently and to |
| 113 | * prevent ID collisions in an application, it is strongly suggested that |
| 114 | * applications allocate an exception ID for each possible exception |
| 115 | * type. As a bonus of this process, the application can identify |
| 116 | * the name of the exception when the particular exception is thrown. |
| 117 | * |
| 118 | * Exception ID management are performed with the following APIs: |
| 119 | * - #pj_exception_id_alloc(). |
| 120 | * - #pj_exception_id_free(). |
| 121 | * - #pj_exception_id_name(). |
| 122 | * |
| 123 | * |
| 124 | * PJLIB itself automatically allocates one exception id, i.e. |
| 125 | * #PJ_NO_MEMORY_EXCEPTION which is declared in <pj/pool.h>. This exception |
| 126 | * ID is raised by default pool policy when it fails to allocate memory. |
| 127 | * |
| 128 | * \section PJ_EX_KEYWORDS Keywords |
| 129 | * |
| 130 | * \subsection PJ_THROW PJ_THROW(expression) |
| 131 | * Throw an exception. The expression thrown is an integer as the result of |
| 132 | * the \a expression. This keyword can be specified anywhere within the |
| 133 | * program. |
| 134 | * |
| 135 | * \subsection PJ_USE_EXCEPTION PJ_USE_EXCEPTION |
| 136 | * Specify this in the variable definition section of the function block |
| 137 | * (or any blocks) to specify that the block has \a PJ_TRY/PJ_CATCH exception |
| 138 | * block. |
| 139 | * Actually, this is just a macro to declare local variable which is used to |
| 140 | * push the exception state to the exception stack. |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 141 | * Note: you must specify PJ_USE_EXCEPTION as the last statement in the |
| 142 | * local variable declarations, since it may evaluate to nothing. |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 143 | * |
| 144 | * \subsection PJ_TRY PJ_TRY |
| 145 | * The \a PJ_TRY keyword is typically followed by a block. If an exception is |
| 146 | * thrown in this block, then the execution will resume to the \a PJ_CATCH |
| 147 | * handler. |
| 148 | * |
| 149 | * \subsection PJ_CATCH PJ_CATCH(expression) |
| 150 | * The \a PJ_CATCH is normally followed by a block. This block will be executed |
| 151 | * if the exception being thrown is equal to the expression specified in the |
| 152 | * \a PJ_CATCH. |
| 153 | * |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 154 | * \subsection PJ_CATCH_ANY PJ_CATCH_ANY |
| 155 | * The \a PJ_CATCH is normally followed by a block. This block will be executed |
| 156 | * if any exception was raised in the TRY block. |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 157 | * |
| 158 | * \subsection PJ_END PJ_END |
| 159 | * Specify this keyword to mark the end of \a PJ_TRY / \a PJ_CATCH blocks. |
| 160 | * |
| 161 | * \subsection PJ_GET_EXCEPTION PJ_GET_EXCEPTION(void) |
| 162 | * Get the last exception thrown. This macro is normally called inside the |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 163 | * \a PJ_CATCH or \a PJ_CATCH_ANY block, altough it can be used anywhere where |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 164 | * the \a PJ_USE_EXCEPTION definition is in scope. |
| 165 | * |
| 166 | * |
| 167 | * \section pj_except_examples_sec Examples |
| 168 | * |
| 169 | * For some examples on how to use the exception construct, please see: |
| 170 | * - @ref page_pjlib_samples_except_c |
| 171 | * - @ref page_pjlib_exception_test |
| 172 | */ |
| 173 | |
| 174 | /** |
| 175 | * Allocate a unique exception id. |
| 176 | * Applications don't have to allocate a unique exception ID before using |
| 177 | * the exception construct. However, by doing so it ensures that there is |
| 178 | * no collisions of exception ID. |
| 179 | * |
| 180 | * As a bonus, when exception number is acquired through this function, |
| 181 | * the library can assign name to the exception (only if |
| 182 | * PJ_HAS_EXCEPTION_NAMES is enabled (default is yes)) and find out the |
| 183 | * exception name when it catches an exception. |
| 184 | * |
| 185 | * @param name Name to be associated with the exception ID. |
| 186 | * @param id Pointer to receive the ID. |
| 187 | * |
| 188 | * @return PJ_SUCCESS on success or PJ_ETOOMANY if the library |
| 189 | * is running out out ids. |
| 190 | */ |
| 191 | PJ_DECL(pj_status_t) pj_exception_id_alloc(const char *name, |
| 192 | pj_exception_id_t *id); |
| 193 | |
| 194 | /** |
| 195 | * Free an exception id. |
| 196 | * |
| 197 | * @param id The exception ID. |
| 198 | * |
| 199 | * @return PJ_SUCCESS or the appropriate error code. |
| 200 | */ |
| 201 | PJ_DECL(pj_status_t) pj_exception_id_free(pj_exception_id_t id); |
| 202 | |
| 203 | /** |
| 204 | * Retrieve name associated with the exception id. |
| 205 | * |
| 206 | * @param id The exception ID. |
| 207 | * |
| 208 | * @return The name associated with the specified ID. |
| 209 | */ |
| 210 | PJ_DECL(const char*) pj_exception_id_name(pj_exception_id_t id); |
| 211 | |
| 212 | |
| 213 | /** @} */ |
| 214 | |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 215 | #if defined(PJ_EXCEPTION_USE_WIN32_SEH) && PJ_EXCEPTION_USE_WIN32_SEH != 0 |
| 216 | /***************************************************************************** |
| 217 | ** |
| 218 | ** IMPLEMENTATION OF EXCEPTION USING WINDOWS SEH |
| 219 | ** |
| 220 | ****************************************************************************/ |
| 221 | #define WIN32_LEAN_AND_MEAN |
| 222 | #include <windows.h> |
| 223 | |
| 224 | PJ_IDECL_NO_RETURN(void) |
| 225 | pj_throw_exception_(pj_exception_id_t id) PJ_ATTR_NORETURN |
| 226 | { |
| 227 | RaiseException(id,1,0,NULL); |
| 228 | } |
| 229 | |
| 230 | #define PJ_USE_EXCEPTION |
| 231 | #define PJ_TRY __try |
| 232 | #define PJ_CATCH(id) __except(GetExceptionCode()==id ? \ |
| 233 | EXCEPTION_EXECUTE_HANDLER : \ |
| 234 | EXCEPTION_CONTINUE_SEARCH) |
| 235 | #define PJ_CATCH_ANY __except(EXCEPTION_EXECUTE_HANDLER) |
| 236 | #define PJ_END |
| 237 | #define PJ_THROW(id) pj_throw_exception_(id) |
| 238 | #define PJ_GET_EXCEPTION() GetExceptionCode() |
| 239 | |
| 240 | #else |
| 241 | /***************************************************************************** |
| 242 | ** |
| 243 | ** IMPLEMENTATION OF EXCEPTION USING GENERIC SETJMP/LONGJMP |
| 244 | ** |
| 245 | ****************************************************************************/ |
| 246 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 247 | /** |
| 248 | * This structure (which should be invisible to user) manages the TRY handler |
| 249 | * stack. |
| 250 | */ |
| 251 | struct pj_exception_state_t |
| 252 | { |
| 253 | struct pj_exception_state_t *prev; /**< Previous state in the list. */ |
| 254 | pj_jmp_buf state; /**< jmp_buf. */ |
| 255 | }; |
| 256 | |
| 257 | /** |
| 258 | * Throw exception. |
| 259 | * @param id Exception Id. |
| 260 | */ |
| 261 | PJ_DECL_NO_RETURN(void) |
| 262 | pj_throw_exception_(pj_exception_id_t id) PJ_ATTR_NORETURN; |
| 263 | |
| 264 | /** |
| 265 | * Push exception handler. |
| 266 | */ |
| 267 | PJ_DECL(void) pj_push_exception_handler_(struct pj_exception_state_t *rec); |
| 268 | |
| 269 | /** |
| 270 | * Pop exception handler. |
| 271 | */ |
| 272 | PJ_DECL(void) pj_pop_exception_handler_(void); |
| 273 | |
| 274 | /** |
| 275 | * Declare that the function will use exception. |
| 276 | * @hideinitializer |
| 277 | */ |
| 278 | #define PJ_USE_EXCEPTION struct pj_exception_state_t pj_x_except__; int pj_x_code__ |
| 279 | |
| 280 | /** |
| 281 | * Start exception specification block. |
| 282 | * @hideinitializer |
| 283 | */ |
| 284 | #define PJ_TRY if (1) { \ |
| 285 | pj_push_exception_handler_(&pj_x_except__); \ |
| 286 | pj_x_code__ = pj_setjmp(pj_x_except__.state); \ |
| 287 | if (pj_x_code__ == 0) |
| 288 | /** |
| 289 | * Catch the specified exception Id. |
| 290 | * @param id The exception number to catch. |
| 291 | * @hideinitializer |
| 292 | */ |
| 293 | #define PJ_CATCH(id) else if (pj_x_code__ == (id)) |
| 294 | |
| 295 | /** |
| 296 | * Catch any exception number. |
| 297 | * @hideinitializer |
| 298 | */ |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 299 | #define PJ_CATCH_ANY else |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 300 | |
| 301 | /** |
| 302 | * End of exception specification block. |
| 303 | * @hideinitializer |
| 304 | */ |
| 305 | #define PJ_END pj_pop_exception_handler_(); \ |
| 306 | } else {} |
| 307 | |
| 308 | /** |
| 309 | * Throw exception. |
| 310 | * @param exception_id The exception number. |
| 311 | * @hideinitializer |
| 312 | */ |
| 313 | #define PJ_THROW(exception_id) pj_throw_exception_(exception_id) |
| 314 | |
| 315 | /** |
| 316 | * Get current exception. |
| 317 | * @return Current exception code. |
| 318 | * @hideinitializer |
| 319 | */ |
| 320 | #define PJ_GET_EXCEPTION() (pj_x_code__) |
| 321 | |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 322 | #endif /* PJ_EXCEPTION_USE_WIN32_SEH */ |
| 323 | |
| 324 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 325 | PJ_END_DECL |
| 326 | |
| 327 | |
| 328 | |
| 329 | #endif /* __PJ_EXCEPTION_H__ */ |
| 330 | |
| 331 | |