1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/bindings/Cocoa/MYVersionDictionary.mm Thu Sep 24 21:47:06 2009 -0700
1.3 @@ -0,0 +1,187 @@
1.4 +//
1.5 +// MYVersionDictionary.m
1.6 +// Ottoman
1.7 +//
1.8 +// Created by Jens Alfke on 9/21/09.
1.9 +// Copyright 2009 Jens Alfke. All rights reserved.
1.10 +//
1.11 +
1.12 +#import "MYVersionDictionary.h"
1.13 +#import "MYOttoman_internal.h"
1.14 +#import <Foundation/Foundation.h>
1.15 +
1.16 +#include "VersionDictionary.h"
1.17 +
1.18 +using namespace Mooseyard;
1.19 +
1.20 +
1.21 +@interface MYDictionaryEnumerator : NSEnumerator
1.22 +{
1.23 + Dictionary::Iterator *_iter;
1.24 + BOOL _started;
1.25 +}
1.26 +- (id) initWithDict: (const Dictionary*)dict;
1.27 +@end
1.28 +
1.29 +
1.30 +
1.31 +static id BlobToNSData (const Blob &blob, bool copy =true) {
1.32 + if (!blob)
1.33 + return nil;
1.34 + else if (copy)
1.35 + return [NSData dataWithBytes: blob.bytes length: blob.length];
1.36 + else
1.37 + return [NSData dataWithBytesNoCopy: (void*)blob.bytes
1.38 + length: blob.length
1.39 + freeWhenDone: NO];
1.40 +}
1.41 +
1.42 +static Blob BlobFromId (id object) {
1.43 + if ([object isKindOfClass: [NSData class]])
1.44 + return Blob([object bytes], [object length]);
1.45 + else
1.46 + return Key();
1.47 +}
1.48 +
1.49 +
1.50 +
1.51 +@implementation MYVersionDictionary
1.52 +
1.53 +
1.54 +- (id) _initWithVersionDictionary: (VersionDictionary*)dict
1.55 +{
1.56 + self = [super init];
1.57 + if (self != nil) {
1.58 + if (!dict) {
1.59 + [self release];
1.60 + return nil;
1.61 + }
1.62 + _dict = dict;
1.63 + }
1.64 + return self;
1.65 +}
1.66 +
1.67 +
1.68 +- (NSUInteger)count {
1.69 + return _dict->count();
1.70 +}
1.71 +
1.72 +- (id)objectForKey:(id)keyObject {
1.73 + return BlobToNSData( _dict->get( Key(BlobFromId(keyObject)) ) );
1.74 +}
1.75 +
1.76 +- (NSEnumerator *)keyEnumerator {
1.77 + return [[[MYDictionaryEnumerator alloc] initWithDict: _dict] autorelease];
1.78 +}
1.79 +
1.80 +- (int) generation {
1.81 + return _dict->generation();
1.82 +}
1.83 +
1.84 +- (NSDate*) dateCreated {
1.85 + return [NSDate dateWithTimeIntervalSince1970: _dict->timestamp()];
1.86 +}
1.87 +
1.88 +- (BOOL) isLatestVersion {
1.89 + return _dict->isLatestVersion();
1.90 +}
1.91 +
1.92 +- (MYVersionDictionary*) previousVersion {
1.93 + const VersionDictionary *prev = _dict->previousVersion();
1.94 + if (prev)
1.95 + return [[[MYVersionDictionary alloc] _initWithVersionDictionary: prev] autorelease];
1.96 + else
1.97 + return nil;
1.98 +}
1.99 +
1.100 +@end
1.101 +
1.102 +
1.103 +
1.104 +
1.105 +@implementation MYCurrentVersionDictionary
1.106 +
1.107 +
1.108 +- (id) _initWithOverlayDictionary: (OverlayDictionary*)dict
1.109 +{
1.110 + self = [super init];
1.111 + if (self != nil) {
1.112 + if (!dict) {
1.113 + [self release];
1.114 + return nil;
1.115 + }
1.116 + _dict = dict;
1.117 + }
1.118 + return self;
1.119 +}
1.120 +
1.121 +
1.122 +- (NSUInteger)count {
1.123 + return _dict->count();
1.124 +}
1.125 +
1.126 +- (id)objectForKey:(id)keyObject {
1.127 + Blob keyBlob = BlobFromId(keyObject);
1.128 + return keyBlob ? BlobToNSData( _dict->get( Key(keyBlob) ) ) :nil;
1.129 +}
1.130 +
1.131 +- (NSEnumerator *)keyEnumerator {
1.132 + return [[[MYDictionaryEnumerator alloc] initWithDict: _dict] autorelease];
1.133 +}
1.134 +
1.135 +- (void)setObject:(id)anObject forKey:(id)aKey {
1.136 + Key key = Key(BlobFromId(aKey));
1.137 + NSAssert1(key, @"Invalid %@ key; only NSData supported", [aKey class]);
1.138 + Blob value = BlobFromId(anObject);
1.139 + NSAssert1(key, @"Invalid %@ value; only NSData supported", [aKey class]);
1.140 + _dict->put(key, value);
1.141 +}
1.142 +
1.143 +- (void)removeObjectForKey:(id)aKey {
1.144 + Key key = Key(BlobFromId(aKey));
1.145 + NSAssert1(key, @"Invalid key class %@; only NSData supported", [aKey class]);
1.146 + _dict->remove(key);
1.147 +}
1.148 +
1.149 +@end
1.150 +
1.151 +
1.152 +
1.153 +
1.154 +@implementation MYDictionaryEnumerator
1.155 +
1.156 +- (id) initWithDict: (Dictionary*)dict
1.157 +{
1.158 + self = [super init];
1.159 + if (self != nil) {
1.160 + _iter = dict->iterate();
1.161 + }
1.162 + return self;
1.163 +}
1.164 +
1.165 +- (void) dealloc
1.166 +{
1.167 + delete _iter;
1.168 + [super dealloc];
1.169 +}
1.170 +
1.171 +- (void) finalize {
1.172 + delete _iter;
1.173 + [super finalize];
1.174 +}
1.175 +
1.176 +- (id)nextObject {
1.177 + if (_started) {
1.178 + if (!_iter->next())
1.179 + return nil;
1.180 + } else {
1.181 + if (!_iter->hasValue())
1.182 + return nil;
1.183 + _started = YES;
1.184 + }
1.185 + Key key = _iter->key();
1.186 + return [NSData dataWithBytes: key.bytes length: key.length];
1.187 +}
1.188 +
1.189 +
1.190 +@end