home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / IBMCLASS / IEXCEPT.HPP < prev    next >
C/C++ Source or Header  |  1993-10-22  |  24KB  |  400 lines

  1. #ifndef _IEXCEPT_
  2. #define _IEXCEPT_
  3. /*******************************************************************************
  4. * FILE NAME: iexcept.hpp                                                       *
  5. *                                                                              *
  6. * DESCRIPTION:                                                                 *
  7. *   Declaration of the classes:                                                *
  8. *     IErrorInfo            - Abstract base class for the following 2 classes. *
  9. *     IGUIErrorInfo         - Used to get exception information from the       *
  10. *                             windowing system.                                *
  11. *     ISystemErrorInfo      - Used to get exception information from the       *
  12. *                             operating system.                                *
  13. *                                                                              *
  14. *   This file also contains several macros and functions which can be used     *
  15. *   to facilitate throwing exceptions.  This includes the IASSERTPARM and      *
  16. *   IASSERTSTATE macros, and the IExcept__assertParameter and                  *
  17. *   IExcept__assertState functions.                                            *
  18. *                                                                              *
  19. * COPYRIGHT:                                                                   *
  20. *   Licensed Materials - Property of IBM                                       *
  21. *   (C) Copyright IBM Corporation 1992, 1993                                   *
  22. *   All Rights Reserved                                                        *
  23. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  24. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  25. *                                                                              *
  26. *******************************************************************************/
  27. #ifndef _IVBASE_
  28.   #include <ivbase.hpp>
  29. #endif
  30. #ifndef _IEXCBASE_
  31.   #include <iexcbase.hpp>
  32. #endif
  33.  
  34. /*----------------------------------------------------------------------------*/
  35. /* Align classes on four byte boundary.                                       */
  36. /*----------------------------------------------------------------------------*/
  37. #pragma pack(4)
  38.  
  39. class IErrorInfo : public IVBase {
  40. typedef IVBase
  41.   Inherited;
  42. /*******************************************************************************
  43. * The IErrorInfo class is an abstract base class that defines the interface    *
  44. * for its subclasses, IGUIErrorInfo and ISystemErrorInfo.  IGUIErrorInfo is    *
  45. * used to obtain information about Presentation Manager errors and             *
  46. * ISystemErrorInfo is used to obtain information about OS/2 errors.            *
  47. *                                                                              *
  48. *   The following macros and functions assist in throwing exceptions:          *
  49. *                                                                              *
  50. *     IASSERTPARM         - This macro accepts an expression to be tested as   *
  51. *                           input.  The expression is asserted to be true.     *
  52. *                           If it evaluates to false, it invokes the           *
  53. *                           IExcept__assertParameter function, which creates   *
  54. *                           an IInvalidParameter exception.  Location          *
  55. *                           information is added to the exception, which is    *
  56. *                           then logged and thrown.                            *
  57. *     IASSERTSTATE        - This macro accepts an expression to be tested as   *
  58. *                           input.  The expression is asserted to be true.     *
  59. *                           If it evaluates to false, it invokes the           *
  60. *                           IExcept__assertState function, which creates an    *
  61. *                           IInvalidRequest exception.  Location information   *
  62. *                           is added to the exception, which is then logged    *
  63. *                           and thrown.                                        *
  64. *     ITHROWLIBRARYERROR  - This macro takes three arguments:                  *
  65. *                              - The ID of the message to be loaded from the   *
  66. *                                User Interface Class Library message file     *
  67. *                              - One of the values of the                      *
  68. *                                IErrorInfo::ExceptionType enumeration, which  *
  69. *                                indicates the type of exception to be         *
  70. *                                created                                       *
  71. *                              - One of the values of the                      *
  72. *                                IException::Severity enumeration, which       *
  73. *                                indicates the severity of the exception       *
  74. *                           The macro calls the IExcept__::throwLibraryError   *
  75. *                           function, which loads the message text from the    *
  76. *                           message file and uses it to create an exception    *
  77. *                           instance, adds location information, logs out      *
  78. *                           the exception data, and throws the exception.      *
  79. *     ITHROWLIBRARYERROR1 - This macro is identical to the ITHROWLIBRARYERROR  *
  80. *                           macro except it has a fourth argument, which is    *
  81. *                           text to be substituted into the retrieved message. *
  82. *******************************************************************************/
  83. public:
  84. /*-------------------------------- Destructor ----------------------------------
  85. | This class has a destructor.                                                 |
  86. ------------------------------------------------------------------------------*/
  87. virtual
  88.  ~IErrorInfo ( );
  89.  
  90. /*-------------------------------- Accessors -----------------------------------
  91. | These functions and conversion operator provide means of getting the         |
  92. | accessible attributes of instances of this class.  All the functions and the |
  93. | conversion operator are pure virtual.                                        |
  94. |   operator const char* - Returns the error text.                             |
  95. |   text                 - Returns the error text.                             |
  96. |   errorId              - Returns the error ID.                               |
  97. |   isAvailable          - Returns true if error information is available;     |
  98. |                          otherwise, it returns false.                        |
  99. ------------------------------------------------------------------------------*/
  100.   virtual operator const char* () const = 0;
  101.  
  102. virtual const char
  103.  *text ( ) const = 0;
  104.  
  105. virtual unsigned long
  106.   errorId ( ) const = 0;
  107.  
  108. virtual Boolean
  109.   isAvailable ( ) const = 0;
  110.  
  111. /*------------------------------ Related Type  ---------------------------------
  112. | The following enumeration type is defined to pass "modes" to various         |
  113. | implementation functions and macros:                                         |
  114. | ExceptionType - Used to specify the type of exception to be created.  The    |
  115. |                 allowable values are:                                        |
  116. |    accessError         - Creates an IAccessError instance.                   |
  117. |    deviceError         - Creates an IDeviceError instance.                   |
  118. |    invalidParameter    - Creates an IInvalidParameter instance.              |
  119. |    invalidRequest      - Creates an IInvalidRequest instance.                |
  120. |    outOfSystemResource - Creates an IOutOfSystemResource instance.           |
  121. |    outOfWindowResource - Creates an IOutOfWindowResource instance.           |
  122. |    outOfMemory         - Creates an IOutOfMemory instance.                   |
  123. |    resourceExhausted   - Creates an IResourceExhausted instance.             |
  124. ------------------------------------------------------------------------------*/
  125. enum ExceptionType { accessError, deviceError, invalidParameter,
  126.                      invalidRequest, outOfSystemResource, outOfWindowResource,
  127.                      outOfMemory, resourceExhausted };
  128. }; // IErrorInfo
  129.  
  130. class IGUIErrorInfo : public IErrorInfo {
  131. typedef IErrorInfo
  132.   Inherited;
  133. /*******************************************************************************
  134. *  The IGUIErrorInfo class allows you to obtain information about the last     *
  135. *  error that occurred on a call to Presentation Manager.                      *
  136. *                                                                              *
  137. *  Typically, an instance of this class is created when an error condition has *
  138. *  been detected and an exception is going to be thrown.  The error text       *
  139. *  can be used to construct an IException subclass instance.                   *
  140. *                                                                              *
  141. *  The following macros are provided for throwing exceptions                   *
  142. *  constructed with IGUIErrorInfo information:                                 *
  143. *                                                                              *
  144. *    ITHROWGUIERROR  - This macro takes as its only argument the name of the   *
  145. *                      GUI function that returned an error code.  It calls     *
  146. *                      the IGUIError::throwGUIError function, which creates    *
  147. *                      an IGUIError instance and uses it to create an          *
  148. *                      IAccessError instance, adds location information, logs  *
  149. *                      out the exception data, and throws the exception.       *
  150. *                      Note: The exception Severity is set to recoverable.     *
  151. *                                                                              *
  152. *    ITHROWGUIERROR2 - This macro takes three arguments:                       *
  153. *                          - The name of the GUI function that returned an     *
  154. *                            error code.                                       *
  155. *                          - One of the values of the                          *
  156. *                            IErrorInfo::ExceptionType enumeration, which      *
  157. *                            indicates the type of exception to be created.    *
  158. *                          - One of the values of the                          *
  159. *                            IException::Severity enumeration, which indicates *
  160. *                            the severity of the exception.                    *
  161. *                      The macro calls IGUIError::throwGUIError, which         *
  162. *                      creates an IGUIError instance and uses it to create an  *
  163. *                      exception instance, adds location information, logs out *
  164. *                      the exception data, and throws the exception.           *
  165. *                                                                              *
  166. * Example:                                                                     *
  167. *                                                                              *
  168. *  The following example illustrates using this class to obtain error text,    *
  169. *  and using that text to construct and throw an exception:                    *
  170. *                                                                              *
  171. *    void* pv = WinQueryWindowPtr(hCl, sThisPtrOffset);                        *
  172. *    if (pv == 0)                                                              *
  173. *    {                                                                         *
  174. *       IGUIErrorInfo myErr("WinQueryWindowPtr");                              *
  175. *       IAccessError exc(myErr.text(), myErr.errorId());                       *
  176. *       ITHROW(exc);                                                           *
  177. *    }                                                                         *
  178. *                                                                              *
  179. *  An easier way to accomplish this is:                                        *
  180. *                                                                              *
  181. *       ITHROWGUIERROR("WinQueryWindowPtr");                                   *
  182. *                                                                              *
  183. *******************************************************************************/
  184. public:
  185. /*------------------------ Constructor/Destructor ------------------------------
  186. | There is only one way to construct instances of this class.  If the name of  |
  187. | the failing API is passed in, it will be prefixed to the error text.  If     |
  188. | the error text cannot be loaded, the following default text is provided:     |
  189. | "No error text is available."                                                |
  190. ------------------------------------------------------------------------------*/
  191.   IGUIErrorInfo  ( const char* GUIFunctionName = 0 );
  192.  
  193. virtual
  194.   ~IGUIErrorInfo ( );
  195.  
  196. /*-------------------------------- Accessors -----------------------------------
  197. | These functions and conversion operator provide means of getting the         |
  198. | accessible attributes of instances of this class:                            |
  199. |   operator const char* - Returns the error text.                             |
  200. |   text                 - Returns the error text.                             |
  201. |   errorId              - Returns the error ID.                               |
  202. |   isAvailable          - Returns true if error information is available;     |
  203. |                          otherwise, it returns false.                        |
  204. ------------------------------------------------------------------------------*/
  205.   virtual operator const char* () const;
  206.  
  207. virtual const char
  208.  *text ( ) const;
  209.  
  210. virtual unsigned long
  211.   errorId ( ) const;
  212.  
  213. virtual Boolean
  214.   isAvailable ( ) const;
  215.  
  216. /*------------------------------ Throw Support ---------------------------------
  217. | The following functions provide support for throwing exceptions using        |
  218. | information from an IGUIErrorInfo instance.  The first function is           |
  219. | is used by the ITHROWGUIERROR macro:                                         |
  220. |   throwGUIError - Creates an IGUIErrorInfo instance and uses the text from   |
  221. |                   it to create an exception instance, adds the               |
  222. |                   location information to it, logs out the exception data,   |
  223. |                   and throws the exception.                                  |
  224. |   throwError    - Creates an exception instance from the IGUIErrorInfo       |
  225. |                   instance, adds the location information to it, logs out    |
  226. |                   the exception data, and throws the exception.              |
  227. ------------------------------------------------------------------------------*/
  228. static void
  229.   throwGUIError  ( const char*               functionName,
  230.                    const IExceptionLocation& location,
  231.                    IErrorInfo::ExceptionType name = accessError,
  232.                    IException::Severity      severity = recoverable );
  233.  
  234. void
  235.   throwError ( const IExceptionLocation& location,
  236.                IErrorInfo::ExceptionType name = accessError,
  237.                IException::Severity      severity = recoverable );
  238.  
  239. private:
  240. /*--------------------------------- PRIVATE ----------------------------------*/
  241. void
  242.  *perrinfoCl;
  243. char
  244.  *pClFuncNameText;
  245.  
  246.   IGUIErrorInfo ( const IGUIErrorInfo& errorInformation );
  247.  
  248. IGUIErrorInfo
  249.  &operator= ( const IGUIErrorInfo& errorInformation );
  250.  
  251. }; // IGUIErrorInfo
  252.  
  253. class ISystemErrorInfo : public IErrorInfo {
  254. typedef IErrorInfo
  255.   Inherited;
  256. /*******************************************************************************
  257. *  The ISystemErrorInfo class allows you to obtain information about an error  *
  258. *  on a call to OS/2.                                                          *
  259. *  Typically an instance of this class is created when an error condition has  *
  260. *  been detected, and an exception is going to be thrown.  The error text      *
  261. *  can be used to construct an IException subclass instance.                   *
  262. *                                                                              *
  263. *  The following macro is provided for throwing exceptions constructed with    *
  264. *  ISystemErrorInfo information:                                               *
  265. *                                                                              *
  266. *    ITHROWSYSTEMERROR - This macro takes four arguments:                      *
  267. *                          - The error ID returned from the system function.   *
  268. *                          - The name of the system function that returned     *
  269. *                            an error code.                                    *
  270. *                          - One of the values of the                          *
  271. *                            IErrorInfo::ExceptionType enumeration, which      *
  272. *                            indicates the type of exception to be created.    *
  273. *                          - One of the values of the IException::Severity     *
  274. *                            enumeration, which indicates the severity of the  *
  275. *                            exception.                                        *
  276. *                                                                              *
  277. *  The macro calls ISystemErrorInfo::throwSystemError, which creates an        *
  278. *  ISystemErrorInfo instance and uses it to create an exception instance,      *
  279. *  adds location information, logs out the exception data, and throws the      *
  280. *  exception.                                                                  *
  281. *                                                                              *
  282. * Example:                                                                     *
  283. *                                                                              *
  284. *  The following example demonstrates usage of this class to obtain error text,*
  285. *  and construct and throw an exception:                                       *
  286. *                                                                              *
  287. *    unsigned long ulErrCode = DosFreeModule(hmodResource);                    *
  288. *    if (ulErrCode)                                                            *
  289. *    {                                                                         *
  290. *       ISystemErrorInfo myErr(ulErrCode, "DosFreeModule");                    *
  291. *       IAccessError exc(myErr.text(), myErr.errorId());                       *
  292. *       ITHROW(exc);                                                           *
  293. *    }                                                                         *
  294. *                                                                              *
  295. *******************************************************************************/
  296. public:
  297. /*------------------------ Constructor/Destructor -----------------------------
  298. | There is only one way to construct instances of this class.  An              |
  299. | ISystemErrorInfo object requires one argument, an error ID, identifying an   |
  300. | operating system error.  If the name of the failing API is passed in, it     |
  301. | will be prefixed to the error text.  If the error text cannot be loaded,     |
  302. | the following default text is provided:  "No error text is available."       |
  303. ------------------------------------------------------------------------------*/
  304.   ISystemErrorInfo  ( unsigned long systemErrorId,
  305.                       const char* systemFunctionName = 0 );
  306.  
  307. virtual
  308.   ~ISystemErrorInfo ( );
  309.  
  310. /*-------------------------------- Accessors -----------------------------------
  311. | These functions and conversion operator provide a means of getting the       |
  312. | accessible attributes of instances of this class:                            |
  313. |   operator const char* - Returns the error text.                             |
  314. |   text                 - Returns the error text.                             |
  315. |   errorId              - Returns the error ID.                               |
  316. |   isAvailable          - Returns true if error information is available;     |
  317. |                          otherwise, it returns false.                        |
  318. ------------------------------------------------------------------------------*/
  319.   virtual operator const char* () const;
  320.  
  321. virtual const char
  322.  *text ( ) const;
  323.  
  324. virtual unsigned long
  325.   errorId ( ) const;
  326.  
  327. virtual Boolean
  328.   isAvailable ( ) const;
  329.  
  330. /*------------------------------ Throw Support ---------------------------------
  331. | The following function is used by the ITHROWSYSTEMERROR macro:               |
  332. |   throwSystemError - Creates an ISystemErrorInfo instance and uses the text  |
  333. |                      from it to create an exception instance, adds the       |
  334. |                      location information to it, logs out the exception      |
  335. |                      data, and throws the exception.                         |
  336. ------------------------------------------------------------------------------*/
  337. static void
  338.   throwSystemError ( unsigned long             systemErrorId,
  339.                      const char*               functionName,
  340.                      const IExceptionLocation& location,
  341.                      IErrorInfo::ExceptionType name = accessError,
  342.                      IException::Severity      severity = recoverable );
  343.  
  344. private:
  345. /*--------------------------------- PRIVATE ----------------------------------*/
  346. Boolean
  347.   bClError;
  348. unsigned long
  349.   ulClErrorId;
  350. char
  351.  *pClErrorText;
  352.  
  353.   ISystemErrorInfo ( const ISystemErrorInfo& errorInformation );
  354.  
  355. ISystemErrorInfo
  356.  &operator= ( const ISystemErrorInfo& errorInformation );
  357.  
  358. }; // ISystemErrorInfo
  359.  
  360. void
  361.   IExcept__assertParameter    ( const char*               assertExpression,
  362.                                 const IExceptionLocation& location ),
  363.   IExcept__assertState        ( const char*               assertExpression,
  364.                                 const IExceptionLocation& location ),
  365.   IExcept__throwLibraryError  ( unsigned long             messageId,
  366.                                 const IExceptionLocation& location,
  367.                                 IErrorInfo::ExceptionType name = IErrorInfo::invalidRequest,
  368.                                 IException::Severity      severity = IException::recoverable,
  369.                                 const char*               text = 0);
  370.  
  371. #define IASSERTPARM(test)\
  372.     if(!(test))\
  373.        IExcept__assertParameter(#test, IEXCEPTION_LOCATION())
  374.  
  375. #define IASSERTSTATE(test)\
  376.     if(!(test))\
  377.        IExcept__assertState(#test, IEXCEPTION_LOCATION())
  378.  
  379. #define ITHROWGUIERROR(func)\
  380.     IGUIErrorInfo::throwGUIError(func, IEXCEPTION_LOCATION())
  381.  
  382. #define ITHROWGUIERROR2(func, exType, sev)\
  383.     IGUIErrorInfo::throwGUIError(func, IEXCEPTION_LOCATION(), exType, sev)
  384.  
  385. #define ITHROWSYSTEMERROR(id, func, exType, sev)\
  386.     ISystemErrorInfo::throwSystemError(id, func, IEXCEPTION_LOCATION(), exType, sev)
  387.  
  388. #define ITHROWLIBRARYERROR(id, exType, sev)\
  389.     IExcept__throwLibraryError(id, IEXCEPTION_LOCATION(), exType, sev)
  390.  
  391. #define ITHROWLIBRARYERROR1(id, exType, sev, text)\
  392.     IExcept__throwLibraryError(id, IEXCEPTION_LOCATION(), exType, sev, text)
  393.  
  394. /*----------------------------------------------------------------------------*/
  395. /* Resume compiler default packing.                                           */
  396. /*----------------------------------------------------------------------------*/
  397. #pragma pack()
  398.  
  399. #endif /* _IEXCEPT_ */
  400.