# HG changeset patch # User Jens Alfke # Date 1210204064 25200 # Node ID 5588347dfcbdb19f021cf6d9e8a0affcae104f2c # Parent 59addced5e2a7703db2635154862babcd50c7879 * Added $apply and some other collection utils. * Moved logging code to a separate code segment. * Fixed uninitialized variable in Target. diff -r 59addced5e2a -r 5588347dfcbd CollectionUtils.h --- a/CollectionUtils.h Fri May 02 12:49:43 2008 -0700 +++ b/CollectionUtils.h Wed May 07 16:47:44 2008 -0700 @@ -23,6 +23,11 @@ #define $object(VAL) ({__typeof(VAL) v=(VAL); _box(&v,@encode(__typeof(v)));}) +// Apply a selector to each array element, returning an array of the results: +NSArray* $apply( NSArray *src, SEL selector, id defaultValue ); +NSArray* $applyKeyPath( NSArray *src, NSString *keyPath, id defaultValue ); + + // Object conveniences: BOOL $equal(id obj1, id obj2); // Like -isEqual: but works even if either/both are nil @@ -40,6 +45,10 @@ BOOL ifSetString( NSString **var, NSString *value ); +#define $true ((NSNumber*)kCFBooleanTrue) +#define $false ((NSNumber*)kCFBooleanFalse) + + @interface NSArray (MYUtils) - (BOOL) my_containsObjectIdenticalTo: (id)object; @end diff -r 59addced5e2a -r 5588347dfcbd CollectionUtils.m --- a/CollectionUtils.m Fri May 02 12:49:43 2008 -0700 +++ b/CollectionUtils.m Wed May 07 16:47:44 2008 -0700 @@ -42,6 +42,27 @@ } +NSArray* $apply( NSArray *src, SEL selector, id defaultValue ) +{ + NSMutableArray *dst = [NSMutableArray arrayWithCapacity: src.count]; + for( id obj in src ) { + id result = [obj performSelector: selector] ?: defaultValue; + [dst addObject: result]; + } + return dst; +} + +NSArray* $applyKeyPath( NSArray *src, NSString *keyPath, id defaultValue ) +{ + NSMutableArray *dst = [NSMutableArray arrayWithCapacity: src.count]; + for( id obj in src ) { + id result = [obj valueForKeyPath: keyPath] ?: defaultValue; + [dst addObject: result]; + } + return dst; +} + + BOOL $equal(id obj1, id obj2) // Like -isEqual: but works even if either/both are nil { if( obj1 ) diff -r 59addced5e2a -r 5588347dfcbd Logging.h --- a/Logging.h Fri May 02 12:49:43 2008 -0700 +++ b/Logging.h Wed May 07 16:47:44 2008 -0700 @@ -12,13 +12,23 @@ NSString* LOC( NSString *key ); // Localized string lookup -#define Log(FMT,ARGS...) do{if(__builtin_expect(_gShouldLog,0)) _Log(FMT,##ARGS);}while(0) +#ifdef __cplusplus + #define IN_SEGMENT(SEG) +#else + #define IN_SEGMENT(SEG) auto __attribute__ ((section ("__TEXT, "#SEG))) __attribute__ ((noinline)) void _outofband_(void);\ + _outofband_();\ + void _outofband_(void) +#endif + +#define Log(FMT,ARGS...) do{if(__builtin_expect(_gShouldLog,0)) {\ + IN_SEGMENT(Logging){_Log(FMT,##ARGS);}\ + } }while(0) +#define LogTo(DOMAIN,FMT,ARGS...) do{if(__builtin_expect(_gShouldLog,0)) {\ + IN_SEGMENT(Logging) {_LogTo(@""#DOMAIN,FMT,##ARGS);}\ + } }while(0) #define Warn Warn void AlwaysLog( NSString *msg, ... ) __attribute__((format(__NSString__, 1, 2))); - -#define LogTo(DOMAIN,FMT,ARGS...) do{if(__builtin_expect(_gShouldLog,0)) _LogTo(@""#DOMAIN,FMT,##ARGS);}while(0) - BOOL _WillLogTo( NSString *domain ); BOOL EnableLog( BOOL enable ); #define EnableLogTo( DOMAIN, VALUE ) _EnableLogTo(@""#DOMAIN, VALUE) diff -r 59addced5e2a -r 5588347dfcbd Target.m --- a/Target.m Fri May 02 12:49:43 2008 -0700 +++ b/Target.m Wed May 07 16:47:44 2008 -0700 @@ -136,6 +136,7 @@ id result; NSMutableArray *invocations = $castIf(NSMutableArray,_invocations); if( invocations ) { + result = nil; for( NSInvocation *invocation in invocations ) result = invokeSingleTarget(invocation,sender); } else {