Oolite
Loading...
Searching...
No Matches
OODeepCopy.m
Go to the documentation of this file.
1/*
2
3OODeepCopy.m
4
5
6Copyright (C) 2009-2013 Jens Ayton
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25
26*/
27
28#import "OODeepCopy.h"
29
30id OODeepCopy(id object)
31{
32 NSAutoreleasePool* pool = nil;
33 NSMutableSet* objects = nil;
34
35 if (object == nil)
36 return nil;
37
38 pool = [[NSAutoreleasePool alloc] init];
39 objects = [NSMutableSet set];
40
41 object = [object ooDeepCopyWithSharedObjects:objects];
42
43 [pool release];
44
45 return object;
46}
47
48@implementation NSObject (OODeepCopy)
49
50- (id)ooDeepCopyWithSharedObjects:(NSMutableSet*)objects
51{
52 if ([self conformsToProtocol:@protocol(NSCopying)]) {
53 return [self copy];
54 } else {
55 return [self retain];
56 }
57}
58
59@end
60
61@implementation NSString (OODeepCopy)
62
63- (id)ooDeepCopyWithSharedObjects:(NSMutableSet*)objects
64{
65 NSUInteger length = [self length];
66 if (length == 0)
67 return [[NSString string] retain];
68 if (length > 128)
69 return [self copy];
70
71 id object = [objects member:self];
72 if (object != nil && [object isKindOfClass:[NSString class]]) {
73 return [object retain];
74 } else {
75 object = [self copy];
76 [objects addObject:object];
77 return object;
78 }
79}
80
81@end
82
83@implementation NSValue (OODeepCopy) // Includes NSNumber
84
85- (id)ooDeepCopyWithSharedObjects:(NSMutableSet*)objects
86{
87 id object = [objects member:self];
88 if (object != nil && [object isKindOfClass:[NSValue class]]) {
89 return [object retain];
90 } else {
91 object = [self copy];
92 [objects addObject:object];
93 return object;
94 }
95}
96
97@end
98
99@implementation NSArray (OODeepCopy)
100
101- (id)ooDeepCopyWithSharedObjects:(NSMutableSet*)objects
102{
103 NSUInteger i, count;
104 id* members = NULL;
105 NSArray* result = nil;
106 BOOL tempObjects = NO;
107
108 count = [self count];
109 if (count == 0)
110 return [[NSArray array] retain];
111
112 members = calloc(sizeof *members, count);
113 if (members == NULL) {
114 [NSException raise:NSMallocException format:@"Failed to allocate space for %zu objects in %s.", count, __PRETTY_FUNCTION__];
115 }
116
117 // Ensure there's an objects set even if passed nil.
118 if (objects == nil) {
119 objects = [[NSMutableSet alloc] init];
120 tempObjects = YES;
121 }
122
123 [self getObjects:members];
124 @try {
125 // Deep copy members.
126 for (i = 0; i < count; i++) {
127 members[i] = [members[i] ooDeepCopyWithSharedObjects:objects];
128 }
129
130 // Make NSArray of results.
131 result = [[NSArray alloc] initWithObjects:members count:count];
132 }
133 @finally {
134 // Release objects.
135 for (i = 0; i < count; i++) {
136 [members[i] release];
137 }
138
139 free(members);
140 if (tempObjects)
141 [objects release];
142 }
143
144 // Collections are not reused because comparing them is arbitrarily slow.
145 return result;
146}
147
148@end
149
150@implementation NSSet (OODeepCopy)
151
152- (id)ooDeepCopyWithSharedObjects:(NSMutableSet*)objects
153{
154 NSUInteger i, count;
155 id* members = NULL;
156 NSSet* result = nil;
157 BOOL tempObjects = NO;
158
159 count = [self count];
160 if (count == 0)
161 return [[NSSet set] retain];
162
163 members = malloc(sizeof *members * count);
164 if (members == NULL) {
165 [NSException raise:NSMallocException format:@"Failed to allocate space for %zu objects in %s.", count, __PRETTY_FUNCTION__];
166 }
167
168 // Ensure there's an objects set even if passed nil.
169 if (objects == nil) {
170 objects = [[NSMutableSet alloc] init];
171 tempObjects = YES;
172 }
173
174 @try {
175 i = 0;
176 id member = nil;
177 // Deep copy members.
178 foreach (member, self) {
179 members[i] = [member ooDeepCopyWithSharedObjects:objects];
180 i++;
181 }
182
183 // Make NSSet of results.
184 result = [[NSSet alloc] initWithObjects:members count:count];
185 }
186 @finally {
187 // Release objects.
188 for (i = 0; i < count; i++) {
189 [members[i] release];
190 }
191
192 free(members);
193 if (tempObjects)
194 [objects release];
195 }
196
197 // Collections are not reused because comparing them is arbitrarily slow.
198 return result;
199}
200
201@end
202
203@implementation NSDictionary (OODeepCopy)
204
205- (id)ooDeepCopyWithSharedObjects:(NSMutableSet*)objects
206{
207 NSUInteger i, count;
208 id* keys = NULL;
209 id* values = NULL;
210 NSDictionary* result = nil;
211 BOOL tempObjects = NO;
212
213 count = [self count];
214 if (count == 0)
215 return [[NSDictionary dictionary] retain];
216
217 keys = malloc(sizeof *keys * count);
218 values = malloc(sizeof *values * count);
219 if (keys == NULL || values == NULL) {
220 free(keys);
221 free(values);
222 [NSException raise:NSMallocException format:@"Failed to allocate space for %zu objects in %s.", count, __PRETTY_FUNCTION__];
223 }
224
225 // Ensure there's an objects set even if passed nil.
226 if (objects == nil) {
227 objects = [[NSMutableSet alloc] init];
228 tempObjects = YES;
229 }
230
231 @try {
232 i = 0;
233 id key = nil;
234 // Deep copy members.
235 foreachkey(key, self)
236 {
237 keys[i] = [key ooDeepCopyWithSharedObjects:objects];
238 values[i] = [[self objectForKey:key] ooDeepCopyWithSharedObjects:objects];
239 i++;
240 }
241
242 // Make NSDictionary of results.
243 result = [[NSDictionary alloc] initWithObjects:values forKeys:keys count:count];
244 }
245 @finally {
246 // Release objects.
247 for (i = 0; i < count; i++) {
248 [keys[i] release];
249 [values[i] release];
250 }
251
252 free(keys);
253 free(values);
254 if (tempObjects)
255 [objects release];
256 }
257
258 // Collections are not reused because comparing them is arbitrarily slow.
259 return result;
260}
261
262@end
id OODeepCopy(id object)
Definition OODeepCopy.m:30
#define foreachkey(VAR, DICT)
unsigned count
return nil