CollectionUtils.h
author Jens Alfke <jens@mooseyard.com>
Sat Mar 28 09:36:46 2009 -0700 (2009-03-28)
changeset 20 5a71993a1a70
parent 11 e5976864dfe9
child 29 8874aff14cc9
permissions -rw-r--r--
Added some new utilities, taken from Murky.
     1 //
     2 //  CollectionUtils.h
     3 //  MYUtilities
     4 //
     5 //  Created by Jens Alfke on 1/5/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 #define _MYUTILITIES_COLLECTIONUTILS_ 1
    11 
    12 // Collection creation conveniences:
    13 
    14 #define $array(OBJS...)     ({id objs[]={OBJS}; \
    15                               [NSArray arrayWithObjects: objs count: sizeof(objs)/sizeof(id)];})
    16 #define $marray(OBJS...)    ({id objs[]={OBJS}; \
    17                               [NSMutableArray arrayWithObjects: objs count: sizeof(objs)/sizeof(id)];})
    18 
    19 #define $dict(PAIRS...)     ({struct _dictpair pairs[]={PAIRS}; \
    20                               _dictof(pairs,sizeof(pairs)/sizeof(struct _dictpair));})
    21 #define $mdict(PAIRS...)    ({struct _dictpair pairs[]={PAIRS}; \
    22                               _mdictof(pairs,sizeof(pairs)/sizeof(struct _dictpair));})
    23 
    24 #define $object(VAL)        ({__typeof(VAL) v=(VAL); _box(&v,@encode(__typeof(v)));})
    25 
    26 
    27 // Apply a selector to each array element, returning an array of the results:
    28 NSArray* $apply( NSArray *src, SEL selector, id defaultValue );
    29 NSArray* $applyKeyPath( NSArray *src, NSString *keyPath, id defaultValue );
    30 
    31 
    32 // Object conveniences:
    33 
    34 BOOL $equal(id obj1, id obj2);      // Like -isEqual: but works even if either/both are nil
    35 
    36 NSString* $string( const char *utf8Str );
    37 
    38 #define $sprintf(FORMAT, ARGS... )  [NSString stringWithFormat: (FORMAT), ARGS]
    39 
    40 #define $cast(CLASSNAME,OBJ)        ((CLASSNAME*)(_cast([CLASSNAME class],(OBJ))))
    41 #define $castNotNil(CLASSNAME,OBJ)  ((CLASSNAME*)(_castNotNil([CLASSNAME class],(OBJ))))
    42 #define $castIf(CLASSNAME,OBJ)      ((CLASSNAME*)(_castIf([CLASSNAME class],(OBJ))))
    43 #define $castArrayOf(ITEMCLASSNAME,OBJ) _castArrayOf([ITEMCLASSNAME class],(OBJ)))
    44 
    45 void setObj( id *var, id value );
    46 BOOL ifSetObj( id *var, id value );
    47 void setString( NSString **var, NSString *value );
    48 BOOL ifSetString( NSString **var, NSString *value );
    49 
    50 
    51 #define $true   ((NSNumber*)kCFBooleanTrue)
    52 #define $false  ((NSNumber*)kCFBooleanFalse)
    53 
    54 
    55 @interface NSArray (MYUtils)
    56 - (BOOL) my_containsObjectIdenticalTo: (id)object;
    57 @end
    58 
    59 
    60 @interface NSSet (MYUtils)
    61 + (NSSet*) my_unionOfSet: (NSSet*)set1 andSet: (NSSet*)set2;
    62 + (NSSet*) my_intersectionOfSet: (NSSet*)set1 andSet: (NSSet*)set2;
    63 + (NSSet*) my_differenceOfSet: (NSSet*)set1 andSet: (NSSet*)set2;
    64 @end
    65 
    66 
    67 #pragma mark -
    68 #pragma mark FOREACH:
    69     
    70 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
    71 #define foreach(VAR,ARR) for(VAR in ARR)
    72 
    73 #else
    74 struct foreachstate {NSArray *array; unsigned n, i;};
    75 static inline struct foreachstate _initforeach( NSArray *arr ) {
    76     struct foreachstate s;
    77     s.array = arr;
    78     s.n = [arr count];
    79     s.i = 0;
    80     return s;
    81 }
    82 #define foreach(VAR,ARR) for( struct foreachstate _s = _initforeach((ARR)); \
    83                                    _s.i<_s.n && ((VAR)=[_s.array objectAtIndex: _s.i], YES); \
    84                                    _s.i++ )
    85 #endif
    86 
    87 
    88 // Internals (don't use directly)
    89 struct _dictpair { id key; id value; };
    90 NSDictionary* _dictof(const struct _dictpair*, size_t count);
    91 NSMutableDictionary* _mdictof(const struct _dictpair*, size_t count);
    92 NSValue* _box(const void *value, const char *encoding);
    93 id _cast(Class,id);
    94 id _castNotNil(Class,id);
    95 id _castIf(Class,id);
    96 NSArray* _castArrayOf(Class,NSArray*);