Added some new utilities, taken from Murky.
5 // Created by Jens Alfke on 1/5/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
9 #import <Foundation/Foundation.h>
10 #define _MYUTILITIES_COLLECTIONUTILS_ 1
12 // Collection creation conveniences:
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)];})
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));})
24 #define $object(VAL) ({__typeof(VAL) v=(VAL); _box(&v,@encode(__typeof(v)));})
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 );
32 // Object conveniences:
34 BOOL $equal(id obj1, id obj2); // Like -isEqual: but works even if either/both are nil
36 NSString* $string( const char *utf8Str );
38 #define $sprintf(FORMAT, ARGS... ) [NSString stringWithFormat: (FORMAT), ARGS]
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)))
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 );
51 #define $true ((NSNumber*)kCFBooleanTrue)
52 #define $false ((NSNumber*)kCFBooleanFalse)
55 @interface NSArray (MYUtils)
56 - (BOOL) my_containsObjectIdenticalTo: (id)object;
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;
70 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
71 #define foreach(VAR,ARR) for(VAR in ARR)
74 struct foreachstate {NSArray *array; unsigned n, i;};
75 static inline struct foreachstate _initforeach( NSArray *arr ) {
76 struct foreachstate s;
82 #define foreach(VAR,ARR) for( struct foreachstate _s = _initforeach((ARR)); \
83 _s.i<_s.n && ((VAR)=[_s.array objectAtIndex: _s.i], YES); \
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);
94 id _castNotNil(Class,id);
96 NSArray* _castArrayOf(Class,NSArray*);