jens@0: // jens@0: // With.m jens@0: // MYUtilities jens@0: // jens@0: // Copyright 2008 Jens Alfke. All rights reserved. jens@0: // jens@0: jens@0: #import "With.h" jens@0: #import "Logging.h" jens@0: jens@0: jens@0: @interface NSObject (With) jens@0: - (BOOL) endWith: (NSException*)x; jens@0: @end jens@0: jens@0: jens@0: void _catchWith( id with, NSException *x ) jens@0: { jens@0: Warn(@"Exception thrown from WITH(%@) block: %@",[with class],x); jens@0: if( [with respondsToSelector: @selector(endWith:)] ) { jens@0: if( ! [with endWith: x] ) jens@0: @throw x; // propagate the exception if -endWith: returns NO jens@0: } else { jens@0: [with endWith]; jens@0: } jens@0: } jens@0: jens@0: jens@0: jens@0: @implementation NSAutoreleasePool (With) jens@0: + (NSAutoreleasePool*) beginWith {return [self new];} jens@0: - (void) endWith {[self drain];} jens@0: - (BOOL) endWith: (NSException*)x {[self drain]; return YES;} jens@0: @end