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