# HG changeset patch
# User Jens Alfke <jens@mooseyard.com>
# Date 1242007063 25200
# Node ID 8874aff14cc9ee287832aa9723e7b55c5fa9e910
# Parent  206903ba35a57f7f44528243004237349777c266
* Added kv*Set utility functions for handling KV grunge when mutating an NSSet property.
* Change CFMakeCollectable to NSMakeCollectable.

diff -r 206903ba35a5 -r 8874aff14cc9 CollectionUtils.h
--- a/CollectionUtils.h	Tue May 05 11:09:06 2009 -0700
+++ b/CollectionUtils.h	Sun May 10 18:57:43 2009 -0700
@@ -47,6 +47,10 @@
 void setString( NSString **var, NSString *value );
 BOOL ifSetString( NSString **var, NSString *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 $true   ((NSNumber*)kCFBooleanTrue)
 #define $false  ((NSNumber*)kCFBooleanFalse)
diff -r 206903ba35a5 -r 8874aff14cc9 CollectionUtils.m
--- a/CollectionUtils.m	Tue May 05 11:09:06 2009 -0700
+++ b/CollectionUtils.m	Sun May 10 18:57:43 2009 -0700
@@ -180,6 +180,59 @@
 }
 
 
+BOOL kvSetSet( id owner, NSString *property, NSMutableSet *set, NSSet *newSet ) {
+    CAssert(set);
+    if (!newSet)
+        newSet = [NSSet set];
+    if (![set isEqualToSet: newSet]) {
+        [owner willChangeValueForKey: property
+                     withSetMutation:NSKeyValueSetSetMutation 
+                        usingObjects:newSet]; 
+        [set setSet: newSet];
+        [owner didChangeValueForKey: property 
+                    withSetMutation:NSKeyValueSetSetMutation 
+                       usingObjects:newSet]; 
+        return YES;
+    } else
+        return NO;
+}
+
+
+BOOL kvAddToSet( id owner, NSString *property, NSMutableSet *set, id objToAdd ) {
+    CAssert(set);
+    if (![set containsObject: objToAdd]) {
+        NSSet *changedObjects = [[NSSet alloc] initWithObjects: &objToAdd count: 1];
+        [owner willChangeValueForKey: property
+                     withSetMutation: NSKeyValueUnionSetMutation 
+                        usingObjects: changedObjects]; 
+        [set addObject: objToAdd];
+        [owner didChangeValueForKey: property 
+                    withSetMutation: NSKeyValueUnionSetMutation 
+                       usingObjects: changedObjects]; 
+        [changedObjects release];
+        return YES;
+    } else
+        return NO;
+}
+
+
+BOOL kvRemoveFromSet( id owner, NSString *property, NSMutableSet *set, id objToRemove ) {
+    if ([set containsObject: objToRemove]) {
+        NSSet *changedObjects = [[NSSet alloc] initWithObjects: &objToRemove count: 1];
+        [owner willChangeValueForKey: property
+                     withSetMutation: NSKeyValueMinusSetMutation 
+                        usingObjects: changedObjects]; 
+        [set removeObject: objToRemove];
+        [owner didChangeValueForKey: property 
+                    withSetMutation: NSKeyValueMinusSetMutation 
+                       usingObjects: changedObjects]; 
+        [changedObjects release];
+        return YES;
+    } else
+        return NO;
+}
+
+
 @implementation NSArray (MYUtils)
 
 - (BOOL) my_containsObjectIdenticalTo: (id)object
diff -r 206903ba35a5 -r 8874aff14cc9 MYErrorUtils.m
--- a/MYErrorUtils.m	Tue May 05 11:09:06 2009 -0700
+++ b/MYErrorUtils.m	Sun May 10 18:57:43 2009 -0700
@@ -140,7 +140,7 @@
 #if USE_SECURITY_API
             result = (id) SecCopyErrorMessageString(code,NULL);
             if (result) {
-                [(id)CFMakeCollectable(result) autorelease];
+                [NSMakeCollectable(result) autorelease];
                 if ([result hasPrefix: @"OSStatus "])
                     result = nil; // just a generic message
             }