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