ValueArray.m
changeset 3 8fad19466c59
child 11 e5976864dfe9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ValueArray.m	Sun Apr 06 19:13:27 2008 -0700
     1.3 @@ -0,0 +1,93 @@
     1.4 +//
     1.5 +//  ValueArray.m
     1.6 +//  MYUtilities
     1.7 +//
     1.8 +//  Copyright 2008 Jens Alfke. All rights reserved.
     1.9 +//
    1.10 +
    1.11 +#import "ValueArray.h"
    1.12 +
    1.13 +
    1.14 +@implementation ValueArray
    1.15 +
    1.16 +
    1.17 +- (id) initWithCount: (unsigned)count valueSize: (size_t)valueSize
    1.18 +{
    1.19 +    self = [super init];
    1.20 +    if( self ) {
    1.21 +        _count = count;
    1.22 +        _valueSize = valueSize;
    1.23 +    }
    1.24 +    return self;
    1.25 +}
    1.26 +
    1.27 ++ (ValueArray*) valueArrayWithCount: (unsigned)count valueSize: (size_t)valueSize
    1.28 +{
    1.29 +    return [[(ValueArray*)NSAllocateObject(self,count*valueSize,nil)
    1.30 +                            initWithCount: count valueSize: valueSize] 
    1.31 +                                autorelease];
    1.32 +}
    1.33 +
    1.34 +- (unsigned) count      {return _count;}
    1.35 +- (size_t) valueSize    {return _valueSize;}
    1.36 +
    1.37 +- (const void*) valueAtIndex: (unsigned)i
    1.38 +{
    1.39 +    NSParameterAssert(i<_count);
    1.40 +    return (const char*)object_getIndexedIvars(self) + i*_valueSize;
    1.41 +}
    1.42 +
    1.43 +- (void) getValue: (void*)value atIndex: (unsigned)i
    1.44 +{
    1.45 +    NSParameterAssert(i<_count);
    1.46 +    NSParameterAssert(value!=NULL);
    1.47 +    memcpy(value, object_getIndexedIvars(self) + i*_valueSize, _valueSize);
    1.48 +}
    1.49 +
    1.50 +- (void) setValue: (const void*)value atIndex: (unsigned)i
    1.51 +{
    1.52 +    NSParameterAssert(i<_count);
    1.53 +    NSParameterAssert(value!=NULL);
    1.54 +    memcpy(object_getIndexedIvars(self) + i*_valueSize, value, _valueSize);
    1.55 +}
    1.56 +
    1.57 +
    1.58 +
    1.59 +- (id)initWithCoder:(NSCoder *)decoder
    1.60 +{
    1.61 +    NSParameterAssert([decoder allowsKeyedCoding]);
    1.62 +    NSKeyedUnarchiver *arch = (NSKeyedUnarchiver*)decoder;
    1.63 +    unsigned count = [arch decodeIntForKey: @"count"];
    1.64 +    size_t valueSize = [arch decodeIntForKey: @"valueSize"];
    1.65 +    
    1.66 +    [super release];
    1.67 +    self = [[[self class] valueArrayWithCount: count valueSize: valueSize] retain];
    1.68 +    if( self ) {
    1.69 +        unsigned nBytes;
    1.70 +        const void *bytes = [arch decodeBytesForKey: @"values" returnedLength: &nBytes];
    1.71 +        NSAssert(nBytes==count*valueSize,@"Value size mismatch");
    1.72 +        memcpy( object_getIndexedIvars(self), bytes, nBytes );
    1.73 +    }
    1.74 +    return self;
    1.75 +}
    1.76 +
    1.77 +
    1.78 +- (void)encodeWithCoder:(NSCoder *)coder
    1.79 +{
    1.80 +    NSParameterAssert([coder allowsKeyedCoding]);
    1.81 +    NSKeyedArchiver *arch = (NSKeyedArchiver*)coder;
    1.82 +    
    1.83 +    [arch encodeInt: _count forKey: @"count"];
    1.84 +    [arch encodeInt: _valueSize forKey: @"valueSize"];
    1.85 +    [arch encodeBytes: object_getIndexedIvars(self)
    1.86 +               length: _count*_valueSize
    1.87 +               forKey: @"values"];
    1.88 +}    
    1.89 +
    1.90 +
    1.91 +@end
    1.92 +
    1.93 +
    1.94 +ImplementValueArrayOf(Int,int)
    1.95 +
    1.96 +ImplementValueArrayOf(Double,double)