Doc fixes.
[bertos.git] / mware / except.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
31  *
32  * -->
33  *
34  * \brief C++-like structured exception handling for C programs
35  *
36  * \version $Id$
37  * \author Bernardo Innocenti <bernie@develer.com>
38  */
39 #ifndef MWARE_EXCEPT_H
40 #define MWARE_EXCEPT_H
41
42 #include <cfg/debug.h>
43
44 #include <setjmp.h>
45
46 #define EXCEPT_CONTEXTS 8
47
48 /**
49  * A stack of jump buffers used to record try sites
50  * so they can be reached from throw sites.
51  *
52  * The stack contains return points for each nested
53  * context. jmp_buf's are pushed into the stack at
54  * try points and popped out when the try block ends
55  * normally or when an exception is thrown.
56  */
57 extern jmp_buf except_stack[EXCEPT_CONTEXTS];
58 extern int except_top;
59
60 #define PUSH_EXCEPT     (ASSERT(except_top < EXCEPT_CONTEXTS), setjmp(except_stack[except_top++]))
61 #define POP_EXCEPT      (ASSERT(except_top > 0), --except_top)
62 #define DO_EXCEPT       (ASSERT(except_top > 0), longjmp(except_stack[--except_top], true))
63
64 /**
65  * Jump buffer to use when throwing an exception or aborting an operation
66  *
67  * User code can throw exceptions like this:
68  *
69  * \code
70  *   void a_function_throwing_exceptions(void)
71  *   {
72  *       if (some_error_condition)
73  *          THROW;
74  *   }
75  * \endcode
76  *
77  * Catching exceptions (brackets are optional):
78  *
79  * \code
80  *    EXCEPT_DEFINE;
81  *
82  *    void a_function_catching_an_exception(void)
83  *    {
84  *        TRY
85  *        {
86  *            printf("Entered try block\n");
87  *            a_function_throwing_exceptions();
88  *            printf("Survived execution of critical code\n");
89  *        }
90  *        CATCH
91  *        {
92  *            printf("Exception caught!\n");
93  *        }
94  *        CATCH_END
95  *    }
96  * \endcode
97  *
98  * Simple syntax when you don't need to do anything when catching an excaption:
99  *
100  * \code
101  *    TRY
102  *        printf("Entered try block\n");
103  *        a_function_throwing_exceptions();
104  *        printf("Survived execution of critical code\n");
105  *    TRY_END
106  * \endcode
107  *
108  * You also need to declare the exception stack once in
109  * your global declarations:
110  * \code
111  *    EXCEPT_DEFINE;
112  * \endcode
113  */
114 #define TRY          if (PUSH_EXCEPT) { {
115 #define TRY_END      } POP_EXCEPT; }
116 #define CATCH        } POP_EXCEPT; } else {
117 #define CATCH_END    }
118 #define THROW        DO_EXCEPT
119
120
121 #define EXCEPT_DEFINE \
122         jmp_buf except_stack[EXCEPT_CONTEXTS]; \
123         int except_top;
124
125 #endif /* MWARE_EXCEPT_H */