Skip to content

Instantly share code, notes, and snippets.

@PsychoH13
Created November 20, 2009 08:54
Show Gist options
  • Save PsychoH13/239363 to your computer and use it in GitHub Desktop.
Save PsychoH13/239363 to your computer and use it in GitHub Desktop.
SmallTalkish ObjC blocks in Foundation
#import <Foundation/Foundation.h>
#import "PSYSmallTalkBlocks.h"
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int value = 0;
[PSYNumObj(value == 10) ifTrue:PSYIdVoidBlk({
NSLog(@"Mouhahaha");
})];
[PSYNumObj(value == 10) ifFalse:PSYIdVoidBlk({
NSLog(@"Mouhahaha");
})];
[PSYNumObj(value == 10) ifFalse:PSYIdVoidBlk({ NSLog(@"Mouhahaha"); })
ifTrue:PSYIdVoidBlk({ NSLog(@"BAAAAAAAAAAAAAAAH"); })];
[PSYNumObj(value == 10) ifTrue:PSYIdVoidBlk({ NSLog(@"Mouhahaha"); })
ifFalse:PSYIdVoidBlk({ NSLog(@"BAAAAAAAAAAAAAAAH"); })];
NSLog(@"%@", [PSYNumObj(value == 10) and:
PSYIdVoidBlk({
return @"BAAAAAAAAAAAAAAAH";
})]);
NSLog(@"%@", [PSYNumObj(value == 10) or:
PSYIdVoidBlk({
return @"BAAAAAAAAAAAAAAAH";
})]);
[PSYNumObj(10) to: PSYNumObj(100) by: PSYNumObj(6) do:^(NSInteger value, BOOL *stop)
{
NSLog(@"%ld", value);
}];
__block int i = 0;
[^ BOOL { return i < 10; } whileTrue:^{
NSLog(@"value = %ld", i++);
}];
i = 0;
[^(BOOL *stop){
NSLog(@"BAAH"); i++;
} until:^BOOL(){ return i >= 3; }];
[^(BOOL *stop){
NSLog(@"BAAH"); i++;
} while:^BOOL(){ return i < 5; }];
[^{
NSLog(@"I HAS A BLK");
} unless: i == 0];
[^{
NSLog(@"I HAS A BLK");
} if: i == 5];
NSLog(@"%@", [(id)nil or:^id{
return @"BAAAAAAAAAAAAAAAAAAAAH";
}]);
// Chaining ifTrue: messages through blocks:
NSFileManager *manager = [NSFileManager defaultManager];
__block NSError *err = nil;
id ret = [[[^{
return PSYNumObj([manager createFileAtPath:[@"~/myTest" stringByExpandingTildeInPath]
contents:[NSData data] attributes:nil]);
} ifTrue:^{
return PSYNumObj([manager moveItemAtPath:[@"~/myTest" stringByExpandingTildeInPath]
toPath:[@"~/myTest2" stringByExpandingTildeInPath]
error:&err]);
}] ifTrue:^{
return PSYNumObj([manager copyItemAtPath:[@"~/myTest" stringByExpandingTildeInPath]
toPath:[@"~/myTest1" stringByExpandingTildeInPath]
error:&err]);
}] ifTrue:^{
return PSYNumObj([manager removeItemAtPath:[@"~/myTest2" stringByExpandingTildeInPath] error:&err]);
}];
if(ret == nil) NSLog(@"ERROR: %@", err);
[pool drain];
return 0;
}
/*
Copyright (c) 2009 Remy Demarest
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#import <Cocoa/Cocoa.h>
#define PSYNumObj(n) ([NSNumber numberWithInteger:(n)])
#define PSYIdVoidBlk(blk) ^id (void){ {blk} return nil; }
#define PSYTrue ((NSNumber *)kCFBooleanTrue)
#define PSYFalse ((NSNumber *)kCFBooleanFalse)
#define YES$ PSYTrue
#define NO$ PSYFalse
@interface NSObject (PSYSmallTalkBlocks)
- (id)and:(id(^)())blk;
- (id)or:(id(^)())blk;
- (id)ifTrue:(id(^)())blkTrue;
- (id)ifFalse:(id(^)())blkFalse ifTrue:(id(^)())blkTrue;
- (id)ifFalse:(id(^)())blkFalse;
- (id)ifTrue:(id(^)())blkTrue ifFalse:(id(^)())blkFalse;
// In a category of NSObject, but it only works on blocks, sorry
- (void)whileTrue:(void (^)())blk; // receiver must be of type (BOOL(^)())
- (void)whileFalse:(void (^)())blk; // receiver must be of type (BOOL(^)())
- (void)unless:(BOOL)b; // receiver must be of type (void(^)())
- (void)if:(BOOL)b; // receiver must be of type (void(^)())
- (void)until:(BOOL(^)())blk; // receiver must be of type (void(^)(BOOL *stop))
- (void)while:(BOOL(^)())blk; // receiver must be of type (void(^)(BOOL *stop))
- (void)forever; // receiver must be of type (void(^)(BOOL *stop))
- (void)adVitamAeternam; // receiver must be of type (void(^)(BOOL *stop))
@end
@interface NSNumber (PSYSmallTalkBlocks)
- (void)to:(NSNumber *)end by:(NSNumber *)increment do:(void(^)(NSInteger i, BOOL *stop))blk;
@end
/*
Copyright (c) 2009 Remy Demarest
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#import "PSYSmallTalkBlocks.h"
#import <objc/runtime.h>
@implementation NSNumber (PSYSmallTalkBlocks)
- (id)and:(id(^)())blk
{
return ([self boolValue] ? blk() : (id)kCFBooleanFalse);
}
- (id)or:(id(^)())blk
{
return ([self boolValue] ? (id)kCFBooleanTrue : blk());
}
- (id)ifTrue:(id(^)())blkTrue
{
return ([self boolValue] ? blkTrue() : nil);
}
- (id)ifFalse:(id(^)())blkFalse
{
return ([self boolValue] ? nil : blkFalse());
}
- (id)ifFalse:(id(^)())blkFalse ifTrue:(id(^)())blkTrue
{
return ([self boolValue] ? blkTrue() : blkFalse());
}
- (id)ifTrue:(id(^)())blkTrue ifFalse:(id(^)())blkFalse
{
return ([self boolValue] ? blkTrue() : blkFalse());
}
- (void)to:(NSNumber *)end by:(NSNumber *)increment do:(void(^)(NSInteger, BOOL*))blk
{
NSInteger start = [self integerValue];
NSInteger inc = [increment integerValue];
NSInteger max = [end integerValue];
BOOL stop = NO;
for(; start < max; start += inc)
{
blk(start, &stop);
if(stop) break;
}
}
@end
extern id _objc_setNilReceiver(id newNilReceiver);
@interface _PSYNil : NSNull @end
@implementation _PSYNil
static id _oldNilReceiver = nil;
static _PSYNil *_sharedNil = nil;
+ (void)load
{
_sharedNil = NSAllocateObject(self, 0, NSDefaultMallocZone());
_oldNilReceiver = _objc_setNilReceiver(_sharedNil);
}
- (id)forwardingTargetForSelector:(SEL)aSelector
{
return (_oldNilReceiver == nil ? self : _oldNilReceiver);
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
return [NSMethodSignature signatureWithObjCTypes:"v@:"];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
}
@end
@interface NSNull (PSYSmallTalkBlocks)
@end
@implementation NSNull (PSYSmallTalkBlocks)
- (id)and:(id(^)())blk
{
return (id)kCFBooleanFalse;
}
- (id)or:(id(^)())blk
{
return blk();
}
- (id)ifTrue:(id(^)())blkTrue
{
return nil;
}
- (id)ifFalse:(id(^)())blkFalse ifTrue:(id(^)())blkTrue
{
return blkFalse();
}
- (id)ifFalse:(id(^)())blkFalse
{
return blkFalse();
}
- (id)ifTrue:(id(^)())blkTrue ifFalse:(id(^)())blkFalse
{
return blkFalse();
}
@end
@implementation NSObject (PSYSmallTalkBlocks)
- (id)and:(id(^)())blk
{
return blk();
}
- (id)or:(id(^)())blk
{
return (id)kCFBooleanTrue;
}
- (id)ifTrue:(id(^)())blkTrue
{
return blkTrue();
}
- (id)ifFalse:(id(^)())blkFalse ifTrue:(id(^)())blkTrue
{
return blkTrue();
}
- (id)ifFalse:(id(^)())blkFalse
{
return nil;
}
- (id)ifTrue:(id(^)())blkTrue ifFalse:(id(^)())blkFalse
{
return blkTrue();
}
- (void)selectorOnlyAvailableToBlocks:(SEL)aSel ofType:(const char *)strDesc
{
NSAssert2(NO, @"%@ message can only be sent to a block of type: %s", NSStringFromSelector(aSel), strDesc);
}
- (void)whileTrue:(void (^)())blk
{
[self selectorOnlyAvailableToBlocks:_cmd ofType:"void (^)()"];
}
- (void)whileFalse:(void (^)())blk
{
[self selectorOnlyAvailableToBlocks:_cmd ofType:"void (^)()"];
}
- (void)unless:(BOOL)b
{
[self selectorOnlyAvailableToBlocks:_cmd ofType:"void (^)()"];
}
- (void)if:(BOOL)b
{
[self selectorOnlyAvailableToBlocks:_cmd ofType:"void (^)()"];
}
- (void)until:(BOOL(^)())blk
{
[self selectorOnlyAvailableToBlocks:_cmd ofType:"void (^)(BOOL *)"];
}
- (void)while:(BOOL(^)())blk
{
[self selectorOnlyAvailableToBlocks:_cmd ofType:"void (^)(BOOL *)"];
}
- (void)forever
{
[self selectorOnlyAvailableToBlocks:_cmd ofType:"void (^)(BOOL *)"];
}
- (void)adVitamAeternam
{
[self selectorOnlyAvailableToBlocks:_cmd ofType:"void (^)(BOOL *)"];
}
static void PSY_NSBlock_whileTrue_(BOOL (^self)(), SEL _cmd, void (^blk)())
{
while(self()) blk();
}
static void PSY_NSBlock_whileFalse_(BOOL (^self)(), SEL _cmd, void (^blk)())
{
while(!self()) blk();
}
static void PSY_NSBlock_unless_(void (^self)(), SEL _cmd, BOOL b)
{
if(!b) self();
}
static void PSY_NSBlock_if_(void (^self)(), SEL _cmd, BOOL b)
{
if(b) self();
}
static void PSY_NSBlock_while_(void (^self)(BOOL *stop), SEL _cmd, BOOL(^blk)())
{
BOOL stop = NO;
while(blk())
{
self(&stop);
if(stop) break;
}
}
static void PSY_NSBlock_until_(void (^self)(BOOL *stop), SEL _cmd, BOOL(^blk)())
{
BOOL stop = NO;
while(!blk())
{
self(&stop);
if(stop) break;
}
}
static void PSY_NSBlock_forever(void (^self)(BOOL *stop), SEL _cmd)
{
BOOL stop = NO;
while(YES)
{
self(&stop);
if(stop) break;
}
}
static id PSY_NSBlock_and_(id (^self)(), SEL _cmd, id (^blk)())
{
return [self() and:blk];
}
static id PSY_NSBlock_or_(id (^self)(), SEL _cmd, id (^blk)())
{
return [self() or:blk];
}
static id PSY_NSBlock_ifTrue_(id (^self)(), SEL _cmd, id (^blk)())
{
return [self() ifTrue:blk];
}
static id PSY_NSBlock_ifFalse_(id (^self)(), SEL _cmd, id (^blk)())
{
return [self() ifFalse:blk];
}
static id PSY_NSBlock_ifTrue_ifFalse_(id (^self)(), SEL _cmd, id (^blkTrue)(), id (^blkFalse)())
{
return [self() ifTrue:blkTrue ifFalse:blkFalse];
}
static id PSY_NSBlock_ifFalse_ifTrue_(id (^self)(), SEL _cmd, id (^blkFalse)(), id (^blkTrue)())
{
return [self() ifFalse:blkTrue ifTrue:blkFalse];
}
+ (void)load
{
Class blkClass = objc_getClass("NSBlock");
class_addMethod(blkClass, @selector(whileTrue:), (IMP)PSY_NSBlock_whileTrue_, "v@?:@?");
class_addMethod(blkClass, @selector(whileFalse:), (IMP)PSY_NSBlock_whileFalse_, "v@?:@?");
class_addMethod(blkClass, @selector(while:), (IMP)PSY_NSBlock_while_, "v@?:@?");
class_addMethod(blkClass, @selector(until:), (IMP)PSY_NSBlock_until_, "v@?:@?");
class_addMethod(blkClass, @selector(unless:), (IMP)PSY_NSBlock_unless_, "v@?:c");
class_addMethod(blkClass, @selector(if:), (IMP)PSY_NSBlock_if_, "v@?:c");
class_addMethod(blkClass, @selector(forever), (IMP)PSY_NSBlock_forever, "v@?:");
class_addMethod(blkClass, @selector(adVitamAeternam), (IMP)PSY_NSBlock_forever, "v@?:");
class_addMethod(blkClass, @selector(and:), (IMP)PSY_NSBlock_and_, "@@?:@?");
class_addMethod(blkClass, @selector(or:), (IMP)PSY_NSBlock_or_, "@@?:@?");
class_addMethod(blkClass, @selector(ifTrue:), (IMP)PSY_NSBlock_ifTrue_, "@@?:@?");
class_addMethod(blkClass, @selector(ifFalse:), (IMP)PSY_NSBlock_ifFalse_, "@@?:@?");
class_addMethod(blkClass, @selector(ifTrue:ifFalse:), (IMP)PSY_NSBlock_ifTrue_ifFalse_, "@@?:@?@?");
class_addMethod(blkClass, @selector(ifFalse:ifTrue:), (IMP)PSY_NSBlock_ifFalse_ifTrue_, "@@?:@?@?");
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment