Commits

Jens Alfke  committed e40c8ea

Added a few new CollectionUtils.
Fixed a few compiler warnings.

  • Participants
  • Parent commits e75ce5d

Comments (0)

Files changed (6)

File CollectionUtils.h

 static inline void setString( NSString **var, NSString *value ) {setObjCopy(var,value);}
 static inline BOOL ifSetString( NSString **var, NSString *value ) {return ifSetObjCopy(var,value);}
 
+BOOL kvSetObj( id owner, NSString *property, id *varPtr, id value );
+BOOL kvSetObjCopy( id owner, NSString *property, id *varPtr, id value );
 BOOL kvSetSet( id owner, NSString *property, NSMutableSet *set, NSSet *newSet );
 BOOL kvAddToSet( id owner, NSString *property, NSMutableSet *set, id objToAdd );
 BOOL kvRemoveFromSet( id owner, NSString *property, NSMutableSet *set, id objToRemove );
 #define $false  ((NSNumber*)kCFBooleanFalse)
 
 
+@interface NSObject (MYUtils)
+- (NSString*) my_compactDescription;
+@end
+
 @interface NSArray (MYUtils)
 - (BOOL) my_containsObjectIdenticalTo: (id)object;
+- (NSArray*) my_arrayByApplyingSelector: (SEL)selector;
+- (NSArray*) my_arrayByApplyingSelector: (SEL)selector withObject: (id)object;
 @end
 
 

File CollectionUtils.m

     if( e=='r' )                // ignore 'const' modifier
         e = encoding[1];
     switch( e ) {
+        case 'B':   return [NSNumber numberWithBool: *(BOOL*)value];
         case 'c':   return [NSNumber numberWithChar: *(char*)value];
         case 'C':   return [NSNumber numberWithUnsignedChar: *(char*)value];
         case 's':   return [NSNumber numberWithShort: *(short*)value];
 }
 
 
+BOOL kvSetObj( id owner, NSString *property, id *varPtr, id value )
+{
+    if( *varPtr != value && ![*varPtr isEqual: value] ) {
+        [owner willChangeValueForKey: property];
+        [*varPtr autorelease];
+        *varPtr = [value retain];
+        [owner didChangeValueForKey: property];
+        return YES;
+    } else {
+        return NO;
+    }
+}
+
+
+BOOL kvSetObjCopy( id owner, NSString *property, id *varPtr, id value )
+{
+    if( *varPtr != value && ![*varPtr isEqual: value] ) {
+        [owner willChangeValueForKey: property];
+        [*varPtr autorelease];
+        *varPtr = [value copy];
+        [owner didChangeValueForKey: property];
+        return YES;
+    } else {
+        return NO;
+    }
+}
+
+
 BOOL kvSetSet( id owner, NSString *property, NSMutableSet *set, NSSet *newSet ) {
     CAssert(set);
     if (!newSet)
 }
 
 
+@implementation NSObject (MYUtils)
+- (NSString*) my_compactDescription
+{
+    return [self description];
+}
+@end
+
+
 @implementation NSArray (MYUtils)
 
 - (BOOL) my_containsObjectIdenticalTo: (id)object
     return [self indexOfObjectIdenticalTo: object] != NSNotFound;
 }
 
+- (NSArray*) my_arrayByApplyingSelector: (SEL)selector
+{
+    return [self my_arrayByApplyingSelector: selector withObject: nil];
+}
+
+- (NSArray*) my_arrayByApplyingSelector: (SEL)selector withObject: (id)object
+{
+    NSUInteger count = [self count];
+    NSMutableArray *temp = [[NSMutableArray alloc] initWithCapacity: count];
+    NSArray *result;
+    NSUInteger i;
+    for( i=0; i<count; i++ )
+        [temp addObject: [[self objectAtIndex: i] performSelector: selector withObject: object]];
+    result = [NSArray arrayWithArray: temp];
+    [temp release];
+    return result;
+}
+
+- (NSString*) my_compactDescription
+{
+    NSMutableString *desc = [NSMutableString stringWithCapacity: 100];
+    [desc appendString: @"["];
+    BOOL first = YES;
+    for(id item in self) {
+        if( first )
+            first = NO;
+        else
+            [desc appendString: @", "];
+        [desc appendString: [item my_compactDescription]];
+    }
+    [desc appendString: @"]"];
+    return desc;
+}
+
 @end
 
 
 
 @implementation NSSet (MYUtils)
 
+- (NSString*) my_compactDescription
+{
+    return [[self allObjects] my_compactDescription];
+}
+
 + (NSSet*) my_unionOfSet: (NSSet*)set1 andSet: (NSSet*)set2
 {
     if( set1 == set2 || set2.count==0 )
 @end
 
 
+@implementation NSDictionary (MYUtils)
+
+- (NSString*) my_compactDescription
+{
+    NSMutableString *desc = [NSMutableString stringWithCapacity: 100];
+    [desc appendString: @"{"];
+    BOOL first = YES;
+    for(id key in [[self allKeys] sortedArrayUsingSelector: @selector(compare:)]) {
+        if( first )
+            first = NO;
+        else
+            [desc appendString: @", "];
+        id value = [self objectForKey: key];
+        [desc appendString: [key description]];
+        [desc appendString: @"= "];
+        [desc appendString: [value my_compactDescription]];
+    }
+    [desc appendString: @"}"];
+    return desc;
+}
+
+@end
+
 
 @implementation NSData (MYUtils)
 

File ExceptionUtils.m

 - (void) _showExceptionAlert: (NSException*)x
 {
     NSString *stack = [x my_callStack] ?:@"";
-    int r = NSRunCriticalAlertPanel( @"Internal Error!",
+    NSInteger r = NSRunCriticalAlertPanel( @"Internal Error!",
                             [NSString stringWithFormat: @"Uncaught exception: %@\n%@\n\n%@\n\n"
                              "Please report this bug (you can copy & paste the text).",
                              [x name], [x reason], stack],

File MYErrorUtils.h

                                 __attribute__ ((format (__NSString__, 2, 3)));
 
 /** Returns an NSError based on the global variable 'errno'. Useful with POSIX calls. */
-NSError *MYErrorFromErrno();
+NSError *MYErrorFromErrno(void);
 
 /** Utilities for converting NSErrors to user-readable (but geeky) strings.
     If your code uses Security or SSL APIs, you will probably want to #define

File MYErrorUtils.m

 }
 
 
-NSError *MYErrorFromErrno()
+NSError *MYErrorFromErrno(void)
 {
     int err = errno;
     if (!err)

File MYUtilities.xcconfig

 
 // Warnings:
 GCC_TREAT_WARNINGS_AS_ERRORS = YES
+GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES
 GCC_WARN_ABOUT_RETURN_TYPE = YES
 GCC_WARN_TYPECHECK_CALLS_TO_PRINTF = YES
 GCC_WARN_UNUSED_VARIABLE = YES