home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / IBMCLASS / IEXCBASE.HPP < prev    next >
Text File  |  1993-09-22  |  29KB  |  486 lines

  1. #ifndef _IEXCBASE_
  2. #define _IEXCBASE_
  3. /*******************************************************************************
  4. * FILE NAME: iexcbase.hpp                                                      *
  5. *                                                                              *
  6. * DESCRIPTION:                                                                 *
  7. *   Declaration of the class(es):                                              *
  8. *     IExceptionLocation   - Holds location information for thrown exceptions. *
  9. *     IException           - Base class for all exceptions in the library.     *
  10. *     IAccessError         - For logical errors, e.g. "resource not found".    *
  11. *     IAssertionFailure    - Generated by the IASSERT macro for assertions.    *
  12. *     IDeviceError         - Used for hardware related errors.                 *
  13. *     IInvalidParameter    - For null pointer, invalid value type errors.      *
  14. *     IInvalidRequest      - For invalid requests for an object's state.       *
  15. *     IResourceExhausted   - Generic out-of-resource class.                    *
  16. *     IOutOfMemory         - Generated by our new_handler for heap exhaustion. *
  17. *     IOutOfSystemResource - For operating system resource exhaustion.         *
  18. *     IOutOfWindowResource - For windowing system resource exhaustion.         *
  19. *                                                                              *
  20. *   This file also contains many of the macros used to implement the           *
  21. *   library exception handling mechanism.  This includes the IASSERT, ITHROW,  *
  22. *   IRETHROW, IEXCEPTION_LOCATION, IEXCLASSDECLARE, IEXCLASSIMPLEMENT, and     *
  23. *   and INO_EXCEPTIONS_SUPPORT macros.                                         *
  24. *                                                                              *
  25. * COPYRIGHT:                                                                   *
  26. *   IBM C/C++ Tools Version 2.01 - Collection Class Library                    *
  27. *   Licensed Materials - Property of IBM                                       *
  28. *   (C) Copyright IBM Corporation 1992, 1993                                   *
  29. *   All Rights Reserved                                                        *
  30. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  31. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  32. *                                                                              *
  33. *******************************************************************************/
  34. #ifndef _IMSGTEXT_
  35.   #include <imsgtext.hpp>
  36. #endif
  37.  
  38. // Forward declarations for other classes:
  39. class IExcText;
  40.  
  41. class IExceptionLocation {
  42. /*******************************************************************************
  43. *                                                                              *
  44. * The IExceptionLocation class saves the location information when             *
  45. * an exception is thrown or re-thrown.  No exceptions are thrown by            *
  46. * any function in this class, because an exception probably has                *
  47. * been thrown already, or is about to be thrown.                               *
  48. *                                                                              *
  49. * Typically, an instance of the IExceptionLocation class is created            *
  50. * by either the ITHROW or IRETHROW macro when an exception is going            *
  51. * to be thrown or re-thrown. However, you can create your own instance         *
  52. * of IExceptionLocation by constructing it yourself or by using the            *
  53. * IEXCEPTION_LOCATION macro.                                                   *
  54. *                                                                              *
  55. * Example:                                                                     *
  56. *                                                                              *
  57. * The following example shows how you could use this class if you decided      *
  58. * to use the IEXCEPTION_LOCATION macro instead of the ITHROW and               *
  59. * IRETHROW macros:                                                             *
  60. *                                                                              *
  61. *   IInvalidRequest exc = IInvalidRequest("Func B invalid before Func A");     *
  62. *   exc.addLocation(IEXCEPTION_LOCATION());                                    *
  63. *   exc.logExceptionData();        // to log the exception information         *
  64. *   throw exc;                                                                 *
  65. *                                                                              *
  66. *******************************************************************************/
  67. public:
  68. /*------------------------ Constructors/Destructors ----------------------------
  69. | There is one way to construct instances of this class:                       |
  70. |                                                                              |
  71. |     The constructor takes three arguments:                                   |
  72. |                                                                              |
  73. |     - The filename where the function creating this instance is              |
  74. |       located                                                                |
  75. |     - The name of the function in which the instance is being                |
  76. |       created                                                                |
  77. |     - The line number in the file in which the instance is being             |
  78. |       created.                                                               |
  79. |                                                                              |
  80. |     The easiest way to construct an IExceptionLocation instance              |
  81. |     is by using the IEXCEPTION_LOCATION macro, which captures                |
  82. |     current location information using constants that are                    |
  83. |     provided by the compiler for all of the arguments.  Default              |
  84. |     values are provided for all arguments to support environments            |
  85. |     in which all constants or alternative means for getting                  |
  86. |     location information are not provided.                                   |
  87. |                                                                              |
  88. |     You can also use the ITHROW or IRETHROW macros to create an              |
  89. |     instance, as they will invoke the IEXCEPTION_LOCATION macro.             |
  90. ------------------------------------------------------------------------------*/
  91.   IExceptionLocation ( const char* fileName = 0,
  92.                        const char* functionName = 0,
  93.                        unsigned long lineNumber = 0 );
  94.  
  95. /*-------------------------------- Accessors -----------------------------------
  96. | The following functions provide a means of getting and setting               |
  97. | the accessible attributes of instances of this class:                        |
  98. |   fileName     - Returns the path-qualified source file name where an        |
  99. |                  exception has been thrown or re-thrown.                     |
  100. |   functionName - Returns the name of the function that has thrown            |
  101. |                  or re-thrown an exception.                                  |
  102. |   lineNumber   - Returns the line number of the statement in the             |
  103. |                  source file from which an exception has been                |
  104. |                  thrown or re-thrown.                                        |
  105. ------------------------------------------------------------------------------*/
  106. const char
  107.  *fileName     ( ) const,
  108.  *functionName ( ) const;
  109.  
  110. unsigned long
  111.   lineNumber ( ) const;
  112.  
  113. private:
  114. /*--------------------------------- Private ----------------------------------*/
  115. const char
  116.  *pClFile,
  117.  *pClFunction;
  118. unsigned long
  119.   ulClLineNo;
  120.  
  121. }; // IExceptionLocation
  122.  
  123. class IException {
  124. /*******************************************************************************
  125. *                                                                              *
  126. *   This is the base class from which all exceptions thrown in the             *
  127. *   library are derived.  No exceptions are thrown by any function in          *
  128. *   this class, because one probably has been thrown already, or is            *
  129. *   about to be thrown.                                                        *
  130. *                                                                              *
  131. *   Instances of IException or one of its subclasses are created for           *
  132. *   all error conditions encountered in the library.  Each instance            *
  133. *   contains a "stack" of exception text strings.  IException provides         *
  134. *   all of the functions required for it and its subclasses, including         *
  135. *   functions that operate on the text strings in the "stack".                 *
  136. *                                                                              *
  137. *   The subclasses are defined so that exceptions can be caught by             *
  138. *   their type, as desired.  In general, an instance of IException             *
  139. *   should never be created. Instead, an instance of the appropriate           *
  140. *   subclass should be created and thrown.                                     *
  141. *                                                                              *
  142. *   The subclasses of IException are:                                          *
  143. *                                                                              *
  144. *   - IAccessError                                                             *
  145. *   - IAssertionFailure                                                        *
  146. *   - IDeviceError                                                             *
  147. *   - IInvalidParameter                                                        *
  148. *   - IInvalidRequest                                                          *
  149. *   - IResourceExhausted                                                       *
  150. *                                                                              *
  151. *   In addition, IResourceExhausted has the following subclasses:              *
  152. *                                                                              *
  153. *   - IOutOfMemory                                                             *
  154. *   - IOutOfSystemResource                                                     *
  155. *   - IOutOfWindowResource                                                     *
  156. *                                                                              *
  157. *   The following macros are provided to assist in using exception             *
  158. *   handling:                                                                  *
  159. *                                                                              *
  160. *   ITHROW                 - Accepts as input a pre-defined instance           *
  161. *                            of any IException subclass                        *
  162. *                            It expands to add the location                    *
  163. *                            information to the instance, logs all             *
  164. *                            instance data, and then throws the                *
  165. *                            exception.                                        *
  166. *                                                                              *
  167. *   IRETHROW               - Accepts as input a pre-defined instance           *
  168. *                            of any subclass of IException that has            *
  169. *                            been previously thrown and caught.                *
  170. *                            Like the ITHROW macro, it also captures           *
  171. *                            the location information, and logs all            *
  172. *                            instance data before re-throwing the              *
  173. *                            exception.                                        *
  174. *                                                                              *
  175. *   IASSERT                - Expands to provide assertion support in           *
  176. *                            the library, but only if IC_DEVELOP has           *
  177. *                            been defined.  This macro accepts an              *
  178. *                            expression to be tested as input.  If             *
  179. *                            the test evaluation returns false, an             *
  180. *                            exception of type IAssertionFailure is            *
  181. *                            thrown.                                           *
  182. *                                                                              *
  183. *   IEXCLASSDECLARE        - Creates a declaration for a subclass of           *
  184. *                            IException or one of its subclasses.              *
  185. *                                                                              *
  186. *   IEXCLASSIMPLEMENT      - Creates a definition for a subclass of            *
  187. *                            IException or one of its subclasses.              *
  188. *                                                                              *
  189. *   IEXCEPTION_LOCATION    - Expands to create an instance of the              *
  190. *                            IExceptionLocation class.                         *
  191. *                                                                              *
  192. *   INO_EXCEPTIONS_SUPPORT - Provided in support of compilers that             *
  193. *                            lack exception handling implementation.           *
  194. *                            If it is defined, the ITHROW macro ends           *
  195. *                            the program after capturing the location          *
  196. *                            information and logging it, instead of            *
  197. *                            throwing an exception.  This macro may            *
  198. *                            not work correctly on all compilers.              *
  199. *                                                                              *
  200. *   Example:                                                                   *
  201. *                                                                              *
  202. *   Following is an example of creating and throwing an exception.             *
  203. *   It creates an instance of InvalidRequest, one of IException's              *
  204. *   subclasses.                                                                *
  205. *                                                                              *
  206. *     IInvalidRequest exc = IInvalidRequest("Func B invalid before Func A");   *
  207. *     exc.addLocation(IEXCEPTION_LOCATION());                                  *
  208. *     exc.logExceptionData();        // to log the exception information       *
  209. *     throw exc;                                                               *
  210. *                                                                              *
  211. *   The following example is a variation of the preceding                      *
  212. *   example, this time using the ITHROW macro.                                 *
  213. *                                                                              *
  214. *     IInvalidRequest exc = IInvalidRequest("Func B invalid before Func A");   *
  215. *     ITHROW(exc);                                                             *
  216. *                                                                              *
  217. *******************************************************************************/
  218. public:
  219. /*------------------------------ Related Type ----------------------------------
  220. | The following enumeration type is defined to pass "modes" to various         |
  221. | implementation functions:                                                    |
  222. |   Severity         - Used to determine the severity of the exception.        |
  223. |    unrecoverable - The exception is classified as unrecoverable.             |
  224. |    recoverable   - The exception is classified as recoverable.               |
  225. ------------------------------------------------------------------------------*/
  226. enum Severity { unrecoverable, recoverable };
  227.  
  228. /*------------------------ Constructors/Destructors ----------------------------
  229. | There are two ways to construct instances of this class:                     |
  230. |                                                                              |
  231. |  1. The primary constructor                                                  |
  232. |                                                                              |
  233. |     This is normally the only way an instance of this class will             |
  234. |     be constructed.  The only required argument is the exception             |
  235. |     text string.  The third argument is the severity of the                  |
  236. |     exception.                                                               |
  237. |                                                                              |
  238. |  2. The copy constructor                                                     |
  239. |                                                                              |
  240. |     This constructor is provided so the compiler can copy the                |
  241. |     exception when it is thrown.  This should be the only reason             |
  242. |     to copy an exception instance.                                           |
  243. ------------------------------------------------------------------------------*/
  244.   IException  ( const char*       errorText,
  245.                 unsigned long     errorId = 0,
  246.                 Severity          severity = IException::unrecoverable );
  247.   IException  ( const IException& exception );
  248.  
  249. virtual
  250.   ~IException ( );
  251.  
  252. class TraceFn {
  253. /*******************************************************************************
  254. | This class is used by instances of IException and its subclasses             |
  255. | to log exception instance data.                                              |
  256. |                                                                              |
  257. | If you want to provide your own tracing function, you should                 |
  258. | subclass the IException::TraceFn class and register it with                  |
  259. | IException by using the 'setTraceFunction' function.  This will              |
  260. | allow you to implement IException::TraceFn's 'write' function,               |
  261. | which is called during exception processing.  An instance of a               |
  262. | default subclass of IException::TraceFn is used if the application           |
  263. | does not register one.  This default function writes output to wherever      |
  264. | ITrace output is being directed based on the ICLUI TRACETO environment       |
  265. | variable.                                                                    |
  266. |                                                                              |
  267. | Example:                                                                     |
  268. |                                                                              |
  269. | class ExampleTraceFn : public IException::TraceFn                            |
  270. | {                                                                            |
  271. |  public:                                                                     |
  272. |  virtual void                                                                |
  273. |    write ( const char* buffer )                                              |
  274. |    {                                                                         |
  275. |       fprintf(stderr,"%s\n", buffer);                                        |
  276. |    }                                                                         |
  277. | };                                                                           |
  278. |                                                                              |
  279. | ExampleTraceFn exampleTraceFn = new ExampleTraceFn;                          |
  280. | IException::setTraceFunction(exampleTraceFn);                                |
  281. *******************************************************************************/
  282. public:
  283. /*------------------------ Constructors/Destructors ----------------------------
  284. | The only way to construct members of this class is to use                    |
  285. | the default constructor with no arguments.                                   |
  286. ------------------------------------------------------------------------------*/
  287.  
  288. /*---------------------------------- Trace -------------------------------------
  289. | The following function is used by IException's 'logExceptionData'            |
  290. | function:                                                                    |
  291. |   write - Writes a buffer of exception data.                                 |
  292. ------------------------------------------------------------------------------*/
  293. virtual void
  294.   write ( const char* buffer ) = 0;
  295.  
  296. }; // TraceFn
  297.  
  298. /*-------------------------------- Accessors -----------------------------------
  299. | The following functions provide a means of getting and setting               |
  300. | the accessible attributes of instances of this class:                        |
  301. |   appendText      - Appends text to the string that is on the top            |
  302. |                     of the exception text "stack".                           |
  303. |   setText         - Adds a text string to the top of the exception text      |
  304. |                     "stack".                                                 |
  305. |   setSeverity     - Sets the severity of the exception.                      |
  306. |   setErrorId      - Sets the error ID of the exception.                      |
  307. |   addLocation     - Adds location information regarding where an exception   |
  308. |                     is thrown or re-thrown to an array of                    |
  309. |                     IExceptionLocation instances.  Only 5 locations will be  |
  310. |                     stored in the array.  After the array is full, each      |
  311. |                     location that is added will replace the fifth member of  |
  312. |                     the array.                                               |
  313. |   isRecoverable   - Returns 1 if the Severity is recoverable or 0 if the     |
  314. |                     Severity is unrecoverable.                               |
  315. |   name            - Returns the class name (type) of the exception.          |
  316. |   text            - Returns an exception text string from the "stack" of     |
  317. |                     text strings.  Index 0, which is the top of the stack,   |
  318. |                     is the default.                                          |
  319. |   textCount       - Returns the number of exception text strings in the      |
  320. |                     exception text "stack".                                  |
  321. |   errorId         - Returns the error ID of the exception.                   |
  322. |   locationCount   - Returns the number of IExceptionLocation instances       |
  323. |                     stored in the location array.                            |
  324. |   locationAtIndex - Returns a pointer to the IExceptionLocation instance     |
  325. |                     stored at the zero-based index requested.  If            |
  326. |                     the index value is invalid, a zero pointer is            |
  327. |                     returned.                                                |
  328. ------------------------------------------------------------------------------*/
  329. IException
  330.  &appendText  ( const char*   errorText ),
  331.  &setText     ( const char*   errorText ),
  332.  &setSeverity ( Severity      severity ),
  333.  &setErrorId  ( unsigned long errorId );
  334.  
  335. virtual IException
  336.  &addLocation      ( const IExceptionLocation& location );
  337.  
  338. virtual int
  339.  isRecoverable ( ) const;
  340.  
  341. virtual const char
  342.  *name ( ) const;
  343.  
  344. const char
  345.  *text ( unsigned long indexFromTop = 0 ) const;
  346.  
  347. unsigned long
  348.   textCount     ( ) const,
  349.   errorId       ( ) const,
  350.   locationCount ( ) const;
  351.  
  352. const IExceptionLocation
  353.  *locationAtIndex ( unsigned long locationIndex ) const;
  354.  
  355. /*---------------------------- Logging Functions -------------------------------
  356. | The following functions are provided for logging the exception               |
  357. | information:                                                                 |
  358. |   setTraceFunction - Registers an IException::TraceFn object to be used to   |
  359. |                      log exception data. The object's 'write' function is    |
  360. |                      called during exception processing to write the data.   |
  361. |                      If no object is provided, data is written to            |
  362. |                      standard error output.                                  |
  363. |   logExceptionData - Logs exception instance data.  If a trace function has  |
  364. |                      been registered by using the 'setTraceFunction'         |
  365. |                      function, it is used to do the logging.  Otherwise the  |
  366. |                      data is written to standard error output.               |
  367. ------------------------------------------------------------------------------*/
  368. static void
  369.   setTraceFunction ( IException::TraceFn& traceFunction );
  370.  
  371. virtual IException
  372.  &logExceptionData ( );
  373.  
  374. /*------------------------------ Ending an Application -------------------------
  375. | The following function is part of the termination model that was             |
  376. | put in place to allow exceptions to be used by compilers that do             |
  377. | not fully support exception handling:                                        |
  378. |   terminate - Ends the application.  It is invoked by the ITHROW             |
  379. |               macro if INO_EXCEPTIONS_SUPPORT is defined.                    |
  380. ------------------------------------------------------------------------------*/
  381. virtual void
  382.   terminate ( );
  383.  
  384. /*------------------------------ Throw Support ---------------------------------
  385. | The following function is used by the IASSERT macro:                         |
  386. |   assertParameter - Creates an IAssertionFailure exception, adds the         |
  387. |                     location information to it, logs out the exception data  |
  388. |                     and throws the exception.                                |
  389. ------------------------------------------------------------------------------*/
  390. static void
  391.   assertParameter ( const char*        exceptionText,
  392.                     IExceptionLocation location );
  393.  
  394. private:
  395. /*--------------------------------- PRIVATE ----------------------------------*/
  396. Severity
  397.   exsevCl;
  398. unsigned long
  399.   ulClErrorId;
  400. IExceptionLocation
  401.   exlocClArray[5];
  402. unsigned long
  403.   ulexlocClCount,
  404.   ulClTxtLvlCount;
  405. IExcText
  406.  *msgtxtClTop;
  407.  
  408. IException
  409.  &operator= ( const IException& exc );
  410.  
  411. }; // IException
  412.  
  413. #if !defined( INO_EXCEPTIONS_SUPPORT )
  414.   #define INO_EXCEPTIONS_SUPPORT 0
  415. #endif
  416.  
  417.  
  418. #if defined ( INO_FUNCTION_NAMES )
  419.   #define IEXCEPTION_LOCATION()  IExceptionLocation(__FILE__, 0, __LINE__)
  420. #elif defined ( __FUNCTION__ )
  421.   #define IEXCEPTION_LOCATION()  IExceptionLocation(__FILE__, __FUNCTION__, __LINE__)
  422. #else
  423.   #define IEXCEPTION_LOCATION()  IExceptionLocation(__FILE__, 0, __LINE__)
  424. #endif
  425.  
  426. #if (INO_EXCEPTIONS_SUPPORT)
  427.   #define ITHROW(exc)\
  428.      exc.addLocation(IEXCEPTION_LOCATION()),\
  429.      exc.logExceptionData(),\
  430.      exc.terminate()
  431. #else
  432.   #define ITHROW(exc)\
  433.      exc.addLocation(IEXCEPTION_LOCATION()),\
  434.      exc.logExceptionData(),\
  435.      throw exc
  436. #endif
  437.  
  438. #define IRETHROW(exc)\
  439.      exc.addLocation(IEXCEPTION_LOCATION()),\
  440.      exc.logExceptionData(),\
  441.      throw
  442.  
  443. #if defined(IC_DEVELOP)
  444.   #define IASSERT(test)\
  445.       if(!(test))\
  446.          IException::assertParameter("The following expression must be true, but evaluated to false: " #test,\
  447.          IEXCEPTION_LOCATION())
  448. #else
  449.   #define IASSERT(test)
  450. #endif
  451.  
  452. #define IEXCLASSDECLARE(child,parent) class child : public parent {\
  453. public:\
  454.   child(const char* a, unsigned long b = 0,\
  455.         Severity c = IException::unrecoverable);\
  456.   virtual const char* name() const;\
  457.   virtual ~child();\
  458.   child(const child &);\
  459. private:\
  460.   operator = ( const child & );\
  461. }
  462.  
  463. #define IEXCLASSIMPLEMENT(child,parent)\
  464.   child :: child(const char* a, unsigned long b,\
  465.                  Severity c)\
  466.     : parent(a, b, c)\
  467.         { }\
  468.   const char* child :: name() const\
  469.     {\
  470.      return ( # child);\
  471.     }\
  472.   child :: ~child() {;}\
  473.   child :: child(const child & a) : parent(a) {;}
  474.  
  475. IEXCLASSDECLARE(IAccessError,IException);
  476. IEXCLASSDECLARE(IAssertionFailure,IException);
  477. IEXCLASSDECLARE(IDeviceError,IException);
  478. IEXCLASSDECLARE(IInvalidParameter,IException);
  479. IEXCLASSDECLARE(IInvalidRequest,IException);
  480. IEXCLASSDECLARE(IResourceExhausted,IException);
  481.   IEXCLASSDECLARE(IOutOfMemory,IResourceExhausted);
  482.   IEXCLASSDECLARE(IOutOfSystemResource,IResourceExhausted);
  483.   IEXCLASSDECLARE(IOutOfWindowResource,IResourceExhausted);
  484.  
  485. #endif /* _IEXCBASE_ */
  486.