With.m
changeset 7 59addced5e2a
child 11 e5976864dfe9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/With.m	Fri May 02 12:49:43 2008 -0700
     1.3 @@ -0,0 +1,34 @@
     1.4 +//
     1.5 +//  With.m
     1.6 +//  MYUtilities
     1.7 +//
     1.8 +//  Copyright 2008 Jens Alfke. All rights reserved.
     1.9 +//
    1.10 +
    1.11 +#import "With.h"
    1.12 +#import "Logging.h"
    1.13 +
    1.14 +
    1.15 +@interface NSObject (With)
    1.16 +- (BOOL) endWith: (NSException*)x;
    1.17 +@end
    1.18 +
    1.19 +
    1.20 +void _catchWith( id with, NSException *x )
    1.21 +{
    1.22 +    Warn(@"Exception thrown from WITH(%@) block: %@",[with class],x);
    1.23 +    if( [with respondsToSelector: @selector(endWith:)] ) {
    1.24 +        if( ! [with endWith: x] )
    1.25 +            @throw x;                           // propagate the exception if -endWith: returns NO
    1.26 +    } else {
    1.27 +        [with endWith];
    1.28 +    }
    1.29 +}
    1.30 +
    1.31 +
    1.32 +
    1.33 +@implementation NSAutoreleasePool (With)
    1.34 ++ (NSAutoreleasePool*) beginWith    {return [self new];}
    1.35 +- (void) endWith                    {[self drain];}
    1.36 +- (BOOL) endWith: (NSException*)x   {[self drain]; return YES;}
    1.37 +@end
    1.38 \ No newline at end of file