With.m
author Jens Alfke <jens@mooseyard.com>
Sat May 24 13:24:33 2008 -0700 (2008-05-24)
changeset 11 e5976864dfe9
parent 0 d84d25d6cdbb
permissions -rw-r--r--
* Added BSD license to more .m files.
* Updated some copyright notices.
* Added some #imports to make the MYNetwork classes build without using the MYUtilities.pch prefix header.
     1 //
     2 //  With.m
     3 //  MYUtilities
     4 //
     5 //  Copyright 2008 Jens Alfke. All rights reserved.
     6 //
     7 
     8 #import "With.h"
     9 #import "Logging.h"
    10 
    11 
    12 @interface NSObject (With)
    13 - (BOOL) endWith: (NSException*)x;
    14 @end
    15 
    16 
    17 void _catchWith( id with, NSException *x )
    18 {
    19     Warn(@"Exception thrown from WITH(%@) block: %@",[with class],x);
    20     if( [with respondsToSelector: @selector(endWith:)] ) {
    21         if( ! [with endWith: x] )
    22             @throw x;                           // propagate the exception if -endWith: returns NO
    23     } else {
    24         [with endWith];
    25     }
    26 }
    27 
    28 
    29 
    30 @implementation NSAutoreleasePool (With)
    31 + (NSAutoreleasePool*) beginWith    {return [self new];}
    32 - (void) endWith                    {[self drain];}
    33 - (BOOL) endWith: (NSException*)x   {[self drain]; return YES;}
    34 @end
    35 
    36 
    37 /*
    38  Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
    39  
    40  Redistribution and use in source and binary forms, with or without modification, are permitted
    41  provided that the following conditions are met:
    42  
    43  * Redistributions of source code must retain the above copyright notice, this list of conditions
    44  and the following disclaimer.
    45  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
    46  and the following disclaimer in the documentation and/or other materials provided with the
    47  distribution.
    48  
    49  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    50  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    51  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    52  BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    53  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    54   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    55  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    56  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    57  */