jens@0
|
1 |
//
|
jens@0
|
2 |
// CollectionUtils.h
|
jens@0
|
3 |
// MYUtilities
|
jens@0
|
4 |
//
|
jens@0
|
5 |
// Created by Jens Alfke on 1/5/08.
|
jens@0
|
6 |
// Copyright 2008 Jens Alfke. All rights reserved.
|
jens@0
|
7 |
//
|
jens@0
|
8 |
|
jens@0
|
9 |
#import <Cocoa/Cocoa.h>
|
jens@0
|
10 |
|
jens@0
|
11 |
// Collection creation conveniences:
|
jens@0
|
12 |
|
jens@0
|
13 |
#define $array(OBJS...) ({id objs[]={OBJS}; \
|
jens@0
|
14 |
[NSArray arrayWithObjects: objs count: sizeof(objs)/sizeof(id)];})
|
jens@0
|
15 |
#define $marray(OBJS...) ({id objs[]={OBJS}; \
|
jens@0
|
16 |
[NSMutableArray arrayWithObjects: objs count: sizeof(objs)/sizeof(id)];})
|
jens@0
|
17 |
|
jens@0
|
18 |
#define $dict(PAIRS...) ({struct _dictpair pairs[]={PAIRS}; \
|
jens@0
|
19 |
_dictof(pairs,sizeof(pairs)/sizeof(struct _dictpair));})
|
jens@0
|
20 |
#define $mdict(PAIRS...) ({struct _dictpair pairs[]={PAIRS}; \
|
jens@0
|
21 |
_mdictof(pairs,sizeof(pairs)/sizeof(struct _dictpair));})
|
jens@0
|
22 |
|
jens@0
|
23 |
#define $object(VAL) ({__typeof(VAL) v=(VAL); _box(&v,@encode(__typeof(v)));})
|
jens@0
|
24 |
|
jens@0
|
25 |
|
jens@0
|
26 |
// Object conveniences:
|
jens@0
|
27 |
|
jens@0
|
28 |
BOOL $equal(id obj1, id obj2); // Like -isEqual: but works even if either/both are nil
|
jens@0
|
29 |
|
jens@0
|
30 |
#define $sprintf(FORMAT, ARGS... ) [NSString stringWithFormat: (FORMAT), ARGS]
|
jens@0
|
31 |
|
jens@4
|
32 |
#define $cast(CLASSNAME,OBJ) ((CLASSNAME*)(_cast([CLASSNAME class],(OBJ))))
|
jens@4
|
33 |
#define $castNotNil(CLASSNAME,OBJ) ((CLASSNAME*)(_castNotNil([CLASSNAME class],(OBJ))))
|
jens@4
|
34 |
#define $castIf(CLASSNAME,OBJ) ((CLASSNAME*)(_castIf([CLASSNAME class],(OBJ))))
|
jens@0
|
35 |
#define $castArrayOf(ITEMCLASSNAME,OBJ) _castArrayOf([ITEMCLASSNAME class],(OBJ)))
|
jens@0
|
36 |
|
jens@0
|
37 |
void setObj( id *var, id value );
|
jens@0
|
38 |
BOOL ifSetObj( id *var, id value );
|
jens@0
|
39 |
void setString( NSString **var, NSString *value );
|
jens@0
|
40 |
BOOL ifSetString( NSString **var, NSString *value );
|
jens@0
|
41 |
|
jens@0
|
42 |
|
jens@0
|
43 |
@interface NSArray (MYUtils)
|
jens@0
|
44 |
- (BOOL) my_containsObjectIdenticalTo: (id)object;
|
jens@0
|
45 |
@end
|
jens@0
|
46 |
|
jens@0
|
47 |
|
jens@5
|
48 |
@interface NSSet (MYUtils)
|
jens@5
|
49 |
+ (NSSet*) my_unionOfSet: (NSSet*)set1 andSet: (NSSet*)set2;
|
jens@5
|
50 |
+ (NSSet*) my_differenceOfSet: (NSSet*)set1 andSet: (NSSet*)set2;
|
jens@5
|
51 |
@end
|
jens@5
|
52 |
|
jens@5
|
53 |
|
jens@0
|
54 |
#pragma mark -
|
jens@0
|
55 |
#pragma mark FOREACH:
|
jens@0
|
56 |
|
jens@0
|
57 |
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
jens@0
|
58 |
#define foreach(VAR,ARR) for(VAR in ARR)
|
jens@0
|
59 |
|
jens@0
|
60 |
#else
|
jens@0
|
61 |
struct foreachstate {NSArray *array; unsigned n, i;};
|
jens@0
|
62 |
static inline struct foreachstate _initforeach( NSArray *arr ) {
|
jens@0
|
63 |
struct foreachstate s;
|
jens@0
|
64 |
s.array = arr;
|
jens@0
|
65 |
s.n = [arr count];
|
jens@0
|
66 |
s.i = 0;
|
jens@0
|
67 |
return s;
|
jens@0
|
68 |
}
|
jens@0
|
69 |
#define foreach(VAR,ARR) for( struct foreachstate _s = _initforeach((ARR)); \
|
jens@0
|
70 |
_s.i<_s.n && ((VAR)=[_s.array objectAtIndex: _s.i], YES); \
|
jens@0
|
71 |
_s.i++ )
|
jens@0
|
72 |
#endif
|
jens@0
|
73 |
|
jens@0
|
74 |
|
jens@0
|
75 |
// Internals (don't use directly)
|
jens@0
|
76 |
struct _dictpair { id key; id value; };
|
jens@0
|
77 |
NSDictionary* _dictof(const struct _dictpair*, size_t count);
|
jens@0
|
78 |
NSMutableDictionary* _mdictof(const struct _dictpair*, size_t count);
|
jens@0
|
79 |
NSValue* _box(const void *value, const char *encoding);
|
jens@0
|
80 |
id _cast(Class,id);
|
jens@0
|
81 |
id _castNotNil(Class,id);
|
jens@0
|
82 |
id _castIf(Class,id);
|
jens@0
|
83 |
NSArray* _castArrayOf(Class,NSArray*);
|