Exceptions.hh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. // $XConsortium: Exceptions.hh /main/4 1996/07/05 15:16:42 rws $
  24. #ifndef _Exceptions_hh
  25. #define _Exceptions_hh
  26. #define _Exceptions_hh_active
  27. #undef MIN
  28. #define MIN(x, y) (((x) < (y)) ? (x) : (y))
  29. #ifndef C_API
  30. #ifndef NATIVE_EXCEPTIONS
  31. #define NATIVE_EXCEPTIONS
  32. #endif
  33. #define Exception mException
  34. #endif
  35. #ifndef NATIVE_EXCEPTIONS
  36. extern "C" {
  37. #include <setjmp.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. }
  41. #else
  42. extern "C" {
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. }
  46. #endif
  47. #ifndef NATIVE_EXCEPTIONS
  48. #ifdef EXC_DEBUG
  49. #define PRINTF(MSG) printf MSG
  50. #ifndef DEBUG
  51. #define DEBUG
  52. #endif
  53. #ifndef DEBUG_THROW
  54. #define DEBUG_THROW
  55. #endif
  56. #ifndef DEBUG_CATCH
  57. #define DEBUG_CATCH
  58. #endif
  59. #else
  60. #define PRINTF(MSG)
  61. #endif
  62. #ifndef STRINGIFY
  63. #if !defined(__STDC__)
  64. #define STRINGIFY(S) "S"
  65. #else
  66. #define STRINGIFY(S) #S
  67. #endif
  68. #endif
  69. #ifndef UNUSED_VARIABLE
  70. # if defined(__GNUC__)
  71. # define UNUSED_VARIABLE(x) x __attribute__((unused))
  72. # elif defined(__LCLINT__)
  73. # define UNUSED_VARIABLE(x) /*@unused@*/ x
  74. # else
  75. # define UNUSED_VARIABLE(x) x
  76. # endif
  77. #endif
  78. #endif /* NATIVE_EXCEPTIONS */
  79. #include "terminate.hh"
  80. #include "Destructable.hh"
  81. #include "Exception.hh"
  82. #ifdef NATIVE_EXCEPTIONS
  83. #define INIT_EXCEPTIONS()
  84. #define mthrow(OBJ) throw OBJ
  85. #define rethrow throw
  86. #define mtry try
  87. #define mcatch_any() catch(...)
  88. #define mcatch_noarg(OBJ) catch(OBJ)
  89. #define mcatch(TYPE,OBJ) catch(TYPE OBJ)
  90. #define end_try
  91. #else
  92. // This macro, which should be the first thing in main, establishes a jump
  93. // environment for the context of the entire program.
  94. #define INIT_EXCEPTIONS() \
  95. { int __stack_start; Exceptions::initialize (&__stack_start); }
  96. // TRY MACRO
  97. #define mtry \
  98. { \
  99. Jump_Environment __jump_env; \
  100. if (setjmp (__jump_env.f_env) == 0) {
  101. // THROW MACROS
  102. #ifdef DEBUG_THROW
  103. #define DEBUG_THROW_FLAG 1
  104. #else
  105. #define DEBUG_THROW_FLAG 0
  106. #endif
  107. // This works if OBJ is an object or a pointer since Exception objects
  108. // overload operator ->.
  109. #define mthrow(OBJ) \
  110. (OBJ)->throw_it (__LINE__, __FILE__, DEBUG_THROW_FLAG)
  111. #define rethrow \
  112. Exception::current_exception().do_throw (__LINE__, __FILE__)
  113. // CATCH MACROS
  114. #ifdef DEBUG_CATCH
  115. #define PRINT_CATCH \
  116. fprintf (stderr, "Application caught exception:\n"); \
  117. fprintf (stderr, " Thrown from file \"%s\", line %d\n", \
  118. Exception::current_exception().file(), \
  119. Exception::current_exception().line()); \
  120. fprintf (stderr, " Caught in file \"%s\", line %d.\n", \
  121. __FILE__, __LINE__);
  122. #else
  123. #define PRINT_CATCH
  124. #endif
  125. #define mcatch_any() \
  126. } else if (1) { \
  127. PRINT_CATCH
  128. #define mcatch_noarg(OBJ) \
  129. } else if (Exception::current_exception().isa (STRINGIFY(OBJ))) { \
  130. PRINT_CATCH
  131. #define mcatch(TYPE,OBJ) \
  132. mcatch_noarg (TYPE) \
  133. TYPE UNUSED_VARIABLE(OBJ) = (TYPE) Exception::current_exception();
  134. #define end_try \
  135. } else { \
  136. rethrow; \
  137. } \
  138. }
  139. class Exceptions
  140. {
  141. public:
  142. typedef void (*error_handler_t) (const char *[], int);
  143. static void initialize (void *ptr);
  144. // Error handling stuff (message appear below).
  145. enum error_type_t { INTERNAL_ERROR, APPLICATION_ERROR, THROW_MESSAGE };
  146. static void error (const char *, error_type_t);
  147. static error_handler_t set_error_handler (error_handler_t);
  148. private:
  149. friend class Destructable;
  150. friend class Jump_Environment;
  151. friend class Unwind_Stack;
  152. friend class Exception;
  153. friend void terminate();
  154. static void check_initialized();
  155. protected: // variables
  156. // function pointer to error message handler
  157. static error_handler_t g_error_handler;
  158. // Error types
  159. static char *f_msg_internal_error;
  160. static char *f_msg_application_error;
  161. static char *f_msg_throw_message;
  162. // Usage errors.
  163. static char *f_msg_not_initialized;
  164. static char *f_msg_initialized_twice;
  165. static char *f_msg_not_caught;
  166. static char *f_msg_no_current_exception;
  167. static char *f_msg_throw_from_terminate;
  168. static char *f_msg_throw_from_error_handler;
  169. static char *f_msg_throw_from_destructor;
  170. static char *f_msg_throw_ptr_to_stack;
  171. // Internal memory errors.
  172. static char *f_msg_out_of_exception_memory;
  173. static char *f_msg_out_of_obj_stack_memory;
  174. static char *f_msg_memory_already_freed;
  175. #ifdef C_API
  176. friend void initialize_exception();
  177. friend void quit_exception();
  178. #endif
  179. };
  180. // includes for inline functions
  181. #include "Jump_Environment.hh"
  182. #include "Destructable_il.hh"
  183. #endif /* NATIVE_EXCEPTIONS */
  184. #undef _Exceptions_hh_active
  185. #endif /* _Exceptions_hh */
  186. /* DO NOT ADD ANY LINES AFTER THIS #endif */