jens@0: // jens@0: // CollectionUtils.h jens@0: // MYUtilities jens@0: // jens@0: // Created by Jens Alfke on 1/5/08. jens@0: // Copyright 2008 Jens Alfke. All rights reserved. jens@0: // jens@0: jens@0: #import jens@0: jens@0: // Collection creation conveniences: jens@0: jens@0: #define $array(OBJS...) ({id objs[]={OBJS}; \ jens@0: [NSArray arrayWithObjects: objs count: sizeof(objs)/sizeof(id)];}) jens@0: #define $marray(OBJS...) ({id objs[]={OBJS}; \ jens@0: [NSMutableArray arrayWithObjects: objs count: sizeof(objs)/sizeof(id)];}) jens@0: jens@0: #define $dict(PAIRS...) ({struct _dictpair pairs[]={PAIRS}; \ jens@0: _dictof(pairs,sizeof(pairs)/sizeof(struct _dictpair));}) jens@0: #define $mdict(PAIRS...) ({struct _dictpair pairs[]={PAIRS}; \ jens@0: _mdictof(pairs,sizeof(pairs)/sizeof(struct _dictpair));}) jens@0: jens@0: #define $object(VAL) ({__typeof(VAL) v=(VAL); _box(&v,@encode(__typeof(v)));}) jens@0: jens@0: jens@0: // Object conveniences: jens@0: jens@0: BOOL $equal(id obj1, id obj2); // Like -isEqual: but works even if either/both are nil jens@0: jens@0: #define $sprintf(FORMAT, ARGS... ) [NSString stringWithFormat: (FORMAT), ARGS] jens@0: jens@4: #define $cast(CLASSNAME,OBJ) ((CLASSNAME*)(_cast([CLASSNAME class],(OBJ)))) jens@4: #define $castNotNil(CLASSNAME,OBJ) ((CLASSNAME*)(_castNotNil([CLASSNAME class],(OBJ)))) jens@4: #define $castIf(CLASSNAME,OBJ) ((CLASSNAME*)(_castIf([CLASSNAME class],(OBJ)))) jens@0: #define $castArrayOf(ITEMCLASSNAME,OBJ) _castArrayOf([ITEMCLASSNAME class],(OBJ))) jens@0: jens@0: void setObj( id *var, id value ); jens@0: BOOL ifSetObj( id *var, id value ); jens@0: void setString( NSString **var, NSString *value ); jens@0: BOOL ifSetString( NSString **var, NSString *value ); jens@0: jens@0: jens@0: @interface NSArray (MYUtils) jens@0: - (BOOL) my_containsObjectIdenticalTo: (id)object; jens@0: @end jens@0: jens@0: jens@5: @interface NSSet (MYUtils) jens@5: + (NSSet*) my_unionOfSet: (NSSet*)set1 andSet: (NSSet*)set2; jens@5: + (NSSet*) my_differenceOfSet: (NSSet*)set1 andSet: (NSSet*)set2; jens@5: @end jens@5: jens@5: jens@0: #pragma mark - jens@0: #pragma mark FOREACH: jens@0: jens@0: #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 jens@0: #define foreach(VAR,ARR) for(VAR in ARR) jens@0: jens@0: #else jens@0: struct foreachstate {NSArray *array; unsigned n, i;}; jens@0: static inline struct foreachstate _initforeach( NSArray *arr ) { jens@0: struct foreachstate s; jens@0: s.array = arr; jens@0: s.n = [arr count]; jens@0: s.i = 0; jens@0: return s; jens@0: } jens@0: #define foreach(VAR,ARR) for( struct foreachstate _s = _initforeach((ARR)); \ jens@0: _s.i<_s.n && ((VAR)=[_s.array objectAtIndex: _s.i], YES); \ jens@0: _s.i++ ) jens@0: #endif jens@0: jens@0: jens@0: // Internals (don't use directly) jens@0: struct _dictpair { id key; id value; }; jens@0: NSDictionary* _dictof(const struct _dictpair*, size_t count); jens@0: NSMutableDictionary* _mdictof(const struct _dictpair*, size_t count); jens@0: NSValue* _box(const void *value, const char *encoding); jens@0: id _cast(Class,id); jens@0: id _castNotNil(Class,id); jens@0: id _castIf(Class,id); jens@0: NSArray* _castArrayOf(Class,NSArray*);