diff --git a/SOPE/NGCards/CardElement.h b/SOPE/NGCards/CardElement.h index de633d07c..1b5ada19f 100644 --- a/SOPE/NGCards/CardElement.h +++ b/SOPE/NGCards/CardElement.h @@ -100,10 +100,16 @@ - (CardGroup *) searchParentOfClass: (Class) parentClass; -- (CardElement *) elementWithClass: (Class) elementClass; +- (id) elementWithClass: (Class) elementClass; + - (void) setValuesAsCopy: (NSMutableArray *) someValues; - (void) setAttributesAsCopy: (NSMutableDictionary *) someAttributes; +- (NSMutableArray *) deepCopyOfArray: (NSArray *) oldArray + withZone: (NSZone *) aZone; +- (NSMutableDictionary *) deepCopyOfDictionary: (NSDictionary *) oldDictionary + withZone: (NSZone *) aZone; + @end #define IS_EQUAL(a,b,sel) \ diff --git a/SOPE/NGCards/CardElement.m b/SOPE/NGCards/CardElement.m index a6b1b5869..a1a6b2c9b 100644 --- a/SOPE/NGCards/CardElement.m +++ b/SOPE/NGCards/CardElement.m @@ -464,7 +464,7 @@ return string; } -- (CardElement *) elementWithClass: (Class) elementClass +- (id) elementWithClass: (Class) elementClass { CardElement *newElement; @@ -522,6 +522,41 @@ /* NSCopying */ +- (NSMutableArray *) deepCopyOfArray: (NSArray *) oldArray + withZone: (NSZone *) aZone +{ + NSMutableArray *newArray; + unsigned int count, max; + id newChild; + + newArray = [NSMutableArray array]; + + max = [oldArray count]; + for (count = 0; count < max; count++) + { + newChild = [[oldArray objectAtIndex: count] mutableCopyWithZone: aZone]; + [newArray addObject: newChild]; + } + + return newArray; +} + +- (NSMutableDictionary *) deepCopyOfDictionary: (NSDictionary *) oldDictionary + withZone: (NSZone *) aZone +{ + NSMutableDictionary *newDict; + NSArray *newKeys, *newValues; + + newKeys = [self deepCopyOfArray: [oldDictionary allKeys] + withZone: aZone]; + newValues = [self deepCopyOfArray: [oldDictionary allValues] + withZone: aZone]; + newDict = [NSMutableDictionary dictionaryWithObjects: newValues + forKeys: newKeys]; + + return newDict; +} + - (id) copyWithZone: (NSZone *) aZone { CardElement *new; @@ -529,9 +564,10 @@ new = [[self class] new]; [new setTag: [tag copyWithZone: aZone]]; [new setGroup: [group copyWithZone: aZone]]; - [new setParent: parent]; - [new setValuesAsCopy: [values copyWithZone: aZone]]; - [new setAttributesAsCopy: [attributes copyWithZone: aZone]]; + [new setParent: nil]; + [new setValuesAsCopy: [self deepCopyOfArray: values withZone: aZone]]; + [new setAttributesAsCopy: [self deepCopyOfDictionary: attributes + withZone: aZone]]; return new; } @@ -544,9 +580,10 @@ new = [[self class] new]; [new setTag: [tag mutableCopyWithZone: aZone]]; [new setGroup: [group mutableCopyWithZone: aZone]]; - [new setParent: parent]; - [new setValuesAsCopy: [values mutableCopyWithZone: aZone]]; - [new setAttributesAsCopy: [attributes mutableCopyWithZone: aZone]]; + [new setParent: nil]; + [new setValuesAsCopy: [self deepCopyOfArray: values withZone: aZone]]; + [new setAttributesAsCopy: [self deepCopyOfDictionary: attributes + withZone: aZone]]; return new; } diff --git a/SOPE/NGCards/CardGroup.h b/SOPE/NGCards/CardGroup.h index 2ef4adc46..50d7a130c 100644 --- a/SOPE/NGCards/CardGroup.h +++ b/SOPE/NGCards/CardGroup.h @@ -65,7 +65,6 @@ types: (NSArray *) someTypes singleValue: (NSString *) aValue; -- (CardGroup *) groupWithClass: (Class) groupClass; - (void) setChildrenAsCopy: (NSMutableArray *) someChildren; - (void) replaceThisElement: (CardElement *) oldElement diff --git a/SOPE/NGCards/CardGroup.m b/SOPE/NGCards/CardGroup.m index a608c47be..e1391682e 100644 --- a/SOPE/NGCards/CardGroup.m +++ b/SOPE/NGCards/CardGroup.m @@ -160,10 +160,7 @@ static NGCardsSaxHandler *sax = nil; { NSLog (@"warning: new child to entity '%@': '%@' converted to '%@'", tag, childTag, NSStringFromClass(mappedClass)); - if ([aChild isKindOfClass: [CardGroup class]]) - newChild = [(CardGroup *) aChild groupWithClass: mappedClass]; - else - newChild = [aChild elementWithClass: mappedClass]; + newChild = [aChild elementWithClass: mappedClass]; } } // else @@ -235,12 +232,7 @@ static NGCardsSaxHandler *sax = nil; child = [existing objectAtIndex: 0]; mappedClass = [self classForTag: [aTag uppercaseString]]; if (mappedClass) - { - if ([child isKindOfClass: [CardGroup class]]) - mappedChild = [(CardGroup *) child groupWithClass: mappedClass]; - else - mappedChild = [child elementWithClass: mappedClass]; - } + mappedChild = [child elementWithClass: mappedClass]; else mappedChild = child; } @@ -298,8 +290,7 @@ static NGCardsSaxHandler *sax = nil; CardGroup *element; NSString *value; - elements = [NSMutableArray new]; - [elements autorelease]; + elements = [NSMutableArray array]; allElements = [[self childrenWithTag: aTag] objectEnumerator]; element = [allElements nextObject]; @@ -317,8 +308,7 @@ static NGCardsSaxHandler *sax = nil; return elements; } -#warning should be renamed to elementWithClass... -- (CardGroup *) groupWithClass: (Class) groupClass +- (id) elementWithClass: (Class) groupClass { CardGroup *newGroup; @@ -326,7 +316,7 @@ static NGCardsSaxHandler *sax = nil; newGroup = self; else { - newGroup = (CardGroup *) [self elementWithClass: groupClass]; + newGroup = [super elementWithClass: groupClass]; [newGroup setChildrenAsCopy: children]; } @@ -335,14 +325,13 @@ static NGCardsSaxHandler *sax = nil; - (void) setChildrenAsCopy: (NSMutableArray *) someChildren { - NSEnumerator *list; - CardElement *currentChild; + unsigned int count, max; ASSIGN (children, someChildren); - list = [children objectEnumerator]; - while ((currentChild = [list nextObject])) - [currentChild setParent: self]; + max = [children count]; + for (count = 0; count < max; count++) + [[children objectAtIndex: count] setParent: self]; } - (void) addChildWithTag: (NSString *) aTag @@ -398,20 +387,10 @@ static NGCardsSaxHandler *sax = nil; - (id) copyWithZone: (NSZone *) aZone { CardGroup *new; - CardElement *newChild; - NSMutableArray *newChildren; - unsigned int count, max; new = [super copyWithZone: aZone]; - newChildren = [NSMutableArray new]; - max = [children count]; - for (count = 0; count < max; count++) - { - newChild = [[children objectAtIndex: count] copyWithZone: aZone]; - [newChildren addObject: newChild]; - } - [new setChildrenAsCopy: newChildren]; - [newChildren release]; + [new setChildrenAsCopy: [self deepCopyOfArray: children + withZone: aZone]]; return new; } @@ -421,21 +400,10 @@ static NGCardsSaxHandler *sax = nil; - (id) mutableCopyWithZone: (NSZone *) aZone { CardGroup *new; - CardElement *newChild; - NSMutableArray *newChildren; - unsigned int count, max; new = [super mutableCopyWithZone: aZone]; - newChildren = [NSMutableArray new]; - max = [children count]; - for (count = 0; count < max; count++) - { - newChild - = [[children objectAtIndex: count] mutableCopyWithZone: aZone]; - [newChildren addObject: newChild]; - } - [new setChildrenAsCopy: newChildren]; - [newChildren release]; + [new setChildrenAsCopy: [self deepCopyOfArray: children + withZone: aZone]]; return new; } diff --git a/SOPE/NGCards/ChangeLog b/SOPE/NGCards/ChangeLog index 7e6a37822..149b881df 100644 --- a/SOPE/NGCards/ChangeLog +++ b/SOPE/NGCards/ChangeLog @@ -1,3 +1,17 @@ +2008-08-04 Wolfgang Sourdeau + + * CardGroup.m ([CardGroup -elementWithClass:groupClass]): replaces + "groupWithClass:". + + * CardElement.m ([CardElement + -deepCopyOfArray:oldArraywithZone:aZone]): new self-explaining + method. + ([CardElement -deepCopyOfDictionary:oldDictionarywithZone:aZone]): + new self-explaining method. + ([CardElement -copyWithZone:aZone]) + ([CardElement -mutableCopyWithZone:aZone]): make a deep copy of + the children elements. + 2008-07-17 Wolfgang Sourdeau * iCalRepeatableEntityObject.m ([iCalRepeatableEntityObject