home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / include / iexcbase.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  9.9 KB  |  338 lines

  1.  
  2.  
  3. #ifndef _IEXCBASE_
  4. #define _IEXCBASE_
  5. /*******************************************************************************
  6. * FILE NAME: iexcbase.hpp                                                      *
  7. *                                                                              *
  8. * DESCRIPTION:                                                                 *
  9. *   Declaration of the classes:                                                *
  10. *     IExceptionLocation                                                       *
  11. *     IException                                                               *
  12. *     IAccessError                                                             *
  13. *     IAssertionFailure                                                        *
  14. *     IDeviceError                                                             *
  15. *     IInvalidParameter                                                        *
  16. *     IInvalidRequest                                                          *
  17. *     IResourceExhausted                                                       *
  18. *     IOutOfMemory                                                             *
  19. *     IOutOfSystemResource                                                     *
  20. *     IOutOfWindowResource                                                     *
  21. *                                                                              *
  22. *   This file also contains many of the macros used to implement the           *
  23. *   library exception handling mechanism.  This includes the IASSERT, ITHROW,  *
  24. *   IRETHROW, IEXCEPTION_LOCATION, IEXCLASSDECLARE, IEXCLASSIMPLEMENT, and     *
  25. *   and INO_EXCEPTIONS_SUPPORT macros.                                         *
  26. *                                                                              *
  27. * COPYRIGHT:                                                                   *
  28. *   IBM Open Class Library                                                     *
  29. *   (C) Copyright International Business Machines Corporation 1992, 1996       *
  30. *   Licensed Material - Program-Property of IBM - All Rights Reserved.         *
  31. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  32. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  33. *                                                                              *
  34. *******************************************************************************/
  35. #ifndef IC_IMPORTB
  36.   #if defined(__WINDOWS__) && defined(__IMPORTLIB__)
  37.     #define IC_IMPORTB  _Import
  38.   #else
  39.     #define IC_IMPORTB
  40.   #endif
  41. #endif
  42. #ifndef IC_IMPORTU
  43.   #if defined(__WINDOWS__) && defined(__IMPORTLIB__)
  44.     #define IC_IMPORTU  _Import
  45.   #else
  46.     #define IC_IMPORTU
  47.   #endif
  48. #endif
  49. #ifndef IC_IMPORTD
  50.   #if defined(__WINDOWS__) && defined(__IMPORTLIB__)
  51.     #define IC_IMPORTD  _Import
  52.   #else
  53.     #define IC_IMPORTD
  54.   #endif
  55. #endif
  56. #ifndef IC_IMPORTM
  57.   #if defined(__WINDOWS__) && defined(__IMPORTLIB__)
  58.     #define IC_IMPORTM  _Import
  59.   #else
  60.     #define IC_IMPORTM
  61.   #endif
  62. #endif
  63.  
  64. #include <imsgtext.hpp>
  65.  
  66. #pragma pack(4)
  67.  
  68.  
  69. class IExcText;
  70.  
  71. class IExceptionLocation {
  72. public:
  73.  
  74. /*------------------------------- Constructors -------------------------------*/
  75.   IExceptionLocation ( const char*   fileName = 0,
  76.                        const char*   functionName = 0,
  77.                        unsigned long lineNumber = 0 );
  78.  
  79. /*-------------------------------- Attributes --------------------------------*/
  80. const char
  81.  *fileName     ( ) const,
  82.  *functionName ( ) const;
  83.  
  84. unsigned long
  85.   lineNumber ( ) const;
  86.  
  87. private:
  88. /*--------------------------------- Private ----------------------------------*/
  89. const char
  90.  *pClFile,
  91.  *pClFunction;
  92. unsigned long
  93.   ulClLineNo;
  94.  
  95.  
  96.  
  97.  
  98. }; // IExceptionLocation
  99.  
  100. class IException {
  101. public:
  102.  
  103. enum Severity { unrecoverable, recoverable };
  104.  
  105. /*------------------------------- Constructors -------------------------------*/
  106.   IException ( const char*       errorText,
  107.                unsigned long     errorId = 0,
  108.                Severity          severity = IException::unrecoverable );
  109.   IException ( const IException& exception );
  110.  
  111. virtual
  112.  ~IException ( );
  113.  
  114. class TraceFn {
  115. public:
  116. /*--------------------------------- Tracing ----------------------------------*/
  117. virtual void
  118.   write   ( const char* buffer ),
  119.   logData ( IException& exception );
  120.  
  121. protected:
  122. /*-------------------------- Protected Constructors --------------------------*/
  123.   TraceFn ( );
  124.  
  125. /*--------------------------------- Tracing ----------------------------------*/
  126. virtual void
  127.   exceptionLogged ( );
  128.  
  129.  
  130.  
  131.  
  132. }; // TraceFn
  133.  
  134. /*---------------------------- Error Code Groups -----------------------------*/
  135. typedef const char* ErrorCodeGroup;
  136.  
  137. static ErrorCodeGroup const
  138.   IC_IMPORTB baseLibrary,
  139.   IC_IMPORTB CLibrary,
  140.   IC_IMPORTB operatingSystem,
  141.   IC_IMPORTB presentationSystem,
  142.   IC_IMPORTB other;
  143.  
  144. IException&
  145.   setErrorCodeGroup ( ErrorCodeGroup errorGroup );
  146.  
  147. ErrorCodeGroup
  148.   errorCodeGroup ( ) const;
  149.  
  150. /*------------------------------ Exception Text ------------------------------*/
  151. IException
  152.  &appendText  ( const char*   errorText ),
  153.  &setText     ( const char*   errorText );
  154.  
  155. const char
  156.  *text ( unsigned long indexFromTop = 0 ) const;
  157.  
  158. unsigned long
  159.   textCount     ( ) const;
  160.  
  161. /*---------------------------- Exception Severity ----------------------------*/
  162. IException
  163.  &setSeverity ( Severity      severity );
  164.  
  165. virtual int
  166.  isRecoverable ( ) const;
  167.  
  168. /*----------------------------- Error Identifier -----------------------------*/
  169. IException
  170.  &setErrorId  ( unsigned long errorId );
  171.  
  172. unsigned long
  173.   errorId       ( ) const;
  174.  
  175. /*---------------------------- Exception Location ----------------------------*/
  176. virtual IException
  177.  &addLocation      ( const IExceptionLocation& location );
  178.  
  179. unsigned long
  180.   locationCount ( ) const;
  181.  
  182. const IExceptionLocation
  183.  *locationAtIndex ( unsigned long locationIndex ) const;
  184.  
  185. /*------------------------------ Exception Type ------------------------------*/
  186. virtual const char
  187.  *name ( ) const;
  188.  
  189. /*---------------------------- Exception Logging -----------------------------*/
  190. static IException::TraceFn
  191.   *setTraceFunction ( IException::TraceFn& traceFunction );
  192.  
  193. virtual IException
  194.  &logExceptionData ( );
  195.  
  196. /*------------------------- Application Termination --------------------------*/
  197. virtual void
  198.   terminate ( );
  199.  
  200. /*------------------------------ Throw Support -------------------------------*/
  201. static void
  202.   assertParameter ( const char*        exceptionText,
  203.                     IExceptionLocation location );
  204.  
  205. private:
  206. /*----------------------------- Hidden Functions -----------------------------*/
  207. IException
  208.  &operator= ( const IException& exc );
  209.  
  210. /*--------------------------------- Private ----------------------------------*/
  211. friend class IException::TraceFn;
  212.  
  213. Severity
  214.   exsevCl;
  215. unsigned long
  216.   ulClErrorId;
  217. IExceptionLocation
  218.   exlocClArray[5];
  219. unsigned long
  220.   ulexlocClCount,
  221.   ulClTxtLvlCount;
  222. IExcText
  223.  *msgtxtClTop;
  224. ErrorCodeGroup
  225.   fErrorGroup;
  226.  
  227.  
  228.  
  229. }; // IException
  230.  
  231. #if !defined( INO_EXCEPTIONS_SUPPORT )
  232.   #define INO_EXCEPTIONS_SUPPORT 0
  233. #endif
  234.  
  235.  
  236. #if defined ( INO_FUNCTION_NAMES )
  237.   #define IEXCEPTION_LOCATION() \
  238.                 IExceptionLocation(__FILE__, 0, __LINE__)
  239. #elif defined ( __FUNCTION__ )
  240.   #define IEXCEPTION_LOCATION()  \
  241.                 IExceptionLocation(__FILE__, __FUNCTION__, __LINE__)
  242. #else
  243.   #define IEXCEPTION_LOCATION()   \
  244.                 IExceptionLocation(__FILE__, 0, __LINE__)
  245. #endif
  246.  
  247. #if (INO_EXCEPTIONS_SUPPORT)
  248.   #define ITHROW(exc)\
  249.      exc.addLocation(IEXCEPTION_LOCATION()),\
  250.      exc.logExceptionData(),\
  251.      exc.terminate()
  252. #else
  253.   #define ITHROW(exc)\
  254.      exc.addLocation(IEXCEPTION_LOCATION()),\
  255.      exc.logExceptionData(),\
  256.      throw(exc)
  257. #endif
  258.  
  259. #define IRETHROW(exc)\
  260.      exc.addLocation(IEXCEPTION_LOCATION()),\
  261.      exc.logExceptionData(),\
  262.      throw
  263.  
  264. #if defined(IC_DEVELOP)
  265.   #define IASSERT(test)\
  266.       if(!(test))\
  267.       {\
  268.          IException::assertParameter(   \
  269.    "The following expression must be true, but evaluated to false: " #test,\
  270.          IEXCEPTION_LOCATION());\
  271.       }
  272. #else
  273.   #define IASSERT(test)
  274. #endif
  275.  
  276. #define IEXCLASSDECLARE(child,parent) class child : public parent {\
  277. public:\
  278.   child(const char* a, unsigned long b = 0,\
  279.         Severity c = IException::unrecoverable);\
  280.   virtual const char* name() const;\
  281.   virtual ~child();\
  282.   child(const child &);\
  283. private:\
  284.   child &operator = ( const child & );\
  285. }
  286.  
  287. #define IEXCLASSIMPLEMENT(child,parent)\
  288.   child :: child(const char* a, unsigned long b,\
  289.                  Severity c)\
  290.     : parent(a, b, c)\
  291.         { }\
  292.   const char* child :: name() const\
  293.     {\
  294.      return ( # child);\
  295.     }\
  296.   child :: ~child() {;}\
  297.   child :: child(const child & a) : parent(a) {;}
  298.  
  299. IEXCLASSDECLARE(IAccessError,IException);
  300. IEXCLASSDECLARE(IAssertionFailure,IException);
  301. IEXCLASSDECLARE(IDeviceError,IException);
  302. IEXCLASSDECLARE(IInvalidParameter,IException);
  303. IEXCLASSDECLARE(IInvalidRequest,IException);
  304. IEXCLASSDECLARE(IResourceExhausted,IException);
  305.   IEXCLASSDECLARE(IOutOfMemory,IResourceExhausted);
  306.   IEXCLASSDECLARE(IOutOfSystemResource,IResourceExhausted);
  307.   IEXCLASSDECLARE(IOutOfWindowResource,IResourceExhausted);
  308.  
  309.  
  310.  
  311. /*-------------------------- Pragma Library Support --------------------------*/
  312. #ifndef __NO_DEFAULT_LIBS__
  313.   #ifdef __OS2__
  314.     #ifdef __IMPORTLIB__
  315.        #pragma library("CPPOOC3I.LIB")
  316.     #else
  317.        #pragma library("CPPOOC3.LIB")
  318.     #endif
  319.   #endif
  320.   #ifdef __WINDOWS__
  321.     #ifdef __IMPORTLIB__
  322.        #pragma library("CPPWOB3I.LIB")
  323.        #pragma library("CPPWOU3I.LIB")
  324.        #pragma library("CPPWOD3I.LIB")
  325.        #pragma library("CPPWOM3I.LIB")
  326.        #pragma library("CPPWOF3I.LIB")
  327.     #else
  328.        #pragma library("CPPWOC3.LIB")
  329.     #endif
  330.   #endif
  331. #endif
  332.  
  333. #pragma pack()
  334.  
  335. #endif /* _IEXCBASE_ */
  336.  
  337.  
  338.