home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / DEV.Z / NSException.h < prev    next >
Encoding:
Text File  |  1996-09-11  |  6.4 KB  |  203 lines

  1. /*    NSException.h
  2.     Exception raising and handling
  3.     Copyright 1994-1996, NeXT Software, Inc.  All rights reserved.
  4. */
  5.  
  6. #import <Foundation/NSObject.h>
  7. #import <stdarg.h>
  8. #import <setjmp.h>
  9.  
  10. @class NSString, NSDictionary;
  11.  
  12. /***************    Generic Exception names        ***************/
  13.  
  14. FOUNDATION_EXPORT NSString *NSGenericException;
  15. FOUNDATION_EXPORT NSString *NSRangeException;
  16. FOUNDATION_EXPORT NSString *NSInvalidArgumentException;
  17. FOUNDATION_EXPORT NSString *NSInternalInconsistencyException;
  18.  
  19. FOUNDATION_EXPORT NSString *NSMallocException;
  20.  
  21. #if !defined(STRICT_OPENSTEP)
  22.  
  23. FOUNDATION_EXPORT NSString *NSObjectInaccessibleException;
  24. FOUNDATION_EXPORT NSString *NSObjectNotAvailableException;
  25. FOUNDATION_EXPORT NSString *NSDestinationInvalidException;
  26.     
  27. FOUNDATION_EXPORT NSString *NSPortTimeoutException;
  28. FOUNDATION_EXPORT NSString *NSInvalidSendPortException;
  29. FOUNDATION_EXPORT NSString *NSInvalidReceivePortException;
  30. FOUNDATION_EXPORT NSString *NSPortSendException;
  31. FOUNDATION_EXPORT NSString *NSPortReceiveException;
  32.  
  33. FOUNDATION_EXPORT NSString *NSOldStyleException;
  34.  
  35. #endif /* !STRICT_OPENSTEP */
  36.  
  37. /***************    Exception object    ***************/
  38.  
  39. @interface NSException:NSObject <NSCopying, NSCoding> {
  40.     @private
  41.     NSString        *name;
  42.     NSString        *reason;
  43.     NSDictionary    *userInfo;
  44.     void        *reserved;
  45. }
  46.  
  47. + (NSException *)exceptionWithName:(NSString *)name reason:(NSString *)reason userInfo:(NSDictionary *)userInfo;
  48.  
  49. - (NSString *)name;
  50.  
  51. - (NSString *)reason;
  52.  
  53. - (NSDictionary *)userInfo;
  54.  
  55. - (volatile void)raise;
  56.  
  57. - (id)initWithName:(NSString *)aName reason:(NSString *)aReason userInfo:(NSDictionary *)aUserInfo;
  58.  
  59. @end
  60.  
  61. @interface NSException (NSExceptionRaisingConveniences)
  62.  
  63. + (volatile void)raise:(NSString *)name format:(NSString *)format, ...;
  64.  
  65. + (volatile void)raise:(NSString *)name format:(NSString *)format arguments:(va_list)argList;
  66.  
  67. @end
  68.  
  69. typedef struct _NSHandler {    /* Private */
  70.     jmp_buf        _state;
  71.     struct _NSHandler    *_next;
  72.     int         _code;
  73.     const void        *_data1;
  74.     const void        *_data2;
  75. } NSHandler;
  76.  
  77. /* private support routines.  Do not call directly. */
  78. FOUNDATION_EXPORT void _NSAddHandler(NSHandler *handler);
  79. FOUNDATION_EXPORT void _NSRemoveHandler(NSHandler *handler);
  80. FOUNDATION_EXPORT NSException *_NSExceptionObjectFromHandler(NSHandler *handler);
  81.  
  82. #if !defined(NSSETJMP)
  83. #if defined(__svr4__) || defined(WIN32)
  84. #define NSSETJMP(B, S)    setjmp(B)
  85. #elif defined(hpux)
  86. #define NSSETJMP(B, S)    sigsetjmp((B), (S))
  87. #else
  88. FOUNDATION_IMPORT int _setjmp(jmp_buf);
  89. #define NSSETJMP(B, S)    _setjmp(B)
  90. #endif
  91. #endif
  92.  
  93. #define NS_DURING { NSHandler _localHandler;            \
  94.             _NSAddHandler(&_localHandler);        \
  95.             if (!NSSETJMP(_localHandler._state, 0)) {
  96.  
  97. #define NS_HANDLER _NSRemoveHandler(&_localHandler); } else { \
  98.             NSException    *localException = _NSExceptionObjectFromHandler(&_localHandler);
  99.  
  100. #define NS_ENDHANDLER localException = nil; /* to avoid compiler warning */}}
  101.  
  102. #define NS_VALUERETURN(val,type)  do { type temp = (val);    \
  103.             _NSRemoveHandler(&_localHandler);    \
  104.             return(temp); } while (0)
  105.  
  106. #define NS_VOIDRETURN    do { _NSRemoveHandler(&_localHandler);    \
  107.             return; } while (0)
  108.  
  109.  
  110. typedef volatile void NSUncaughtExceptionHandler(NSException *exception);
  111.  
  112. FOUNDATION_EXPORT NSUncaughtExceptionHandler *NSGetUncaughtExceptionHandler(void);
  113. FOUNDATION_EXPORT void NSSetUncaughtExceptionHandler(NSUncaughtExceptionHandler *);
  114.  
  115.  
  116. @class NSAssertionHandler;
  117.  
  118. /* Implementation of asserts (ignore) */
  119. #if !defined(NS_BLOCK_ASSERTIONS)
  120. #define _NSAssertBody(condition, desc, arg1, arg2, arg3, arg4, arg5)    \
  121.     do {                        \
  122.     if (!(condition)) {                \
  123.         [[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd object:self file:[NSString stringWithCString:__FILE__] \
  124.             lineNumber:__LINE__ description:(desc), (arg1), (arg2), (arg3), (arg4), (arg5)];    \
  125.     }                        \
  126.     } while(0)
  127. #define _NSCAssertBody(condition, desc, arg1, arg2, arg3, arg4, arg5)    \
  128.     do {                        \
  129.     if (!(condition)) {                \
  130.         [[NSAssertionHandler currentHandler] handleFailureInFunction:[NSString stringWithCString:__PRETTY_FUNCTION__] file:[NSString stringWithCString:__FILE__] \
  131.             lineNumber:__LINE__ description:(desc), (arg1), (arg2), (arg3), (arg4), (arg5)];    \
  132.     }                        \
  133.     } while(0)
  134. #else
  135. #define _NSAssertBody(condition, desc, arg1, arg2, arg3, arg4, arg5)
  136. #define _NSCAssertBody(condition, desc, arg1, arg2, arg3, arg4, arg5)
  137. #endif
  138.  
  139.  
  140. /*
  141.  * Asserts to use in Objective-C method bodies
  142.  */
  143.  
  144. #define NSAssert5(condition, desc, arg1, arg2, arg3, arg4, arg5)    \
  145.     _NSAssertBody((condition), (desc), (arg1), (arg2), (arg3), (arg4), (arg5))
  146.  
  147. #define NSAssert4(condition, desc, arg1, arg2, arg3, arg4)    \
  148.     _NSAssertBody((condition), (desc), (arg1), (arg2), (arg3), (arg4), 0)
  149.  
  150. #define NSAssert3(condition, desc, arg1, arg2, arg3)    \
  151.     _NSAssertBody((condition), (desc), (arg1), (arg2), (arg3), 0, 0)
  152.  
  153. #define NSAssert2(condition, desc, arg1, arg2)        \
  154.     _NSAssertBody((condition), (desc), (arg1), (arg2), 0, 0, 0)
  155.  
  156. #define NSAssert1(condition, desc, arg1)        \
  157.     _NSAssertBody((condition), (desc), (arg1), 0, 0, 0, 0)
  158.  
  159. #define NSAssert(condition, desc)            \
  160.     _NSAssertBody((condition), (desc), 0, 0, 0, 0, 0)
  161.  
  162.  
  163. #define NSParameterAssert(condition)            \
  164.     _NSAssertBody((condition), @"Invalid parameter not satisfying: %s", #condition, 0, 0, 0, 0)
  165.  
  166. #define NSCAssert5(condition, desc, arg1, arg2, arg3, arg4, arg5)    \
  167.     _NSCAssertBody((condition), (desc), (arg1), (arg2), (arg3), (arg4), (arg5))
  168.  
  169. #define NSCAssert4(condition, desc, arg1, arg2, arg3, arg4)    \
  170.     _NSCAssertBody((condition), (desc), (arg1), (arg2), (arg3), (arg4), 0)
  171.  
  172. #define NSCAssert3(condition, desc, arg1, arg2, arg3)    \
  173.     _NSCAssertBody((condition), (desc), (arg1), (arg2), (arg3), 0, 0)
  174.  
  175. #define NSCAssert2(condition, desc, arg1, arg2)    \
  176.     _NSCAssertBody((condition), (desc), (arg1), (arg2), 0, 0, 0)
  177.  
  178. #define NSCAssert1(condition, desc, arg1)        \
  179.     _NSCAssertBody((condition), (desc), (arg1), 0, 0, 0, 0)
  180.  
  181. #define NSCAssert(condition, desc)            \
  182.     _NSCAssertBody((condition), (desc), 0, 0, 0, 0, 0)
  183.  
  184.  
  185. #define NSCParameterAssert(condition)            \
  186.     _NSCAssertBody((condition), @"Invalid parameter not satisfying: %s", #condition, 0, 0, 0, 0)
  187.  
  188.  
  189. @interface NSAssertionHandler : NSObject {
  190.     @private
  191.     void *_reserved;
  192. }
  193.  
  194. + (NSAssertionHandler *)currentHandler;
  195.  
  196. - (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(int)line description:(NSString *)format,...;
  197.  
  198.  
  199. - (void)handleFailureInFunction:(NSString *)functionName file:(NSString *)fileName lineNumber:(int)line description:(NSString *)format,...;
  200.  
  201.  
  202. @end
  203.