Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / dti_excs / Exceptions.hh
1 // $XConsortium: Exceptions.hh /main/4 1996/07/05 15:16:42 rws $
2 #ifndef _Exceptions_hh
3 #define _Exceptions_hh
4
5 #define _Exceptions_hh_active
6
7 #undef MIN
8 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
9
10 #ifndef C_API
11 #ifndef NATIVE_EXCEPTIONS
12 #define NATIVE_EXCEPTIONS
13 #endif
14 #define Exception mException
15 #endif
16
17 #ifndef NATIVE_EXCEPTIONS
18 extern "C" {
19 #include <setjmp.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 }
23 #else
24 extern "C" {
25 #include <stdio.h>
26 #include <stdlib.h>
27 }
28 #endif
29
30 #ifndef NATIVE_EXCEPTIONS
31 #ifdef EXC_DEBUG
32 #define PRINTF(MSG) printf MSG
33 #ifndef DEBUG
34 #define DEBUG
35 #endif
36 #ifndef DEBUG_THROW
37 #define DEBUG_THROW
38 #endif
39 #ifndef DEBUG_CATCH
40 #define DEBUG_CATCH
41 #endif
42 #else
43 #define PRINTF(MSG)
44 #endif
45
46 #ifndef STRINGIFY
47 #if !defined(__STDC__) && !defined(hpux)
48 #define STRINGIFY(S) "S"
49 #else
50 #define STRINGIFY(S) #S
51 #endif
52 #endif
53
54 #ifndef UNUSED_VARIABLE
55 # if defined(__GNUC__)
56 #  define UNUSED_VARIABLE(x) x __attribute__((unused))
57 # elif defined(__LCLINT__)
58 #  define UNUSED_VARIABLE(x) /*@unused@*/ x
59 # else
60 #  define UNUSED_VARIABLE(x) x
61 # endif
62 #endif
63
64 #endif /* NATIVE_EXCEPTIONS */
65
66 #include "terminate.hh"
67 #include "Destructable.hh"
68 #include "Exception.hh"
69
70 #ifdef NATIVE_EXCEPTIONS
71
72 #define INIT_EXCEPTIONS()
73
74 #define mthrow(OBJ) throw OBJ
75 #define rethrow throw
76
77 #define mtry try
78
79 #define mcatch_any() catch(...)
80 #define mcatch_noarg(OBJ) catch(OBJ)
81 #define mcatch(TYPE,OBJ) catch(TYPE OBJ)
82
83 #define end_try
84
85 #else
86
87 // This macro, which should be the first thing in main, establishes a jump
88 // environment for the context of the entire program. 
89
90 #define INIT_EXCEPTIONS() \
91   { int __stack_start; Exceptions::initialize (&__stack_start); }
92
93 // TRY MACRO 
94
95 #define mtry \
96   { \
97     Jump_Environment __jump_env; \
98     if (setjmp (__jump_env.f_env) == 0) {
99
100       
101 // THROW MACROS 
102
103 #ifdef DEBUG_THROW
104 #define DEBUG_THROW_FLAG 1
105 #else
106 #define DEBUG_THROW_FLAG 0
107 #endif
108
109 // This works if OBJ is an object or a pointer since Exception objects
110 // overload operator ->.
111 #if !defined(hpux) && !defined(USL)
112 #define mthrow(OBJ) \
113   (OBJ)->throw_it (__LINE__, __FILE__, DEBUG_THROW_FLAG)
114 #else
115 #define mthrow(OBJ) \
116   OBJ->throw_it (__LINE__, __FILE__, DEBUG_THROW_FLAG)
117 #endif
118
119 #define rethrow \
120   Exception::current_exception().do_throw (__LINE__, __FILE__)
121
122 // CATCH MACROS 
123       
124 #ifdef DEBUG_CATCH
125 #define PRINT_CATCH \
126         fprintf (stderr, "Application caught exception:\n"); \
127         fprintf (stderr, "   Thrown from file \"%s\", line %d\n", \
128                  Exception::current_exception().file(), \
129                  Exception::current_exception().line()); \
130         fprintf (stderr, "   Caught in file \"%s\", line %d.\n", \
131                  __FILE__, __LINE__);
132 #else
133 #define PRINT_CATCH
134 #endif
135
136 #define mcatch_any() \
137     } else if (1) { \
138       PRINT_CATCH
139
140 #define mcatch_noarg(OBJ) \
141     } else if (Exception::current_exception().isa (STRINGIFY(OBJ))) { \
142       PRINT_CATCH
143
144 #define mcatch(TYPE,OBJ) \
145     mcatch_noarg (TYPE) \
146     TYPE UNUSED_VARIABLE(OBJ) = (TYPE) Exception::current_exception();
147
148 #define end_try \
149     } else { \
150       rethrow; \
151     } \
152   }
153
154 class Exceptions
155 {
156 public:
157   typedef void (*error_handler_t) (const char *[], int);
158
159   static void initialize (void *ptr);
160
161   // Error handling stuff (message appear below).
162   enum error_type_t { INTERNAL_ERROR, APPLICATION_ERROR, THROW_MESSAGE };
163   static void error (const char *, error_type_t);
164   static error_handler_t set_error_handler (error_handler_t);
165
166 private:
167 friend class Destructable;
168 friend class Jump_Environment;
169 friend class Unwind_Stack;
170 friend class Exception;
171 friend void terminate();
172
173   static void check_initialized();
174
175 protected: // variables
176   // function pointer to error message handler
177   static error_handler_t     g_error_handler;
178
179   // Error types 
180   static char *f_msg_internal_error;
181   static char *f_msg_application_error;
182   static char *f_msg_throw_message;
183
184   // Usage errors. 
185   static char *f_msg_not_initialized;
186   static char *f_msg_initialized_twice;
187   static char *f_msg_not_caught;
188   static char *f_msg_no_current_exception;
189   static char *f_msg_throw_from_terminate;
190   static char *f_msg_throw_from_error_handler;
191   static char *f_msg_throw_from_destructor;
192   static char *f_msg_throw_ptr_to_stack;
193
194   // Internal memory errors. 
195   static char *f_msg_out_of_exception_memory;
196   static char *f_msg_out_of_obj_stack_memory;
197   static char *f_msg_memory_already_freed;
198
199 #ifdef C_API
200   friend void initialize_exception();
201   friend void quit_exception();
202 #endif
203 };
204
205 // includes for inline functions 
206
207 #include "Jump_Environment.hh"
208 #include "Destructable_il.hh"
209
210 #endif /* NATIVE_EXCEPTIONS */
211
212 #undef _Exceptions_hh_active
213
214 #endif /* _Exceptions_hh */
215 /* DO NOT ADD ANY LINES AFTER THIS #endif */