Debian Codegraph @

Xpjproject/pjlib/include/pj/except.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 #include <pj/log.h>
30
31
32 PJ_BEGIN_DECL
33
34
35 /**
36 * @defgroup PJ_EXCEPT Exception Handling
37 * @ingroup PJ_MISC
38 * @{
39 *
40 * \section pj_except_sample_sec Quick Example
41 *
42 * For the impatient, take a look at some examples:
43 * - Exception Handling sample: \src{pjlib/src/pjlib-samples/except.c}
44 * - Exception Handling test: \src{pjlib/src/pjlib-test/exception.c}
45 *
46 * \section pj_except_except Exception Handling
47 *
48 * This module provides exception handling syntactically similar to C++ in
49 * C language. In Win32 systems, it uses Windows Structured Exception
50 * Handling (SEH) if macro PJ_EXCEPTION_USE_WIN32_SEH is non-zero.
51 * Otherwise it will use setjmp() and longjmp().
52 *
53 * On some platforms where setjmp/longjmp is not available, setjmp/longjmp
54 * implementation is provided. See <pj/compat/setjmp.h> for compatibility.
55 *
56 * The exception handling mechanism is completely thread safe, so the exception
57 * thrown by one thread will not interfere with other thread.
58 *
59 * The exception handling constructs are similar to C++. The blocks will be
60 * constructed similar to the following sample:
61 *
62 * \verbatim
63 #define NO_MEMORY 1
64 #define SYNTAX_ERROR 2
65
66 int sample1()
67 {
68 PJ_USE_EXCEPTION; // declare local exception stack.
69
70 PJ_TRY {
71 ...// do something..
72 }
73 PJ_CATCH(NO_MEMORY) {
74 ... // handle exception 1
75 }
76 PJ_END;
77 }
78
79 int sample2()
80 {
81 PJ_USE_EXCEPTION; // declare local exception stack.
82
83 PJ_TRY {
84 ...// do something..
85 }
86 PJ_CATCH_ANY {
87 if (PJ_GET_EXCEPTION() == NO_MEMORY)
88 ...; // handle no memory situation
89 else if (PJ_GET_EXCEPTION() == SYNTAX_ERROR)
90 ...; // handle syntax error
91 }
92 PJ_END;
93 }
94 \endverbatim
95 *
96 * The above sample uses hard coded exception ID. It is @b strongly
97 * recommended that applications request a unique exception ID instead
98 * of hard coded value like above.
99 *
100 * \section pj_except_reg Exception ID Allocation
101 *
102 * To ensure that exception ID (number) are used consistently and to
103 * prevent ID collisions in an application, it is strongly suggested that
104 * applications allocate an exception ID for each possible exception
105 * type. As a bonus of this process, the application can identify
106 * the name of the exception when the particular exception is thrown.
107 *
108 * Exception ID management are performed with the following APIs:
109 * - #pj_exception_id_alloc().
110 * - #pj_exception_id_free().
111 * - #pj_exception_id_name().
112 *
113 *
114 * PJLIB itself automatically allocates one exception id, i.e.
115 * #PJ_NO_MEMORY_EXCEPTION which is declared in <pj/pool.h>. This exception
116 * ID is raised by default pool policy when it fails to allocate memory.
117 *
118 * CAVEATS:
119 * - unlike C++ exception, the scheme here won't call destructors of local
120 * objects if exception is thrown. Care must be taken when a function
121 * hold some resorce such as pool or mutex etc.
122 * - You CAN NOT make nested exception in one single function without using
123 * a nested PJ_USE_EXCEPTION. Samples:
124 \verbatim
125 void wrong_sample()
126 {
127 PJ_USE_EXCEPTION;
128
129 PJ_TRY {
130 // Do stuffs
131 ...
132 }
133 PJ_CATCH_ANY {
134 // Do other stuffs
135 ....
136 ..
137
138 // The following block is WRONG! You MUST declare
139 // PJ_USE_EXCEPTION once again in this block.
140 PJ_TRY {
141 ..
142 }
143 PJ_CATCH_ANY {
144 ..
145 }
146 PJ_END;
147 }
148 PJ_END;
149 }
150
151 \endverbatim
152
153 * - You MUST NOT exit the function inside the PJ_TRY block. The correct way
154 * is to return from the function after PJ_END block is executed.
155 * For example, the following code will yield crash not in this code,
156 * but rather in the subsequent execution of PJ_TRY block:
157 \verbatim
158 void wrong_sample()
159 {
160 PJ_USE_EXCEPTION;
161
162 PJ_TRY {
163 // do some stuffs
164 ...
165 return; <======= DO NOT DO THIS!
166 }
167 PJ_CATCH_ANY {
168 }
169 PJ_END;
170 }
171 \endverbatim
172
173 * - You can not provide more than PJ_CATCH or PJ_CATCH_ANY nor use PJ_CATCH
174 * and PJ_CATCH_ANY for a single PJ_TRY.
175 * - Exceptions will always be caught by the first handler (unlike C++ where
176 * exception is only caught if the type matches.
177
178 * \section PJ_EX_KEYWORDS Keywords
179 *
180 * \subsection PJ_THROW PJ_THROW(expression)
181 * Throw an exception. The expression thrown is an integer as the result of
182 * the \a expression. This keyword can be specified anywhere within the
183 * program.
184 *
185 * \subsection PJ_USE_EXCEPTION PJ_USE_EXCEPTION
186 * Specify this in the variable definition section of the function block
187 * (or any blocks) to specify that the block has \a PJ_TRY/PJ_CATCH exception
188 * block.
189 * Actually, this is just a macro to declare local variable which is used to
190 * push the exception state to the exception stack.
191 * Note: you must specify PJ_USE_EXCEPTION as the last statement in the
192 * local variable declarations, since it may evaluate to nothing.
193 *
194 * \subsection PJ_TRY PJ_TRY
195 * The \a PJ_TRY keyword is typically followed by a block. If an exception is
196 * thrown in this block, then the execution will resume to the \a PJ_CATCH
197 * handler.
198 *
199 * \subsection PJ_CATCH PJ_CATCH(expression)
200 * The \a PJ_CATCH is normally followed by a block. This block will be executed
201 * if the exception being thrown is equal to the expression specified in the
202 * \a PJ_CATCH.
203 *
204 * \subsection PJ_CATCH_ANY PJ_CATCH_ANY
205 * The \a PJ_CATCH is normally followed by a block. This block will be executed
206 * if any exception was raised in the TRY block.
207 *
208 * \subsection PJ_END PJ_END
209 * Specify this keyword to mark the end of \a PJ_TRY / \a PJ_CATCH blocks.
210 *
211 * \subsection PJ_GET_EXCEPTION PJ_GET_EXCEPTION(void)
212 * Get the last exception thrown. This macro is normally called inside the
213 * \a PJ_CATCH or \a PJ_CATCH_ANY block, altough it can be used anywhere where
214 * the \a PJ_USE_EXCEPTION definition is in scope.
215 *
216 *
217 * \section pj_except_examples_sec Examples
218 *
219 * For some examples on how to use the exception construct, please see:
220 * - Exception Handling sample: \src{pjlib/src/pjlib-samples/except.c}
221 * - Exception Handling test: \src{pjlib/src/pjlib-test/exception.c}
222 */
223
224 /**
225 * Allocate a unique exception id.
226 * Applications don't have to allocate a unique exception ID before using
227 * the exception construct. However, by doing so it ensures that there is
228 * no collisions of exception ID.
229 *
230 * As a bonus, when exception number is acquired through this function,
231 * the library can assign name to the exception (only if
232 * PJ_HAS_EXCEPTION_NAMES is enabled (default is yes)) and find out the
233 * exception name when it catches an exception.
234 *
235 * @param name Name to be associated with the exception ID.
236 * @param id Pointer to receive the ID.
237 *
238 * @return PJ_SUCCESS on success or PJ_ETOOMANY if the library
239 * is running out out ids.
240 */
241 PJ_DECL(pj_status_t) pj_exception_id_alloc(const char *name,
242 pj_exception_id_t *id);
243
244 /**
245 * Free an exception id.
246 *
247 * @param id The exception ID.
248 *
249 * @return PJ_SUCCESS or the appropriate error code.
250 */
251 PJ_DECL(pj_status_t) pj_exception_id_free(pj_exception_id_t id);
252
253 /**
254 * Retrieve name associated with the exception id.
255 *
256 * @param id The exception ID.
257 *
258 * @return The name associated with the specified ID.
259 */
260 PJ_DECL(const char*) pj_exception_id_name(pj_exception_id_t id);
261
262
263 /** @} */
264
265 #if defined(PJ_EXCEPTION_USE_WIN32_SEH) && PJ_EXCEPTION_USE_WIN32_SEH != 0
266 /*****************************************************************************
267 **
268 ** IMPLEMENTATION OF EXCEPTION USING WINDOWS SEH
269 **
270 ****************************************************************************/
271 #define WIN32_LEAN_AND_MEAN
272 #include <windows.h>
273
274 PJ_IDECL_NO_RETURN(void)
275 pj_throw_exception_(pj_exception_id_t id) PJ_ATTR_NORETURN
276 {
277 RaiseException(id,1,0,NULL);
278 }
279
280 #define PJ_USE_EXCEPTION
281 #define PJ_TRY __try
282 #define PJ_CATCH(id) __except(GetExceptionCode()==id ? \
283 EXCEPTION_EXECUTE_HANDLER : \
284 EXCEPTION_CONTINUE_SEARCH)
285 #define PJ_CATCH_ANY __except(EXCEPTION_EXECUTE_HANDLER)
286 #define PJ_END
287 #define PJ_THROW(id) pj_throw_exception_(id)
288 #define PJ_GET_EXCEPTION() GetExceptionCode()
289
290
291 #elif defined(PJ_SYMBIAN) && PJ_SYMBIAN!=0
292 /*****************************************************************************
293 **
294 ** IMPLEMENTATION OF EXCEPTION USING SYMBIAN LEAVE/TRAP FRAMEWORK
295 **
296 ****************************************************************************/
297
298 /* To include this file, the source file must be compiled as
299 * C++ code!
300 */
301 #ifdef __cplusplus
302
303 class TPjException
304 {
305 public:
306 int code_;
307 };
308
309 #define PJ_USE_EXCEPTION
310 #define PJ_TRY try
311 //#define PJ_CATCH(id)
312 #define PJ_CATCH_ANY catch (const TPjException & pj_excp_)
313 #define PJ_END
314 #define PJ_THROW(x_id) do { TPjException e; e.code_=x_id; throw e;} \
315 while (0)
316 #define PJ_GET_EXCEPTION() pj_excp_.code_
317
318 #else
319
320 #define PJ_USE_EXCEPTION
321 #define PJ_TRY
322 #define PJ_CATCH_ANY if (0)
323 #define PJ_END
324 #define PJ_THROW(x_id) do { PJ_LOG(1,("PJ_THROW"," error code = %d",x_id)); } while (0)
325 #define PJ_GET_EXCEPTION() 0
326
327 #endif /* __cplusplus */
328
329 #else
330 /*****************************************************************************
331 **
332 ** IMPLEMENTATION OF EXCEPTION USING GENERIC SETJMP/LONGJMP
333 **
334 ****************************************************************************/
335
336 /**
337 * This structure (which should be invisible to user) manages the TRY handler
338 * stack.
339 */
340 struct pj_exception_state_t
341 {
342 pj_jmp_buf state; /**< jmp_buf. */
343 struct pj_exception_state_t *prev; /**< Previous state in the list. */
344 };
345
346 /**
347 * Throw exception.
348 * @param id Exception Id.
349 */
350 PJ_DECL_NO_RETURN(void)
351 pj_throw_exception_(pj_exception_id_t id) PJ_ATTR_NORETURN;
352
353 /**
354 * Push exception handler.
355 */
356 PJ_DECL(void) pj_push_exception_handler_(struct pj_exception_state_t *rec);
357
358 /**
359 * Pop exception handler.
360 */
361 PJ_DECL(void) pj_pop_exception_handler_(struct pj_exception_state_t *rec);
362
363 /**
364 * Declare that the function will use exception.
365 * @hideinitializer
366 */
367 #define PJ_USE_EXCEPTION struct pj_exception_state_t pj_x_except__; int pj_x_code__
368
369 /**
370 * Start exception specification block.
371 * @hideinitializer
372 */
373 #define PJ_TRY if (1) { \
374 pj_push_exception_handler_(&pj_x_except__); \
375 pj_x_code__ = pj_setjmp(pj_x_except__.state); \
376 if (pj_x_code__ == 0)
377 /**
378 * Catch the specified exception Id.
379 * @param id The exception number to catch.
380 * @hideinitializer
381 */
382 #define PJ_CATCH(id) else if (pj_x_code__ == (id))
383
384 /**
385 * Catch any exception number.
386 * @hideinitializer
387 */
388 #define PJ_CATCH_ANY else
389
390 /**
391 * End of exception specification block.
392 * @hideinitializer
393 */
394 #define PJ_END pj_pop_exception_handler_(&pj_x_except__); \
395 } else {}
396
397 /**
398 * Throw exception.
399 * @param exception_id The exception number.
400 * @hideinitializer
401 */
402 #define PJ_THROW(exception_id) pj_throw_exception_(exception_id)
403
404 /**
405 * Get current exception.
406 * @return Current exception code.
407 * @hideinitializer
408 */
409 #define PJ_GET_EXCEPTION() (pj_x_code__)
410
411 #endif /* PJ_EXCEPTION_USE_WIN32_SEH */
412
413
414 PJ_END_DECL
415
416
417
418 #endif /* __PJ_EXCEPTION_H__ */
419
420