Miscellaneous improvements.
5 // Created by Jens Alfke on 1/5/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
9 #import <Cocoa/Cocoa.h>
11 // Collection creation conveniences:
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)];})
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));})
23 #define $object(VAL) ({__typeof(VAL) v=(VAL); _box(&v,@encode(__typeof(v)));})
26 // Object conveniences:
28 BOOL $equal(id obj1, id obj2); // Like -isEqual: but works even if either/both are nil
30 #define $sprintf(FORMAT, ARGS... ) [NSString stringWithFormat: (FORMAT), ARGS]
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)))
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 );
43 @interface NSArray (MYUtils)
44 - (BOOL) my_containsObjectIdenticalTo: (id)object;
51 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
52 #define foreach(VAR,ARR) for(VAR in ARR)
55 struct foreachstate {NSArray *array; unsigned n, i;};
56 static inline struct foreachstate _initforeach( NSArray *arr ) {
57 struct foreachstate s;
63 #define foreach(VAR,ARR) for( struct foreachstate _s = _initforeach((ARR)); \
64 _s.i<_s.n && ((VAR)=[_s.array objectAtIndex: _s.i], YES); \
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);
75 id _castNotNil(Class,id);
77 NSArray* _castArrayOf(Class,NSArray*);