CollectionUtils.h
author Jens Alfke <jens@mooseyard.com>
Wed Sep 02 08:41:25 2009 -0700 (2009-09-02)
changeset 35 5cab3034d3a1
parent 31 2068331949ee
permissions -rw-r--r--
10.6 compatibility: Fix some new compiler warnings, and work around apparent regressions in NSTask and -stringByStandardizingPath.
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@11
     9
#import <Foundation/Foundation.h>
jens@16
    10
#define _MYUTILITIES_COLLECTIONUTILS_ 1
jens@0
    11
jens@0
    12
// Collection creation conveniences:
jens@0
    13
jens@0
    14
#define $array(OBJS...)     ({id objs[]={OBJS}; \
jens@0
    15
                              [NSArray arrayWithObjects: objs count: sizeof(objs)/sizeof(id)];})
jens@0
    16
#define $marray(OBJS...)    ({id objs[]={OBJS}; \
jens@0
    17
                              [NSMutableArray arrayWithObjects: objs count: sizeof(objs)/sizeof(id)];})
jens@0
    18
jens@0
    19
#define $dict(PAIRS...)     ({struct _dictpair pairs[]={PAIRS}; \
jens@0
    20
                              _dictof(pairs,sizeof(pairs)/sizeof(struct _dictpair));})
jens@0
    21
#define $mdict(PAIRS...)    ({struct _dictpair pairs[]={PAIRS}; \
jens@0
    22
                              _mdictof(pairs,sizeof(pairs)/sizeof(struct _dictpair));})
jens@0
    23
jens@0
    24
#define $object(VAL)        ({__typeof(VAL) v=(VAL); _box(&v,@encode(__typeof(v)));})
jens@0
    25
jens@0
    26
jens@8
    27
// Apply a selector to each array element, returning an array of the results:
jens@8
    28
NSArray* $apply( NSArray *src, SEL selector, id defaultValue );
jens@8
    29
NSArray* $applyKeyPath( NSArray *src, NSString *keyPath, id defaultValue );
jens@8
    30
jens@8
    31
jens@0
    32
// Object conveniences:
jens@0
    33
jens@0
    34
BOOL $equal(id obj1, id obj2);      // Like -isEqual: but works even if either/both are nil
jens@0
    35
jens@9
    36
NSString* $string( const char *utf8Str );
jens@9
    37
jens@0
    38
#define $sprintf(FORMAT, ARGS... )  [NSString stringWithFormat: (FORMAT), ARGS]
jens@0
    39
jens@4
    40
#define $cast(CLASSNAME,OBJ)        ((CLASSNAME*)(_cast([CLASSNAME class],(OBJ))))
jens@4
    41
#define $castNotNil(CLASSNAME,OBJ)  ((CLASSNAME*)(_castNotNil([CLASSNAME class],(OBJ))))
jens@4
    42
#define $castIf(CLASSNAME,OBJ)      ((CLASSNAME*)(_castIf([CLASSNAME class],(OBJ))))
jens@0
    43
#define $castArrayOf(ITEMCLASSNAME,OBJ) _castArrayOf([ITEMCLASSNAME class],(OBJ)))
jens@0
    44
jens@0
    45
void setObj( id *var, id value );
jens@0
    46
BOOL ifSetObj( id *var, id value );
jens@33
    47
void setObjCopy( id *var, id valueToCopy );
jens@33
    48
BOOL ifSetObjCopy( id *var, id value );
jens@33
    49
jens@33
    50
static inline void setString( NSString **var, NSString *value ) {setObjCopy(var,value);}
jens@33
    51
static inline BOOL ifSetString( NSString **var, NSString *value ) {return ifSetObjCopy(var,value);}
jens@0
    52
jens@29
    53
BOOL kvSetSet( id owner, NSString *property, NSMutableSet *set, NSSet *newSet );
jens@29
    54
BOOL kvAddToSet( id owner, NSString *property, NSMutableSet *set, id objToAdd );
jens@29
    55
BOOL kvRemoveFromSet( id owner, NSString *property, NSMutableSet *set, id objToRemove );
jens@29
    56
jens@0
    57
jens@8
    58
#define $true   ((NSNumber*)kCFBooleanTrue)
jens@8
    59
#define $false  ((NSNumber*)kCFBooleanFalse)
jens@8
    60
jens@8
    61
jens@0
    62
@interface NSArray (MYUtils)
jens@0
    63
- (BOOL) my_containsObjectIdenticalTo: (id)object;
jens@0
    64
@end
jens@0
    65
jens@0
    66
jens@5
    67
@interface NSSet (MYUtils)
jens@5
    68
+ (NSSet*) my_unionOfSet: (NSSet*)set1 andSet: (NSSet*)set2;
jens@7
    69
+ (NSSet*) my_intersectionOfSet: (NSSet*)set1 andSet: (NSSet*)set2;
jens@5
    70
+ (NSSet*) my_differenceOfSet: (NSSet*)set1 andSet: (NSSet*)set2;
jens@5
    71
@end
jens@5
    72
jens@5
    73
jens@31
    74
@interface NSData (MYUtils)
jens@31
    75
- (NSString*) my_UTF8ToString;
jens@31
    76
@end
jens@31
    77
jens@31
    78
jens@31
    79
jens@0
    80
#pragma mark -
jens@0
    81
#pragma mark FOREACH:
jens@0
    82
    
jens@0
    83
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
jens@0
    84
#define foreach(VAR,ARR) for(VAR in ARR)
jens@0
    85
jens@0
    86
#else
jens@0
    87
struct foreachstate {NSArray *array; unsigned n, i;};
jens@0
    88
static inline struct foreachstate _initforeach( NSArray *arr ) {
jens@0
    89
    struct foreachstate s;
jens@0
    90
    s.array = arr;
jens@0
    91
    s.n = [arr count];
jens@0
    92
    s.i = 0;
jens@0
    93
    return s;
jens@0
    94
}
jens@0
    95
#define foreach(VAR,ARR) for( struct foreachstate _s = _initforeach((ARR)); \
jens@0
    96
                                   _s.i<_s.n && ((VAR)=[_s.array objectAtIndex: _s.i], YES); \
jens@0
    97
                                   _s.i++ )
jens@0
    98
#endif
jens@0
    99
jens@0
   100
jens@0
   101
// Internals (don't use directly)
jens@0
   102
struct _dictpair { id key; id value; };
jens@0
   103
NSDictionary* _dictof(const struct _dictpair*, size_t count);
jens@0
   104
NSMutableDictionary* _mdictof(const struct _dictpair*, size_t count);
jens@0
   105
NSValue* _box(const void *value, const char *encoding);
jens@0
   106
id _cast(Class,id);
jens@0
   107
id _castNotNil(Class,id);
jens@0
   108
id _castIf(Class,id);
jens@0
   109
NSArray* _castArrayOf(Class,NSArray*);