* Added $apply and some other collection utils.
authorJens Alfke <jens@mooseyard.com>
Wed May 07 16:47:44 2008 -0700 (2008-05-07)
changeset 85588347dfcbd
parent 7 59addced5e2a
child 9 823e7e74088e
* Added $apply and some other collection utils.
* Moved logging code to a separate code segment.
* Fixed uninitialized variable in Target.
CollectionUtils.h
CollectionUtils.m
Logging.h
Target.m
     1.1 --- a/CollectionUtils.h	Fri May 02 12:49:43 2008 -0700
     1.2 +++ b/CollectionUtils.h	Wed May 07 16:47:44 2008 -0700
     1.3 @@ -23,6 +23,11 @@
     1.4  #define $object(VAL)        ({__typeof(VAL) v=(VAL); _box(&v,@encode(__typeof(v)));})
     1.5  
     1.6  
     1.7 +// Apply a selector to each array element, returning an array of the results:
     1.8 +NSArray* $apply( NSArray *src, SEL selector, id defaultValue );
     1.9 +NSArray* $applyKeyPath( NSArray *src, NSString *keyPath, id defaultValue );
    1.10 +
    1.11 +
    1.12  // Object conveniences:
    1.13  
    1.14  BOOL $equal(id obj1, id obj2);      // Like -isEqual: but works even if either/both are nil
    1.15 @@ -40,6 +45,10 @@
    1.16  BOOL ifSetString( NSString **var, NSString *value );
    1.17  
    1.18  
    1.19 +#define $true   ((NSNumber*)kCFBooleanTrue)
    1.20 +#define $false  ((NSNumber*)kCFBooleanFalse)
    1.21 +
    1.22 +
    1.23  @interface NSArray (MYUtils)
    1.24  - (BOOL) my_containsObjectIdenticalTo: (id)object;
    1.25  @end
     2.1 --- a/CollectionUtils.m	Fri May 02 12:49:43 2008 -0700
     2.2 +++ b/CollectionUtils.m	Wed May 07 16:47:44 2008 -0700
     2.3 @@ -42,6 +42,27 @@
     2.4  }
     2.5  
     2.6  
     2.7 +NSArray* $apply( NSArray *src, SEL selector, id defaultValue )
     2.8 +{
     2.9 +    NSMutableArray *dst = [NSMutableArray arrayWithCapacity: src.count];
    2.10 +    for( id obj in src ) {
    2.11 +        id result = [obj performSelector: selector] ?: defaultValue;
    2.12 +        [dst addObject: result];
    2.13 +    }
    2.14 +    return dst;
    2.15 +}
    2.16 +
    2.17 +NSArray* $applyKeyPath( NSArray *src, NSString *keyPath, id defaultValue )
    2.18 +{
    2.19 +    NSMutableArray *dst = [NSMutableArray arrayWithCapacity: src.count];
    2.20 +    for( id obj in src ) {
    2.21 +        id result = [obj valueForKeyPath: keyPath] ?: defaultValue;
    2.22 +        [dst addObject: result];
    2.23 +    }
    2.24 +    return dst;
    2.25 +}
    2.26 +
    2.27 +
    2.28  BOOL $equal(id obj1, id obj2)      // Like -isEqual: but works even if either/both are nil
    2.29  {
    2.30      if( obj1 )
     3.1 --- a/Logging.h	Fri May 02 12:49:43 2008 -0700
     3.2 +++ b/Logging.h	Wed May 07 16:47:44 2008 -0700
     3.3 @@ -12,13 +12,23 @@
     3.4  NSString* LOC( NSString *key );     // Localized string lookup
     3.5  
     3.6  
     3.7 -#define Log(FMT,ARGS...) do{if(__builtin_expect(_gShouldLog,0)) _Log(FMT,##ARGS);}while(0)
     3.8 +#ifdef __cplusplus
     3.9 +    #define IN_SEGMENT(SEG)
    3.10 +#else
    3.11 +    #define IN_SEGMENT(SEG) auto __attribute__ ((section ("__TEXT, "#SEG))) __attribute__ ((noinline)) void _outofband_(void);\
    3.12 +                            _outofband_();\
    3.13 +                            void _outofband_(void)
    3.14 +#endif
    3.15 +
    3.16 +#define Log(FMT,ARGS...) do{if(__builtin_expect(_gShouldLog,0)) {\
    3.17 +                            IN_SEGMENT(Logging){_Log(FMT,##ARGS);}\
    3.18 +                         } }while(0)
    3.19 +#define LogTo(DOMAIN,FMT,ARGS...) do{if(__builtin_expect(_gShouldLog,0)) {\
    3.20 +                                    IN_SEGMENT(Logging) {_LogTo(@""#DOMAIN,FMT,##ARGS);}\
    3.21 +                                  } }while(0)
    3.22  #define Warn Warn
    3.23  
    3.24  void AlwaysLog( NSString *msg, ... ) __attribute__((format(__NSString__, 1, 2)));
    3.25 -
    3.26 -#define LogTo(DOMAIN,FMT,ARGS...) do{if(__builtin_expect(_gShouldLog,0)) _LogTo(@""#DOMAIN,FMT,##ARGS);}while(0)
    3.27 -
    3.28  BOOL _WillLogTo( NSString *domain );
    3.29  BOOL EnableLog( BOOL enable );
    3.30  #define EnableLogTo( DOMAIN, VALUE )  _EnableLogTo(@""#DOMAIN, VALUE)
     4.1 --- a/Target.m	Fri May 02 12:49:43 2008 -0700
     4.2 +++ b/Target.m	Wed May 07 16:47:44 2008 -0700
     4.3 @@ -136,6 +136,7 @@
     4.4      id result;
     4.5      NSMutableArray *invocations = $castIf(NSMutableArray,_invocations);
     4.6      if( invocations ) {
     4.7 +        result = nil;
     4.8          for( NSInvocation *invocation in invocations )
     4.9              result = invokeSingleTarget(invocation,sender);
    4.10      } else {