This repository was archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCOObject.m
More file actions
662 lines (567 loc) · 16.4 KB
/
COObject.m
File metadata and controls
662 lines (567 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#import "COObject.h"
#import "COType.h"
#import "COItem.h"
#import "COItemGraph.h"
#import "COObjectGraphContext.h"
#import "COEditingContextPrivate.h"
#import "COItemPath.h"
#import <EtoileFoundation/Macros.h>
#import "COSchemaRegistry.h"
#import "COSchema.h"
NSString *kCOSchemaName = @"COSchemaName";
@implementation COObject (Private)
- (id) initWithItem: (COItem *)anItem
parentContext: (COObjectGraphContext *)aContext
{
SUPERINIT;
item_ = [anItem mutableCopy]; // Important
parentContext_ = aContext;
return self;
}
- (COItem *) serializedItem
{
return [item_ copy];
}
/**
* Does not automatically update relationship cache in context
*/
- (void) setSeriailzedItem: (COItem *)anItem
{
// TODO:
//[self discardState];
if (item_ != anItem)
{
[item_ release];
item_ = [anItem mutableCopy]; // Important
}
// TODO:
//[self reloadStateFromNewItem: item_];
}
- (void) markAsRemovedFromContext
{
parentContext_ = nil;
// FIXME: Add checks in various places that throw an exception if a user
// tries to do anything with a COObject with a nil editingContext
}
@end
@implementation COObject
- (void) dealloc
{
parentContext_ = nil;
DESTROY(item_);
[super dealloc];
}
#pragma mark Access to the receivers attributes/values
- (COObjectGraphContext *) editingContext
{
return parentContext_;
}
- (ETUUID *) UUID
{
return [item_ UUID];
}
- (NSArray *) attributeNames
{
COSchema *schema = [self schema];
if (schema != nil)
{
// TODO: Performance
return [[[schema propertyNames] setByAddingObjectsFromArray:
[item_ attributeNames]] allObjects];
}
else
{
return [item_ attributeNames];
}
}
- (COType) typeForAttribute: (NSString *)anAttribute
{
COType schemaType = [[self schema] typeForProperty: anAttribute];
if (schemaType != 0)
{
return schemaType;
}
return [item_ typeForAttribute: anAttribute];
}
- (id) valueForAttribute: (NSString*)anAttribute
{
id rootValue = [item_ valueForAttribute: anAttribute];
COType type = [self typeForAttribute: anAttribute];
// TODO: Perofmrance. Cache COObject version?
return [self convertCOUUIDValueToCOObject: rootValue type_: type];
}
- (NSString *) schemaName
{
// NOTE: -valueForAttribute: calls -typeForAttribute:, which calls -schemaName,
// so we can't call -valueForAttribute:. Access item_ directly, instead.
return [item_ valueForAttribute: kCOSchemaName];
}
- (COSchema *) schema
{
return [[parentContext_ schemaRegistry] schemaForName: [self schemaName]];
}
#pragma mark Access to the tree stucture
- (COObject *) embeddedObjectParent
{
return [parentContext_ embeddedObjectParent: self];
}
- (BOOL) containsObject: (COObject *)anObject
{
for (; anObject != nil; anObject = [anObject embeddedObjectParent])
{
if (anObject == self)
{
return YES;
}
}
return NO;
}
- (NSSet *) allObjectUUIDs
{
return [[self allDescendentObjectUUIDs] setByAddingObject: [self UUID]];
}
- (NSSet *) allDescendentObjectUUIDs
{
NSMutableSet *result = [NSMutableSet set];
for (COObject *node in [self directDescendentObjects])
{
[result addObject: [node UUID]];
[result unionSet: [node allDescendentObjectUUIDs]];
}
return result;
}
- (NSSet *) directDescendentObjectUUIDs
{
return [item_ embeddedItemUUIDs];
}
- (NSSet *) objectSetForUUIDs_: (NSSet *)uuids
{
NSMutableSet *result = [NSMutableSet setWithCapacity: [uuids count]];
for (ETUUID *uuid in uuids)
{
[result addObject: [parentContext_ objectForUUID: uuid]];
}
return result;
}
- (NSSet *) directDescendentObjects
{
NSSet *uuids = [item_ embeddedItemUUIDs];
return [self objectSetForUUIDs_: uuids];
}
- (NSSet *) embeddedOrReferencedObjects
{
NSSet *uuids = [[item_ referencedItemUUIDs] setByAddingObjectsFromSet: [item_ embeddedItemUUIDs]];
return [self objectSetForUUIDs_: uuids];
}
/**
* Searches the receiver for the Object with the givent UUID.
* Returns nil if not present
*/
- (COObject *) descendentObjectForUUID: (ETUUID *)aUUID
{
COObject *object = [parentContext_ objectForUUID: aUUID];
if ([self containsObject: object])
{
return object;
}
else
{
return nil;
}
}
- (COItemPath *) itemPathOfDescendentObjectWithUUID: (ETUUID *)aUUID
{
COObject *destObject = [self descendentObjectForUUID: aUUID];
if (destObject == nil)
{
return nil;
}
COObject *destObjectParent = [destObject embeddedObjectParent];
// Search destObjectParent's attributes for [destObject UUID];
// FIXME: Factor out?
for (NSString *attr in [destObjectParent attributeNames])
{
if ([[destObjectParent->item_ allObjectsForAttribute: attr] containsObject: [destObject UUID]])
{
COType type = [destObjectParent typeForAttribute: attr];
if (COTypeIsMultivalued(type))
{
if (COTypeIsOrdered(type))
{
NSUInteger index = [[destObjectParent->item_ allObjectsForAttribute: attr] indexOfObject: [destObject UUID]];
return [COItemPath pathWithItemUUID: [destObjectParent UUID]
arrayName: attr
insertionIndex: index
type: type];
}
else
{
return [COItemPath pathWithItemUUID: [destObjectParent UUID]
unorderedCollectionName: attr
type: type];
}
}
else
{
return [COItemPath pathWithItemUUID: [destObjectParent UUID]
valueName: attr
type: type];
}
}
}
[NSException raise: NSInternalInconsistencyException
format: @"COObject inconsistent"];
return nil;
}
#pragma mark Mutation
- (id) convertCOObjectValueToCOUUID: (id)aValue
type_: (COType)aType
{
if (COPrimitiveType(aType) != kCOCompositeReferenceType
&& COPrimitiveType(aType) != kCOReferenceType)
{
return aValue;
}
if ([aValue isKindOfClass: [COObject class]])
{
return [aValue UUID];
}
else if ([aValue isKindOfClass: [NSCountedSet class]])
{
NSCountedSet *replacement = [NSCountedSet set];
for (COObject *object in aValue)
{
[replacement addObject: [object UUID]];
}
return replacement;
}
else if ([aValue isKindOfClass: [NSSet class]])
{
NSMutableSet *replacement = [NSMutableSet set];
for (COObject *object in aValue)
{
[replacement addObject: [object UUID]];
}
return [NSSet setWithSet: replacement];
}
else if ([aValue isKindOfClass: [NSArray class]])
{
NSMutableArray *replacement = [NSMutableArray array];
for (COObject *object in aValue)
{
[replacement addObject: [object UUID]];
}
return [NSArray arrayWithArray: replacement];
}
NSParameterAssert(NO);
return nil;
}
- (id) convertCOUUIDValueToCOObject: (id)aValue
type_: (COType)aType
{
if (COPrimitiveType(aType) != kCOCompositeReferenceType
&& COPrimitiveType(aType) != kCOReferenceType)
{
return aValue;
}
if ([aValue isKindOfClass: [ETUUID class]])
{
return [parentContext_ objectForUUID: aValue];
}
else if ([aValue isKindOfClass: [NSCountedSet class]])
{
NSCountedSet *replacement = [NSCountedSet set];
for (ETUUID *object in aValue)
{
[replacement addObject: [parentContext_ objectForUUID: object]];
}
return replacement;
}
else if ([aValue isKindOfClass: [NSSet class]])
{
NSMutableSet *replacement = [NSMutableSet set];
for (ETUUID *object in aValue)
{
[replacement addObject: [parentContext_ objectForUUID: object]];
}
return [NSSet setWithSet: replacement];
}
else if ([aValue isKindOfClass: [NSArray class]])
{
NSMutableArray *replacement = [NSMutableArray array];
for (ETUUID *object in aValue)
{
[replacement addObject: [parentContext_ objectForUUID: object]];
}
return [NSArray arrayWithArray: replacement];
}
NSParameterAssert(NO);
return nil;
}
/**
* Can only be used if we have a schema already set, or that attribute already had an
* explicit type set.
*/
- (void) setValue: (id)aValue
forAttribute: (NSString*)anAttribute
{
[self setValue: aValue
forAttribute: anAttribute
type: [self typeForAttribute: anAttribute]];
}
- (void) setValueWithCOUUID: (id)aValue
forAttribute: (NSString*)anAttribute
type: (COType)aType
{
id oldValue = [item_ valueForAttribute: anAttribute];
COType oldType = [item_ typeForAttribute: anAttribute];
// Remove objects in newValue from their old parents
// as perscribed by the COEditingContext class docs
// FIXME: Ugly implementation
if (COPrimitiveType(aType) == kCOCompositeReferenceType)
{
for (ETUUID *beingInserted in aValue)
{
COObject *objectBeingInserted = [parentContext_ objectForUUID: beingInserted];
COObject *objectBeingInsertedParent = [objectBeingInserted embeddedObjectParent];
if (objectBeingInsertedParent != nil)
{
COItemPath *path = [objectBeingInsertedParent itemPathOfDescendentObjectWithUUID: beingInserted];
assert([[path UUID] isEqual: [objectBeingInsertedParent UUID]]);
[path removeValue: beingInserted inStoreItem: objectBeingInsertedParent->item_];
}
}
}
[item_ setValue: aValue
forAttribute: anAttribute
type: aType];
[parentContext_ updateRelationshipIntegrityWithOldValue: oldValue
oldType: oldType
newValue: aValue
newType: aType
forProperty: anAttribute
ofObject: [self UUID]];
}
- (void) setValue: (id)aValue
forAttribute: (NSString*)anAttribute
type: (COType)aType
{
[self setValueWithCOUUID: [self convertCOObjectValueToCOUUID: aValue type_: aType]
forAttribute: anAttribute
type: aType];
}
// TODO: Reuse or scrap...
//- (void) addObject: (id)aValue
//toUnorderedAttribute: (NSString*)anAttribute
//{
// COType aType = [self typeForAttribute: anAttribute];
// NSParameterAssert(aType != 0);
//
// if (COPrimitiveType(aType) == kCOCompositeReferenceType)
// {
// [self addObject: aValue atItemPath: [COItemPath pathWithItemUUID: [self UUID]
// unorderedCollectionName: anAttribute
// type: aType]];
// }
// else if (COPrimitiveType(aType) == kCOReferenceType)
// {
// [item_ addObject: [aValue UUID]
// toUnorderedAttribute: anAttribute
// type: aType];
//
// [parentContext_ recordModifiedObjectUUID: [self UUID]];
// }
// else
// {
// [item_ addObject: aValue
// toUnorderedAttribute: anAttribute
// type: aType];
//
// [parentContext_ recordModifiedObjectUUID: [self UUID]];
// }
//
//}
//
//- (void) addObject: (id)aValue
// toOrderedAttribute: (NSString*)anAttribute
// atIndex: (NSUInteger)anIndex
//{
// COType aType = [self typeForAttribute: anAttribute];
// NSParameterAssert(aType != 0);
//
// if (COPrimitiveType(aType) == kCOCompositeReferenceType)
// {
// [self addObject: aValue atItemPath: [COItemPath pathWithItemUUID: [self UUID]
// arrayName: anAttribute
// insertionIndex: anIndex
// type: aType]];
// }
// else if (COPrimitiveType(aType) == kCOReferenceType)
// {
// [item_ addObject: [aValue UUID]
// toOrderedAttribute: anAttribute
// atIndex: anIndex
// type: aType];
//
// [parentContext_ recordModifiedObjectUUID: [self UUID]];
// }
// else
// {
// [item_ addObject: aValue
// toOrderedAttribute: anAttribute
// atIndex: anIndex
// type: aType];
//
// [parentContext_ recordModifiedObjectUUID: [self UUID]];
// }
//}
//
//- (void) addObject: (id)aValue
// toOrderedAttribute: (NSString*)anAttribute
//{
// COType aType = [self typeForAttribute: anAttribute];
// NSParameterAssert(COTypeIsOrdered(aType));
//
// NSUInteger anIndex = [[item_ valueForAttribute: anAttribute] count];
//
// [self addObject: aValue
// toOrderedAttribute: anAttribute
// atIndex: anIndex];
//}
//
//- (void) removeValueForAttribute: (NSString*)anAttribute
//{
// if (COPrimitiveType([item_ typeForAttribute: anAttribute]) == kCOCompositeReferenceType)
// {
// for (COUUID *uuidToRemove in [item_ allObjectsForAttribute: anAttribute])
// {
// [parentContext_ removeUnreachableObjectAndChildren: uuidToRemove];
// }
// }
//
// [item_ removeValueForAttribute: anAttribute];
//}
//
///**
// * Removes a Object (regardless of where in the receiver or the receiver's children
// * it is located.) Throws an exception if the guven UUID is not present in the receiver.
// */
//- (void) removeDescendentObjectWithUUID: (COUUID *)aUUID
//{
// [self removeDescendentObject: [self descendentObjectForUUID: aUUID]];
//}
//
//- (void) removeDescendentObject: (COObject *)anObject
//{
// if (anObject == self)
// {
// [NSException raise: NSInvalidArgumentException
// format: @"-removeObjectWithUUID: can not remove the receiver"];
// }
// if (![self containsObject: anObject])
// {
// [NSException raise: NSInvalidArgumentException
// format: @"argument must be inside the reciever to remove it"];
// }
//
// COUUID *aUUID = [anObject UUID];
// COObject *parentOfObjectToRemove = [anObject embeddedObjectParent];
// COItemPath *itemPath = [self itemPathOfDescendentObjectWithUUID: aUUID];
// NSAssert([[itemPath UUID] isEqual: [parentOfObjectToRemove UUID]], @"");
// [itemPath removeValue: aUUID inStoreItem: parentOfObjectToRemove->item_];
//
// [parentContext_ removeUnreachableObjectAndChildren: aUUID];
//}
#pragma mark KVC-like ordered collection accessors and mutation methods
- (NSUInteger) countOfAttribute: (NSString *)attribute
{
}
- (id) objectInAttribute: (NSString *)attribute
atIndex: (NSUInteger)index
{
}
- (void) insertObject: (id)anObject
inAttribute: (NSString*)anAttribute
atIndex: (NSString *)anIndex
{
}
- (void) removeObjectFromAttribute: (NSString*)anAttribute
atIndex: (NSString *)anIndex
{
}
#pragma mark KVC-like unordered collection accessors and mutation methods
- (NSEnumerator *) enumeratorOfAttribute: (NSString*)anAttribute
{
}
- (BOOL) memberOfAttribute: (NSString*)anAttribute
{
}
- (void) addObject: (id)anObject
inAttribute: (NSString*)anAttribute
{
}
- (void) removeObject: (id)anObject
fromAttribute: (NSString*)anAttribute
{
}
// Logging
- (NSString *) selfDescription
{
return [NSString stringWithFormat: @"[COObject %@]", [self UUID]];
}
static NSString *tabs(NSUInteger i)
{
NSMutableString *result = [NSMutableString string];
for (NSUInteger j=0; j<i; j++)
{
[result appendFormat: @"\t"];
}
return result;
}
- (NSString *) descriptionWithIndent: (NSUInteger)i
{
NSMutableString *result = [NSMutableString string];
[result appendFormat: @"%@%@\n", tabs(i), [self selfDescription]];
for (COObject *content in [self directDescendentObjects])
{
[result appendFormat: @"%@", [content descriptionWithIndent: i+1]];
}
return result;
}
- (NSString *) description
{
return [self descriptionWithIndent: 0];
}
// Equality
- (BOOL) isEqual:(id)object
{
if (self == object)
{
return YES;
}
if (![object isKindOfClass: [self class]])
{
return NO;
}
COObject *otherObject = (COObject *)object;
if (otherObject->parentContext_ == parentContext_)
{
// Within a context, an object can only be equal to itself.
return object == self;
}
else
{
// Deep equality test between objects in two contexts
if (![item_ isEqual: otherObject->item_])
{
return NO;
}
// Recursively call -isEqual on the NSSet of contained COObject instances
return [[self directDescendentObjects] isEqual: [otherObject directDescendentObjects]];
}
}
- (NSUInteger) hash
{
return [[item_ UUID] hash] ^ 8748262350970910369ULL;
}
@end