Added AudioUtils.
5 // Created by Jens Alfke on 1/5/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
9 #import <Foundation/Foundation.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 // Apply a selector to each array element, returning an array of the results:
27 NSArray* $apply( NSArray *src, SEL selector, id defaultValue );
28 NSArray* $applyKeyPath( NSArray *src, NSString *keyPath, id defaultValue );
31 // Object conveniences:
33 BOOL $equal(id obj1, id obj2); // Like -isEqual: but works even if either/both are nil
35 NSString* $string( const char *utf8Str );
37 #define $sprintf(FORMAT, ARGS... ) [NSString stringWithFormat: (FORMAT), ARGS]
39 #define $cast(CLASSNAME,OBJ) ((CLASSNAME*)(_cast([CLASSNAME class],(OBJ))))
40 #define $castNotNil(CLASSNAME,OBJ) ((CLASSNAME*)(_castNotNil([CLASSNAME class],(OBJ))))
41 #define $castIf(CLASSNAME,OBJ) ((CLASSNAME*)(_castIf([CLASSNAME class],(OBJ))))
42 #define $castArrayOf(ITEMCLASSNAME,OBJ) _castArrayOf([ITEMCLASSNAME class],(OBJ)))
44 void setObj( id *var, id value );
45 BOOL ifSetObj( id *var, id value );
46 void setString( NSString **var, NSString *value );
47 BOOL ifSetString( NSString **var, NSString *value );
50 #define $true ((NSNumber*)kCFBooleanTrue)
51 #define $false ((NSNumber*)kCFBooleanFalse)
54 @interface NSArray (MYUtils)
55 - (BOOL) my_containsObjectIdenticalTo: (id)object;
59 @interface NSSet (MYUtils)
60 + (NSSet*) my_unionOfSet: (NSSet*)set1 andSet: (NSSet*)set2;
61 + (NSSet*) my_intersectionOfSet: (NSSet*)set1 andSet: (NSSet*)set2;
62 + (NSSet*) my_differenceOfSet: (NSSet*)set1 andSet: (NSSet*)set2;
69 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
70 #define foreach(VAR,ARR) for(VAR in ARR)
73 struct foreachstate {NSArray *array; unsigned n, i;};
74 static inline struct foreachstate _initforeach( NSArray *arr ) {
75 struct foreachstate s;
81 #define foreach(VAR,ARR) for( struct foreachstate _s = _initforeach((ARR)); \
82 _s.i<_s.n && ((VAR)=[_s.array objectAtIndex: _s.i], YES); \
87 // Internals (don't use directly)
88 struct _dictpair { id key; id value; };
89 NSDictionary* _dictof(const struct _dictpair*, size_t count);
90 NSMutableDictionary* _mdictof(const struct _dictpair*, size_t count);
91 NSValue* _box(const void *value, const char *encoding);
93 id _castNotNil(Class,id);
95 NSArray* _castArrayOf(Class,NSArray*);