CollectionUtils.h
author Jens Alfke <jens@mooseyard.com>
Sun Apr 06 19:13:27 2008 -0700 (2008-04-06)
changeset 3 8fad19466c59
child 4 64823cdde6a5
permissions -rw-r--r--
Miscellaneous improvements.
     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 <Cocoa/Cocoa.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 // Object conveniences:
    27 
    28 BOOL $equal(id obj1, id obj2);      // Like -isEqual: but works even if either/both are nil
    29 
    30 #define $sprintf(FORMAT, ARGS... )  [NSString stringWithFormat: (FORMAT), ARGS]
    31 
    32 #define $cast(CLASSNAME,OBJ)        (CLASSNAME*)(_cast([CLASSNAME class],(OBJ)))
    33 #define $castNotNil(CLASSNAME,OBJ)  (CLASSNAME*)(_castNotNil([CLASSNAME class],(OBJ)))
    34 #define $castIf(CLASSNAME,OBJ)      (CLASSNAME*)(_castIf([CLASSNAME class],(OBJ)))
    35 #define $castArrayOf(ITEMCLASSNAME,OBJ) _castArrayOf([ITEMCLASSNAME class],(OBJ)))
    36 
    37 void setObj( id *var, id value );
    38 BOOL ifSetObj( id *var, id value );
    39 void setString( NSString **var, NSString *value );
    40 BOOL ifSetString( NSString **var, NSString *value );
    41 
    42 
    43 @interface NSArray (MYUtils)
    44 - (BOOL) my_containsObjectIdenticalTo: (id)object;
    45 @end
    46 
    47 
    48 #pragma mark -
    49 #pragma mark FOREACH:
    50     
    51 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
    52 #define foreach(VAR,ARR) for(VAR in ARR)
    53 
    54 #else
    55 struct foreachstate {NSArray *array; unsigned n, i;};
    56 static inline struct foreachstate _initforeach( NSArray *arr ) {
    57     struct foreachstate s;
    58     s.array = arr;
    59     s.n = [arr count];
    60     s.i = 0;
    61     return s;
    62 }
    63 #define foreach(VAR,ARR) for( struct foreachstate _s = _initforeach((ARR)); \
    64                                    _s.i<_s.n && ((VAR)=[_s.array objectAtIndex: _s.i], YES); \
    65                                    _s.i++ )
    66 #endif
    67 
    68 
    69 // Internals (don't use directly)
    70 struct _dictpair { id key; id value; };
    71 NSDictionary* _dictof(const struct _dictpair*, size_t count);
    72 NSMutableDictionary* _mdictof(const struct _dictpair*, size_t count);
    73 NSValue* _box(const void *value, const char *encoding);
    74 id _cast(Class,id);
    75 id _castNotNil(Class,id);
    76 id _castIf(Class,id);
    77 NSArray* _castArrayOf(Class,NSArray*);