//
//  With.m
//  MYUtilities
//
//  Copyright 2008 Jens Alfke. All rights reserved.
//

#import "With.h"
#import "Logging.h"


@interface NSObject (With)
- (BOOL) endWith: (NSException*)x;
@end


void _catchWith( id with, NSException *x )
{
    Warn(@"Exception thrown from WITH(%@) block: %@",[with class],x);
    if( [with respondsToSelector: @selector(endWith:)] ) {
        if( ! [with endWith: x] )
            @throw x;                           // propagate the exception if -endWith: returns NO
    } else {
        [with endWith];
    }
}



@implementation NSAutoreleasePool (With)
+ (NSAutoreleasePool*) beginWith    {return [self new];}
- (void) endWith                    {[self drain];}
- (BOOL) endWith: (NSException*)x   {[self drain]; return YES;}
@end