Move unpack lwip ip address macro to macros module.
[bertos.git] / bertos / 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 Bernie Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \brief C++-like structured exception handling for C programs
35  *
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  */
38 #ifndef MWARE_EXCEPT_H
39 #define MWARE_EXCEPT_H
40
41 #include <cfg/debug.h>
42
43 #include <setjmp.h>
44
45 #define EXCEPT_CONTEXTS 8
46
47 /**
48  * A stack of jump buffers used to record try sites
49  * so they can be reached from throw sites.
50  *
51  * The stack contains return points for each nested
52  * context. jmp_buf's are pushed into the stack at
53  * try points and popped out when the try block ends
54  * normally or when an exception is thrown.
55  */
56 extern jmp_buf except_stack[EXCEPT_CONTEXTS];
57 extern int except_top;
58
59 #define PUSH_EXCEPT     (ASSERT(except_top < EXCEPT_CONTEXTS), setjmp(except_stack[except_top++]))
60 #define POP_EXCEPT      (ASSERT(except_top > 0), --except_top)
61 #define DO_EXCEPT       (ASSERT(except_top > 0), longjmp(except_stack[--except_top], true))
62
63 /**
64  * Jump buffer to use when throwing an exception or aborting an operation
65  *
66  * User code can throw exceptions like this:
67  *
68  * \code
69  *   void a_function_throwing_exceptions(void)
70  *   {
71  *       if (some_error_condition)
72  *          THROW;
73  *   }
74  * \endcode
75  *
76  * Catching exceptions (brackets are optional):
77  *
78  * \code
79  *    EXCEPT_DEFINE;
80  *
81  *    void a_function_catching_an_exception(void)
82  *    {
83  *        TRY
84  *        {
85  *            printf("Entered try block\n");
86  *            a_function_throwing_exceptions();
87  *            printf("Survived execution of critical code\n");
88  *        }
89  *        CATCH
90  *        {
91  *            printf("Exception caught!\n");
92  *        }
93  *        CATCH_END
94  *    }
95  * \endcode
96  *
97  * Simple syntax when you don't need to do anything when catching an excaption:
98  *
99  * \code
100  *    TRY
101  *        printf("Entered try block\n");
102  *        a_function_throwing_exceptions();
103  *        printf("Survived execution of critical code\n");
104  *    TRY_END
105  * \endcode
106  *
107  * You also need to declare the exception stack once in
108  * your global declarations:
109  * \code
110  *    EXCEPT_DEFINE;
111  * \endcode
112  */
113 #define TRY          if (PUSH_EXCEPT) { {
114 #define TRY_END      } POP_EXCEPT; }
115 #define CATCH        } POP_EXCEPT; } else {
116 #define CATCH_END    }
117 #define THROW        DO_EXCEPT
118
119
120 #define EXCEPT_DEFINE \
121         jmp_buf except_stack[EXCEPT_CONTEXTS]; \
122         int except_top;
123
124 #endif /* MWARE_EXCEPT_H */