author | Jens Alfke <jens@mooseyard.com> |
Thu Sep 24 21:47:06 2009 -0700 (2009-09-24) | |
changeset 6 | f2cd752db494 |
permissions | -rw-r--r-- |
jens@6 | 1 |
// |
jens@6 | 2 |
// MYVersionDictionary.m |
jens@6 | 3 |
// Ottoman |
jens@6 | 4 |
// |
jens@6 | 5 |
// Created by Jens Alfke on 9/21/09. |
jens@6 | 6 |
// Copyright 2009 Jens Alfke. All rights reserved. |
jens@6 | 7 |
// |
jens@6 | 8 |
|
jens@6 | 9 |
#import "MYVersionDictionary.h" |
jens@6 | 10 |
#import "MYOttoman_internal.h" |
jens@6 | 11 |
#import <Foundation/Foundation.h> |
jens@6 | 12 |
|
jens@6 | 13 |
#include "VersionDictionary.h" |
jens@6 | 14 |
|
jens@6 | 15 |
using namespace Mooseyard; |
jens@6 | 16 |
|
jens@6 | 17 |
|
jens@6 | 18 |
@interface MYDictionaryEnumerator : NSEnumerator |
jens@6 | 19 |
{ |
jens@6 | 20 |
Dictionary::Iterator *_iter; |
jens@6 | 21 |
BOOL _started; |
jens@6 | 22 |
} |
jens@6 | 23 |
- (id) initWithDict: (const Dictionary*)dict; |
jens@6 | 24 |
@end |
jens@6 | 25 |
|
jens@6 | 26 |
|
jens@6 | 27 |
|
jens@6 | 28 |
static id BlobToNSData (const Blob &blob, bool copy =true) { |
jens@6 | 29 |
if (!blob) |
jens@6 | 30 |
return nil; |
jens@6 | 31 |
else if (copy) |
jens@6 | 32 |
return [NSData dataWithBytes: blob.bytes length: blob.length]; |
jens@6 | 33 |
else |
jens@6 | 34 |
return [NSData dataWithBytesNoCopy: (void*)blob.bytes |
jens@6 | 35 |
length: blob.length |
jens@6 | 36 |
freeWhenDone: NO]; |
jens@6 | 37 |
} |
jens@6 | 38 |
|
jens@6 | 39 |
static Blob BlobFromId (id object) { |
jens@6 | 40 |
if ([object isKindOfClass: [NSData class]]) |
jens@6 | 41 |
return Blob([object bytes], [object length]); |
jens@6 | 42 |
else |
jens@6 | 43 |
return Key(); |
jens@6 | 44 |
} |
jens@6 | 45 |
|
jens@6 | 46 |
|
jens@6 | 47 |
|
jens@6 | 48 |
@implementation MYVersionDictionary |
jens@6 | 49 |
|
jens@6 | 50 |
|
jens@6 | 51 |
- (id) _initWithVersionDictionary: (VersionDictionary*)dict |
jens@6 | 52 |
{ |
jens@6 | 53 |
self = [super init]; |
jens@6 | 54 |
if (self != nil) { |
jens@6 | 55 |
if (!dict) { |
jens@6 | 56 |
[self release]; |
jens@6 | 57 |
return nil; |
jens@6 | 58 |
} |
jens@6 | 59 |
_dict = dict; |
jens@6 | 60 |
} |
jens@6 | 61 |
return self; |
jens@6 | 62 |
} |
jens@6 | 63 |
|
jens@6 | 64 |
|
jens@6 | 65 |
- (NSUInteger)count { |
jens@6 | 66 |
return _dict->count(); |
jens@6 | 67 |
} |
jens@6 | 68 |
|
jens@6 | 69 |
- (id)objectForKey:(id)keyObject { |
jens@6 | 70 |
return BlobToNSData( _dict->get( Key(BlobFromId(keyObject)) ) ); |
jens@6 | 71 |
} |
jens@6 | 72 |
|
jens@6 | 73 |
- (NSEnumerator *)keyEnumerator { |
jens@6 | 74 |
return [[[MYDictionaryEnumerator alloc] initWithDict: _dict] autorelease]; |
jens@6 | 75 |
} |
jens@6 | 76 |
|
jens@6 | 77 |
- (int) generation { |
jens@6 | 78 |
return _dict->generation(); |
jens@6 | 79 |
} |
jens@6 | 80 |
|
jens@6 | 81 |
- (NSDate*) dateCreated { |
jens@6 | 82 |
return [NSDate dateWithTimeIntervalSince1970: _dict->timestamp()]; |
jens@6 | 83 |
} |
jens@6 | 84 |
|
jens@6 | 85 |
- (BOOL) isLatestVersion { |
jens@6 | 86 |
return _dict->isLatestVersion(); |
jens@6 | 87 |
} |
jens@6 | 88 |
|
jens@6 | 89 |
- (MYVersionDictionary*) previousVersion { |
jens@6 | 90 |
const VersionDictionary *prev = _dict->previousVersion(); |
jens@6 | 91 |
if (prev) |
jens@6 | 92 |
return [[[MYVersionDictionary alloc] _initWithVersionDictionary: prev] autorelease]; |
jens@6 | 93 |
else |
jens@6 | 94 |
return nil; |
jens@6 | 95 |
} |
jens@6 | 96 |
|
jens@6 | 97 |
@end |
jens@6 | 98 |
|
jens@6 | 99 |
|
jens@6 | 100 |
|
jens@6 | 101 |
|
jens@6 | 102 |
@implementation MYCurrentVersionDictionary |
jens@6 | 103 |
|
jens@6 | 104 |
|
jens@6 | 105 |
- (id) _initWithOverlayDictionary: (OverlayDictionary*)dict |
jens@6 | 106 |
{ |
jens@6 | 107 |
self = [super init]; |
jens@6 | 108 |
if (self != nil) { |
jens@6 | 109 |
if (!dict) { |
jens@6 | 110 |
[self release]; |
jens@6 | 111 |
return nil; |
jens@6 | 112 |
} |
jens@6 | 113 |
_dict = dict; |
jens@6 | 114 |
} |
jens@6 | 115 |
return self; |
jens@6 | 116 |
} |
jens@6 | 117 |
|
jens@6 | 118 |
|
jens@6 | 119 |
- (NSUInteger)count { |
jens@6 | 120 |
return _dict->count(); |
jens@6 | 121 |
} |
jens@6 | 122 |
|
jens@6 | 123 |
- (id)objectForKey:(id)keyObject { |
jens@6 | 124 |
Blob keyBlob = BlobFromId(keyObject); |
jens@6 | 125 |
return keyBlob ? BlobToNSData( _dict->get( Key(keyBlob) ) ) :nil; |
jens@6 | 126 |
} |
jens@6 | 127 |
|
jens@6 | 128 |
- (NSEnumerator *)keyEnumerator { |
jens@6 | 129 |
return [[[MYDictionaryEnumerator alloc] initWithDict: _dict] autorelease]; |
jens@6 | 130 |
} |
jens@6 | 131 |
|
jens@6 | 132 |
- (void)setObject:(id)anObject forKey:(id)aKey { |
jens@6 | 133 |
Key key = Key(BlobFromId(aKey)); |
jens@6 | 134 |
NSAssert1(key, @"Invalid %@ key; only NSData supported", [aKey class]); |
jens@6 | 135 |
Blob value = BlobFromId(anObject); |
jens@6 | 136 |
NSAssert1(key, @"Invalid %@ value; only NSData supported", [aKey class]); |
jens@6 | 137 |
_dict->put(key, value); |
jens@6 | 138 |
} |
jens@6 | 139 |
|
jens@6 | 140 |
- (void)removeObjectForKey:(id)aKey { |
jens@6 | 141 |
Key key = Key(BlobFromId(aKey)); |
jens@6 | 142 |
NSAssert1(key, @"Invalid key class %@; only NSData supported", [aKey class]); |
jens@6 | 143 |
_dict->remove(key); |
jens@6 | 144 |
} |
jens@6 | 145 |
|
jens@6 | 146 |
@end |
jens@6 | 147 |
|
jens@6 | 148 |
|
jens@6 | 149 |
|
jens@6 | 150 |
|
jens@6 | 151 |
@implementation MYDictionaryEnumerator |
jens@6 | 152 |
|
jens@6 | 153 |
- (id) initWithDict: (Dictionary*)dict |
jens@6 | 154 |
{ |
jens@6 | 155 |
self = [super init]; |
jens@6 | 156 |
if (self != nil) { |
jens@6 | 157 |
_iter = dict->iterate(); |
jens@6 | 158 |
} |
jens@6 | 159 |
return self; |
jens@6 | 160 |
} |
jens@6 | 161 |
|
jens@6 | 162 |
- (void) dealloc |
jens@6 | 163 |
{ |
jens@6 | 164 |
delete _iter; |
jens@6 | 165 |
[super dealloc]; |
jens@6 | 166 |
} |
jens@6 | 167 |
|
jens@6 | 168 |
- (void) finalize { |
jens@6 | 169 |
delete _iter; |
jens@6 | 170 |
[super finalize]; |
jens@6 | 171 |
} |
jens@6 | 172 |
|
jens@6 | 173 |
- (id)nextObject { |
jens@6 | 174 |
if (_started) { |
jens@6 | 175 |
if (!_iter->next()) |
jens@6 | 176 |
return nil; |
jens@6 | 177 |
} else { |
jens@6 | 178 |
if (!_iter->hasValue()) |
jens@6 | 179 |
return nil; |
jens@6 | 180 |
_started = YES; |
jens@6 | 181 |
} |
jens@6 | 182 |
Key key = _iter->key(); |
jens@6 | 183 |
return [NSData dataWithBytes: key.bytes length: key.length]; |
jens@6 | 184 |
} |
jens@6 | 185 |
|
jens@6 | 186 |
|
jens@6 | 187 |
@end |