ValueArray.m
author Jens Alfke <jens@mooseyard.com>
Sat Mar 08 21:04:41 2008 -0800 (2008-03-08)
changeset 0 d84d25d6cdbb
child 11 e5976864dfe9
permissions -rw-r--r--
Initial checkin.
     1 //
     2 //  ValueArray.m
     3 //  MYUtilities
     4 //
     5 //  Copyright 2008 Jens Alfke. All rights reserved.
     6 //
     7 
     8 #import "ValueArray.h"
     9 
    10 
    11 @implementation ValueArray
    12 
    13 
    14 - (id) initWithCount: (unsigned)count valueSize: (size_t)valueSize
    15 {
    16     self = [super init];
    17     if( self ) {
    18         _count = count;
    19         _valueSize = valueSize;
    20     }
    21     return self;
    22 }
    23 
    24 + (ValueArray*) valueArrayWithCount: (unsigned)count valueSize: (size_t)valueSize
    25 {
    26     return [[(ValueArray*)NSAllocateObject(self,count*valueSize,nil)
    27                             initWithCount: count valueSize: valueSize] 
    28                                 autorelease];
    29 }
    30 
    31 - (unsigned) count      {return _count;}
    32 - (size_t) valueSize    {return _valueSize;}
    33 
    34 - (const void*) valueAtIndex: (unsigned)i
    35 {
    36     NSParameterAssert(i<_count);
    37     return (const char*)object_getIndexedIvars(self) + i*_valueSize;
    38 }
    39 
    40 - (void) getValue: (void*)value atIndex: (unsigned)i
    41 {
    42     NSParameterAssert(i<_count);
    43     NSParameterAssert(value!=NULL);
    44     memcpy(value, object_getIndexedIvars(self) + i*_valueSize, _valueSize);
    45 }
    46 
    47 - (void) setValue: (const void*)value atIndex: (unsigned)i
    48 {
    49     NSParameterAssert(i<_count);
    50     NSParameterAssert(value!=NULL);
    51     memcpy(object_getIndexedIvars(self) + i*_valueSize, value, _valueSize);
    52 }
    53 
    54 
    55 
    56 - (id)initWithCoder:(NSCoder *)decoder
    57 {
    58     NSParameterAssert([decoder allowsKeyedCoding]);
    59     NSKeyedUnarchiver *arch = (NSKeyedUnarchiver*)decoder;
    60     unsigned count = [arch decodeIntForKey: @"count"];
    61     size_t valueSize = [arch decodeIntForKey: @"valueSize"];
    62     
    63     [super release];
    64     self = [[[self class] valueArrayWithCount: count valueSize: valueSize] retain];
    65     if( self ) {
    66         unsigned nBytes;
    67         const void *bytes = [arch decodeBytesForKey: @"values" returnedLength: &nBytes];
    68         NSAssert(nBytes==count*valueSize,@"Value size mismatch");
    69         memcpy( object_getIndexedIvars(self), bytes, nBytes );
    70     }
    71     return self;
    72 }
    73 
    74 
    75 - (void)encodeWithCoder:(NSCoder *)coder
    76 {
    77     NSParameterAssert([coder allowsKeyedCoding]);
    78     NSKeyedArchiver *arch = (NSKeyedArchiver*)coder;
    79     
    80     [arch encodeInt: _count forKey: @"count"];
    81     [arch encodeInt: _valueSize forKey: @"valueSize"];
    82     [arch encodeBytes: object_getIndexedIvars(self)
    83                length: _count*_valueSize
    84                forKey: @"values"];
    85 }    
    86 
    87 
    88 @end
    89 
    90 
    91 ImplementValueArrayOf(Int,int)
    92 
    93 ImplementValueArrayOf(Double,double)